#!/usr/bin/env python3 """Tell broken DNSSEC from ordinary DNS problems using DNS-over-HTTPS JSON (no dig needed). For each name: ask a validating resolver twice (normal, and with CD=1 = 'checking disabled'), then classify. Usage: probe.py NAME [NAME...] [--resolver cloudflare|google] """ import json, sys, urllib.request, urllib.parse RESOLVERS = { "cloudflare": "https://cloudflare-dns.com/dns-query", "google": "https://dns.google/resolve", } RCODE = {0: "NOERROR", 2: "SERVFAIL", 3: "NXDOMAIN", 5: "REFUSED"} def query(resolver, name, rtype="A", cd=False, fetch=None): fetch = fetch or _fetch return fetch(resolver, name, rtype, cd) def _fetch(resolver, name, rtype, cd): q = urllib.parse.urlencode({"name": name, "type": rtype, "cd": "1" if cd else "0"}) req = urllib.request.Request(f"{RESOLVERS[resolver]}?{q}", headers={"accept": "application/dns-json"}) with urllib.request.urlopen(req, timeout=15) as r: return json.load(r) def classify(normal, cd, ds_answers): """Pure function (unit-tested): returns (verdict, explanation).""" n, c = normal["Status"], cd["Status"] if n == 2 and c == 0: return ("DNSSEC-BROKEN", "SERVFAIL with validation on, but the records resolve when validation is skipped (CD=1). The zone is signed but the chain of trust does not validate: look at the DS record at the parent and the DNSKEY/RRSIG at the zone.") if n == 2 and c == 2: return ("DNS-BROKEN", "SERVFAIL even with validation skipped, so this is an ordinary DNS failure (lame or unreachable nameservers), not DNSSEC.") if n == 3: return ("NXDOMAIN", "The name does not exist (this answer is itself DNSSEC-validated when AD=true).") if n == 0 and normal.get("AD"): return ("DNSSEC-VALID", "The resolver validated the whole chain of trust (AD flag set).") if n == 0 and not normal.get("AD") and not ds_answers: return ("UNSIGNED", "Resolves, no AD flag and no DS record at the parent: the zone is not DNSSEC-signed. That is normal for many domains.") if n == 0 and not normal.get("AD") and ds_answers: return ("CHECK", "Resolves without AD but a DS record exists. The answer may be a CNAME/CDN name outside the signed zone, or the resolver does not validate. Query the zone apex.") return ("UNKNOWN", f"Unexpected status {RCODE.get(n, n)}/{RCODE.get(c, c)}.") def main(argv): resolver = "cloudflare" if "--resolver" in argv: i = argv.index("--resolver"); resolver = argv[i + 1]; del argv[i:i + 2] for name in argv: a = query(resolver, name, "A") b = query(resolver, name, "A", cd=True) apex = name.split(".", 1)[1] if name.startswith("www.") else name ds = query(resolver, apex, "DS").get("Answer", []) ds = [x for x in ds if x["type"] == 43] verdict, why = classify(a, b, ds) ede = a.get("Comment") if isinstance(ede, list): ede = " ".join(ede) print(f"{name} [{resolver}]") print(f" validating : {RCODE.get(a['Status'], a['Status'])} AD={a.get('AD')} answers={[x['data'] for x in a.get('Answer', []) if x['type'] == 1][:2]}") print(f" CD=1 : {RCODE.get(b['Status'], b['Status'])} AD={b.get('AD')} answers={[x['data'] for x in b.get('Answer', []) if x['type'] == 1][:2]}") print(f" DS at parent: {len(ds)} record(s)" + (f" e.g. {ds[0]['data'][:48]}..." if ds else "")) if ede: print(f" resolver note: {ede}") if a["Status"] == 0 and not [x for x in a.get("Answer", []) if x["type"] == 1]: why += " (Note: the answer holds no A record: NOERROR/NODATA, so the name exists but has no IPv4 address.)" print(f" verdict : {verdict} - {why}\n") if __name__ == "__main__": main(sys.argv[1:])