-
Notifications
You must be signed in to change notification settings - Fork 486
/
main.go
145 lines (115 loc) · 2.28 KB
/
main.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
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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
)
func main() {
var subsOnly bool
flag.BoolVar(&subsOnly, "subs-only", false, "Only include subdomains of search domain")
flag.Parse()
var domains io.Reader
domains = os.Stdin
domain := flag.Arg(0)
if domain != "" {
domains = strings.NewReader(domain)
}
sources := []fetchFn{
fetchCertSpotter,
fetchHackerTarget,
fetchThreatCrowd,
fetchCrtSh,
fetchFacebook,
//fetchWayback, // A little too slow :(
fetchVirusTotal,
fetchFindSubDomains,
fetchUrlscan,
fetchBufferOverrun,
}
out := make(chan string)
var wg sync.WaitGroup
sc := bufio.NewScanner(domains)
rl := newRateLimiter(time.Second)
for sc.Scan() {
domain := strings.ToLower(sc.Text())
// call each of the source workers in a goroutine
for _, source := range sources {
wg.Add(1)
fn := source
go func() {
defer wg.Done()
rl.Block(fmt.Sprintf("%#v", fn))
names, err := fn(domain)
if err != nil {
//fmt.Fprintf(os.Stderr, "err: %s\n", err)
return
}
for _, n := range names {
n = cleanDomain(n)
if subsOnly && !strings.HasSuffix(n, domain) {
continue
}
out <- n
}
}()
}
}
// close the output channel when all the workers are done
go func() {
wg.Wait()
close(out)
}()
// track what we've already printed to avoid duplicates
printed := make(map[string]bool)
for n := range out {
if _, ok := printed[n]; ok {
continue
}
printed[n] = true
fmt.Println(n)
}
}
type fetchFn func(string) ([]string, error)
func httpGet(url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
return []byte{}, err
}
raw, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return []byte{}, err
}
return raw, nil
}
func cleanDomain(d string) string {
d = strings.ToLower(d)
// no idea what this is, but we can't clean it ¯\_(ツ)_/¯
if len(d) < 2 {
return d
}
if d[0] == '*' || d[0] == '%' {
d = d[1:]
}
if d[0] == '.' {
d = d[1:]
}
return d
}
func fetchJSON(url string, wrapper interface{}) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
return dec.Decode(wrapper)
}