Skip to content

Instantly share code, notes, and snippets.

@oraccha
Created January 7, 2010 08:09
Show Gist options
  • Save oraccha/271079 to your computer and use it in GitHub Desktop.
Save oraccha/271079 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Packet sniffer by using PF_PACKET.
# Copyright (c) 2010 oraccha
import sys
from socket import *
ETH_P_IP = 0x800
s = socket(PF_PACKET, SOCK_RAW, ETH_P_IP)
s.bind(("eth1", ETH_P_IP))
while 1:
p = s.recv(2024)
plen = len(p)
# dump header
src = ":".join(["%02x" % ord(x) for x in p[0:6]])
dst = ":".join(["%02x" % ord(x) for x in p[6:12]])
type = ntohs(ord(p[12:13]))
print("%s > %s, ethertype %04x, length %d" % (src, dst, type, plen))
# dump body
print("\t"),
for i in xrange(0, plen, 2):
print("%02x%02x" % (ord(p[i]), ord(p[i+1]))),
if i % 16 == 14:
print("\n\t"),
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment