DNS A-Record Packet Unpacker
DNS operates primarily over UDP using compact binary structures defined in RFC 1035. Unpacking DNS responses requires navigating a 12-byte header, skipping length-prefixed domain labels in question sections, and extracting 4-byte IPv4 addresses from Type 1 (A) resource records.
import struct
# DNS Header: ID, Flags, QDCOUNT, ANCOUNT, NSCOUNT, ARCOUNT
header = struct.unpack_from('>HHHHHH', packet, 0)
# DNS A-Record Packet Unpacker Whenever you query an API endpoint or browse a site, your operating system fires a raw UDP datagram to port 53. Understanding binary network protocols requires knowing how DNS encodes and decodes addresses on the wire. 🌐🔍 A DNS message (RFC 1035) consists of: 1. **Header (12 bytes)**: Transaction ID, 16-bit Flags, and 4 section counts (`QDCOUNT`, `ANCOUNT`, `NSCOUNT`, `ARCOUNT`). 2. **Question Section**: Domain labels where each segment is preceded by its byte length (`\x07example\x03com\x00`), followed by QTYPE (2 bytes) and QCLASS (2 bytes). 3. **Answer Section**: Name pointer (`0xC0` prefix for DNS pointer compression), TYPE (1 for `A`), CLASS (1 for `IN`), TTL (4 bytes), RDLENGTH (2 bytes), and RDATA (4 bytes for IPv4). ```python import struct tx_id, flags, qd, an, ns, ar = struct.unpack_from(">HHHHHH", packet, 0) ``` ## Pop Quiz How are domain names encoded in DNS question and answer sections according to RFC 1035? - [ ] As a sequence of length-prefixed labels terminated with a null byte (0x00) or compression offset pointers. - [ ] As null-terminated ASCII strings separated by literal dot characters (0x2E). - [ ] As UTF-8 byte strings padded to 64-byte word boundaries. - [ ] As base64-encoded strings with CRLF line endings. ## Real-World Challenge Implement `parse_dns_a_response(packet: bytes) -> str | None`. It must inspect the header, traverse the question section, unpack the first A-record in the answer section, and return the resolved IPv4 address as a string (e.g. `"93.184.216.34"`), or `None` if no A-record exists. <details> <summary>Solution</summary> ```python import struct def parse_dns_a_response(packet: bytes) -> str | None: if len(packet) < 12: return None txid, flags, qdcount, ancount, nscount, arcount = struct.unpack_from(">HHHHHH", packet, 0) offset = 12 # Skip Question Section labels for _ in range(qdcount): while offset < len(packet) and packet[offset] != 0: label_len = packet[offset] offset += 1 + label_len offset += 1 # Skip terminating null byte offset += 4 # Skip QTYPE (2) + QCLASS (2) # Parse Answer Section if ancount < 1 or offset >= len(packet): return None # Handle domain name compression pointer (0xC0XX) or raw labels if (packet[offset] & 0xC0) == 0xC0: offset += 2 else: while offset < len(packet) and packet[offset] != 0: offset += 1 + packet[offset] offset += 1 rtype, rclass, ttl, rdlength = struct.unpack_from(">HHIH", packet, offset) offset += 10 # Type 1 = A record, Length 4 = IPv4 if rtype == 1 and rdlength == 4: ip_bytes = packet[offset : offset + 4] return ".".join(str(b) for b in ip_bytes) return None # Verification # Craft synthetic DNS response for example.com -> 192.0.2.1 header = struct.pack(">HHHHHH", 0xABCD, 0x8180, 1, 1, 0, 0) question = b"\x07example\x03com\x00" + struct.pack(">HH", 1, 1) answer = struct.pack(">HHHIH", 0xC00C, 1, 1, 60, 4) + bytes([192, 0, 2, 1]) packet = header + question + answer result_ip = parse_dns_a_response(packet) assert result_ip == "192.0.2.1" ``` </details>