-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
names.go
172 lines (148 loc) · 3.81 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
// Copyright © by Jeff Foley 2017-2023. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package enum
import (
"context"
"strings"
"github.com/OWASP/Amass/v3/requests"
"github.com/caffix/pipeline"
"github.com/caffix/stringset"
)
// subdomainTask handles newly discovered proper subdomain names in the enumeration.
type subdomainTask struct {
enum *Enumeration
cnames *stringset.Set
withinWildcards *stringset.Set
timesChan chan *timesReq
done chan struct{}
}
// newSubdomainTask returns an initialized SubdomainTask.
func newSubdomainTask(e *Enumeration) *subdomainTask {
r := &subdomainTask{
enum: e,
cnames: stringset.New(),
withinWildcards: stringset.New(),
timesChan: make(chan *timesReq, 10),
done: make(chan struct{}, 2),
}
go r.timesManager()
return r
}
// Stop releases resources allocated by the instance.
func (r *subdomainTask) Stop() {
close(r.done)
r.cnames.Close()
r.withinWildcards.Close()
}
// Process implements the pipeline Task interface.
func (r *subdomainTask) Process(ctx context.Context, data pipeline.Data, tp pipeline.TaskParams) (pipeline.Data, error) {
select {
case <-ctx.Done():
return nil, nil
default:
}
req, ok := data.(*requests.DNSRequest)
if !ok {
return data, nil
}
if req == nil || !r.enum.Config.IsDomainInScope(req.Name) {
return nil, nil
}
// Do not further evaluate service subdomains
for _, label := range strings.Split(req.Name, ".") {
l := strings.ToLower(label)
if l == "_tcp" || l == "_udp" || l == "_tls" {
return nil, nil
}
}
if r.checkForSubdomains(ctx, req, tp) {
r.enum.sendRequests(&requests.ResolvedRequest{
Name: req.Name,
Domain: req.Domain,
Records: req.Records,
})
}
return req, nil
}
func (r *subdomainTask) checkForSubdomains(ctx context.Context, req *requests.DNSRequest, tp pipeline.TaskParams) bool {
nlabels := strings.Split(req.Name, ".")
// Is this large enough to consider further?
if len(nlabels) < 2 {
return false
}
dlabels := strings.Split(req.Domain, ".")
// It cannot have fewer labels than the root domain name
if len(nlabels)-1 < len(dlabels) {
return true
}
sub := strings.TrimSpace(strings.Join(nlabels[1:], "."))
times := r.timesForSubdomain(sub)
if times == 1 && r.subWithinWildcard(ctx, sub, req.Domain) {
r.withinWildcards.Insert(sub)
return false
} else if times > 1 && r.withinWildcards.Has(sub) {
return false
} else if times == 1 && r.enum.graph.IsCNAMENode(ctx, sub) {
r.cnames.Insert(sub)
return true
} else if times > 1 && r.cnames.Has(sub) {
return true
}
subreq := &requests.SubdomainRequest{
Name: sub,
Domain: req.Domain,
Tag: req.Tag,
Source: req.Source,
Times: times,
}
r.enum.sendRequests(subreq)
if times == 1 {
pipeline.SendData(ctx, "root", subreq, tp)
}
return true
}
func (r *subdomainTask) subWithinWildcard(ctx context.Context, name, domain string) bool {
for _, t := range FwdQueryTypes {
select {
case <-ctx.Done():
return false
default:
}
if resp, err := r.enum.fwdQuery(ctx, "a."+name, t); err == nil &&
len(resp.Answer) > 0 && r.enum.Sys.TrustedResolvers().WildcardDetected(ctx, resp, domain) {
return true
}
}
return false
}
func (r *subdomainTask) timesForSubdomain(sub string) int {
ch := make(chan int, 2)
r.timesChan <- ×Req{
Sub: sub,
Ch: ch,
}
return <-ch
}
type timesReq struct {
Sub string
Ch chan int
}
func (r *subdomainTask) timesManager() {
subdomains := make(map[string]int)
for {
select {
case <-r.done:
return
case req := <-r.timesChan:
times, found := subdomains[req.Sub]
if found {
times++
} else {
times = 1
}
subdomains[req.Sub] = times
req.Ch <- times
}
}
}