Commit b7f2b3abf3 for bind
commit b7f2b3abf358b3a89450d7f9c22f697e2c5c47f2
Author: Štěpán Balážik <stepan@isc.org>
Date: Mon Oct 19 18:09:07 2026 +0200
Sign the f023 zone with isctest.zone
ans4 loaded a key from keys.json and signed every answer by hand. The
fixture now signs f023.test with its insecure delegation to evil, and
the server answers from the zone except for the forged NSEC. Its RRSIG
keeps the key tag no parent key has, which is what makes the resolver
take the NSEC for insecure rather than bogus. The forging handler goes
into nsec_synthesis_ans.py, for ans1 to share.
Assisted-by: Claude:claude-fable-5-1
diff --git a/bin/tests/system/nsec_synthesis/ans4/ans.py b/bin/tests/system/nsec_synthesis/ans4/ans.py
index d30e96bc3f..5c91b5988c 100644
--- a/bin/tests/system/nsec_synthesis/ans4/ans.py
+++ b/bin/tests/system/nsec_synthesis/ans4/ans.py
@@ -9,14 +9,30 @@
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
+
+import dns.rcode
+import dns.rdatatype
+
from isctest.asyncserver import AsyncDnsServer
-from nsec_synthesis.ans4 import common, f023
+from isctest.asyncserver.matchers import Qname, Qtype
+
+from ..nsec_synthesis_ans import AttackNsecHandler
+
+
+class AttackChildNsecHandler(AttackNsecHandler):
+ """
+ An NSEC for a name in the insecure child zone evil.f023.test whose next
+ name reaches past target.f023.test in the signed parent.
+ """
+
+ matcher = Qname("0.evil.f023.test.") & Qtype(dns.rdatatype.NSEC)
+ nsec = "zzz.f023.test. A RRSIG NSEC"
+ key_tag = 12345
def main() -> None:
- keys = common.load_keys()
- server = AsyncDnsServer(default_aa=True)
- server.install_response_handler(f023.F023Handler(keys))
+ server = AsyncDnsServer(default_rcode=dns.rcode.NOERROR)
+ server.install_response_handlers(AttackChildNsecHandler())
server.run()
diff --git a/bin/tests/system/nsec_synthesis/ans4/common.py b/bin/tests/system/nsec_synthesis/ans4/common.py
deleted file mode 100644
index da10366591..0000000000
--- a/bin/tests/system/nsec_synthesis/ans4/common.py
+++ /dev/null
@@ -1,105 +0,0 @@
-# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
-#
-# SPDX-License-Identifier: MPL-2.0
-#
-# This Source Code Form is subject to the terms of the Mozilla Public
-# License, v. 2.0. If a copy of the MPL was not distributed with this
-# file, you can obtain one at https://mozilla.org/MPL/2.0/.
-#
-# See the COPYRIGHT file distributed with this work for additional
-# information regarding copyright ownership.
-
-from dataclasses import dataclass
-from pathlib import Path
-
-import json
-
-from cryptography.hazmat.primitives import serialization
-
-import dns.dnssec
-import dns.flags
-import dns.message
-import dns.name
-import dns.rcode
-import dns.rdata
-import dns.rdataclass
-import dns.rdatatype
-import dns.rrset
-
-from isctest.asyncserver import QueryContext
-
-TTL = 300
-
-
-@dataclass(frozen=True)
-class Key:
- zone: dns.name.Name
- private_key: object
- dnskey: dns.rdata.Rdata
- ds: dns.rdata.Rdata
-
-
-def name(text: str) -> dns.name.Name:
- return dns.name.from_text(text)
-
-
-def load_keys() -> dict[str, Key]:
- keys = {}
-
- path = Path(".") / "keys.json"
- if not path.exists():
- return keys
-
- with path.open(encoding="utf-8") as keys_file:
- raw_keys = json.load(keys_file)
-
- for zone, raw_key in raw_keys.items():
- private_key = serialization.load_pem_private_key(
- raw_key["private_pem"].encode("ascii"),
- password=None,
- )
- dnskey = dns.rdata.from_text(
- dns.rdataclass.IN, dns.rdatatype.DNSKEY, raw_key["dnskey"]
- )
- ds = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.DS, raw_key["ds"])
- keys[zone] = Key(name(zone), private_key, dnskey, ds)
-
- return keys
-
-
-def rrset(owner: str, rdtype: dns.rdatatype.RdataType, *rdatas: str) -> dns.rrset.RRset:
- return dns.rrset.from_text(owner, TTL, dns.rdataclass.IN, rdtype, *rdatas)
-
-
-def rrset_from_rdata(owner: str, rdata: dns.rdata.Rdata) -> dns.rrset.RRset:
- return dns.rrset.from_rdata(owner, TTL, rdata)
-
-
-def add_signed(
- section: list[dns.rrset.RRset], covered: dns.rrset.RRset, signer: Key
-) -> None:
- rrsig = dns.dnssec.sign(
- covered,
- signer.private_key,
- signer.zone,
- signer.dnskey,
- lifetime=86400,
- verify=True,
- )
- section.append(covered)
- section.append(dns.rrset.from_rdata(covered.name, covered.ttl, rrsig))
-
-
-def soa_rrset(zone) -> dns.rrset.RRset:
- return rrset(
- zone,
- dns.rdatatype.SOA,
- f"ns.{zone} hostmaster.{zone} 1 3600 600 86400 300",
- )
-
-
-def prepare_response(qctx: QueryContext) -> dns.message.Message:
- qctx.prepare_new_response(with_zone_data=False)
- qctx.response.flags |= dns.flags.AA
- qctx.response.set_rcode(dns.rcode.NOERROR)
- return qctx.response
diff --git a/bin/tests/system/nsec_synthesis/ans4/f023.py b/bin/tests/system/nsec_synthesis/ans4/f023.py
deleted file mode 100644
index 4f7cd23f30..0000000000
--- a/bin/tests/system/nsec_synthesis/ans4/f023.py
+++ /dev/null
@@ -1,131 +0,0 @@
-# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
-#
-# SPDX-License-Identifier: MPL-2.0
-#
-# This Source Code Form is subject to the terms of the Mozilla Public
-# License, v. 2.0. If a copy of the MPL was not distributed with this
-# file, you can obtain one at https://mozilla.org/MPL/2.0/.
-#
-# See the COPYRIGHT file distributed with this work for additional
-# information regarding copyright ownership.
-
-from collections.abc import AsyncGenerator
-from datetime import datetime, timedelta, timezone
-
-import base64
-
-import dns.message
-import dns.rcode
-import dns.rdata
-import dns.rdataclass
-import dns.rdatatype
-import dns.rrset
-
-from isctest.asyncserver import QueryContext, ResponseHandler
-from isctest.asyncserver.actions import DnsResponseSend
-from isctest.asyncserver.matchers import Domain
-from nsec_synthesis.ans4.common import (
- Key,
- add_signed,
- name,
- prepare_response,
- rrset,
- rrset_from_rdata,
- soa_rrset,
-)
-
-TTL = 300
-F023_ZONE = "f023.test."
-EVIL = f"evil.{F023_ZONE}"
-POISON = f"0.{EVIL}"
-POISON_NEXT = f"zzz.{F023_ZONE}"
-TARGET = f"target.{F023_ZONE}"
-TARGET_A = "192.0.2.77"
-
-
-def nsec_rrset(owner: str, next_name: str, *types: str) -> dns.rrset.RRset:
- return rrset(owner, dns.rdatatype.NSEC, f"{next_name} {' '.join(types)}")
-
-
-def parent_apex_nsec() -> dns.rrset.RRset:
- return nsec_rrset(F023_ZONE, EVIL, "NS", "SOA", "RRSIG", "NSEC", "DNSKEY")
-
-
-def evil_delegation_nsec() -> dns.rrset.RRset:
- return nsec_rrset(EVIL, f"ns.{F023_ZONE}", "NS", "RRSIG", "NSEC")
-
-
-def malicious_nsec() -> dns.rrset.RRset:
- return nsec_rrset(POISON, POISON_NEXT, "A", "RRSIG", "NSEC")
-
-
-def garbage_rrsig(covered: dns.rrset.RRset) -> dns.rrset.RRset:
- now = datetime.now(timezone.utc)
- inception = (now - timedelta(hours=1)).strftime("%Y%m%d%H%M%S")
- expiration = (now + timedelta(days=1)).strftime("%Y%m%d%H%M%S")
- signature = base64.b64encode(bytes(64)).decode("ascii")
- labels = len(covered.name.labels) - 1
- text = (
- f"{dns.rdatatype.to_text(covered.rdtype)} "
- f"13 {labels} {covered.ttl} {expiration} {inception} "
- f"12345 {F023_ZONE} {signature}"
- )
- rdata = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.RRSIG, text)
- return dns.rrset.from_rdata(covered.name, covered.ttl, rdata)
-
-
-def add_parent_denial(
- response: dns.message.Message, signer: Key, nsec: dns.rrset.RRset
-) -> None:
- add_signed(response.authority, soa_rrset(F023_ZONE), signer)
- add_signed(response.authority, nsec, signer)
-
-
-class F023Handler(ResponseHandler):
- matcher = Domain(F023_ZONE, EVIL)
-
- def __init__(self, keys: dict[str, Key]) -> None:
- super().__init__()
- self.keys = keys
-
- if F023_ZONE not in keys:
- return
-
- self.key = keys[F023_ZONE]
- self.parent = name(F023_ZONE)
- self.evil = name(EVIL)
- self.poison = name(POISON)
- self.target = name(TARGET)
-
- async def get_responses(
- self, qctx: QueryContext
- ) -> AsyncGenerator[DnsResponseSend, None]:
- response = prepare_response(qctx)
-
- if qctx.qname == self.parent and qctx.qtype == dns.rdatatype.DNSKEY:
- add_signed(
- response.answer,
- rrset_from_rdata(F023_ZONE, self.key.dnskey),
- self.key,
- )
- elif qctx.qname == self.parent and qctx.qtype == dns.rdatatype.SOA:
- add_signed(response.answer, soa_rrset(F023_ZONE), self.key)
- elif qctx.qname == self.parent and qctx.qtype == dns.rdatatype.NSEC:
- add_signed(response.answer, parent_apex_nsec(), self.key)
- elif qctx.qname == self.evil and qctx.qtype == dns.rdatatype.DS:
- add_parent_denial(response, self.key, evil_delegation_nsec())
- elif qctx.qname == self.poison and qctx.qtype == dns.rdatatype.NSEC:
- nsec = malicious_nsec()
- response.answer.append(nsec)
- response.answer.append(garbage_rrsig(nsec))
- elif qctx.qname == self.target and qctx.qtype == dns.rdatatype.A:
- add_signed(
- response.answer,
- rrset(TARGET, dns.rdatatype.A, TARGET_A),
- self.key,
- )
- else:
- response.set_rcode(dns.rcode.NXDOMAIN)
- add_parent_denial(response, self.key, parent_apex_nsec())
-
- yield DnsResponseSend(response, authoritative=True)
diff --git a/bin/tests/system/nsec_synthesis/ans4/zones/f023.test.db.j2.manual b/bin/tests/system/nsec_synthesis/ans4/zones/f023.test.db.j2.manual
new file mode 100644
index 0000000000..367ff62c2d
--- /dev/null
+++ b/bin/tests/system/nsec_synthesis/ans4/zones/f023.test.db.j2.manual
@@ -0,0 +1,3 @@
+{% include "_common/zones/template.db.j2.manual" %}
+
+target A 192.0.2.77
diff --git a/bin/tests/system/nsec_synthesis/common.py b/bin/tests/system/nsec_synthesis/common.py
index 05beb4e938..a24ad70c06 100644
--- a/bin/tests/system/nsec_synthesis/common.py
+++ b/bin/tests/system/nsec_synthesis/common.py
@@ -25,7 +25,10 @@ SIGNED_ZONE_MARKS = [
isctest.mark.with_ecdsa_deterministic,
pytest.mark.extra_artifacts(
[
+ "ans*/dsset-*",
"ans*/keys.json",
+ "ans*/zones/*.db",
+ "ans*/zones/*.db.signed",
]
),
]
diff --git a/bin/tests/system/nsec_synthesis/nsec_synthesis_ans.py b/bin/tests/system/nsec_synthesis/nsec_synthesis_ans.py
new file mode 100644
index 0000000000..93af3a7a06
--- /dev/null
+++ b/bin/tests/system/nsec_synthesis/nsec_synthesis_ans.py
@@ -0,0 +1,53 @@
+# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+#
+# SPDX-License-Identifier: MPL-2.0
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, you can obtain one at https://mozilla.org/MPL/2.0/.
+#
+# See the COPYRIGHT file distributed with this work for additional
+# information regarding copyright ownership.
+
+
+from collections.abc import AsyncGenerator
+
+import dns.name
+import dns.rdataclass
+import dns.rdatatype
+import dns.rrset
+
+from isctest.asyncserver import QueryContext, ResponseHandler
+from isctest.asyncserver.actions import DnsResponseSend
+
+
+def rrset(
+ owner: str | dns.name.Name,
+ rdtype: dns.rdatatype.RdataType,
+ rdata: str,
+ ttl: int = 300,
+) -> dns.rrset.RRset:
+ return dns.rrset.from_text(owner, ttl, dns.rdataclass.IN, rdtype, rdata)
+
+
+class AttackNsecHandler(ResponseHandler):
+ """
+ Answer an NSEC query with a forged NSEC owned by the query name, under an
+ RRSIG naming the zone as signer but a key tag no key of the zone has, so
+ that the resolver neither verifies nor fails it.
+ """
+
+ nsec: str
+ key_tag: int
+
+ async def get_responses(
+ self, qctx: QueryContext
+ ) -> AsyncGenerator[DnsResponseSend, None]:
+ qctx.prepare_new_response(with_zone_data=False)
+ nsec_rrset = rrset(qctx.qname, dns.rdatatype.NSEC, self.nsec)
+ qctx.response.answer.append(nsec_rrset)
+ rrsig = qctx.sign(nsec_rrset, bogus=True)[0].replace(key_tag=self.key_tag)
+ qctx.response.answer.append(
+ dns.rrset.from_rdata(nsec_rrset.name, nsec_rrset.ttl, rrsig)
+ )
+ yield DnsResponseSend(qctx.response, authoritative=True)
diff --git a/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py b/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
index 56eab9f9af..983c1d3c4e 100644
--- a/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
+++ b/bin/tests/system/nsec_synthesis/tests_nsec_child_next.py
@@ -9,20 +9,11 @@
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
-from pathlib import Path
-
-import json
-
-from isctest.template import TrustAnchor
-from nsec_synthesis.common import (
- SIGNED_ZONE_MARKS,
- check_forged_nsec,
- get_nsec,
- has_a,
- make_key,
-)
+from nsec_synthesis.common import SIGNED_ZONE_MARKS, check_forged_nsec, get_nsec, has_a
import isctest
+import isctest.template
+import isctest.zone
F023_ZONE = "f023.test."
EVIL = f"evil.{F023_ZONE}"
@@ -30,18 +21,18 @@ POISON = f"0.{EVIL}"
POISON_NEXT = f"zzz.{F023_ZONE}"
TARGET = f"target.{F023_ZONE}"
TARGET_A = "192.0.2.77"
-AUTH = "10.53.0.4"
-RESOLVER = "10.53.0.2"
+AUTH = isctest.template.ANS4.ip
+RESOLVER = isctest.template.NS2.ip
pytestmark = SIGNED_ZONE_MARKS
def bootstrap():
- keys = {F023_ZONE: make_key(F023_ZONE)}
- Path("ans4/keys.json").write_text(json.dumps(keys, indent=2), encoding="ascii")
- dnskey = keys[F023_ZONE]["dnskey"].split()
- contents = f'{" ".join(dnskey[:3])} "{"".join(dnskey[3:])}"'
- return {"trust_anchors": [TrustAnchor(F023_ZONE, "static-key", contents)]}
+ evil = isctest.zone.Zone(EVIL, isctest.template.ANS4)
+ zone = isctest.zone.Zone(F023_ZONE, isctest.template.ANS4, signed=True)
+ zone.delegations = [evil]
+ zone.configure(csk=True)
+ return {"trust_anchors": zone.trust_anchors()}
def _prime_parent_for_aggressive_nsec():