forked from picatz/subzero
/
threatminer.go
83 lines (65 loc) · 1.97 KB
/
threatminer.go
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
package sources
import (
"bufio"
"context"
"errors"
"net/http"
"github.com/subfinder/research/core"
"golang.org/x/sync/semaphore"
)
// Threatminer is a source to process subdomains from https://www.threatminer.org
type Threatminer struct {
lock *semaphore.Weighted
}
// ProcessDomain takes a given base domain and attempts to enumerate subdomains.
func (source *Threatminer) ProcessDomain(ctx context.Context, domain string) <-chan *core.Result {
if source.lock == nil {
source.lock = defaultLockValue()
}
results := make(chan *core.Result)
go func(domain string, results chan *core.Result) {
defer close(results)
if err := source.lock.Acquire(ctx, 1); err != nil {
sendResultWithContext(ctx, results, core.NewResult(threatminerLabel, nil, err))
return
}
defer source.lock.Release(1)
domainExtractor := core.NewSingleSubdomainExtractor(domain)
req, err := http.NewRequest(http.MethodGet, "https://www.threatminer.org/getData.php?e=subdomains_container&q="+domain+"&t=0&rt=10&p=1", nil)
if err != nil {
sendResultWithContext(ctx, results, core.NewResult(threatminerLabel, nil, err))
return
}
req.Cancel = ctx.Done()
req.WithContext(ctx)
resp, err := core.HTTPClient.Do(req)
if err != nil {
sendResultWithContext(ctx, results, core.NewResult(threatminerLabel, nil, err))
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
sendResultWithContext(ctx, results, core.NewResult(threatminerLabel, nil, errors.New(resp.Status)))
return
}
scanner := bufio.NewScanner(resp.Body)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
if ctx.Err() != nil {
return
}
str := domainExtractor(scanner.Bytes())
if str != "" {
if !sendResultWithContext(ctx, results, core.NewResult(threatminerLabel, str, nil)) {
return
}
}
}
err = scanner.Err()
if err != nil {
sendResultWithContext(ctx, results, core.NewResult(threatminerLabel, nil, err))
return
}
}(domain, results)
return results
}