package surfaceingest import ( "net" "net/url" "strings" "golang.org/x/net/publicsuffix" ) // nonTargetReferenceDomains lists external documentation, package registry, // scanner, and OOB callback domains commonly cited in security notes or POCs // that must never be auto-materialized as customer project assets. var nonTargetReferenceDomains = []string{ "github.com", "githubusercontent.com", "gitlab.com", "owasp.org", "mitre.org", "nist.gov", "cve.org", "portswigger.net", "hacktricks.xyz", "w3.org", "xmlsoap.org", "mozilla.org", "googleapis.com", "gstatic.com", "jsdelivr.net", "unpkg.com", "npmjs.org", "pypi.org", "golang.org", "python.org", "dnslog.cn", "ceye.io", "oastify.com", "oast.pro", "oast.fun", "oast.live", "oast.me", "oast.online", "burpcollaborator.net", "interact.sh", "shodan.io", "fofa.info", "qianxin.com", "360.net", } // ambiguousTwoLabelExtensions lists file extensions that collide with ICANN // 2-letter/new TLDs when written bare in prose without scheme or port // (for example "exploit.py", "run.sh", "README.md", "spac.do", "backup.zip"). var ambiguousTwoLabelExtensions = map[string]bool{ "py": true, "sh": true, "md": true, "rs": true, "pl": true, "cc": true, "so": true, "do": true, "zip": true, "mov": true, } var allowedInternalSuffixes = map[string]bool{ "test": true, "local": true, "internal": true, "lan": true, "corp": true, "example": true, } var publicResolverIPs = map[string]bool{ "8.8.8.8": true, "8.8.4.4": true, "1.1.1.1": true, "1.0.0.1": true, "114.114.114.114": true, "223.5.5.5": true, } // IsNoiseTarget reports whether a candidate IP or domain is loopback, // link-local/metadata, public DNS, or a known third-party reference/OOB host. func IsNoiseTarget(ip, domain string) bool { if ip != "" { parsed := net.ParseIP(strings.TrimSpace(ip)) if parsed == nil || parsed.IsLoopback() || parsed.IsUnspecified() || parsed.IsLinkLocalUnicast() || parsed.IsLinkLocalMulticast() || parsed.IsMulticast() { return true } if v4 := parsed.To4(); v4 != nil { if v4[0] == 0 || v4[0] == 127 || v4[0] == 255 || (v4[0] == 169 && v4[1] == 254) { return true } // Subnet masks such as 255.255.255.0 if v4[0] >= 224 { return true } } if publicResolverIPs[parsed.String()] { return true } } if domain != "" { lower := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(domain), ".")) if lower == "" || lower == "localhost" || strings.HasSuffix(lower, ".localhost") || strings.HasSuffix(lower, ".invalid") { return true } for _, ref := range nonTargetReferenceDomains { if lower == ref || strings.HasSuffix(lower, "."+ref) { return true } } } return false } func validBarePrecipitatedDomain(domain string) bool { lower := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(domain), ".")) if lower == "" || IsNoiseTarget("", lower) || !validSurfaceDomain(lower) { return false } labels := strings.Split(lower, ".") last := labels[len(labels)-1] if len(labels) == 2 && ambiguousTwoLabelExtensions[last] { return false } _, icann := publicsuffix.PublicSuffix(lower) if icann { return true } return allowedInternalSuffixes[last] } // extractExplicitServiceTokens extracts only explicit http(s) URLs and host:port // pairs from free-form prose (ignoring bare domain-like words unless seeded). func extractExplicitServiceTokens(text string) []SurfaceHit { if len(text) > surfaceTextLimit { text = text[:surfaceTextLimit] } text = strings.ReplaceAll(text, "\r\n", "\n") builder := &surfaceBuilder{scope: newSurfaceScope(nil, true), hits: map[string]*SurfaceHit{}} lines := strings.Split(text, "\n") if len(lines) > surfaceLineLimit { lines = lines[:surfaceLineLimit] } for _, line := range lines { if len(line) > 4000 { line = line[:4000] } method := surfaceLineMethod(line) urls := surfaceURLPattern.FindAllString(line, 20) for _, raw := range urls { raw = strings.TrimRight(raw, ".,;:") parsed, err := url.Parse(raw) if err != nil || parsed.Hostname() == "" { continue } parsed.User = nil scheme := strings.ToLower(parsed.Scheme) if scheme != "http" && scheme != "https" { continue } ip, domain, ok := classifySurfaceHost(parsed.Hostname()) if !ok || IsNoiseTarget(ip, domain) { continue } if domain != "" && !validExplicitHostDomain(domain) { continue } hit := builder.add(ip, domain, scheme, surfacePort(parsed)) if path := cleanSurfacePath(parsed.EscapedPath()); path != "" && path != "/" { builder.addEndpoint(hit, method, path) } } stripped := surfaceURLPattern.ReplaceAllString(line, " ") for _, match := range surfaceHostPattern.FindAllStringSubmatch(stripped, 20) { ip, domain, ok := classifySurfaceHost(match[1]) if !ok || IsNoiseTarget(ip, domain) { continue } if domain != "" && !validExplicitHostDomain(domain) { continue } port := atoiPort(match[2]) if port <= 0 || port > 65535 { continue } protocol := "" if port == 443 { protocol = "https" } else if port == 80 { protocol = "http" } builder.add(ip, domain, protocol, port) } } return builder.finish() } func validExplicitHostDomain(domain string) bool { lower := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(domain), ".")) if lower == "" || IsNoiseTarget("", lower) || !validSurfaceDomain(lower) { return false } labels := strings.Split(lower, ".") last := labels[len(labels)-1] _, icann := publicsuffix.PublicSuffix(lower) return icann || allowedInternalSuffixes[last] } func filterStructuredHits(hits []SurfaceHit) []SurfaceHit { out := make([]SurfaceHit, 0, len(hits)) for _, hit := range hits { if IsNoiseTarget(hit.IP, hit.Domain) { continue } if hit.Domain != "" { if hit.Port > 0 || hit.Protocol != "" || len(hit.Endpoints) > 0 { if !validExplicitHostDomain(hit.Domain) { continue } } else if !validBarePrecipitatedDomain(hit.Domain) { continue } } out = append(out, hit) } return out } // ExtractPrecipitatedTargets extracts project targets from structured target // records (scope_json targets, target/infra facts, attack-chain target nodes, // vulnerability targets, fact summaries) and then expands in-scope subdomains // and endpoints across prose bodies and tool outputs without adopting unrelated // external hosts or filename tokens. func ExtractPrecipitatedTargets(structuredTexts, proseTexts []string, seeds, excluded []ScopeSeed) []SurfaceHit { merged := map[string]*SurfaceHit{} mergeHits := func(items []SurfaceHit) { for _, item := range items { if IsNoiseTarget(item.IP, item.Domain) { continue } key := item.key() if existing := merged[key]; existing != nil { for _, ep := range item.Endpoints { appendSurfaceEndpoint(existing, ep) } continue } copyHit := item if len(item.Endpoints) > 0 { copyHit.Endpoints = append([]SurfaceEndpoint(nil), item.Endpoints...) } merged[key] = ©Hit } } for _, text := range structuredTexts { if strings.TrimSpace(text) == "" { continue } mergeHits(filterStructuredHits(ExtractDeclaredTargets(text))) } for _, text := range proseTexts { if strings.TrimSpace(text) == "" { continue } mergeHits(extractExplicitServiceTokens(text)) } allSeeds := make([]ScopeSeed, 0, len(seeds)+len(merged)) allSeeds = append(allSeeds, seeds...) for _, hit := range merged { allSeeds = append(allSeeds, ScopeSeed{IP: hit.IP, Domain: hit.Domain}) } if len(allSeeds) > 0 { for _, text := range proseTexts { if strings.TrimSpace(text) == "" { continue } inScope, _ := ExtractSurface(text, allSeeds) mergeHits(inScope) } } // Re-run builder.finish()-style specificity collapsing: if host:443 exists, // fold bare host:0 into host:443 so we don't create duplicate port=0 rows. collapsed := &surfaceBuilder{scope: newSurfaceScope(nil, true), hits: map[string]*SurfaceHit{}} for _, hit := range merged { added := collapsed.add(hit.IP, hit.Domain, hit.Protocol, hit.Port) for _, ep := range hit.Endpoints { collapsed.addEndpoint(added, ep.Method, ep.Path) } } return WithoutExcluded(collapsed.finish(), excluded) }