#!/usr/bin/env python3 """Send a DNS query with the DO and CD bits set; print the rcode. Usage: python cd-check.py Exit: 0 if rcode==0 (NOERROR), 1 if rcode==2 (SERVFAIL), 2 otherwise/timeout. """ import socket import struct import sys ip = sys.argv[1] port = int(sys.argv[2]) name = b"\x03www\x08broken\x07example\x00" # www.broken.example qtype, qclass = 1, 1 flags = 0x0110 # RD | CD tid = 0x1234 # OPT record carrying the DO bit: name(root) + type(41) + class(extflags=DO) # + ttl(udp-size) + rdlength(0). The 32-bit TTL field must be present. opt = b"\x00" + struct.pack(">HHIH", 41, 0x8000, 0, 0) hdr = struct.pack(">HHHHHH", tid, flags, 1, 0, 0, 1) pkt = hdr + name + struct.pack(">HH", qtype, qclass) + opt s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(5) try: s.sendto(pkt, (ip, port)) data, _ = s.recvfrom(4096) rcode = data[3] & 0x0F print(f"CD+DO query rcode={rcode} (0=NOERROR, 2=SERVFAIL)") if rcode == 0: sys.exit(0) if rcode == 2: sys.exit(1) sys.exit(2) except Exception as e: print(f"CD probe error: {e}") sys.exit(2) finally: s.close()