/
alteration.go
228 lines (192 loc) · 5.1 KB
/
alteration.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
// 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 amass
import (
"strconv"
"strings"
"unicode"
"github.com/OWASP/Amass/amass/core"
"github.com/miekg/dns"
)
var (
altWords []string
altAlphabet string
)
func init() {
altWords = []string{
"account",
"accounts",
"app",
"auth",
"cfg",
"dev",
"ftp",
"imap",
"login",
"mail",
"mon",
"prd",
"prod",
"proxy",
"qa",
"smtp",
"sql",
"sso",
"stage",
"staging",
"stg",
"test",
"tst",
"uat",
"users",
"web",
"www",
}
altAlphabet = "abcdefghijklmnopqrstuvwxyz"
}
// AlterationService is the Service that handles all DNS name permutation within
// the architecture. This is achieved by receiving all the RESOLVED events.
type AlterationService struct {
core.BaseService
}
// NewAlterationService returns he object initialized, but not yet started.
func NewAlterationService(config *core.Config, bus *core.EventBus) *AlterationService {
as := new(AlterationService)
as.BaseService = *core.NewBaseService(as, "Alterations", config, bus)
return as
}
// OnStart implements the Service interface
func (as *AlterationService) OnStart() error {
as.BaseService.OnStart()
if as.Config().Alterations {
as.Bus().Subscribe(core.NameResolvedTopic, as.SendRequest)
go as.processRequests()
}
return nil
}
func (as *AlterationService) processRequests() {
for {
select {
case <-as.PauseChan():
<-as.ResumeChan()
case <-as.Quit():
return
case req := <-as.RequestChan():
go as.executeAlterations(req)
}
}
}
// executeAlterations runs all the DNS name alteration methods as goroutines.
func (as *AlterationService) executeAlterations(req *core.Request) {
if !as.correctRecordTypes(req) || !as.Config().IsDomainInScope(req.Name) {
return
}
as.SetActive()
as.flipNumbersInName(req)
as.appendNumbers(req)
as.addSuffixWord(req)
as.addSuffixLetter(req)
as.addPrefixWord(req)
as.addPrefixLetter(req)
}
func (as *AlterationService) correctRecordTypes(req *core.Request) bool {
var ok bool
for _, r := range req.Records {
t := uint16(r.Type)
if t == dns.TypeTXT || t == dns.TypeA || t == dns.TypeAAAA || t == dns.TypeCNAME {
ok = true
break
}
}
return ok
}
// flipNumbersInName flips numbers in a subdomain name.
func (as *AlterationService) flipNumbersInName(req *core.Request) {
n := req.Name
parts := strings.SplitN(n, ".", 2)
// Find the first character that is a number
first := strings.IndexFunc(parts[0], unicode.IsNumber)
if first < 0 {
return
}
// Flip the first number and attempt a second number
for i := 0; i < 10; i++ {
sf := n[:first] + strconv.Itoa(i) + n[first+1:]
as.secondNumberFlip(sf, req.Domain, first+1)
}
// Take the first number out
as.secondNumberFlip(n[:first]+n[first+1:], req.Domain, -1)
}
func (as *AlterationService) secondNumberFlip(name, domain string, minIndex int) {
parts := strings.SplitN(name, ".", 2)
// Find the second character that is a number
last := strings.LastIndexFunc(parts[0], unicode.IsNumber)
if last < 0 || last < minIndex {
as.sendAlteredName(name, domain)
return
}
// Flip those numbers and send out the mutations
for i := 0; i < 10; i++ {
n := name[:last] + strconv.Itoa(i) + name[last+1:]
as.sendAlteredName(n, domain)
}
// Take the second number out
as.sendAlteredName(name[:last]+name[last+1:], domain)
}
// appendNumbers appends a number to a subdomain name.
func (as *AlterationService) appendNumbers(req *core.Request) {
n := req.Name
parts := strings.SplitN(n, ".", 2)
for i := 0; i < 10; i++ {
as.addSuffix(parts, strconv.Itoa(i), req.Domain)
}
}
func (as *AlterationService) addSuffix(parts []string, suffix, domain string) {
nn := parts[0] + suffix + "." + parts[1]
as.sendAlteredName(nn, domain)
nn = parts[0] + "-" + suffix + "." + parts[1]
as.sendAlteredName(nn, domain)
}
func (as *AlterationService) addPrefix(name, prefix, domain string) {
nn := prefix + name
as.sendAlteredName(nn, domain)
nn = prefix + "-" + name
as.sendAlteredName(nn, domain)
}
func (as *AlterationService) addSuffixWord(req *core.Request) {
n := req.Name
parts := strings.SplitN(n, ".", 2)
for _, word := range altWords {
as.addSuffix(parts, word, req.Domain)
}
}
func (as *AlterationService) addSuffixLetter(req *core.Request) {
n := req.Name
parts := strings.SplitN(n, ".", 2)
for _, ch := range altAlphabet {
as.addSuffix(parts, string(ch), req.Domain)
}
}
func (as *AlterationService) addPrefixWord(req *core.Request) {
for _, word := range altWords {
as.addPrefix(req.Name, word, req.Domain)
}
}
func (as *AlterationService) addPrefixLetter(req *core.Request) {
for _, ch := range altAlphabet {
as.addPrefix(req.Name, string(ch), req.Domain)
}
}
// sendAlteredName checks that the provided name is valid before publishing it as a new name.
func (as *AlterationService) sendAlteredName(name, domain string) {
re := as.Config().DomainRegex(domain)
if re == nil || !re.MatchString(name) {
return
}
as.Bus().Publish(core.NewNameTopic, &core.Request{
Name: name,
Domain: domain,
Tag: core.ALT,
Source: as.String(),
})
}