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
12import socket3import struct45def sniff():6 # Create a raw socket bound to all interfaces7 # AF_PACKET is Linux specific. For Windows use AF_INET + IP_HDRINCL8 s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))910 while True:11 raw_data, addr = s.recvfrom(65535)12 eth_header = raw_data[:14]1314 # Unpack Ethernet Frame15 dest, src, proto = struct.unpack('! 6s 6s H', eth_header)1617 # Check for IPv4 (0x0800)18 if socket.ntohs(proto) == 8:19 ip_header = raw_data[14:34]20 # Unpack IP Header (Version, IHL, TTL, Protocol, Source, Dest)21 iph = struct.unpack('!BBHHHBBH4s4s', ip_header)2223 version_ihl = iph[0]24 ihl = version_ihl & 0xF2526 print(f"Packet: {addr} | IHL: {ihl} | Protocol: {iph[6]}")27
READ_ONLY_MODEUTF-8