|
| 1 | +/* |
| 2 | +·━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━· |
| 3 | +: : |
| 4 | +: █▀ █ █▀▀ · Blazing-fast pentesting suite : |
| 5 | +: ▄█ █ █▀ · BSD 3-Clause License : |
| 6 | +: : |
| 7 | +: (c) 2022-2026 vmfunc, xyzeva, : |
| 8 | +: lunchcat alumni & contributors : |
| 9 | +: : |
| 10 | +·━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━· |
| 11 | +*/ |
| 12 | + |
| 13 | +package modules |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "fmt" |
| 18 | + "net/url" |
| 19 | + "regexp" |
| 20 | + "strings" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/miekg/dns" |
| 24 | + retryabledns "github.com/projectdiscovery/retryabledns" |
| 25 | +) |
| 26 | + |
| 27 | +// dnsMaxRetries is how many times the resolver rotates through the pool on a |
| 28 | +// timeout before giving up. |
| 29 | +const dnsMaxRetries = 3 |
| 30 | + |
| 31 | +// defaultDNSResolvers is the bundled pool: fast public anycast servers. |
| 32 | +var defaultDNSResolvers = []string{"1.1.1.1:53", "8.8.8.8:53", "9.9.9.9:53"} |
| 33 | + |
| 34 | +// dnsRequestType maps a module's record-type string to its dns type code. An |
| 35 | +// empty type defaults to A; ANY is deliberately not the default (RFC 8482 |
| 36 | +// discourages relying on ANY against the public resolvers). |
| 37 | +var dnsRequestType = map[string]uint16{ |
| 38 | + "": dns.TypeA, |
| 39 | + "a": dns.TypeA, |
| 40 | + "aaaa": dns.TypeAAAA, |
| 41 | + "cname": dns.TypeCNAME, |
| 42 | + "mx": dns.TypeMX, |
| 43 | + "ns": dns.TypeNS, |
| 44 | + "txt": dns.TypeTXT, |
| 45 | + "soa": dns.TypeSOA, |
| 46 | + "srv": dns.TypeSRV, |
| 47 | + "caa": dns.TypeCAA, |
| 48 | + "ptr": dns.TypePTR, |
| 49 | + "any": dns.TypeANY, |
| 50 | +} |
| 51 | + |
| 52 | +// dnsResolver is the slice of the retryabledns client the executor needs; tests |
| 53 | +// inject a fake through newDNSResolver. |
| 54 | +type dnsResolver interface { |
| 55 | + Query(host string, requestType uint16) (*retryabledns.DNSData, error) |
| 56 | +} |
| 57 | + |
| 58 | +// newDNSResolver builds a resolver over the bundled pool with the given timeout. |
| 59 | +// It is a package var so tests can supply a fake without touching the network. |
| 60 | +var newDNSResolver = func(timeout time.Duration) (dnsResolver, error) { |
| 61 | + opts := retryabledns.Options{ |
| 62 | + BaseResolvers: defaultDNSResolvers, |
| 63 | + MaxRetries: dnsMaxRetries, |
| 64 | + } |
| 65 | + if timeout > 0 { |
| 66 | + opts.Timeout = timeout |
| 67 | + } |
| 68 | + client, err := retryabledns.NewWithOptions(opts) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("build dns resolver: %w", err) |
| 71 | + } |
| 72 | + client.TCPFallback = true |
| 73 | + return client, nil |
| 74 | +} |
| 75 | + |
| 76 | +// dnsResponse holds the parts of a resolved answer a matcher can target. |
| 77 | +type dnsResponse struct { |
| 78 | + answer []string // the resource records, one per line |
| 79 | + rcode string // the response status, e.g. NOERROR or NXDOMAIN |
| 80 | + raw string // the full text of the response message |
| 81 | +} |
| 82 | + |
| 83 | +// validateDNS rejects, at load time, a dns config the executor cannot run: an |
| 84 | +// unknown record type, or a matcher type other than word or regex (status is |
| 85 | +// http only). |
| 86 | +func validateDNS(cfg *DNSConfig) error { |
| 87 | + if _, ok := dnsRequestType[strings.ToLower(cfg.Type)]; !ok { |
| 88 | + return fmt.Errorf("unsupported dns record type %q", cfg.Type) |
| 89 | + } |
| 90 | + for i := range cfg.Matchers { |
| 91 | + switch cfg.Matchers[i].Type { |
| 92 | + case "word", "regex": |
| 93 | + default: |
| 94 | + return fmt.Errorf("dns matcher type %q is not supported (use word or regex)", cfg.Matchers[i].Type) |
| 95 | + } |
| 96 | + } |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// ExecuteDNSModule resolves the configured name and record type, then applies |
| 101 | +// the module's matchers and extractors to the answer. |
| 102 | +func ExecuteDNSModule(ctx context.Context, target string, def *YAMLModule, opts Options) (*Result, error) { |
| 103 | + if def.DNS == nil { |
| 104 | + return nil, fmt.Errorf("no DNS configuration") |
| 105 | + } |
| 106 | + cfg := def.DNS |
| 107 | + result := &Result{ |
| 108 | + ModuleID: def.ID, |
| 109 | + Target: target, |
| 110 | + Findings: make([]Finding, 0), |
| 111 | + } |
| 112 | + |
| 113 | + qtype, ok := dnsRequestType[strings.ToLower(cfg.Type)] |
| 114 | + if !ok { |
| 115 | + return nil, fmt.Errorf("unsupported dns record type %q", cfg.Type) |
| 116 | + } |
| 117 | + |
| 118 | + resolver, err := newDNSResolver(opts.Timeout) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + // retryabledns has no context hook, so honor cancellation before the lookup. |
| 124 | + if err := ctx.Err(); err != nil { |
| 125 | + return result, err |
| 126 | + } |
| 127 | + |
| 128 | + name := dnsName(cfg.Name, target) |
| 129 | + data, err := resolver.Query(name, qtype) |
| 130 | + if err != nil { |
| 131 | + return nil, fmt.Errorf("dns query %q: %w", name, err) |
| 132 | + } |
| 133 | + |
| 134 | + resp := newDNSResponse(data) |
| 135 | + if !checkDNSMatchers(cfg.Matchers, resp) { |
| 136 | + return result, nil |
| 137 | + } |
| 138 | + |
| 139 | + result.Findings = append(result.Findings, Finding{ |
| 140 | + Severity: def.Info.Severity, |
| 141 | + Evidence: truncateEvidence(resp.raw), |
| 142 | + Extracted: runDNSExtractors(cfg.Extractors, resp), |
| 143 | + }) |
| 144 | + return result, nil |
| 145 | +} |
| 146 | + |
| 147 | +// newDNSResponse extracts the matchable parts from a resolved answer. The raw |
| 148 | +// text comes from RawResp (the single final message) rather than data.Raw, which |
| 149 | +// the resolver's retry loop concatenates across attempts. |
| 150 | +func newDNSResponse(data *retryabledns.DNSData) dnsResponse { |
| 151 | + if data == nil { |
| 152 | + return dnsResponse{} |
| 153 | + } |
| 154 | + raw := data.Raw |
| 155 | + if data.RawResp != nil { |
| 156 | + raw = data.RawResp.String() |
| 157 | + } |
| 158 | + return dnsResponse{ |
| 159 | + answer: data.AllRecords, |
| 160 | + rcode: data.StatusCode, |
| 161 | + raw: raw, |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// getDNSPart returns the slice of the response a matcher or extractor targets. |
| 166 | +// The default (and the explicit "all"/"body") is the full response text; |
| 167 | +// "answer" is the record set; "rcode" is the response status. |
| 168 | +func getDNSPart(part string, resp dnsResponse) string { |
| 169 | + switch strings.ToLower(part) { |
| 170 | + case "answer": |
| 171 | + return strings.Join(resp.answer, "\n") |
| 172 | + case "rcode": |
| 173 | + return resp.rcode |
| 174 | + default: |
| 175 | + return resp.raw |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// checkDNSMatchers evaluates all matchers against the response with AND logic. |
| 180 | +func checkDNSMatchers(matchers []Matcher, resp dnsResponse) bool { |
| 181 | + if len(matchers) == 0 { |
| 182 | + return false |
| 183 | + } |
| 184 | + |
| 185 | + for i := range matchers { |
| 186 | + matched := checkDNSMatcher(&matchers[i], resp) |
| 187 | + if matchers[i].Negative { |
| 188 | + matched = !matched |
| 189 | + } |
| 190 | + if !matched { |
| 191 | + return false // AND logic |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return true |
| 196 | +} |
| 197 | + |
| 198 | +// checkDNSMatcher evaluates a single matcher. The status matcher type is HTTP |
| 199 | +// only; match a response code with a word or regex matcher on part "rcode". |
| 200 | +func checkDNSMatcher(m *Matcher, resp dnsResponse) bool { |
| 201 | + part := getDNSPart(m.Part, resp) |
| 202 | + |
| 203 | + switch m.Type { |
| 204 | + case "word": |
| 205 | + return checkWords(part, m.Words, m.Condition) |
| 206 | + case "regex": |
| 207 | + return checkRegex(part, m.Regex, m.Condition) |
| 208 | + default: |
| 209 | + return false |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +// runDNSExtractors pulls regex captures from the response. DNS answers are text, |
| 214 | +// so regex is the available extractor; other types are skipped. |
| 215 | +func runDNSExtractors(extractors []Extractor, resp dnsResponse) map[string]string { |
| 216 | + if len(extractors) == 0 { |
| 217 | + return nil |
| 218 | + } |
| 219 | + |
| 220 | + result := make(map[string]string) |
| 221 | + for _, e := range extractors { |
| 222 | + if e.Type != "regex" { |
| 223 | + continue |
| 224 | + } |
| 225 | + part := getDNSPart(e.Part, resp) |
| 226 | + for _, pattern := range e.Regex { |
| 227 | + re, err := regexp.Compile(pattern) |
| 228 | + if err != nil { |
| 229 | + continue |
| 230 | + } |
| 231 | + matches := re.FindStringSubmatch(part) |
| 232 | + if len(matches) > e.Group { |
| 233 | + result[e.Name] = matches[e.Group] |
| 234 | + break |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return result |
| 240 | +} |
| 241 | + |
| 242 | +// dnsName resolves the lookup name: the module's name with {{FQDN}} replaced by |
| 243 | +// the target host, or the bare target host when no name is set. |
| 244 | +func dnsName(name, target string) string { |
| 245 | + host := dnsHost(target) |
| 246 | + if name == "" { |
| 247 | + return host |
| 248 | + } |
| 249 | + name = strings.ReplaceAll(name, "{{FQDN}}", host) |
| 250 | + name = strings.ReplaceAll(name, "{{fqdn}}", host) |
| 251 | + return name |
| 252 | +} |
| 253 | + |
| 254 | +// dnsHost reduces target to its hostname, stripping any scheme, port, path, or |
| 255 | +// userinfo. A bare host is returned unchanged. |
| 256 | +func dnsHost(target string) string { |
| 257 | + target = strings.TrimSpace(target) |
| 258 | + if target == "" { |
| 259 | + return target |
| 260 | + } |
| 261 | + // url.Parse only populates Host when a scheme is present; add one for a bare |
| 262 | + // host or host:port so the same parse handles every form. |
| 263 | + parse := target |
| 264 | + if !strings.Contains(parse, "://") { |
| 265 | + parse = "//" + parse |
| 266 | + } |
| 267 | + if u, err := url.Parse(parse); err == nil && u.Hostname() != "" { |
| 268 | + return u.Hostname() |
| 269 | + } |
| 270 | + return target |
| 271 | +} |
0 commit comments