/
scope.go
296 lines (245 loc) · 6.49 KB
/
scope.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
// 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 config
import (
"fmt"
"net"
"regexp"
"strconv"
"strings"
amassnet "github.com/gprime31/amass-prime/net"
"github.com/gprime31/amass-prime/net/dns"
"github.com/caffix/stringset"
"github.com/go-ini/ini"
)
// DomainRegex returns the Regexp object for the domain name identified by the parameter.
func (c *Config) DomainRegex(domain string) *regexp.Regexp {
c.Lock()
defer c.Unlock()
if re, found := c.regexps[domain]; found {
return re
}
return nil
}
// AddDomains appends the domain names provided in the parameter to the list in the configuration.
func (c *Config) AddDomains(domains ...string) {
for _, d := range domains {
c.AddDomain(d)
}
}
// AddDomain appends the domain name provided in the parameter to the list in the configuration.
func (c *Config) AddDomain(domain string) {
c.Lock()
defer c.Unlock()
// Check that the domain string is not empty
d := strings.TrimSpace(domain)
if d == "" {
return
}
// Check that it is a domain with at least two labels
labels := strings.Split(d, ".")
if len(labels) < 2 {
return
}
// Check that none of the labels are empty
for _, label := range labels {
if label == "" {
return
}
}
// Check that the regular expression map has been initialized
if c.regexps == nil {
c.regexps = make(map[string]*regexp.Regexp)
}
// Create the regular expression for this domain
c.regexps[d] = dns.SubdomainRegex(d)
if c.regexps[d] != nil {
// Add the domain string to the list
c.domains = append(c.domains, d)
}
c.domains = stringset.Deduplicate(c.domains)
}
// Domains returns the list of domain names currently in the configuration.
func (c *Config) Domains() []string {
c.Lock()
defer c.Unlock()
return c.domains
}
// IsDomainInScope returns true if the DNS name in the parameter ends with a domain in the config list.
func (c *Config) IsDomainInScope(name string) bool {
var discovered bool
if domain := c.WhichDomain(name); domain != "" {
discovered = true
}
return discovered
}
// WhichDomain returns the domain in the config list that the DNS name in the parameter ends with.
func (c *Config) WhichDomain(name string) string {
n := strings.ToLower(strings.TrimSpace(name))
for _, d := range c.Domains() {
if hasPathSuffix(n, d) {
return d
}
}
return ""
}
func hasPathSuffix(path, suffix string) bool {
if strings.HasSuffix(path, suffix) {
plen := len(path)
slen := len(suffix)
// Check for exact match first to guard against out of bound index
if plen == slen || path[plen-slen-1] == '.' {
return true
}
}
return false
}
// IsAddressInScope returns true if the addr parameter matches provided network scope and when
// no network scope has been set.
func (c *Config) IsAddressInScope(addr string) bool {
ip := net.ParseIP(addr)
if ip == nil {
return false
}
if len(c.Addresses) == 0 && len(c.CIDRs) == 0 {
return true
}
for _, a := range c.Addresses {
if a.String() == ip.String() {
return true
}
}
for _, cidr := range c.CIDRs {
if cidr.Contains(ip) {
return true
}
}
return false
}
// BlacklistSubdomain adds a subdomain name to the config blacklist.
func (c *Config) BlacklistSubdomain(name string) {
c.blacklistLock.Lock()
defer c.blacklistLock.Unlock()
set := stringset.New(c.Blacklist...)
defer set.Close()
set.Insert(strings.TrimSpace(name))
c.Blacklist = set.Slice()
}
// Blacklisted returns true is the name in the parameter ends with a subdomain name in the config blacklist.
func (c *Config) Blacklisted(name string) bool {
c.blacklistLock.Lock()
defer c.blacklistLock.Unlock()
n := strings.ToLower(strings.TrimSpace(name))
for _, bl := range c.Blacklist {
if hasPathSuffix(n, bl) {
return true
}
}
return false
}
func (c *Config) loadScopeSettings(cfg *ini.File) error {
scope, err := cfg.GetSection("scope")
if err != nil {
return nil
}
if scope.HasKey("address") {
for _, addr := range scope.Key("address").ValueWithShadows() {
var ips parseIPs
if err := ips.Set(addr); err != nil {
return err
}
c.Addresses = append(c.Addresses, ips...)
}
}
if scope.HasKey("cidr") {
for _, cidr := range scope.Key("cidr").ValueWithShadows() {
var ipnet *net.IPNet
if _, ipnet, err = net.ParseCIDR(cidr); err != nil {
return err
}
c.CIDRs = append(c.CIDRs, ipnet)
}
}
if scope.HasKey("asn") {
for _, asn := range scope.Key("asn").ValueWithShadows() {
c.ASNs = uniqueIntAppend(c.ASNs, asn)
}
}
if scope.HasKey("port") {
for _, port := range scope.Key("port").ValueWithShadows() {
c.Ports = uniqueIntAppend(c.Ports, port)
}
}
// Load up all the DNS domain names
if domains, err := cfg.GetSection("scope.domains"); err == nil {
for _, domain := range domains.Key("domain").ValueWithShadows() {
c.AddDomain(domain)
}
}
// Load up all the blacklisted subdomain names
if blacklisted, err := cfg.GetSection("scope.blacklisted"); err == nil {
c.Blacklist = stringset.Deduplicate(blacklisted.Key("subdomain").ValueWithShadows())
}
return nil
}
type parseIPs []net.IP
func (p *parseIPs) String() string {
if p == nil {
return ""
}
var ipaddrs []string
for _, ipaddr := range *p {
ipaddrs = append(ipaddrs, ipaddr.String())
}
return strings.Join(ipaddrs, ",")
}
// Set implements the flag.Value interface.
func (p *parseIPs) Set(s string) error {
if s == "" {
return fmt.Errorf("IP address parsing failed")
}
ips := strings.Split(s, ",")
for _, ip := range ips {
// Is this an IP range?
err := p.parseRange(ip)
if err == nil {
continue
}
addr := net.ParseIP(ip)
if addr == nil {
return fmt.Errorf("%s is not a valid IP address or range", ip)
}
*p = append(*p, addr)
}
return nil
}
func (p *parseIPs) appendIPs(addrs []net.IP) error {
for _, addr := range addrs {
*p = append(*p, addr)
}
return nil
}
func (p *parseIPs) parseRange(s string) error {
twoIPs := strings.Split(s, "-")
if twoIPs[0] == s {
return fmt.Errorf("%s is not a valid IP range", s)
}
start := net.ParseIP(twoIPs[0])
end := net.ParseIP(twoIPs[1])
if end == nil {
num, err := strconv.Atoi(twoIPs[1])
if err == nil {
end = net.ParseIP(twoIPs[0])
end[len(end)-1] = byte(num)
}
}
if start == nil || end == nil {
return fmt.Errorf("%s is not a valid IP range", s)
}
ips := amassnet.RangeHosts(start, end)
if len(ips) == 0 {
return fmt.Errorf("%s is not a valid IP range", s)
}
return p.appendIPs(ips)
}