Note that there are some explanatory texts on larger screens.

plurals
  1. PORaw socket python packet sniffer
    primarykey
    data
    text
    <p>I have created a simple RAW socket based packet sniffer. But when I run it, it rarely captures up a packet. First I created this to capture packets in 1 second time intervals, but seeing no packets are captured I commented that line. I was connected to internet and a lot of http traffic are going here and there, but I could not capture a one. Is there a problem in this in the code where I created the socket? Please someone give me a solution. I am fairly new to python programming and could not understand how to solve this.</p> <pre><code>import socket, binascii, struct import time sock = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(0x800)) print "Waiting.." pkt = sock.recv(2048) print "received" def processEth(data): #some code to process source mac and dest. mac return [smac, dmac] def processIP(data): sip = str(binascii.hexlify(data[1])) dip = str(binascii.hexlify(data[2])) return [sip, dip] def processTCP(data): sport = str(data[0]) dport = str(data[1]) return [sport, dport] while len(pkt) &gt; 0 : if(len(pkt)) &gt; 54: pkt = sock.recv(2048) ethHeader = pkt[0][0:14] ipHeader = pkt[0][14:34] tcpHeader = pkt[0][34:54] ethH = struct.unpack("!6s6s2s",ethHeader) ethdata = processEth(ethH) ipH = struct.unpack("!12s4s4s",ipHeader) ipdata = processIP(ipH) tcpH = struct.unpack("!HH16", tcpHeader) tcpdata = processTCP(tcpH) print "S.mac "+ethdata[0]+" D.mac "+ethdata[1]+" from: "+ipdata[0]+":"+tcpdata[0]+" to: "+ipdata[1]+":"+tcpdata[1] #time.sleep(1); else: continue </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload