Last active
October 22, 2026 16:39
-
-
Save acoster/97c36aeb96cf4bfb85720cd334fbb44f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| declare(strict_types=1); | |
| // docker run -it --rm --name cursed-dns -p "53:53/udp" -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:8.2-cli php your-script.php | |
| $socket = stream_socket_server("udp://0.0.0.0:53", $errno, $errstr, STREAM_SERVER_BIND); | |
| if (!$socket) { | |
| die("Failed to bind UDP socket: {$errstr} ({$errno})\n"); | |
| } | |
| echo "Cursed PHP DNS server listening on udp://0.0.0.0:53...\n"; | |
| while (true) { | |
| // 1. Read raw query packet | |
| $packet = @stream_socket_recvfrom($socket, 512, 0, $peer); | |
| if (!$packet || strlen($packet) < 12) { | |
| continue; | |
| } | |
| // 2. Unpack the standard 12-byte DNS header | |
| $header = unpack('nID/nFlags/nQDCOUNT/nANCOUNT/nNSCOUNT/nARCOUNT', substr($packet, 0, 12)); | |
| // Parse QNAME labels | |
| $offset = 12; | |
| $domainLabels = []; | |
| while ($offset < strlen($packet)) { | |
| $length = ord($packet[$offset++]); | |
| if ($length === 0) break; | |
| $domainLabels[] = substr($packet, $offset, $length); | |
| $offset += $length; | |
| } | |
| $domain = implode('.', $domainLabels); | |
| // Unpack QTYPE and QCLASS | |
| $qinfo = unpack('nType/nClass', substr($packet, $offset, 4)); | |
| $offset += 4; | |
| $questionSection = substr($packet, 12, $offset - 12); | |
| $qtype = $qinfo['Type']; | |
| echo "Query from {$peer} for: {$domain} (QTYPE: {$qtype})\n"; | |
| // 3. Branch by record type | |
| if ($qtype === 1) { | |
| // --- TYPE A (IPv4) --- | |
| // Flags: QR=1, AA=1, RD=1, RA=0, RCODE=0 (No Error) -> 0x8400 (or 0x8180) | |
| $flags = 0x8500; | |
| $targetIp = '127.0.0.1'; | |
| $rdata = inet_pton($targetIp); | |
| $responseHeader = pack('nnnnnn', $header['ID'], $flags, 1, 1, 0, 0); | |
| $answerSection = pack( | |
| 'nnnNn', | |
| 0xC00C, // Pointer to domain name at byte offset 12 | |
| 1, // Type: A | |
| 1, // Class: IN | |
| 60, // TTL: 60s | |
| strlen($rdata) // RDLENGTH: 4 bytes | |
| ) . $rdata; | |
| $response = $responseHeader . $questionSection . $answerSection; | |
| } elseif ($qtype === 16) { | |
| // --- TYPE TXT --- | |
| $flags = 0x8500; | |
| $txtMessage = "I'm just a dumb php script, what do I know?"; | |
| // DNS TXT wire format: 1-byte string length followed by text | |
| $txtPayload = chr(strlen($txtMessage)) . $txtMessage; | |
| $responseHeader = pack('nnnnnn', $header['ID'], $flags, 1, 1, 0, 0); | |
| $answerSection = pack( | |
| 'nnnNn', | |
| 0xC00C, // Compression pointer to QNAME | |
| 16, // Type: TXT | |
| 1, // Class: IN | |
| 60, // TTL: 60s | |
| strlen($txtPayload) // RDLENGTH | |
| ) . $txtPayload; | |
| $response = $responseHeader . $questionSection . $answerSection; | |
| } else { | |
| // --- ALL OTHER TYPES -> NXDOMAIN --- | |
| // QR=1 (0x8000), AA=1 (0x0400), RD=1 (0x0100), RCODE=3 (NXDOMAIN: 0x0003) -> 0x8503 | |
| $flags = 0x8503; | |
| // 0 answers returned, question preserved | |
| $responseHeader = pack('nnnnnn', $header['ID'], $flags, 1, 0, 0, 0); | |
| $response = $responseHeader . $questionSection; | |
| } | |
| // 4. Send back datagram | |
| stream_socket_sendto($socket, $response, 0, $peer); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment