Skip to content

Commit b8ffe26

Browse files
TMHSDigitalclaude
andcommitted
Let MCP scans run a permutation pass and a zone transfer (#405)
An agent driving subenum mcp got a weaker scan than the CLI: nothing mapped to -permute or -axfr. permute, on scan and lab_scan, runs a second pass after the scan over the variants of the names found that the scan did not test, within what it left of max_queries and -max-duration. Its finds join results, and the report's permutation gives what it tested and its own verdict. subenum mcp -axfr lets scan take zone_transfer. It asks the domain's name servers for a transfer before the scan, skipping excluded ones, and its names are verified like a source's. The report's zone_transfer lists the servers that allowed one and the zone's denial type. Without -axfr a call that sets it is refused. lab_scan has no zone_transfer: the lab server does not answer transfers (#434). Fixes #405 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a61dae6 commit b8ffe26

7 files changed

Lines changed: 404 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- An [output field reference](https://tmhsdigital.github.io/subenum/output.html) lists every JSON, JSONL and CSV result field and every `-stats` report field, with its type, when it is present and its values, and the site serves JSON Schema files for both (`schemas/result.v1.json`, `schemas/stats.v1.json`). A test fails when a field is added, renamed or removed in the code without the schema files (#433).
1313
- `-type SRV` looks up SRV records, written `<priority> <weight> <port> <target>` and sorted, so the same answer always reads the same. They live at service names, so the wordlist names those (`_sip._tcp`, `_autodiscover._tcp`). Lab zone files take `<name> SRV <priority> <weight> <port> <host>`, and `pkg/subenum` accepts `"SRV"` in `Config.Types` (#419).
1414
- `-recursive-w <file>` gives `-recursive` its own wordlist for depth 2 and below, so recursion no longer has to run the whole `-w` list under every name it finds: `-w` with 5,000 words and `-recursive-w` with 100 cost 5,000 + 100 per name found instead of 5,000 per name found. It is repeatable like `-w`, `-` reads stdin, and it can be set in the config file or `SUBENUM_RECURSIVE_W`. The recursion guard counts it (`with 5000 entries (100 below depth 1)`), the `-dry-run` notes say what each found name adds, and a resume refuses a changed file. Without it, every level uses the `-w` list as before (#403).
15+
- `subenum mcp`: `scan` and `lab_scan` take `permute`, a second pass over variants of the names found, as `-permute` does, within what the scan left of `max_queries` and `-max-duration`; the report's `permutation` says what it tested and its finds join `results`. `subenum mcp -axfr` lets `scan` take `zone_transfer`, which asks the domain's name servers for a zone transfer before the scan; the report's `zone_transfer` lists the servers that allowed one and the zone's denial type (`nsec`, `nsec3`, `none`). Without `-axfr` a call that sets it is refused (#405).
1516

1617
### Changed
1718
- The `-permute` pass is built in one set instead of three copies, and a target's pass builds at most 1,000,000 candidates (the seeds taken in sorted order, with a note when it stops there). For 50,000 seed names, as a large Certificate Transparency footprint gives, building the pass took about 11 s and 1.8 GB of allocations and now takes under 1 s and 150 MB. The `-dry-run` notes give the pass's bound: up to 93 candidates per name (#400).

‎docs/mcp.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ scan, and `depth` (up to 3) enumerates again under the names found. The report
8181
counts `retried`, `recovered`, `rate_backoffs` and `excluded`. Every one of
8282
these stays within `max_queries` and `-max-duration`.
8383

84+
`permute`, on `scan` and `lab_scan`, adds a second pass after the scan, as the
85+
command line's `-permute` does: it tests variants of the names found (`api`
86+
suggests `api-dev`, `dev-api`, `api2`) that the scan did not already test,
87+
within what the scan left of `max_queries` and `-max-duration`. Its finds join
88+
`results`, and the report's `permutation` gives its `candidates`, `tested`,
89+
`found`, `skipped_by_cap` and its own `verdict` and `reason`. A scan that
90+
spent the whole budget leaves the pass `degraded` with every variant skipped.
91+
8492
## Running it
8593

8694
```bash
@@ -97,6 +105,7 @@ subenum mcp -allow example.com,example.org # adds scan, for these domains
97105
| `-max-duration` | 2m | Longest one call may scan |
98106
| `-timeout` | 1000 | Live-scan lookup timeout in milliseconds |
99107
| `-sources` | none | Passive sources (`ct`, `wayback`) a `scan` call may ask for |
108+
| `-axfr` | off | Let a `scan` call ask the domain's name servers for a zone transfer (`zone_transfer`) |
100109
| `-max-calls` | 100 | Most `scan` calls one session may make; with `-max-queries` it caps the session's live lookups. A call refused for its arguments (`not_run` before any query) does not count |
101110

102111
Passive sources are off unless you enable them, because they send the domain
@@ -106,6 +115,17 @@ after dedupe, count against `max_queries`, and are each verified by a lookup.
106115
The report lists each source with the number of names it proposed, or the
107116
error it hit; a failed source does not stop the scan.
108117

118+
Zone transfers are off unless you start the server with `-axfr`, because
119+
their queries go to the target's own name servers (about four per server)
120+
and are not paced by `-rate`. With it, `scan` gains a `zone_transfer`
121+
argument that does what the command line's `-axfr` does before the scan:
122+
the names a transfer gives are verified by a lookup like a source's, and the
123+
report's `zone_transfer` lists the name servers that `allowed` one (anyone
124+
can then list the zone, a finding in itself) and the zone's `denial`
125+
(`nsec` means it can be walked). Name servers under `exclude` are not asked.
126+
Without `-axfr`, a call that sets it is refused. `lab_scan` has no
127+
`zone_transfer`: the lab server does not answer transfers.
128+
109129
The caps cannot be turned off: `-rate`, `-max-queries`, `-max-duration`,
110130
`-timeout` and `-max-calls` must be positive. A session is one run of the
111131
server: it ends when the client closes it. Calls the client sent before

‎internal/cli/mcp.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ type mcpConfig struct {
5858
timeout time.Duration // one lookup, live scans
5959
sources []string // passive sources a scan call may ask for (#268)
6060
maxCalls int // scan calls one session may make (#240)
61+
// axfr lets a scan call ask for a zone transfer (#405). Off by default:
62+
// its queries go to the target's own name servers and are not paced.
63+
axfr bool
64+
// transferPort is the port zone transfers go to; tests point it at a
65+
// local server. Empty means 53.
66+
transferPort string
6167
}
6268

6369
// defaultMCPMaxCalls bounds a session's scan calls, so an agent that loops
@@ -126,6 +132,7 @@ func parseMCPFlags(args []string, stderr io.Writer) (cfg mcpConfig, code int, ok
126132
fs.IntVar(&timeoutMs, "timeout", int(subenum.DefaultTimeout/time.Millisecond), "Live-scan DNS lookup timeout in milliseconds")
127133
fs.IntVar(&cfg.maxCalls, "max-calls", defaultMCPMaxCalls, "Most scan calls one session may make; with -max-queries it caps the session's live lookups")
128134
fs.StringVar(&sources, "sources", "", "Comma-separated passive sources ("+strings.Join(mcpSourceNames(), ", ")+") a scan call may ask for. Off by default: they send the domain to third parties")
135+
fs.BoolVar(&cfg.axfr, "axfr", false, "Let a scan call ask each name server of the domain for a zone transfer (zone_transfer). Off by default: those queries go to the target's own name servers and are not paced by -rate")
129136
fs.Usage = func() {
130137
_, _ = fmt.Fprintf(stderr, "Usage: %s mcp [flags]\n\n", ProgramName)
131138
_, _ = fmt.Fprintln(stderr, "Serves subenum to AI agents over the Model Context Protocol (stdio).")
@@ -199,7 +206,8 @@ func parseMCPFlags(args []string, stderr io.Writer) (cfg mcpConfig, code int, ok
199206

200207
func mcpServer(cfg mcpConfig) *mcp.Server {
201208
tools := []mcp.Tool{labScanTool(cfg)}
202-
instructions := "subenum enumerates subdomains by DNS. lab_scan practises on local fictional zones and is always safe. "
209+
instructions := "subenum enumerates subdomains by DNS. lab_scan practises on local fictional zones and is always safe. " +
210+
"Both scan tools take permute, a second pass over variants of the names found (api suggests api-dev, api2) within the same max_queries. "
203211
if len(cfg.allow) > 0 {
204212
tools = append(tools, scanTool(cfg))
205213
instructions += fmt.Sprintf("scan enumerates real domains, only under %s, with at most %d names per call at %d queries per second. ",
@@ -208,6 +216,9 @@ func mcpServer(cfg mcpConfig) *mcp.Server {
208216
instructions += fmt.Sprintf("scan can also seed candidates from passive sources (%s) when a call lists them in sources. ",
209217
strings.Join(cfg.sources, ", "))
210218
}
219+
if cfg.axfr {
220+
instructions += "scan can ask the domain's name servers for a zone transfer when a call sets zone_transfer; one that allows it is a finding in itself. "
221+
}
211222
} else {
212223
instructions += "Live scanning is off: the operator allowed no domains. "
213224
}

‎internal/cli/mcp_permute_test.go‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package cli
2+
3+
import (
4+
"encoding/json"
5+
"net"
6+
"slices"
7+
"strings"
8+
"testing"
9+
"time"
10+
11+
"github.com/TMHSDigital/subenum/internal/dnstest"
12+
"golang.org/x/net/dns/dnsmessage"
13+
)
14+
15+
// mcpTestZone answers A for names, NS and the transfer for the apex, and
16+
// NXDOMAIN for the rest, as a scan's preflight and wildcard probes expect.
17+
func mcpTestZone(t *testing.T, apex string, names, transfer []string) *dnstest.Server {
18+
t.Helper()
19+
return dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
20+
switch {
21+
case q.Type == dnsmessage.TypeAXFR && transfer != nil:
22+
return dnstest.Reply{Transfer: transfer}
23+
case q.Name == apex && q.Type == dnsmessage.TypeNS:
24+
return dnstest.Reply{NS: []string{"ns1." + apex}}
25+
case q.Name == "ns1."+apex && q.Type == dnsmessage.TypeA:
26+
return dnstest.Reply{A: []string{"127.0.0.1"}}
27+
case q.Name == apex || q.Name == "ns1."+apex:
28+
return dnstest.Reply{}
29+
case slices.Contains(names, q.Name):
30+
if q.Type == dnsmessage.TypeA {
31+
return dnstest.Reply{A: []string{"192.0.2.10"}}
32+
}
33+
return dnstest.Reply{}
34+
}
35+
return dnstest.NXDomain
36+
})
37+
}
38+
39+
// callReport makes one tool call and decodes its report.
40+
func callReport(t *testing.T, call func() (string, bool)) mcpReport {
41+
t.Helper()
42+
text, _ := call()
43+
var r mcpReport
44+
if err := json.Unmarshal([]byte(text), &r); err != nil {
45+
t.Fatalf("not a scan report: %s", text)
46+
}
47+
return r
48+
}
49+
50+
// TestMCPScanPermute covers #405: permute runs a second pass over variants
51+
// of the names found, within what is left of max_queries, and its finds join
52+
// the results.
53+
func TestMCPScanPermute(t *testing.T) {
54+
t.Parallel()
55+
srv := mcpTestZone(t, "example.test", []string{"api.example.test", "api-dev.example.test"}, nil)
56+
tool := scanTool(mcpConfig{allow: []string{"example.test"}, dnsServer: srv.Addr, maxQueries: 1000, rate: 1000,
57+
maxDuration: time.Minute, timeout: 5 * time.Second, maxCalls: 10})
58+
call := func(args string) func() (string, bool) {
59+
return func() (string, bool) {
60+
r := tool.Call(t.Context(), json.RawMessage(args))
61+
return r.Text, r.IsError
62+
}
63+
}
64+
65+
r := callReport(t, call(`{"domain": "example.test", "words": ["api", "www"], "types": ["A"], "permute": true}`))
66+
p := r.Permutation
67+
if p == nil {
68+
t.Fatalf("no permutation in the report: %+v", r)
69+
}
70+
var names []string
71+
for _, res := range r.Results {
72+
names = append(names, res.Name)
73+
}
74+
if !slices.Contains(names, "api-dev.example.test") || r.Found != 2 {
75+
t.Errorf("results %v, want api and the variant api-dev", names)
76+
}
77+
if p.Found != 1 || p.Tested == 0 || p.Tested != int64(p.Candidates) || p.Verdict != "complete" {
78+
t.Errorf("permutation %+v, want every variant tested, one found, complete", p)
79+
}
80+
81+
// Without permute there is no pass and no variant.
82+
r = callReport(t, call(`{"domain": "example.test", "words": ["api", "www"], "types": ["A"]}`))
83+
if r.Permutation != nil || r.Found != 1 {
84+
t.Errorf("without permute: found %d, permutation %+v", r.Found, r.Permutation)
85+
}
86+
87+
// A budget the scan spends leaves the pass nothing.
88+
r = callReport(t, call(`{"domain": "example.test", "words": ["api", "www"], "types": ["A"], "permute": true, "max_queries": 2}`))
89+
if p := r.Permutation; p == nil || p.Tested != 0 || p.Skipped == 0 || p.Verdict != "degraded" {
90+
t.Errorf("max_queries spent by the scan: %+v, want every variant skipped and degraded", p)
91+
}
92+
93+
// Part of the budget left: the pass tests that many.
94+
r = callReport(t, call(`{"domain": "example.test", "words": ["api", "www"], "types": ["A"], "permute": true, "max_queries": 5}`))
95+
if p := r.Permutation; p == nil || p.Tested != 3 || p.Skipped != int64(p.Candidates)-3 {
96+
t.Errorf("max_queries 5: %+v, want 3 variants tested and the rest skipped", p)
97+
}
98+
}
99+
100+
// TestE2EMCPLabScanPermute covers #405: lab_scan accepts permute, so agents
101+
// can practise it; lab1 has no variants of its names, so the pass finds
102+
// nothing but tests them.
103+
func TestE2EMCPLabScanPermute(t *testing.T) {
104+
t.Parallel()
105+
_, replies := runMCPSession(t, []string{"-max-queries", "1000"},
106+
mcpCall{"lab_scan", `{"words": ["www", "mail"], "permute": true}`},
107+
)
108+
r := mcpReportOf(t, replies[0])
109+
if p := r.Permutation; p == nil || p.Candidates == 0 || p.Tested != int64(p.Candidates) || p.Found != 0 || p.Verdict != "complete" {
110+
t.Errorf("lab_scan permute: %+v", r.Permutation)
111+
}
112+
}
113+
114+
// TestMCPScanZoneTransfer covers #405: zone_transfer is refused unless the
115+
// server was started with -axfr, and with it the report says which name
116+
// servers allowed a transfer and the names it gave are verified like words.
117+
func TestMCPScanZoneTransfer(t *testing.T) {
118+
t.Parallel()
119+
srv := mcpTestZone(t, "example.test", []string{"zt.example.test", "gone.example.test"}, []string{"zt.example.test", "stale.example.test"})
120+
_, port, err := net.SplitHostPort(srv.Addr)
121+
if err != nil {
122+
t.Fatal(err)
123+
}
124+
cfg := mcpConfig{allow: []string{"example.test"}, dnsServer: srv.Addr, maxQueries: 100, rate: 1000,
125+
maxDuration: time.Minute, timeout: 5 * time.Second, maxCalls: 10, transferPort: port}
126+
127+
off := scanTool(cfg)
128+
r := off.Call(t.Context(), json.RawMessage(`{"domain": "example.test", "words": ["www"], "zone_transfer": true}`))
129+
if !r.IsError || !strings.Contains(r.Text, "the operator did not start the server with -axfr") {
130+
t.Errorf("zone_transfer without -axfr: %+v", r)
131+
}
132+
if n := srv.QueriesFor("example.test", dnsmessage.TypeAXFR); n != 0 {
133+
t.Errorf("a refused call asked for %d transfers", n)
134+
}
135+
136+
cfg.axfr = true
137+
on := scanTool(cfg)
138+
report := callReport(t, func() (string, bool) {
139+
r := on.Call(t.Context(), json.RawMessage(`{"domain": "example.test", "words": ["www"], "types": ["A"], "zone_transfer": true}`))
140+
return r.Text, r.IsError
141+
})
142+
z := report.ZoneTransfer
143+
if z == nil || z.Names != 2 || !slices.Equal(z.Allowed, []string{"ns1.example.test"}) || z.Error != "" {
144+
t.Fatalf("zone_transfer: %+v", z)
145+
}
146+
if report.Found != 1 || report.Results[0].Name != "zt.example.test" {
147+
t.Errorf("results %+v, want the transferred name that resolves", report.Results)
148+
}
149+
150+
// An excluded name server is never asked.
151+
before := srv.QueriesFor("example.test", dnsmessage.TypeAXFR)
152+
report = callReport(t, func() (string, bool) {
153+
r := on.Call(t.Context(), json.RawMessage(`{"domain": "example.test", "words": ["www"], "types": ["A"], "zone_transfer": true, "exclude": ["ns1.example.test"]}`))
154+
return r.Text, r.IsError
155+
})
156+
if n := srv.QueriesFor("example.test", dnsmessage.TypeAXFR); n != before {
157+
t.Errorf("an excluded name server was asked for a transfer")
158+
}
159+
if report.ZoneTransfer == nil || len(report.ZoneTransfer.Allowed) != 0 {
160+
t.Errorf("excluded name server: %+v", report.ZoneTransfer)
161+
}
162+
}
163+
164+
// TestMCPAXFRFlag covers #405: -axfr adds zone_transfer to scan's schema.
165+
func TestMCPAXFRFlag(t *testing.T) {
166+
cfg, code, ok := parseMCPFlags([]string{"-allow", "example.com", "-axfr"}, &strings.Builder{})
167+
if !ok || code != exitOK || !cfg.axfr {
168+
t.Fatalf("-axfr: ok %v code %d axfr %v", ok, code, cfg.axfr)
169+
}
170+
if !strings.Contains(string(scanTool(cfg).InputSchema), `"zone_transfer"`) {
171+
t.Error("scan's schema has no zone_transfer with -axfr")
172+
}
173+
}

0 commit comments

Comments
 (0)