Sniffing traffic with python raw sockets

by Security Dude



'''
Packet sniffer in python for Linux
Sniffs only incoming TCP packet
'''

import socket, sys
from struct import *

try:
	s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
except socket.error , msg:
	print msg
	sys.exit()

while True:
	packet = s.recvfrom(65565)
	packet = packet[0]
	ip_header = packet[0:20] # first 20 packets

https://github.com/bigsnarfdude/pythonNetworkProgrammingN00B/blob/master/simplePacketSniffer.py
Advertisements