Skip to content

Instantly share code, notes, and snippets.

@kjseefried
Created July 15, 2014 01:19
Show Gist options
  • Save kjseefried/360cf8c7636295f42469 to your computer and use it in GitHub Desktop.
Save kjseefried/360cf8c7636295f42469 to your computer and use it in GitHub Desktop.
Example of using the Python socket lib to sniff packets.
import socket
from struct import *
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
#'!BBBHHHHHHBBBBH5s5s'
while True:
packet= s.recvfrom(65565)
print packet
packet =packet[0]
print "\n",packet
header=packet[0:26]
print "\n",header
iph = unpack('!BHBBHHBBBBBHH4s4s' , header)
print "\nall header: ",iph
version_ihl=iph[0]
print "\nVersion_ihl: ",version_ihl
version =version_ihl >>4
print "\nVersion :" ,version
ttl=iph[5]
print"\nTTL: ",ttl
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment