-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdiscovery.go
More file actions
708 lines (610 loc) · 17.9 KB
/
Copy pathdiscovery.go
File metadata and controls
708 lines (610 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"sync"
"golang.org/x/net/html"
)
// ---------------------------------------------------------------------------
// Discovery: SPF records
// ---------------------------------------------------------------------------
func extractIPsFromSPF(domain string) ([]string, error) {
var ips []string
txtRecords, err := net.LookupTXT(domain)
if err != nil {
return nil, err
}
for _, txt := range txtRecords {
if strings.HasPrefix(txt, "v=spf1") {
parts := strings.Fields(txt)
for _, part := range parts {
if strings.HasPrefix(part, "ip4:") {
ip := strings.TrimPrefix(part, "ip4:")
if strings.Contains(ip, "/") {
rangedIps, err := expandIPRange(ip)
if err != nil {
continue
}
ips = append(ips, rangedIps...)
} else {
ips = append(ips, ip)
}
} else if strings.HasPrefix(part, "ip6:") {
ip := strings.TrimPrefix(part, "ip6:")
ips = append(ips, ip)
}
}
}
}
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: MX records
// ---------------------------------------------------------------------------
func extractIPsFromMX(domain string) ([]string, error) {
var ips []string
mxRecords, err := net.LookupMX(domain)
if err != nil {
return nil, err
}
for _, mx := range mxRecords {
host := strings.TrimSuffix(mx.Host, ".")
lowerHost := strings.ToLower(host)
if strings.Contains(lowerHost, "google") ||
strings.Contains(lowerHost, "outlook") ||
strings.Contains(lowerHost, "microsoft") ||
strings.Contains(lowerHost, "mimecast") ||
strings.Contains(lowerHost, "proofpoint") ||
strings.Contains(lowerHost, "barracuda") ||
strings.Contains(lowerHost, "pphosted") {
continue
}
addrs, err := net.LookupHost(host)
if err != nil {
continue
}
ips = append(ips, addrs...)
}
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: Common subdomains
// ---------------------------------------------------------------------------
func extractIPsFromSubdomains(ctx context.Context, domain string, verbose bool) []string {
var ips []string
var mu sync.Mutex
var wg sync.WaitGroup
for _, sub := range originSubdomains {
wg.Add(1)
go func(subdomain string) {
defer wg.Done()
if ctx.Err() != nil {
return
}
fqdn := subdomain + "." + domain
addrs, err := net.LookupHost(fqdn)
if err != nil {
return
}
mu.Lock()
for _, addr := range addrs {
if !isWAFIP(addr) && !isPrivateIP(addr) {
ips = append(ips, addr)
logVerbose(verbose, "Subdomain %s → %s", fqdn, addr)
}
}
mu.Unlock()
}(sub)
}
wg.Wait()
return ips
}
// ---------------------------------------------------------------------------
// Discovery: Certificate Transparency (crt.sh)
// ---------------------------------------------------------------------------
func extractIPsFromCrtSh(ctx context.Context, domain string, verbose bool) ([]string, error) {
urlStr := fmt.Sprintf("https://crt.sh/?q=%%25.%s&output=json", domain)
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return nil, fmt.Errorf("crt.sh request failed: %w", err)
}
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
return nil, fmt.Errorf("crt.sh request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("crt.sh returned status %d", resp.StatusCode)
}
var entries []CrtShEntry
if err := json.NewDecoder(resp.Body).Decode(&entries); err != nil {
return nil, fmt.Errorf("failed to parse crt.sh response: %w", err)
}
subdomainSet := make(map[string]bool)
for _, entry := range entries {
names := strings.Split(entry.NameValue, "\n")
for _, name := range names {
name = strings.TrimSpace(name)
name = strings.TrimPrefix(name, "*.")
if name != "" && !subdomainSet[name] {
subdomainSet[name] = true
}
}
}
logInfo("Found %d unique subdomains in CT logs.", len(subdomainSet))
var ips []string
var mu sync.Mutex
var wg sync.WaitGroup
sem := make(chan struct{}, 20)
for subdomain := range subdomainSet {
wg.Add(1)
go func(sub string) {
defer wg.Done()
if ctx.Err() != nil {
return
}
sem <- struct{}{}
defer func() { <-sem }()
addrs, err := net.LookupHost(sub)
if err != nil {
return
}
mu.Lock()
for _, addr := range addrs {
if !isWAFIP(addr) && !isPrivateIP(addr) {
ips = append(ips, addr)
logVerbose(verbose, "CT subdomain %s → %s", sub, addr)
}
}
mu.Unlock()
}(subdomain)
}
wg.Wait()
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: AlienVault OTX (free, optional API key)
// ---------------------------------------------------------------------------
func fetchIPsFromOTX(ctx context.Context, domain, apiKey string) ([]string, error) {
var ips []string
urlStr := fmt.Sprintf("https://otx.alienvault.com/api/v1/indicators/domain/%s/passive_dns", domain)
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return nil, err
}
if apiKey != "" {
req.Header.Set("X-OTX-API-KEY", apiKey)
}
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("OTX returned status %d", resp.StatusCode)
}
var result struct {
PassiveDNS []struct {
Address string `json:"address"`
RecordType string `json:"record_type"`
} `json:"passive_dns"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to parse OTX response: %v", err)
}
for _, record := range result.PassiveDNS {
if record.RecordType == "A" || record.RecordType == "AAAA" {
if net.ParseIP(record.Address) != nil {
ips = append(ips, record.Address)
}
}
}
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: RapidDNS (free, no key)
// ---------------------------------------------------------------------------
func fetchIPsFromRapidDNS(ctx context.Context, domain string, verbose bool) ([]string, error) {
urlStr := fmt.Sprintf("https://rapiddns.io/subdomain/%s?full=1", domain)
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0")
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("RapidDNS returned status %d", resp.StatusCode)
}
// Parse HTML table to find subdomains
subdomains := make(map[string]bool)
z := html.NewTokenizer(resp.Body)
inTD := false
for {
tt := z.Next()
if tt == html.ErrorToken {
break
}
token := z.Token()
if token.Type == html.StartTagToken && token.Data == "td" {
inTD = true
continue
}
if token.Type == html.EndTagToken && token.Data == "td" {
inTD = false
continue
}
if inTD && token.Type == html.TextToken {
text := strings.TrimSpace(token.Data)
if strings.Contains(text, domain) && !strings.Contains(text, " ") {
subdomains[text] = true
}
}
}
logInfo("Found %d subdomains from RapidDNS.", len(subdomains))
// Resolve to IPs
var ips []string
var mu sync.Mutex
var wg sync.WaitGroup
sem := make(chan struct{}, 20)
for sub := range subdomains {
wg.Add(1)
go func(s string) {
defer wg.Done()
if ctx.Err() != nil {
return
}
sem <- struct{}{}
defer func() { <-sem }()
addrs, err := net.LookupHost(s)
if err != nil {
return
}
mu.Lock()
for _, addr := range addrs {
if !isWAFIP(addr) && !isPrivateIP(addr) {
ips = append(ips, addr)
logVerbose(verbose, "RapidDNS %s → %s", s, addr)
}
}
mu.Unlock()
}(sub)
}
wg.Wait()
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: HackerTarget (free, 50 req/day)
// ---------------------------------------------------------------------------
func fetchIPsFromHackerTarget(ctx context.Context, domain string) ([]string, error) {
var ips []string
urlStr := fmt.Sprintf("https://api.hackertarget.com/hostsearch/?q=%s", domain)
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return nil, err
}
resp, err := doWithRetry(ctx, appHTTPClient, req, 0)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HackerTarget returned status %d", resp.StatusCode)
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "error") || strings.HasPrefix(line, "API") {
continue
}
parts := strings.SplitN(line, ",", 2)
if len(parts) == 2 {
ip := strings.TrimSpace(parts[1])
if net.ParseIP(ip) != nil {
ips = append(ips, ip)
}
}
}
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: Wayback Machine CDX API (free, no key)
// ---------------------------------------------------------------------------
func fetchIPsFromWayback(ctx context.Context, domain string, verbose bool) ([]string, error) {
urlStr := fmt.Sprintf("http://web.archive.org/cdx/search/cdx?url=*.%s&output=json&fl=original&collapse=urlkey&limit=500", url.QueryEscape(domain))
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return nil, err
}
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Wayback CDX returned status %d", resp.StatusCode)
}
var rows [][]string
if err := json.NewDecoder(resp.Body).Decode(&rows); err != nil {
return nil, fmt.Errorf("failed to parse Wayback response: %v", err)
}
// Extract unique hostnames from URLs
hostnames := make(map[string]bool)
for i, row := range rows {
if i == 0 {
continue // skip header row
}
if len(row) < 1 {
continue
}
parsed, err := url.Parse(row[0])
if err != nil {
continue
}
host := parsed.Hostname()
if host != "" && strings.Contains(host, domain) {
hostnames[host] = true
}
}
logInfo("Found %d unique hostnames from Wayback Machine.", len(hostnames))
// Resolve to IPs
var ips []string
var mu sync.Mutex
var wg sync.WaitGroup
sem := make(chan struct{}, 20)
for host := range hostnames {
wg.Add(1)
go func(h string) {
defer wg.Done()
if ctx.Err() != nil {
return
}
sem <- struct{}{}
defer func() { <-sem }()
addrs, err := net.LookupHost(h)
if err != nil {
return
}
mu.Lock()
for _, addr := range addrs {
if !isWAFIP(addr) && !isPrivateIP(addr) {
ips = append(ips, addr)
logVerbose(verbose, "Wayback %s → %s", h, addr)
}
}
mu.Unlock()
}(host)
}
wg.Wait()
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: ViewDNS (API key required, free tier: 250 requests)
// ---------------------------------------------------------------------------
func fetchIPsFromViewDNS(ctx context.Context, domain, apiKey string) ([]string, error) {
var ips []string
urlStr := fmt.Sprintf("https://api.viewdns.info/iphistory/?domain=%s&apikey=%s&output=json", domain, apiKey)
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return nil, err
}
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("received non-200 response code: %d", resp.StatusCode)
}
var result struct {
Query map[string]string `json:"query"`
Response struct {
Records []struct {
IP string `json:"ip"`
} `json:"records"`
} `json:"response"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
for _, record := range result.Response.Records {
ips = append(ips, record.IP)
}
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: SecurityTrails (API key required, free tier available)
// ---------------------------------------------------------------------------
func fetchIPsFromSecurityTrails(ctx context.Context, domain, apiKey string) ([]string, error) {
var ips []string
urlStr := fmt.Sprintf("https://api.securitytrails.com/v1/history/%s/dns/a", domain)
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return nil, err
}
req.Header.Set("APIKEY", apiKey)
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("received non-200 response code: %d", resp.StatusCode)
}
var result struct {
Records []struct {
Values []struct {
IP string `json:"ip"`
} `json:"values"`
} `json:"records"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
for _, record := range result.Records {
for _, value := range record.Values {
ips = append(ips, value.IP)
}
}
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: Censys SSL certificate search (API key required, paid)
// ---------------------------------------------------------------------------
func fetchIPsFromCensys(ctx context.Context, domain, token, orgID string) ([]string, error) {
var ips []string
searchURL := "https://api.platform.censys.io/v3/global/search/query"
if orgID != "" {
searchURL += "?organization_id=" + orgID
}
query := fmt.Sprintf("cert.names: %s", domain)
bodyData := fmt.Sprintf(`{"query":"%s","page_size":50}`, query)
req, err := http.NewRequestWithContext(ctx, "POST", searchURL, strings.NewReader(bodyData))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if orgID != "" {
req.Header.Set("X-Organization-ID", orgID)
}
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body)
hint := ""
if resp.StatusCode == 403 {
hint = " (Censys API requires a paid license with an Organization ID — free accounts cannot use the API)"
}
if resp.StatusCode == 401 {
hint = " (check your PAT is valid at https://app.censys.io/account/api)"
}
return nil, fmt.Errorf("Censys Platform API returned status %d%s: %s", resp.StatusCode, hint, truncateStr(string(body), 200))
}
var searchResult struct {
Result struct {
Hits []struct {
IP string `json:"ip"`
Services []struct {
IP string `json:"ip"`
} `json:"services"`
} `json:"hits"`
} `json:"result"`
}
if err := json.NewDecoder(resp.Body).Decode(&searchResult); err != nil {
return nil, fmt.Errorf("failed to parse Censys response: %v", err)
}
for _, hit := range searchResult.Result.Hits {
if hit.IP != "" && !isWAFIP(hit.IP) {
ips = append(ips, hit.IP)
}
for _, svc := range hit.Services {
if svc.IP != "" && !isWAFIP(svc.IP) {
ips = append(ips, svc.IP)
}
}
}
return ips, nil
}
// ---------------------------------------------------------------------------
// Discovery: Shodan (API key required, free tier available)
// ---------------------------------------------------------------------------
func fetchIPsFromShodan(ctx context.Context, domain, apiKey string, mmh3Hash int32) ([]string, error) {
var allIPs []string
queries := []string{
fmt.Sprintf("ssl.cert.subject.cn:%s", domain),
fmt.Sprintf("hostname:%s", domain),
}
if mmh3Hash != 0 {
queries = append(queries, fmt.Sprintf("http.favicon.hash:%d", mmh3Hash))
}
for _, query := range queries {
if ctx.Err() != nil {
break
}
urlStr := fmt.Sprintf("https://api.shodan.io/shodan/host/search?key=%s&query=%s&minify=true",
apiKey, url.QueryEscape(query))
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
continue
}
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
continue
}
var result struct {
Matches []struct {
IPStr string `json:"ip_str"`
} `json:"matches"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
resp.Body.Close()
continue
}
resp.Body.Close()
for _, match := range result.Matches {
if match.IPStr != "" {
allIPs = append(allIPs, match.IPStr)
}
}
}
return allIPs, nil
}
// ---------------------------------------------------------------------------
// Discovery: DNSDB/Farsight (API key required, free Community Edition: 500 queries/month)
// ---------------------------------------------------------------------------
func fetchIPsFromDNSDB(ctx context.Context, domain, apiKey string) ([]string, error) {
var ips []string
urlStr := fmt.Sprintf("https://api.dnsdb.info/dnsdb/v2/lookup/rrset/name/*.%s/A", domain)
req, err := http.NewRequestWithContext(ctx, "GET", urlStr, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-API-Key", apiKey)
req.Header.Set("Accept", "application/x-ndjson")
resp, err := doWithRetry(ctx, appHTTPClient, req, 1)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("DNSDB returned status %d", resp.StatusCode)
}
// Parse NDJSON
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
var entry struct {
Obj struct {
RData []string `json:"rdata"`
} `json:"obj"`
}
if err := json.Unmarshal([]byte(line), &entry); err != nil {
continue
}
for _, rdata := range entry.Obj.RData {
ip := strings.TrimSpace(rdata)
if net.ParseIP(ip) != nil {
ips = append(ips, ip)
}
}
}
return ips, nil
}