diff --git a/docs/_includes/flags.md b/docs/_includes/flags.md index 89df740..159ca19 100644 --- a/docs/_includes/flags.md +++ b/docs/_includes/flags.md @@ -10,7 +10,7 @@ | `-depth ` | 1 | Max recursion depth when -recursive is set (1 = no recursion) | | `-diff ` | - | 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 ` | 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 ` | 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 ` | - | Comma-separated out-of-scope names and \*.parent patterns; never queried or expanded | | `-exclude-file ` | - | File of out-of-scope names and \*.parent patterns, one per line (# comments allowed) | diff --git a/internal/cli/cli.go b/internal/cli/cli.go index f635f45..98ffae5 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -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 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") @@ -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, diff --git a/internal/cli/mcp.go b/internal/cli/mcp.go index 5c09ea5..2898c68 100644 --- a/internal/cli/mcp.go +++ b/internal/cli/mcp.go @@ -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") @@ -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) } diff --git a/internal/scan/options.go b/internal/scan/options.go index 4e3ea53..4d634ef 100644 --- a/internal/scan/options.go +++ b/internal/scan/options.go @@ -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) } } diff --git a/internal/scan/runner.go b/internal/scan/runner.go index 1a543b2..a4c5373 100644 --- a/internal/scan/runner.go +++ b/internal/scan/runner.go @@ -12,6 +12,7 @@ import ( "time" "github.com/TMHSDigital/subenum/internal/dns" + "github.com/TMHSDigital/subenum/internal/validate" ) const ( @@ -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 diff --git a/internal/tui/form.go b/internal/tui/form.go index b6e558a..2d685c2 100644 --- a/internal/tui/form.go +++ b/internal/tui/form.go @@ -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" diff --git a/internal/validate/validate.go b/internal/validate/validate.go index 77a5648..bc7c7fc 100644 --- a/internal/validate/validate.go +++ b/internal/validate/validate.go @@ -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) @@ -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) diff --git a/internal/validate/validate_test.go b/internal/validate/validate_test.go index 2bec26e..0239aee 100644 --- a/internal/validate/validate_test.go +++ b/internal/validate/validate_test.go @@ -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"}, diff --git a/pkg/subenum/subenum.go b/pkg/subenum/subenum.go index a6ced5a..c9fff0e 100644 --- a/pkg/subenum/subenum.go +++ b/pkg/subenum/subenum.go @@ -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) diff --git a/pkg/subenum/zone.go b/pkg/subenum/zone.go index bfb3b75..bfd9264 100644 --- a/pkg/subenum/zone.go +++ b/pkg/subenum/zone.go @@ -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) }