Raw ICMP Echo Request Packet Crafter
The Internet Control Message Protocol (ICMP) underpins network diagnostic utilities like ping. An Echo Request packet consists of an 8-byte header (type 8, code 0, identifier, sequence number, and a 16-bit one's complement Internet Checksum) followed by arbitrary payload data.
import struct
# ICMP Header: Type (8), Code (0), Checksum, Ident, Seq
hdr = struct.pack('>BBHHH', 8, 0, checksum, ident, seq)
# Raw ICMP Echo Request Packet Crafter When you type `ping 8.8.8.8` in your terminal, your system doesn't open an HTTP connection or TCP handshake. It crafts an **ICMP Echo Request** packet (RFC 792) and transmits it as a raw Layer 3 IP datagram. 🌐🎯 An ICMP Echo Request packet consists of: - **Type (1 byte)**: `8` (Echo Request) - **Code (1 byte)**: `0` - **Checksum (2 bytes)**: RFC 1071 Internet Checksum (one's complement sum of all 16-bit words) - **Identifier (2 bytes)**: Process ID or random session token - **Sequence Number (2 bytes)**: Monotonically increasing counter - **Payload (`N` bytes)**: Optional timestamp or diagnostic payload ```python import struct # Initial header with 0 checksum for calculation header_blank = struct.pack(">BBHHH", 8, 0, 0, ident, seq) ``` ## Pop Quiz How is the RFC 1071 Internet Checksum calculated across ICMP or IP packet words? - [ ] By computing the 16-bit one's complement sum of contiguous 16-bit words, folding carries into the lower 16 bits. - [ ] By executing a CRC-32 hash across packet headers and truncating the digest to 16 bits. - [ ] By calculating an MD5 checksum and taking the lowest two bytes. - [ ] By XORing packet bytes with the local host default gateway MAC address. ## Real-World Challenge Implement: 1. `calculate_checksum(data: bytes) -> int`: Implements RFC 1071 checksum calculation. 2. `craft_icmp_echo(ident: int, seq: int, payload: bytes = b"PING") -> bytes`: Assembles an ICMP Echo Request packet with the correct checksum embedded. Verify that re-checksumming the completed packet yields `0` (indicating valid checksum). <details> <summary>Solution</summary> ```python import struct def calculate_checksum(data: bytes) -> int: # If odd number of bytes, pad with a trailing zero byte if len(data) % 2 == 1: data += b"\x00" num_words = len(data) // 2 words = struct.unpack(f">{num_words}H", data) total = sum(words) # Fold 32-bit carries into 16 bits while (total >> 16) > 0: total = (total & 0xFFFF) + (total >> 16) # One's complement return (~total) & 0xFFFF def craft_icmp_echo(ident: int, seq: int, payload: bytes = b"PING") -> bytes: # Step 1: Pack with checksum 0 header_zero = struct.pack(">BBHHH", 8, 0, 0, ident, seq) packet_for_calc = header_zero + payload # Step 2: Compute checksum csum = calculate_checksum(packet_for_calc) # Step 3: Repack with actual checksum header_final = struct.pack(">BBHHH", 8, 0, csum, ident, seq) return header_final + payload # Verification packet = craft_icmp_echo(ident=0x04D2, seq=1, payload=b"HELLO_ICMP") assert len(packet) == 8 + len(b"HELLO_ICMP") # Header unpacking check icmp_type, code, csum, ident, seq = struct.unpack_from(">BBHHH", packet, 0) assert icmp_type == 8 assert code == 0 assert ident == 0x04D2 assert seq == 1 # Mathematical verification: Checksumming a valid packet with its own checksum yields 0 assert calculate_checksum(packet) == 0 ``` </details>