Skip to content

Commit 6a7447d

Browse files
TMHSDigitalclaude
andcommitted
Stop calling NODATA takeover targets dangling (#362)
Go reports a name that exists without A/AAAA records (NODATA) as not-found, the same as NXDOMAIN, so a CNAME target or NS host with only MX or TXT records was marked dangling or dangling-ns although it cannot be registered by anyone else. When the address lookup comes back empty, the takeover check now sends one A query and reads its RCODE: NOERROR means the target exists, only NXDOMAIN marks it, and a failed probe is inconclusive. The raw query path from lookupCNAME now takes a qtype. Fixes #362 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5a0e395 commit 6a7447d

6 files changed

Lines changed: 125 additions & 22 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6868
- Less memory on large wordlists and busy scans: `-sources`, `-ct`, `-axfr` and `-permute` check their names against the wordlist without building a set as large as it (about 200 MB per feature and target on a 5M-line list), a `-simulate-zone` lab scan no longer keeps a count per queried name (about 170 MB on a 1M-line list), and a `CNAME` lookup reads a UDP answer into a buffer sized for the 1232-byte EDNS payload it advertises instead of 64 KB. Results are unchanged (#321).
6969

7070
### Fixed
71+
- A CNAME target or name server that exists but has no A or AAAA record (NODATA, such as a name with only MX or TXT records) is no longer marked `dangling` or `dangling-ns`: Go reports it as not-found like a name that does not exist, so a target whose address lookup comes back empty now gets one more A query, and only an NXDOMAIN answer marks it. A probe that times out or fails marks nothing (#362).
7172
- A wordlist entry that already ends in the target, such as a full name pasted from another tool, is scanned as the prefix in front of it: `www.example.com` under `example.com` is `www`, not `www.example.com.example.com`, and one that repeats an entry already in the list counts as a duplicate. The scan notes how many entries it shortened (#351).
7273
- With `-retry-inconclusive`, names whose lookup failed before a run stopped (interrupt, `-max-duration` or the reliability guard), and that its retry pass never got to, are saved in the resume state and retried by the resumed run; one that then answers is reported and leaves the `-inconclusive` list (#315).
7374
- A run stopped by `-max-duration`, the reliability guard or an interrupt, then resumed, reports each name once: `excluded` is no longer counted by both runs, and `processed`, `total` and the outcome counts no longer include the lookups the resumed run repeats (a chunk reported after the stop, and under `-recursive` every lookup below the first level), so `processed` cannot exceed `total`. Names the resumed run looks up again also leave the saved inconclusive list (#341).

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ When a result has a CNAME record, subenum resolves the CNAME target with the tru
600600

601601
A recursive resolver asked about a subzone whose every name server is gone usually answers SERVFAIL instead of the delegation, so such a name is reported as inconclusive (see `-inconclusive`) rather than marked; scanning against the parent zone's authoritative server finds it.
602602

603+
A target or name server that exists but has no A or AAAA record (NODATA, such as a name with only MX or TXT records) is not dangling: when the address lookup comes back empty, subenum sends one more A query and marks the name only on an NXDOMAIN answer. A probe that fails (timeout, SERVFAIL) marks nothing.
604+
603605
A CNAME target or name server that `-exclude` puts out of scope is never looked up: it can still get `provider:<name>`, which needs no query, but never `dangling` or `dangling-ns`.
604606

605607
The marker is a `takeover_candidate` field in JSON and JSONL, a `takeover_candidate` column in CSV, `TAKEOVER?=<marker>` in text with `-show-records`, and a count in the breakdown and the run-quality report. A dangling CNAME makes A/AAAA lookups fail, so scan with CNAME records included to find them: `-type A,AAAA,CNAME`.

‎internal/dns/cname.go‎

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,51 @@ func lookupCNAME(ctx context.Context, resolver *net.Resolver, name string) (stri
3737
if err != nil || !validLabels(name) {
3838
return "", &net.DNSError{Err: "no such host", Name: name, IsNotFound: true}
3939
}
40-
msg, err := cnameExchange(ctx, resolver, "udp", qname)
40+
msg, err := rawLookup(ctx, resolver, name, qname, dnsmessage.TypeCNAME)
41+
if err != nil {
42+
return "", err
43+
}
44+
return canonicalName(msg, name), nil
45+
}
46+
47+
// rawLookup sends one qtype query for name over resolver.Dial, asking again
48+
// over TCP when the UDP answer is truncated, and returns a NOERROR answer.
49+
// Any other RCODE comes back as the *net.DNSError Go's resolver would give.
50+
func rawLookup(ctx context.Context, resolver *net.Resolver, name string, qname dnsmessage.Name, qtype dnsmessage.Type) (*dnsmessage.Message, error) {
51+
msg, err := cnameExchange(ctx, resolver, "udp", qname, qtype)
4152
if err == nil && msg.Truncated {
42-
msg, err = cnameExchange(ctx, resolver, "tcp", qname)
53+
msg, err = cnameExchange(ctx, resolver, "tcp", qname, qtype)
4354
}
4455
if err != nil {
45-
return "", dnsErrorFor(ctx, name, err)
56+
return nil, dnsErrorFor(ctx, name, err)
4657
}
4758
switch msg.RCode {
4859
case dnsmessage.RCodeSuccess:
4960
case dnsmessage.RCodeNameError:
50-
return "", &net.DNSError{Err: "no such host", Name: name, IsNotFound: true}
61+
return nil, &net.DNSError{Err: "no such host", Name: name, IsNotFound: true}
5162
default:
5263
// SERVFAIL, REFUSED and the rest, as Go reports them; fixErr names
5364
// REFUSED from the RCODE the connection saw.
54-
return "", &net.DNSError{Err: "server misbehaving", Name: name, IsTemporary: msg.RCode == dnsmessage.RCodeServerFailure}
65+
return nil, &net.DNSError{Err: "server misbehaving", Name: name, IsTemporary: msg.RCode == dnsmessage.RCodeServerFailure}
5566
}
56-
return canonicalName(msg, name), nil
67+
return msg, nil
68+
}
69+
70+
// nameExists sends one A query for name and returns nil when the answer is
71+
// NOERROR, with or without addresses: the name, or the end of its alias
72+
// chain, exists (#362). Go's resolver reports NODATA as not-found, the same
73+
// as NXDOMAIN; only RCODE 3 comes back here as a not-found error.
74+
func nameExists(ctx context.Context, resolver *net.Resolver, name string) error {
75+
if resolver == nil || resolver.Dial == nil {
76+
_, err := resolver.LookupIPAddr(ctx, name)
77+
return err
78+
}
79+
qname, err := dnsmessage.NewName(name)
80+
if err != nil || !validLabels(name) {
81+
return &net.DNSError{Err: "no such host", Name: name, IsNotFound: true}
82+
}
83+
_, err = rawLookup(ctx, resolver, name, qname, dnsmessage.TypeA)
84+
return err
5785
}
5886

5987
// canonicalName follows the CNAME records in msg's answer from name, at
@@ -77,11 +105,11 @@ func canonicalName(msg *dnsmessage.Message, name string) string {
77105
return canonical
78106
}
79107

80-
// cnameExchange sends one CNAME query for qname over a connection from
81-
// resolver.Dial and returns the matching reply. On UDP, replies to other
82-
// queries (a pooled socket can hold a late answer to an earlier one) are
83-
// skipped until the deadline.
84-
func cnameExchange(ctx context.Context, resolver *net.Resolver, network string, qname dnsmessage.Name) (*dnsmessage.Message, error) {
108+
// cnameExchange sends one qtype query (CNAME for lookupCNAME) for qname over
109+
// a connection from resolver.Dial and returns the matching reply. On UDP,
110+
// replies to other queries (a pooled socket can hold a late answer to an
111+
// earlier one) are skipped until the deadline.
112+
func cnameExchange(ctx context.Context, resolver *net.Resolver, network string, qname dnsmessage.Name, qtype dnsmessage.Type) (*dnsmessage.Message, error) {
85113
conn, err := resolver.Dial(ctx, network, "")
86114
if err != nil {
87115
return nil, err
@@ -98,7 +126,7 @@ func cnameExchange(ctx context.Context, resolver *net.Resolver, network string,
98126
return nil, err
99127
}
100128
id := binary.BigEndian.Uint16(idb[:])
101-
query, err := cnameQuery(id, qname)
129+
query, err := cnameQuery(id, qname, qtype)
102130
if err != nil {
103131
return nil, err
104132
}
@@ -144,7 +172,7 @@ func cnameExchange(ctx context.Context, resolver *net.Resolver, network string,
144172
return nil, err
145173
}
146174
if msg.ID != id || !msg.Response || len(msg.Questions) != 1 ||
147-
msg.Questions[0].Type != dnsmessage.TypeCNAME || !strings.EqualFold(msg.Questions[0].Name.String(), qname.String()) {
175+
msg.Questions[0].Type != qtype || !strings.EqualFold(msg.Questions[0].Name.String(), qname.String()) {
148176
if packet {
149177
continue // a late answer to an earlier query on this socket
150178
}
@@ -157,7 +185,7 @@ func cnameExchange(ctx context.Context, resolver *net.Resolver, network string,
157185
// ednsPayload is the UDP answer size cnameQuery advertises, Go's resolver's.
158186
const ednsPayload = 1232
159187

160-
// cnameQuery builds a recursive CNAME query with an EDNS(0) OPT record
188+
// cnameQuery builds a recursive qtype query with an EDNS(0) OPT record
161189
// advertising 1232-byte UDP answers, as Go's resolver sends.
162190
// validLabels reports whether every label of name, which may end in a dot,
163191
// is 1 to 63 bytes long.
@@ -170,13 +198,13 @@ func validLabels(name string) bool {
170198
return true
171199
}
172200

173-
func cnameQuery(id uint16, qname dnsmessage.Name) ([]byte, error) {
201+
func cnameQuery(id uint16, qname dnsmessage.Name, qtype dnsmessage.Type) ([]byte, error) {
174202
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{ID: id, RecursionDesired: true})
175203
b.EnableCompression()
176204
if err := b.StartQuestions(); err != nil {
177205
return nil, err
178206
}
179-
if err := b.Question(dnsmessage.Question{Name: qname, Type: dnsmessage.TypeCNAME, Class: dnsmessage.ClassINET}); err != nil {
207+
if err := b.Question(dnsmessage.Question{Name: qname, Type: qtype, Class: dnsmessage.ClassINET}); err != nil {
180208
return nil, err
181209
}
182210
if err := b.StartAdditionals(); err != nil {

‎internal/dns/resolver.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,21 @@ func (l Lookup) lookupOne(ctx context.Context, domain, t string) ([]Record, erro
391391
return recs, nil
392392
}
393393

394+
// probeExists is nameExists under one rate slot, l.Timeout and a wire
395+
// budget, as lookupOne runs a type's lookup.
396+
func (l Lookup) probeExists(ctx context.Context, domain string) error {
397+
if err := limiterFrom(ctx).Wait(ctx); err != nil {
398+
return err
399+
}
400+
b := &wireBudget{prepaid: 1}
401+
probeCtx, cancel := context.WithTimeout(context.WithValue(ctx, budgetKey{}, b), l.Timeout)
402+
defer cancel()
403+
if err := nameExists(probeCtx, l.Resolver, fqdn(domain)); err != nil {
404+
return b.fixErr(err)
405+
}
406+
return nil
407+
}
408+
394409
// worse keeps the more severe of two lookup errors. Any infrastructure failure
395410
// (timeout, refused, SERVFAIL, cancellation) outranks a not-found answer: a name
396411
// is a definitive negative only if every requested type said so. Otherwise an

‎internal/dns/takeover.go‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,11 @@ func HintsTakeover(types []string) bool {
147147
const DanglingNS = "dangling-ns"
148148

149149
// TakeoverHint inspects a result's CNAME and NS records and returns a
150-
// takeover marker: "dangling" when the CNAME target does not resolve
150+
// takeover marker: "dangling" when the CNAME target does not exist
151151
// (NXDOMAIN), "provider:<name>" when it points at a takeover-prone service,
152152
// "dangling:<name>" when both hold, and "dangling-ns" when the name is
153-
// delegated to a name server that does not resolve. It returns "" when there
153+
// delegated to a name server that does not exist. A target that exists
154+
// without addresses (NODATA) is not dangling (#362). It returns "" when there
154155
// is nothing suspicious. Targets are resolved with l.Resolver, l.Timeout and
155156
// l.Attempts for DefaultTypes, without logging (l.Types and l.Logf are
156157
// unused); an inconclusive lookup (timeout, SERVFAIL) is not called dangling.
@@ -241,6 +242,20 @@ func (c *TakeoverCache) skip(name string) bool {
241242
return c.Skip != nil && c.Skip(strings.TrimSuffix(name, "."))
242243
}
243244

245+
// targetOutcome resolves target (A and AAAA). Go reports a name that exists
246+
// without addresses (NODATA) as not-found, the same as one that does not
247+
// exist, but only the second can be registered by someone else (#362). So a
248+
// negative is checked with one A query whose RCODE tells them apart: NOERROR
249+
// is OutcomeFound (the target exists), NXDOMAIN stays OutcomeNXDomain, and
250+
// anything else is inconclusive.
251+
func targetOutcome(ctx context.Context, l Lookup, target string) Outcome {
252+
_, outcome := l.forTarget().Resolve(ctx, target)
253+
if outcome != OutcomeNXDomain {
254+
return outcome
255+
}
256+
return Classify(l.probeExists(ctx, target))
257+
}
258+
244259
// outcome resolves target (A and AAAA) once for the life of the cache.
245260
func (c *TakeoverCache) outcome(ctx context.Context, l Lookup, target string) Outcome {
246261
key := strings.ToLower(strings.TrimSuffix(target, "."))
@@ -255,7 +270,7 @@ func (c *TakeoverCache) outcome(ctx context.Context, l Lookup, target string) Ou
255270
c.entries[key] = e
256271
c.mu.Unlock()
257272
c.lookups.Add(1)
258-
_, e.outcome = l.forTarget().Resolve(ctx, target)
273+
e.outcome = targetOutcome(ctx, l, target)
259274
if e.outcome != OutcomeFound && e.outcome != OutcomeNXDomain {
260275
c.mu.Lock()
261276
delete(c.entries, key) // inconclusive: let a later name retry

‎internal/dns/takeover_test.go‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"slices"
66
"strings"
77
"sync"
8+
"sync/atomic"
89
"testing"
910
"time"
1011

@@ -196,9 +197,50 @@ func TestTakeoverCache(t *testing.T) {
196197
if c.Lookups() != 2 {
197198
t.Errorf("cache sent %d target lookups, want 2", c.Lookups())
198199
}
199-
for target := range targets {
200-
if n := srv.QueriesFor(target, dnsmessage.TypeA); n != 1 {
201-
t.Errorf("server saw %d A queries for %s, want 1", n, target)
200+
// A negative is checked once more for its RCODE (#362): the address
201+
// lookup plus one A query for the missing target.
202+
for target, want := range map[string]int64{"gone.example.net": 2, "acme.herokuapp.com": 1} {
203+
if n := srv.QueriesFor(target, dnsmessage.TypeA); n != want {
204+
t.Errorf("server saw %d A queries for %s, want %d", n, target, want)
205+
}
206+
}
207+
}
208+
209+
// TestTakeoverHintNODATA covers #362: a CNAME target or name server that
210+
// exists without addresses (NOERROR, no answer) is not dangling, while one
211+
// that does not exist (NXDOMAIN) still is, and a probe that fails is not
212+
// called dangling either.
213+
func TestTakeoverHintNODATA(t *testing.T) {
214+
var flakyCalls atomic.Int32
215+
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
216+
switch q.Name {
217+
case "mail-only.example.net", "ns-noaddr.example.net":
218+
if q.Type == dnsmessage.TypeMX {
219+
return dnstest.Reply{MX: []string{"10 mx1.example.net"}}
220+
}
221+
return dnstest.Reply{} // NODATA
222+
case "flaky.example.net":
223+
if q.Type == dnsmessage.TypeA && flakyCalls.Add(1) > 1 {
224+
return dnstest.ServFail // the RCODE probe
225+
}
226+
return dnstest.Reply{}
227+
}
228+
return dnstest.NXDomain
229+
})
230+
l := Lookup{Resolver: NewResolver(time.Second, srv.Addr), Timeout: time.Second, Attempts: 1}
231+
ctx := context.Background()
232+
for _, tc := range []struct {
233+
records []Record
234+
want string
235+
}{
236+
{[]Record{{Type: "CNAME", Value: "mail-only.example.net"}}, ""},
237+
{[]Record{{Type: "CNAME", Value: "gone.example.net"}}, "dangling"},
238+
{[]Record{{Type: "CNAME", Value: "flaky.example.net"}}, ""},
239+
{[]Record{{Type: "NS", Value: "ns-noaddr.example.net"}}, ""},
240+
{[]Record{{Type: "NS", Value: "ns-gone.example.net"}}, DanglingNS},
241+
} {
242+
if got := TakeoverHint(ctx, l, tc.records); got != tc.want {
243+
t.Errorf("TakeoverHint(%v) = %q, want %q", tc.records, got, tc.want)
202244
}
203245
}
204246
}

0 commit comments

Comments
 (0)