forked from owasp-amass/amass
/
namesrv.go
201 lines (176 loc) · 4.74 KB
/
namesrv.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
// 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 (
"regexp"
"strings"
"time"
"github.com/OWASP/Amass/amass/core"
"github.com/OWASP/Amass/amass/handlers"
"github.com/OWASP/Amass/amass/utils"
)
type timesRequest struct {
Subdomain string
Times chan int
}
// NameService is the Service that handles all newly discovered names
// within the architecture. This is achieved by receiving all the RESOLVED events.
type NameService struct {
core.BaseService
filter *utils.StringFilter
times *utils.Queue
sanityRE *regexp.Regexp
trustedNameFilter *utils.StringFilter
otherNameFilter *utils.StringFilter
graph handlers.DataHandler
}
// NewNameService requires the enumeration configuration and event bus as parameters.
// The object returned is initialized, but has not yet been started.
func NewNameService(config *core.Config, bus *core.EventBus) *NameService {
ns := &NameService{
filter: utils.NewStringFilter(),
times: utils.NewQueue(),
sanityRE: utils.AnySubdomainRegex(),
trustedNameFilter: utils.NewStringFilter(),
otherNameFilter: utils.NewStringFilter(),
}
ns.BaseService = *core.NewBaseService(ns, "Name Service", config, bus)
return ns
}
// OnStart implements the Service interface
func (ns *NameService) OnStart() error {
ns.BaseService.OnStart()
ns.Bus().Subscribe(core.NewNameTopic, ns.newNameEvent)
ns.Bus().Subscribe(core.NameResolvedTopic, ns.Resolved)
go ns.processTimesRequests()
go ns.processRequests()
return nil
}
// RegisterGraph makes the Graph available to the NameService.
func (ns *NameService) RegisterGraph(graph handlers.DataHandler) {
ns.graph = graph
}
func (ns *NameService) newNameEvent(req *core.DNSRequest) {
if req == nil || req.Name == "" || req.Domain == "" {
return
}
req.Name = strings.ToLower(utils.RemoveAsteriskLabel(req.Name))
req.Domain = strings.ToLower(req.Domain)
tt := TrustedTag(req.Tag)
if !tt && ns.otherNameFilter.Duplicate(req.Name) {
return
} else if tt && ns.trustedNameFilter.Duplicate(req.Name) {
return
}
ns.SendDNSRequest(req)
}
func (ns *NameService) processRequests() {
for {
select {
case <-ns.PauseChan():
<-ns.ResumeChan()
case <-ns.Quit():
return
case req := <-ns.DNSRequestChan():
ns.performRequest(req)
case <-ns.AddrRequestChan():
case <-ns.ASNRequestChan():
case <-ns.WhoisRequestChan():
}
}
}
func (ns *NameService) performRequest(req *core.DNSRequest) {
ns.SetActive()
if ns.Config().Passive {
if !ns.filter.Duplicate(req.Name) && ns.sanityRE.MatchString(req.Name) {
ns.Bus().Publish(core.OutputTopic, &core.Output{
Name: req.Name,
Domain: req.Domain,
Tag: req.Tag,
Source: req.Source,
})
}
return
}
ns.Bus().Publish(core.ResolveNameTopic, req)
}
// Resolved is called when a name has been resolved by the DNS Service.
func (ns *NameService) Resolved(req *core.DNSRequest) {
ns.SetActive()
if ns.Config().IsDomainInScope(req.Name) {
ns.checkSubdomain(req)
}
}
func (ns *NameService) checkSubdomain(req *core.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.Join(labels[1:], ".")
if ns.graph != nil {
// CNAMEs are not a proper subdomain
cname := ns.graph.IsCNAMENode(&handlers.DataOptsParams{
UUID: ns.Config().UUID.String(),
Name: sub,
Domain: req.Domain,
})
if cname {
return
}
}
ns.Bus().Publish(core.NewSubdomainTopic, &core.DNSRequest{
Name: sub,
Domain: req.Domain,
Tag: req.Tag,
Source: req.Source,
}, ns.timesForSubdomain(sub))
}
func (ns *NameService) timesForSubdomain(sub string) int {
times := make(chan int)
ns.times.Append(×Request{
Subdomain: sub,
Times: times,
})
return <-times
}
func (ns *NameService) processTimesRequests() {
curIdx := 0
maxIdx := 9
delays := []int{10, 25, 50, 75, 100, 150, 250, 500, 750, 1000}
subdomains := make(map[string]int)
for {
select {
case <-ns.Quit():
return
default:
element, ok := ns.times.Next()
if !ok {
if curIdx < maxIdx {
curIdx++
}
time.Sleep(time.Duration(delays[curIdx]) * time.Millisecond)
continue
}
curIdx = 0
req := element.(*timesRequest)
times, ok := subdomains[req.Subdomain]
if ok {
times++
} else {
times = 1
}
subdomains[req.Subdomain] = times
req.Times <- times
}
}
}