#!/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. The DO bit lives in the OPT record's 4-byte # EXTENDED FLAGS field (0x8000), NOT in the 2-byte CLASS field — this matches # how unbound (and dns-check.py) interpret it. Fields: type(41) + class(0) + # extflags(DO=0x8000) + rdlength(0). opt = b"\x00" + struct.pack(">HHIH", 41, 0, 0x8000, 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()