forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brute.go
347 lines (284 loc) · 7.55 KB
/
brute.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2017 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"
alts "github.com/OWASP/Amass/v3/alterations"
"github.com/OWASP/Amass/v3/queue"
"github.com/OWASP/Amass/v3/requests"
"github.com/OWASP/Amass/v3/stringfilter"
"github.com/OWASP/Amass/v3/stringset"
)
// BruteManager handles the release of FQDNs generated by brute forcing.
type BruteManager struct {
sync.Mutex
enum *Enumeration
queue *queue.Queue
filter stringfilter.Filter
wordlistIdx int
curReq *requests.DNSRequest
}
// NewBruteManager returns an initialized BruteManager.
func NewBruteManager(e *Enumeration) *BruteManager {
return &BruteManager{
enum: e,
queue: new(queue.Queue),
filter: stringfilter.NewStringFilter(),
}
}
// InputName implements the FQDNManager interface.
func (r *BruteManager) InputName(req *requests.DNSRequest) {
if req == nil || req.Name == "" || req.Domain == "" {
return
}
// Clean up the newly discovered name and domain
requests.SanitizeDNSRequest(req)
if !r.enum.Config.IsDomainInScope(req.Name) {
return
}
if !r.enum.Config.BruteForcing || r.filter.Duplicate(req.Name) {
return
}
if len(req.Records) > 0 && (r.enum.hasCNAMERecord(req) || !r.enum.hasARecords(req)) {
return
}
r.queue.Append(req)
}
// OutputNames implements the FQDNManager interface.
func (r *BruteManager) OutputNames(num int) []*requests.DNSRequest {
r.Lock()
defer r.Unlock()
var results []*requests.DNSRequest
if !r.enum.Config.BruteForcing {
return results
}
var count int
loop:
for {
if r.curReq == nil {
// Get the next subdomain to brute force
element, ok := r.queue.Next()
if !ok {
break loop
}
r.curReq = element.(*requests.DNSRequest)
}
for {
// Only return the number of names requested
if count >= num {
break loop
}
// Check that we haven't used all the words in the list
if r.wordlistIdx >= len(r.enum.Config.Wordlist) {
r.curReq = nil
r.wordlistIdx = 0
continue loop
}
word := r.enum.Config.Wordlist[r.wordlistIdx]
r.wordlistIdx++
// Check that we have a good word and generate the new name
if word != "" {
count++
results = append(results, &requests.DNSRequest{
Name: word + "." + r.curReq.Name,
Domain: r.curReq.Domain,
Tag: requests.BRUTE,
Source: "Brute Forcing",
})
}
}
}
return results
}
// Stop implements the FQDNManager interface.
func (r *BruteManager) Stop() error {
r.curReq = nil
r.wordlistIdx = 0
r.queue = new(queue.Queue)
r.filter = stringfilter.NewStringFilter()
return nil
}
// AlterationsManager handles the release of FQDNs generated by name alterations.
type AlterationsManager struct {
enum *Enumeration
inQueue *queue.Queue
outQueue *queue.Queue
altState *alts.State
}
// NewAlterationsManager returns an initialized AlterationsManager.
func NewAlterationsManager(e *Enumeration) *AlterationsManager {
am := &AlterationsManager{
enum: e,
inQueue: new(queue.Queue),
outQueue: new(queue.Queue),
altState: alts.NewState(e.Config.AltWordlist),
}
am.altState.MinForWordFlip = e.Config.MinForWordFlip
am.altState.EditDistance = e.Config.EditDistance
return am
}
// InputName implements the FQDNManager interface.
func (r *AlterationsManager) InputName(req *requests.DNSRequest) {
if req == nil || req.Name == "" || req.Domain == "" {
return
}
// Clean up the newly discovered name and domain
requests.SanitizeDNSRequest(req)
if !r.enum.Config.IsDomainInScope(req.Name) {
return
}
if !r.enum.Config.Alterations {
return
}
if len(strings.Split(req.Name, ".")) <= len(strings.Split(req.Domain, ".")) {
return
}
r.inQueue.Append(req)
}
// OutputNames implements the FQDNManager interface.
func (r *AlterationsManager) OutputNames(num int) []*requests.DNSRequest {
var results []*requests.DNSRequest
if !r.enum.Config.Alterations {
return results
}
for num > r.outQueue.Len() {
if c := r.generateAlts(); c == 0 {
break
}
}
for i := 0; i < num; i++ {
element, ok := r.outQueue.Next()
if !ok {
break
}
results = append(results, element.(*requests.DNSRequest))
}
return results
}
// Stop implements the FQDNManager interface.
func (r *AlterationsManager) Stop() error {
r.inQueue = new(queue.Queue)
r.outQueue = new(queue.Queue)
r.altState = alts.NewState(r.enum.Config.AltWordlist)
return nil
}
func (r *AlterationsManager) generateAlts() int {
names := stringset.New()
// Get the next FQDN to generate alterations from
element, ok := r.inQueue.Next()
if !ok {
return 0
}
req := element.(*requests.DNSRequest)
if r.enum.Config.FlipNumbers {
names.InsertMany(r.altState.FlipNumbers(req.Name)...)
}
if r.enum.Config.AddNumbers {
names.InsertMany(r.altState.AppendNumbers(req.Name)...)
}
if r.enum.Config.FlipWords {
names.InsertMany(r.altState.FlipWords(req.Name)...)
}
if r.enum.Config.AddWords {
names.InsertMany(r.altState.AddSuffixWord(req.Name)...)
names.InsertMany(r.altState.AddPrefixWord(req.Name)...)
}
if r.enum.Config.EditDistance > 0 {
names.InsertMany(r.altState.FuzzyLabelSearches(req.Name)...)
}
var count int
for name := range names {
if !r.enum.Config.IsDomainInScope(name) {
continue
}
count++
r.outQueue.Append(&requests.DNSRequest{
Name: name,
Domain: req.Domain,
Tag: requests.ALT,
Source: "Alterations",
})
}
return count
}
// GuessManager handles the release of FQDNs generated from machine learning.
type GuessManager struct {
sync.Mutex
enum *Enumeration
markovModel *alts.MarkovModel
ttLastOutput int
}
// NewGuessManager returns an initialized GuessManager.
func NewGuessManager(e *Enumeration) *GuessManager {
return &GuessManager{
enum: e,
markovModel: alts.NewMarkovModel(3),
}
}
// InputName implements the FQDNManager interface.
func (r *GuessManager) InputName(req *requests.DNSRequest) {
if req == nil || req.Name == "" || req.Domain == "" {
return
}
// Clean up the newly discovered name and domain
requests.SanitizeDNSRequest(req)
if !r.enum.Config.IsDomainInScope(req.Name) {
return
}
if !r.enum.Config.Alterations {
return
}
if len(strings.Split(req.Name, ".")) <= len(strings.Split(req.Domain, ".")) {
return
}
r.markovModel.Train(req.Name)
}
// OutputNames implements the FQDNManager interface.
func (r *GuessManager) OutputNames(num int) []*requests.DNSRequest {
var results []*requests.DNSRequest
if !r.enum.Config.Alterations {
return results
}
r.Lock()
last := r.ttLastOutput
r.Unlock()
if r.markovModel.TotalTrainings() < 50 || r.markovModel.TotalTrainings() <= last {
return results
}
r.Lock()
r.ttLastOutput = r.markovModel.TotalTrainings()
r.Unlock()
guesses := stringset.New(r.markovModel.GenerateNames(num * 2)...)
for num > guesses.Len() {
guesses.InsertMany(r.markovModel.GenerateNames(num)...)
}
var count int
for name := range guesses {
if count >= num {
break
}
domain := r.enum.Config.WhichDomain(name)
if domain == "" || name == "" {
continue
}
count++
results = append(results, &requests.DNSRequest{
Name: name,
Domain: domain,
Tag: requests.GUESS,
Source: "Markov Model",
})
}
return results
}
// AddSubdomain is unique to the GuessManager and allows newly discovered
// subdomain names to be shared with the MarkovModel object.
func (r *GuessManager) AddSubdomain(sub string) {
r.markovModel.AddSubdomain(sub)
}
// Stop implements the FQDNManager interface.
func (r *GuessManager) Stop() error {
r.markovModel = alts.NewMarkovModel(3)
return nil
}