make-broken-zone.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. """Build a controlled DNSSEC zone (broken.example) as a binary DNS response packet.
  3. The zone presents a well-formed RRSIG (validly signed by its own DNSKEY) plus
  4. NSEC, but the DNSKEY cannot be anchored in the trust chain (broken.example is not
  5. delegated/signed by the root, and no parent DS exists). A validating resolver
  6. therefore MUST reject the data with SERVFAIL (without CD).
  7. Output: a DNS response packet for "www.broken.example A" suitable for a static
  8. UDP responder to serve to any query.
  9. """
  10. import struct, sys
  11. from cryptography.hazmat.primitives.asymmetric import rsa
  12. import dns.dnssec, dns.zone, dns.name, dns.rdataclass, dns.rdatatype
  13. ORIGIN = "broken.example."
  14. z = dns.zone.from_text(
  15. """
  16. broken.example. 300 IN SOA ns1.broken.example. admin.broken.example. 1 3600 900 604800 300
  17. broken.example. 300 IN NS ns1.broken.example.
  18. ns1.broken.example. 300 IN A 192.0.2.53
  19. www.broken.example. 300 IN A 192.0.2.1
  20. """,
  21. origin=dns.name.from_text(ORIGIN), relativize=False,
  22. )
  23. pk = rsa.generate_private_key(public_exponent=65537, key_size=2048)
  24. dnskey = dns.dnssec.make_dnskey(pk.public_key(), 8, flags=257, protocol=3)
  25. dns.dnssec.sign_zone(z, keys=[(pk, dnskey)], add_dnskey=True,
  26. deterministic=True, lifetime=3600)
  27. qname = dns.name.from_text("www.broken.example.")
  28. # Collect rrsets via iteration (find_rrset has a relativize quirk).
  29. # RRSIGs with different 'covers' are separate rdatasets -> gather all.
  30. a_rrset = None
  31. a_rrsig = None
  32. k_rrset = None
  33. for name, rds in z.iterate_rdatasets():
  34. if name == qname and rds.rdtype == dns.rdatatype.A:
  35. a_rrset = rds
  36. if name == qname and rds.rdtype == dns.rdatatype.RRSIG:
  37. # pick the RRSIG that covers the A rrset
  38. if any(r.covers() == dns.rdatatype.A for r in rds):
  39. a_rrsig = rds
  40. if name == dns.name.from_text(ORIGIN) and rds.rdtype == dns.rdatatype.DNSKEY:
  41. k_rrset = rds
  42. ans = list(a_rrset) + list(a_rrsig)
  43. def enc(n):
  44. return n.to_wire()
  45. def rd_bytes(rd):
  46. w = rd.to_wire()
  47. return struct.pack(">HHIH", rd.rdtype, 1, 300, len(w)) + w
  48. tid = 0xabcd
  49. flags = 0x8500 # QR | AA | RA
  50. pkt = struct.pack(">HHHHHH", tid, flags, 1, len(ans), 0, len(k_rrset))
  51. pkt += enc(qname) + struct.pack(">HH", 1, 1)
  52. for rd in ans:
  53. pkt += enc(qname) + rd_bytes(rd)
  54. for rd in k_rrset:
  55. pkt += enc(dns.name.from_text(ORIGIN)) + rd_bytes(rd)
  56. out = sys.argv[1] if len(sys.argv) > 1 else "broken-zone.bin"
  57. open(out, "wb").write(pkt)
  58. print(f"wrote {out}: {len(pkt)} bytes "
  59. f"({len(a_rrset)} A + {len(a_rrsig)} RRSIG answer, {len(k_rrset)} DNSKEY add'l)")
  60. print("A :", a_rrset[0].to_text())
  61. print("RRSIG :", a_rrsig[0].to_text()[:75], "...")
  62. print("DNSKEY:", k_rrset[0].to_text()[:55], "...")