dns-check.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Python container probe: raw DNS query against the unbound container.
  2. # Checks AD bit (DNSSEC validated) with and without DO bit, plus basic resolution.
  3. import socket
  4. import struct
  5. import sys
  6. server = sys.argv[1] if len(sys.argv) > 1 else "unbound"
  7. port = int(sys.argv[2]) if len(sys.argv) > 2 else 5053
  8. def query(name, do_bit):
  9. flags = 0x0100 | (0x0020 if do_bit else 0) # RD (+ DO)
  10. header = struct.pack(">HHHHHH", 0x1234, flags, 1, 0, 0, 0)
  11. qname = b"".join(bytes([len(p)]) + p.encode() for p in name.rstrip(".").split(".")) + b"\x00"
  12. q = header + qname + struct.pack(">HH", 1, 1)
  13. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  14. s.settimeout(10)
  15. s.sendto(q, (server, port))
  16. data, _ = s.recvfrom(4096)
  17. s.close()
  18. rf = struct.unpack(">H", data[2:4])[0]
  19. an = struct.unpack(">H", data[6:8])[0]
  20. return an, (rf >> 5) & 1, rf & 0xF
  21. ok = True
  22. for name in ("example.com", "unbound.net"):
  23. an, ad, rc = query(name, do_bit=1)
  24. good = an > 0 and ad == 1 and rc == 0
  25. ok = ok and good
  26. print(f"{name}: answers={an} AD={ad} rcode={rc} -> {'PASS' if good else 'FAIL'}")
  27. # plain query must also resolve
  28. an, ad, rc = query("example.com", do_bit=0)
  29. good = an > 0 and rc == 0
  30. ok = ok and good
  31. print(f"example.com (no DO): answers={an} rcode={rc} -> {'PASS' if good else 'FAIL'}")
  32. sys.exit(0 if ok else 1)