EXPERIMENT_ID: 002
Raw Socket Packet Sniffer
ACTIVE
Python script utilizing raw sockets to capture and analyze TCP/IP headers. Implements basic signature matching for identifying SYN scans.
OBJECTIVE
Manually parse IP/TCP headers to understand protocol structures and detect scanning patterns without relying on Wireshark.
CONSTRAINTS
Requires root/admin privileges. Promiscuous mode enabled.
Python Networking Scapy
src/main.python
import socketimport structdef sniff():# Create a raw socket bound to all interfaces# AF_PACKET is Linux specific. For Windows use AF_INET + IP_HDRINCLs = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))while True:raw_data, addr = s.recvfrom(65535)eth_header = raw_data[:14]# Unpack Ethernet Framedest, src, proto = struct.unpack('! 6s 6s H', eth_header)# Check for IPv4 (0x0800)if socket.ntohs(proto) == 8:ip_header = raw_data[14:34]# Unpack IP Header (Version, IHL, TTL, Protocol, Source, Dest)iph = struct.unpack('!BBHHHBBH4s4s', ip_header)version_ihl = iph[0]ihl = version_ihl & 0xFprint(f"Packet: {addr} | IHL: {ihl} | Protocol: {iph[6]}")
READ_ONLY_MODEUTF-8