forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
names.go
323 lines (268 loc) · 7.56 KB
/
names.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
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
// Copyright 2017-2020 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package enum
import (
"strings"
"sync"
"github.com/OWASP/Amass/v3/eventbus"
"github.com/OWASP/Amass/v3/queue"
"github.com/OWASP/Amass/v3/requests"
"github.com/OWASP/Amass/v3/stringfilter"
)
var probeNames = []string{
"www",
"online",
"webserver",
"ns1",
"mail",
"smtp",
"webmail",
"prod",
"test",
"vpn",
"ftp",
"ssh",
}
// FQDNManager is the object type for taking in, generating and providing new DNS FQDNs.
type FQDNManager interface {
// InputName shares a newly discovered FQDN with the NameManager
InputName(req *requests.DNSRequest)
// OutputNames requests new FQDNs from the NameManager
OutputNames(num int) []*requests.DNSRequest
Stop() error
}
// DomainManager handles the release of new domains names to data sources used in the enumeration.
type DomainManager struct {
enum *Enumeration
queue *queue.Queue
filter stringfilter.Filter
}
// NewDomainManager returns an initialized DomainManager.
func NewDomainManager(e *Enumeration) *DomainManager {
return &DomainManager{
enum: e,
queue: new(queue.Queue),
filter: stringfilter.NewStringFilter(),
}
}
// InputName implements the FQDNManager interface.
func (r *DomainManager) InputName(req *requests.DNSRequest) {
if req == nil || req.Name == "" || req.Domain == "" {
return
}
if r.filter.Duplicate(req.Domain) {
return
}
r.queue.Append(req)
}
// OutputNames implements the FQDNManager interface.
func (r *DomainManager) OutputNames(num int) []*requests.DNSRequest {
var results []*requests.DNSRequest
if num <= 0 {
return results
}
element, ok := r.queue.Next()
if !ok {
return results
}
req := element.(*requests.DNSRequest)
results = append(results, req)
r.enum.srcsLock.Lock()
// Release the new domain name to all the data sources
for _, src := range r.enum.Sys.DataSources() {
if !r.enum.srcs.Has(src.String()) {
continue
}
src.DNSRequest(r.enum.ctx, &requests.DNSRequest{
Name: req.Domain,
Domain: req.Domain,
Tag: requests.DNS,
Source: "DNS",
})
}
r.enum.srcsLock.Unlock()
return results
}
// Stop implements the FQDNManager interface.
func (r *DomainManager) Stop() error {
r.queue = new(queue.Queue)
r.filter = stringfilter.NewStringFilter()
return nil
}
// SubdomainManager handles newly discovered proper subdomain names in the enumeration.
type SubdomainManager struct {
sync.Mutex
enum *Enumeration
queue *queue.Queue
subdomains map[string]int
}
// NewSubdomainManager returns an initialized SubdomainManager.
func NewSubdomainManager(e *Enumeration) *SubdomainManager {
return &SubdomainManager{
enum: e,
queue: new(queue.Queue),
subdomains: make(map[string]int),
}
}
// InputName implements the FQDNManager interface.
func (r *SubdomainManager) InputName(req *requests.DNSRequest) {
if req == nil || req.Name == "" || req.Domain == "" {
return
}
// Clean up the newly discovered name and domain
requests.SanitizeDNSRequest(req)
// Send every resolved name and associated DNS records to the data manager
r.enum.dataMgr.DNSRequest(r.enum.ctx, req)
if !r.enum.Config.IsDomainInScope(req.Name) {
return
}
// Keep track of all domains and proper subdomains discovered
r.checkSubdomain(req)
// Send out some probe requests to help cause recursive brute forcing
if r.enum.Config.BruteForcing && r.enum.Config.Recursive && r.enum.Config.MinForRecursive > 0 {
for _, probe := range probeNames {
r.queue.Append(&requests.DNSRequest{
Name: probe + "." + req.Name,
Domain: req.Domain,
Tag: requests.BRUTE,
Source: "Enum Probes",
})
}
}
// Queue the resolved name for future brute forcing
if r.enum.Config.BruteForcing && r.enum.Config.Recursive && r.enum.Config.MinForRecursive == 0 {
// Do not send in the resolved root domain names
if len(strings.Split(req.Name, ".")) != len(strings.Split(req.Domain, ".")) {
r.enum.bruteMgr.InputName(req)
}
}
r.enum.srcsLock.Lock()
defer r.enum.srcsLock.Unlock()
// Call DNSRequest for all web archive services
for _, srv := range r.enum.Sys.DataSources() {
if srv.Type() == requests.ARCHIVE && r.enum.srcs.Has(srv.String()) {
srv.DNSRequest(r.enum.ctx, req)
}
}
}
// OutputNames implements the FQDNManager interface.
func (r *SubdomainManager) OutputNames(num int) []*requests.DNSRequest {
var results []*requests.DNSRequest
for i := 0; ; i++ {
if num >= 0 && i >= num {
break
}
element, ok := r.queue.Next()
if !ok {
break
}
req := element.(*requests.DNSRequest)
results = append(results, req)
}
return results
}
// Stop implements the FQDNManager interface.
func (r *SubdomainManager) Stop() error {
r.queue = new(queue.Queue)
return nil
}
func (r *SubdomainManager) checkSubdomain(req *requests.DNSRequest) {
labels := strings.Split(req.Name, ".")
num := len(labels)
// Is this large enough to consider further?
if num < 2 {
return
}
// It cannot have fewer labels than the root domain name
if num-1 < len(strings.Split(req.Domain, ".")) {
return
}
// Do not further evaluate service subdomains
if labels[1] == "_tcp" || labels[1] == "_udp" || labels[1] == "_tls" {
return
}
sub := strings.TrimSpace(strings.Join(labels[1:], "."))
// CNAMEs are not a proper subdomain
if r.enum.Graph.IsCNAMENode(sub) {
return
}
subreq := &requests.DNSRequest{
Name: sub,
Domain: req.Domain,
Tag: req.Tag,
Source: req.Source,
}
times := r.timesForSubdomain(sub)
r.enum.Bus.Publish(requests.SubDiscoveredTopic, eventbus.PriorityHigh, r.enum.ctx, subreq, times)
// Queue the proper subdomain for future brute forcing
if r.enum.Config.BruteForcing && r.enum.Config.Recursive &&
r.enum.Config.MinForRecursive > 0 && r.enum.Config.MinForRecursive == times {
r.enum.bruteMgr.InputName(subreq)
}
// Check if the subdomain should be added to the markov model
if r.enum.Config.Alterations && r.enum.guessMgr != nil && times == 1 {
r.enum.guessMgr.AddSubdomain(sub)
}
r.enum.srcsLock.Lock()
defer r.enum.srcsLock.Unlock()
// Let all the data sources know about the discovered proper subdomain
for _, src := range r.enum.Sys.DataSources() {
if r.enum.srcs.Has(src.String()) {
src.SubdomainDiscovered(r.enum.ctx, subreq, times)
}
}
}
func (r *SubdomainManager) timesForSubdomain(sub string) int {
r.Lock()
defer r.Unlock()
times, found := r.subdomains[sub]
if found {
times++
} else {
times = 1
}
r.subdomains[sub] = times
return times
}
// NameManager handles the filtering and release of newly discovered FQDNs in the enumeration.
type NameManager struct {
enum *Enumeration
queue *queue.Queue
}
// NewNameManager returns an initialized NameManager.
func NewNameManager(e *Enumeration) *NameManager {
return &NameManager{
enum: e,
queue: new(queue.Queue),
}
}
// InputName implements the FQDNManager interface.
func (r *NameManager) InputName(req *requests.DNSRequest) {
if req == nil || req.Name == "" || req.Domain == "" {
return
}
// Clean up the newly discovered name and domain
requests.SanitizeDNSRequest(req)
r.queue.Append(req)
}
// OutputNames implements the FQDNManager interface.
func (r *NameManager) OutputNames(num int) []*requests.DNSRequest {
var results []*requests.DNSRequest
for i := 0; ; i++ {
if num >= 0 && i >= num {
break
}
element, ok := r.queue.Next()
if !ok {
break
}
req := element.(*requests.DNSRequest)
results = append(results, req)
}
return results
}
// Stop implements the FQDNManager interface.
func (r *NameManager) Stop() error {
r.queue = new(queue.Queue)
return nil
}