-
Notifications
You must be signed in to change notification settings - Fork 2
/
udig.go
218 lines (169 loc) · 5.11 KB
/
udig.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
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
package udig
import (
"sync"
"github.com/miekg/dns"
)
type udigImpl struct {
Udig
domainResolvers []DomainResolver
ipResolvers []IPResolver
domainQueue chan string
ipQueue chan string
processed map[string]bool
seen map[string]bool
}
// NewUdig creates a new Udig instances provisioned with
// all supported resolvers. You can also supply your own
// resolvers to the returned instance.
func NewUdig() Udig {
udig := &udigImpl{
domainResolvers: []DomainResolver{},
ipResolvers: []IPResolver{},
domainQueue: make(chan string, 1024),
ipQueue: make(chan string, 1024),
processed: map[string]bool{},
seen: map[string]bool{},
}
udig.AddDomainResolver(NewDNSResolver())
udig.AddDomainResolver(NewWhoisResolver())
udig.AddDomainResolver(NewTLSResolver())
udig.AddDomainResolver(NewHTTPResolver())
udig.AddDomainResolver(NewCTResolver())
udig.AddIPResolver(NewBGPResolver())
udig.AddIPResolver(NewGeoResolver())
return udig
}
func (udig *udigImpl) Resolve(domain string) []Resolution {
udig.domainQueue <- domain
return udig.resolveDomains()
}
func (udig *udigImpl) AddDomainResolver(resolver DomainResolver) {
udig.domainResolvers = append(udig.domainResolvers, resolver)
}
func (udig *udigImpl) AddIPResolver(resolver IPResolver) {
udig.ipResolvers = append(udig.ipResolvers, resolver)
}
func (udig *udigImpl) resolveDomains() (resolutions []Resolution) {
for len(udig.domainQueue) > 0 {
// Poll a domain.
domain := <-udig.domainQueue
// Resolve it.
newResolutions := udig.resolveOneDomain(domain)
// Store the results.
resolutions = append(resolutions, newResolutions...)
// Enqueue all related domains from the result.
udig.enqueueDomains(udig.getRelatedDomains(newResolutions)...)
// Resolve all the discovered IPs.
resolutions = append(resolutions, udig.resolveIPs()...)
}
return resolutions
}
func (udig *udigImpl) resolveIPs() (resolutions []Resolution) {
for len(udig.ipQueue) > 0 {
// Poll an IP.
ip := <-udig.ipQueue
// Resolve it.
newResolutions := udig.resolveOneIP(ip)
resolutions = append(resolutions, newResolutions...)
}
return resolutions
}
func (udig *udigImpl) resolveOneDomain(domain string) (resolutions []Resolution) {
// Make sure we don't repeat ourselves.
if udig.isProcessed(domain) {
return resolutions
}
defer udig.addProcessed(domain)
resolutionChannel := make(chan Resolution, 1024)
var wg sync.WaitGroup
wg.Add(len(udig.domainResolvers))
for _, resolver := range udig.domainResolvers {
go func(resolver DomainResolver) {
resolution := resolver.ResolveDomain(domain)
resolutionChannel <- resolution
// Enqueue all discovered IPs.
udig.enqueueIps(resolution.IPs()...)
wg.Done()
}(resolver)
}
wg.Wait()
for len(resolutionChannel) > 0 {
resolutions = append(resolutions, <-resolutionChannel)
}
return resolutions
}
func (udig *udigImpl) resolveOneIP(ip string) (resolutions []Resolution) {
// Make sure we don't repeat ourselves.
if udig.isProcessed(ip) {
return resolutions
}
defer udig.addProcessed(ip)
resolutionChannel := make(chan Resolution, 1024)
var wg sync.WaitGroup
wg.Add(len(udig.ipResolvers))
for _, resolver := range udig.ipResolvers {
go func(resolver IPResolver) {
resolutionChannel <- resolver.ResolveIP(ip)
wg.Done()
}(resolver)
}
wg.Wait()
for len(resolutionChannel) > 0 {
resolutions = append(resolutions, <-resolutionChannel)
}
return resolutions
}
func (udig *udigImpl) isCnameOrRelated(nextDomain string, resolution Resolution) bool {
switch resolution.Type() {
case TypeDNS:
for _, rr := range resolution.(*DNSResolution).Records {
if rr.Record.Header().Rrtype == dns.TypeCNAME && rr.Record.RR.(*dns.CNAME).Target == nextDomain {
// Follow DNS CNAME pointers.
return true
}
}
break
}
// Otherwise try heuristics.
return IsDomainRelated(nextDomain, resolution.Query())
}
func (udig *udigImpl) getRelatedDomains(resolutions []Resolution) (domains []string) {
for _, resolution := range resolutions {
for _, nextDomain := range resolution.Domains() {
// Crawl new and related domains only.
if udig.isProcessed(nextDomain) || udig.isSeen(nextDomain) {
continue
}
udig.addSeen(nextDomain)
if !udig.isCnameOrRelated(nextDomain, resolution) {
LogDebug("%s: Domain %s is not related to %s -> skipping.", resolution.Type(), nextDomain, resolution.Query())
continue
}
LogDebug("%s: Discovered a related domain %s via %s.", resolution.Type(), nextDomain, resolution.Query())
domains = append(domains, nextDomain)
}
}
return domains
}
func (udig *udigImpl) enqueueDomains(domains ...string) {
for _, domain := range domains {
udig.domainQueue <- domain
}
}
func (udig *udigImpl) enqueueIps(ips ...string) {
for _, ip := range ips {
udig.ipQueue <- ip
}
}
func (udig *udigImpl) isProcessed(query string) bool {
return udig.processed[query]
}
func (udig *udigImpl) addProcessed(query string) {
udig.processed[query] = true
}
func (udig *udigImpl) isSeen(query string) bool {
return udig.seen[query]
}
func (udig *udigImpl) addSeen(query string) {
udig.seen[query] = true
}