forked from owasp-amass/amass
/
wildcards.go
236 lines (206 loc) · 5.44 KB
/
wildcards.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
// 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 (
"errors"
"math/rand"
"strings"
"sync"
"time"
"github.com/OWASP/Amass/amass/core"
"github.com/miekg/dns"
)
const (
numOfWildcardTests = 5
maxDNSNameLen = 253
maxDNSLabelLen = 63
minLabelLen = 6
maxLabelLen = 24
ldhChars = "abcdefghijklmnopqrstuvwxyz0123456789-"
)
// Names for the different types of wildcards that can be detected.
const (
WildcardTypeNone = iota
WildcardTypeStatic
WildcardTypeDynamic
)
var (
wildcardLock sync.Mutex
wildcards map[string]*wildcard
)
type wildcard struct {
sync.RWMutex
WildcardType int
Answers []core.DNSAnswer
}
func init() {
wildcards = make(map[string]*wildcard)
}
// MatchesWildcard returns true if the request provided resolved to a DNS wildcard.
func MatchesWildcard(req *core.DNSRequest) bool {
if performWildcardRequest(req) == WildcardTypeNone {
return false
}
return true
}
// GetWildcardType returns the DNS wildcard type for the provided subdomain name.
func GetWildcardType(req *core.DNSRequest) int {
return performWildcardRequest(req)
}
func performWildcardRequest(req *core.DNSRequest) int {
base := len(strings.Split(req.Domain, "."))
labels := strings.Split(strings.ToLower(req.Name), ".")
if len(labels) > base {
labels = labels[1:]
}
for i := len(labels) - base; i >= 0; i-- {
w := getWildcard(strings.Join(labels[i:], "."))
if w.WildcardType == WildcardTypeDynamic {
return WildcardTypeDynamic
} else if w.WildcardType == WildcardTypeStatic {
if len(req.Records) == 0 || compareAnswers(req.Records, w.Answers) {
return WildcardTypeStatic
}
}
}
return checkIPsAcrossLevels(req)
}
func checkIPsAcrossLevels(req *core.DNSRequest) int {
if len(req.Records) == 0 {
return WildcardTypeNone
}
base := len(strings.Split(req.Domain, "."))
labels := strings.Split(strings.ToLower(req.Name), ".")
if len(labels) <= base || (len(labels)-base) < 3 {
return WildcardTypeNone
}
w1 := getWildcard(strings.Join(labels[1:], "."))
if w1.Answers != nil && compareAnswers(req.Records, w1.Answers) {
w2 := getWildcard(strings.Join(labels[2:], "."))
if w2.Answers != nil && compareAnswers(req.Records, w2.Answers) {
w3 := getWildcard(strings.Join(labels[3:], "."))
if w3.Answers != nil && compareAnswers(req.Records, w3.Answers) {
return WildcardTypeStatic
}
}
}
return WildcardTypeNone
}
func getWildcard(sub string) *wildcard {
var test bool
wildcardLock.Lock()
entry, found := wildcards[sub]
if !found {
entry = &wildcard{
WildcardType: WildcardTypeNone,
Answers: nil,
}
wildcards[sub] = entry
test = true
entry.Lock()
}
wildcardLock.Unlock()
// Check if the subdomain name is still to be tested for a wildcard
if !test {
entry.RLock()
entry.RUnlock()
return entry
}
// Query multiple times with unlikely names against this subdomain
set := make([][]core.DNSAnswer, numOfWildcardTests)
for i := 0; i < numOfWildcardTests; i++ {
a, err := wildcardTest(sub)
if err != nil {
// A test error gives it the most severe wildcard type
entry.WildcardType = WildcardTypeDynamic
entry.Unlock()
return entry
} else if a == nil {
// There is no DNS wildcard
entry.Unlock()
return entry
}
set[i] = a
time.Sleep(time.Second)
}
// Check if we have a static or dynamic DNS wildcard
match := true
for i := 0; i < numOfWildcardTests-1; i++ {
if !compareAnswers(set[i], set[i+1]) {
match = false
break
}
}
if match {
entry.WildcardType = WildcardTypeStatic
entry.Answers = set[0]
} else {
entry.WildcardType = WildcardTypeDynamic
}
entry.Unlock()
return entry
}
var wildcardQueryTypes = []string{
"CNAME",
"A",
"AAAA",
}
func wildcardTest(sub string) ([]core.DNSAnswer, error) {
name := UnlikelyName(sub)
if name == "" {
return nil, errors.New("Failed to generate the unlikely name for DNS wildcard testing")
}
var answers []core.DNSAnswer
for _, t := range wildcardQueryTypes {
if a, err := core.Resolve(name, t, core.PriorityCritical); err == nil {
if a != nil && len(a) > 0 {
answers = append(answers, a...)
}
} else if (err.(*core.ResolveError)).Rcode == 100 ||
(err.(*core.ResolveError)).Rcode == dns.RcodeRefused ||
(err.(*core.ResolveError)).Rcode == dns.RcodeServerFailure ||
(err.(*core.ResolveError)).Rcode == dns.RcodeNotImplemented {
return nil, errors.New("Failed to get a DNS server response during wildcard testing")
}
}
if len(answers) == 0 {
return nil, nil
}
return answers, nil
}
func compareAnswers(ans1, ans2 []core.DNSAnswer) bool {
for _, a1 := range ans1 {
for _, a2 := range ans2 {
if strings.EqualFold(a1.Data, a2.Data) {
return true
}
}
}
return false
}
// UnlikelyName takes a subdomain name and returns an unlikely DNS name within that subdomain.
func UnlikelyName(sub string) string {
var newlabel string
ldh := []rune(ldhChars)
ldhLen := len(ldh)
// Determine the max label length
l := maxDNSNameLen - (len(sub) + 1)
if l > maxLabelLen {
l = maxLabelLen
} else if l < minLabelLen {
return ""
}
// Shuffle our LDH characters
rand.Shuffle(ldhLen, func(i, j int) {
ldh[i], ldh[j] = ldh[j], ldh[i]
})
l = minLabelLen + rand.Intn((l-minLabelLen)+1)
for i := 0; i < l; i++ {
sel := rand.Int() % ldhLen
newlabel = newlabel + string(ldh[sel])
}
if newlabel == "" {
return newlabel
}
return strings.Trim(newlabel, "-") + "." + sub
}