Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/_includes/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| `-depth <int>` | 1 | Max recursion depth when -recursive is set (1 = no recursion) |
| `-diff <string>` | - | Previous results file (any -format); report only names added or removed since then, and exit 4 when there are changes |
| `-diff-records` {% include unreleased.html flag="diff-records" %} | - | With -diff, also report names whose records changed (a re-pointed CNAME, moved addresses), compared over the -type record types; needs a json, jsonl or csv -diff file |
| `-dns-server <string>` | 8.8.8.8:53 | DNS server: ip:port (UDP, TCP fallback), tls://host[:853] (DNS over TLS) or https://host/path (DNS over HTTPS) |
| `-dns-server <string>` | 8.8.8.8:53 | DNS server: ip[:port] (UDP, TCP fallback), tls://host[:853] (DNS over TLS) or https://host/path (DNS over HTTPS) |
| `-dry-run` {% include unreleased.html flag="dry-run" %} | - | Print the plan without scanning: candidates after dedupe and -exclude, query and duration estimates, and the resolver preflight (its only DNS query, also sent to each -r pool resolver); then exit |
| `-exclude <string>` | - | Comma-separated out-of-scope names and \*.parent patterns; never queried or expanded |
| `-exclude-file <string>` | - | File of out-of-scope names and \*.parent patterns, one per line (# comments allowed) |
Expand Down
8 changes: 6 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func parseFlagsWith(args []string, applyDefaults func(*flag.FlagSet) error, stde
fs.StringVar(&f.domainList, "dL", "", "File of apex domains to scan, one per line (- for stdin); replaces the <domain> argument")
fs.IntVar(&f.concurrency, "t", scan.DefaultConcurrency, "Number of concurrent workers")
fs.IntVar(&f.timeoutMs, "timeout", scan.DefaultTimeoutMs, "DNS lookup timeout in milliseconds")
fs.StringVar(&f.dnsServer, "dns-server", DefaultDNSServer, "DNS server: ip:port (UDP, TCP fallback), tls://host[:853] (DNS over TLS) or https://host/path (DNS over HTTPS)")
fs.StringVar(&f.dnsServer, "dns-server", DefaultDNSServer, "DNS server: ip[:port] (UDP, TCP fallback), tls://host[:853] (DNS over TLS) or https://host/path (DNS over HTTPS)")
fs.BoolVar(&f.verbose, "v", false, "Enable verbose output")
fs.BoolVar(&f.showVersion, "version", false, "Show version information")
fs.BoolVar(&f.showProgress, "progress", true, "Show progress during scanning")
Expand Down Expand Up @@ -784,13 +784,17 @@ func scanOptions(f cliFlags, in *runtimeInputs, p scanPass, logf dns.Logf) scan.
if p.flat {
recursive, depth = false, 1
}
dnsServer := f.dnsServer
if normalized, err := validate.NormalizeDNSServer(dnsServer); err == nil {
dnsServer = normalized
}
return scan.Options{
Config: scan.Config{
Domain: p.domain,
Entries: p.entries,
Concurrency: f.concurrency,
Timeout: time.Duration(f.timeoutMs) * time.Millisecond,
DNSServer: f.dnsServer,
DNSServer: dnsServer,
Simulate: f.testMode,
HitRate: f.testHitRate,
Seed: f.seed,
Expand Down
5 changes: 4 additions & 1 deletion internal/cli/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func parseMCPFlags(args []string, stderr io.Writer) (cfg mcpConfig, code int, ok
var allow, sources string
var timeoutMs int
fs.StringVar(&allow, "allow", "", "Comma-separated domains the scan tool may enumerate, each with its subdomains. Without it only lab_scan, explain_stats and diff are offered")
fs.StringVar(&cfg.dnsServer, "dns-server", DefaultDNSServer, "Resolver for live scans: ip:port, tls://host or https://host/path")
fs.StringVar(&cfg.dnsServer, "dns-server", DefaultDNSServer, "Resolver for live scans: ip[:port], tls://host or https://host/path")
fs.IntVar(&cfg.maxQueries, "max-queries", defaultMCPMaxQueries, "Most candidate names one tool call may test; a call can ask for fewer")
fs.IntVar(&cfg.rate, "rate", defaultMCPRate, "DNS queries per second for live scans")
fs.DurationVar(&cfg.maxDuration, "max-duration", defaultMCPMaxDuration, "Longest one tool call may scan")
Expand Down Expand Up @@ -165,6 +165,9 @@ func parseMCPFlags(args []string, stderr io.Writer) (cfg mcpConfig, code int, ok
return fail("-max-calls must be at least 1, got %d", cfg.maxCalls)
}
cfg.timeout = time.Duration(timeoutMs) * time.Millisecond
if normalized, err := validate.NormalizeDNSServer(cfg.dnsServer); err == nil {
cfg.dnsServer = normalized
}
if err := validate.DNSServer(cfg.dnsServer); err != nil {
return fail("-dns-server: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/scan/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ func (o Options) Validate() error {
return fmt.Errorf("rate per resolver ({rate_per_resolver}) must be 0 (unlimited) or a positive integer, got %d", o.RatePerResolver)
}
if !o.Simulate {
if err := validate.DNSServer(o.DNSServer); err != nil {
server := o.DNSServer
if normalized, err := validate.NormalizeDNSServer(server); err == nil {
server = normalized
}
if err := validate.DNSServer(server); err != nil {
return fmt.Errorf("DNS server %s: %w", o.DNSServer, err)
}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/scan/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/TMHSDigital/subenum/internal/dns"
"github.com/TMHSDigital/subenum/internal/validate"
)

const (
Expand Down Expand Up @@ -235,6 +236,9 @@ func Run(ctx context.Context, cfg Config, events chan<- Event) {
limiter = dns.NewAdjustableRateLimiter(cfg.Rate)
}
if cfg.Resolver == nil {
if normalized, err := validate.NormalizeDNSServer(cfg.DNSServer); err == nil {
cfg.DNSServer = normalized
}
cfg.Resolver = dns.NewResolver(cfg.Timeout, cfg.DNSServer)
}
// DoT and DoH connections kept for reuse are not held past the scan
Expand Down
3 changes: 3 additions & 0 deletions internal/tui/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ func (m *formModel) validate() (formValues, string) {
if dnsServer == "" {
dnsServer = validate.DefaultDNSServer
}
if normalized, err := validate.NormalizeDNSServer(dnsServer); err == nil {
dnsServer = normalized
}
resolvers := path(fieldResolvers)
if resolvers == wordlist.Stdin {
return formValues{}, "Resolver pool from standard input (-) is not supported in the TUI; enter a file path"
Expand Down
35 changes: 33 additions & 2 deletions internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,44 @@ var (
tldRegex = regexp.MustCompile(`^([a-zA-Z]{2,}|xn--[a-zA-Z0-9-]{1,59})$`)
)

// NormalizeDNSServer returns server with a default port of 53 appended when it
// is a bare IPv4 or IPv6 literal, so `-dns-server 1.1.1.1` works the way it
// does for `-r` files and for dig/dnsx. Bracketed IPv6 literals are handled
// without double-bracketing. Already-complete addresses, tls:// and https://
// values, and anything else are returned unchanged.
func NormalizeDNSServer(server string) (string, error) {
s := strings.TrimSpace(server)
if s == "" {
return s, fmt.Errorf("empty DNS server")
}
if strings.HasPrefix(s, "tls://") || strings.HasPrefix(s, "https://") || strings.Contains(s, "://") {
return s, nil
}
if _, _, err := net.SplitHostPort(s); err == nil {
return s, nil
}
host := s
if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
host = s[1 : len(s)-1]
}
if net.ParseIP(host) != nil {
return net.JoinHostPort(host, "53"), nil
}
return s, fmt.Errorf("expected ip[:port], got %q", s)
}

// DNSServer checks that server is a valid ip:port address with an IP host and a
// port in the range 1-65535.
// port in the range 1-65535. A bare IPv4 or IPv6 literal is accepted and
// treated as port 53.
//
// Encrypted transports are written as URLs (#86): tls://host[:port] for DNS
// over TLS (port 853 by default) and https://host/path for DNS over HTTPS.
// Their host may be a name, which is resolved with the system resolver.
func DNSServer(server string) error {
normalized, err := NormalizeDNSServer(server)
if err == nil {
server = normalized
}
switch {
case strings.HasPrefix(server, "tls://"):
hostport, host, err := DoTAddress(server)
Expand All @@ -67,7 +98,7 @@ func DNSServer(server string) error {
}
host, portStr, err := net.SplitHostPort(server)
if err != nil {
return fmt.Errorf("invalid format, expected ip:port (e.g., %s), tls://host or https://host/path: %w", DefaultDNSServer, err)
return fmt.Errorf("invalid format, expected ip[:port] (e.g., %s), tls://host or https://host/path: %w", DefaultDNSServer, err)
}
if net.ParseIP(host) == nil {
return fmt.Errorf("invalid IP address: %s", host)
Expand Down
3 changes: 2 additions & 1 deletion internal/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ func TestDNSServer(t *testing.T) {
"1.1.1.1:53",
"192.168.1.1:53",
"[2001:4860:4860::8888]:53",
"1.1.1.1",
"2606:4700:4700::1111",
}

invalidServers := []struct {
server string
reason string
}{
{"8.8.8.8", "missing port"},
{":53", "missing IP"},
{"localhost:53", "not a valid IP"},
{"256.1.1.1:53", "invalid IP octet"},
Expand Down
3 changes: 3 additions & 0 deletions pkg/subenum/subenum.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ func (c Config) options() (scan.Options, error) {
}

resolver := orDefaultString(c.Resolver, DefaultResolver)
if normalized, err := validate.NormalizeDNSServer(resolver); err == nil {
resolver = normalized
}
if !c.Simulate {
if err := validate.DNSServer(resolver); err != nil {
return scan.Options{}, fmt.Errorf("subenum: Resolver %s: %w", resolver, err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/subenum/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func probeZone(ctx context.Context, domain string, opts ZoneOptions, skip func(s
return ZoneInfo{}, fmt.Errorf("subenum: Timeout must not be negative, got %s", opts.Timeout)
}
resolver := orDefaultString(opts.Resolver, DefaultResolver)
if normalized, err := validate.NormalizeDNSServer(resolver); err == nil {
resolver = normalized
}
if err := validate.DNSServer(resolver); err != nil {
return ZoneInfo{}, fmt.Errorf("subenum: Resolver %s: %w", resolver, err)
}
Expand Down