forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
622 lines (531 loc) · 16.6 KB
/
config.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// 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 core
import (
"bufio"
"compress/gzip"
"errors"
"fmt"
homedir "github.com/mitchellh/go-homedir"
"io"
"log"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"github.com/OWASP/Amass/amass/utils"
"github.com/go-ini/ini"
"github.com/google/uuid"
)
const (
// DefaultOutputDirectory is the name of the directory used for output files, such as the graph database.
DefaultOutputDirectory = "amass"
defaultWordlistURL = "https://raw.githubusercontent.com/OWASP/Amass/master/wordlists/namelist.txt"
defaultAltWordlistURL = "https://raw.githubusercontent.com/OWASP/Amass/master/wordlists/alterations.txt"
)
// Config passes along Amass configuration settings and options.
type Config struct {
sync.Mutex
// A Universally Unique Identifier (UUID) for the enumeration
UUID uuid.UUID
// Logger for error messages
Log *log.Logger
// The writer used to save the data operations performed
DataOptsWriter io.Writer
// The directory that stores the bolt db and other files created
Dir string `ini:"output_directory"`
// The settings for connecting with a Gremlin Server
GremlinURL string
GremlinUser string
GremlinPass string
// The maximum number of concurrent DNS queries
MaxDNSQueries int `ini:"maximum_dns_queries"`
// Semaphore to enforce the maximum DNS queries
SemMaxDNSQueries utils.Semaphore
// The IP addresses specified as in scope
Addresses []net.IP
// CIDR that is in scope
CIDRs []*net.IPNet
// ASNs specified as in scope
ASNs []int
// The ports that will be checked for certificates
Ports []int
// The list of words to use when generating names
Wordlist []string
// Will the enumeration including brute forcing techniques
BruteForcing bool
// Will recursive brute forcing be performed?
Recursive bool
// Minimum number of subdomain discoveries before performing recursive brute forcing
MinForRecursive int
// Will discovered subdomain name alterations be generated?
Alterations bool
FlipWords bool
FlipNumbers bool
AddWords bool
AddNumbers bool
MinForWordFlip int
EditDistance int
AltWordlist []string
// Only access the data sources for names and return results?
Passive bool
// Determines if zone transfers will be attempted
Active bool
// Determines if unresolved DNS names will be output by the enumeration
IncludeUnresolvable bool `ini:"include_unresolvable"`
// A blacklist of subdomain names that will not be investigated
Blacklist []string
// A list of data sources that should not be utilized
DisabledDataSources []string
// The root domain names that the enumeration will target
domains []string
// The regular expressions for the root domains added to the enumeration
regexps map[string]*regexp.Regexp
// The API keys used by various data sources
apikeys map[string]*APIKey
}
// APIKey contains values required for authenticating with web APIs.
type APIKey struct {
Username string `ini:"username"`
Password string `ini:"password"`
Key string `ini:"apikey"`
Secret string `ini:"secret"`
}
// CheckSettings runs some sanity checks on the configuration options selected.
func (c *Config) CheckSettings() error {
var err error
if c.BruteForcing {
if c.Passive {
return errors.New("Brute forcing cannot be performed without DNS resolution")
} else if len(c.Wordlist) == 0 {
c.Wordlist, err = getWordlistByURL(defaultWordlistURL)
}
}
if c.Passive && c.Active {
return errors.New("Active enumeration cannot be performed without DNS resolution")
}
if c.MaxDNSQueries <= 0 {
c.MaxDNSQueries = 1000
}
if len(c.Ports) == 0 {
c.Ports = []int{443}
}
if c.Alterations {
if len(c.AltWordlist) == 0 {
c.AltWordlist, err = getWordlistByURL(defaultAltWordlistURL)
}
}
c.SemMaxDNSQueries = utils.NewSimpleSemaphore(c.MaxDNSQueries)
return err
}
// 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] = utils.SubdomainRegex(d)
if c.regexps[d] != nil {
// Add the domain string to the list
c.domains = utils.UniqueAppend(c.domains, d)
}
}
// 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
n := strings.ToLower(strings.TrimSpace(name))
for _, d := range c.Domains() {
if n == d || strings.HasSuffix(n, "."+d) {
discovered = true
break
}
}
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.TrimSpace(name)
for _, d := range c.Domains() {
if n == d || strings.HasSuffix(n, "."+d) {
return d
}
}
return ""
}
// 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
}
// 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 {
var resp bool
n := strings.TrimSpace(name)
for _, bl := range c.Blacklist {
if match := strings.HasSuffix(n, bl); match {
resp = true
break
}
}
return resp
}
// ExcludeDisabledDataSources returns a list of data sources excluding DisabledDataSources.
func (c *Config) ExcludeDisabledDataSources(services []Service) []Service {
var enabled []Service
for _, s := range services {
include := true
for _, disabled := range c.DisabledDataSources {
if strings.EqualFold(disabled, s.String()) {
include = false
break
}
}
if include {
enabled = append(enabled, s)
}
}
return enabled
}
// AddAPIKey adds the data source and API key association provided to the configuration.
func (c *Config) AddAPIKey(source string, ak *APIKey) {
c.Lock()
defer c.Unlock()
idx := strings.TrimSpace(source)
if idx == "" {
return
}
if c.apikeys == nil {
c.apikeys = make(map[string]*APIKey)
}
c.apikeys[strings.ToLower(idx)] = ak
}
// GetAPIKey returns the API key associated with the provided data source name.
func (c *Config) GetAPIKey(source string) *APIKey {
c.Lock()
defer c.Unlock()
idx := strings.TrimSpace(source)
if apikey, found := c.apikeys[strings.ToLower(idx)]; found {
return apikey
}
return nil
}
func (c *Config) loadNetworkSettings(cfg *ini.File) error {
if network, err := cfg.GetSection("network_settings"); err == nil {
for _, addr := range network.Key("address").ValueWithShadows() {
var ips utils.ParseIPs
if err := ips.Set(addr); err != nil {
return err
}
c.Addresses = append(c.Addresses, ips...)
}
for _, cidr := range network.Key("cidr").ValueWithShadows() {
var ipnet *net.IPNet
if _, ipnet, err = net.ParseCIDR(cidr); err != nil {
return err
}
c.CIDRs = append(c.CIDRs, ipnet)
}
for _, asn := range network.Key("asn").ValueWithShadows() {
c.ASNs = uniqueIntAppend(c.ASNs, asn)
}
for _, port := range network.Key("port").ValueWithShadows() {
c.Ports = uniqueIntAppend(c.Ports, port)
}
}
return nil
}
func (c *Config) loadBruteForceSettings(cfg *ini.File) error {
if bruteforce, err := cfg.GetSection("bruteforce"); err == nil {
c.BruteForcing = bruteforce.Key("enabled").MustBool(true)
if c.BruteForcing {
c.Recursive = bruteforce.Key("recursive").MustBool(true)
c.MinForRecursive = bruteforce.Key("minimum_for_recursive").MustInt(0)
if bruteforce.HasKey("wordlist_file") {
for _, wordlist := range bruteforce.Key("wordlist_file").ValueWithShadows() {
list, err := GetListFromFile(wordlist)
if err != nil {
return fmt.Errorf("Unable to load the file in the bruteforce wordlist_file setting: %s: %v", wordlist, err)
}
c.Wordlist = utils.UniqueAppend(c.Wordlist, list...)
}
}
}
}
return nil
}
func (c *Config) loadAlterationSettings(cfg *ini.File) error {
if alterations, err := cfg.GetSection("alterations"); err == nil {
c.Alterations = alterations.Key("enabled").MustBool(true)
if c.Alterations {
c.FlipWords = alterations.Key("flip_words").MustBool(true)
c.AddWords = alterations.Key("add_words").MustBool(true)
c.FlipNumbers = alterations.Key("flip_numbers").MustBool(true)
c.AddNumbers = alterations.Key("add_numbers").MustBool(true)
c.MinForWordFlip = alterations.Key("minimum_for_word_flip").MustInt(2)
c.EditDistance = alterations.Key("edit_distance").MustInt(1)
if alterations.HasKey("wordlist_file") {
for _, wordlist := range alterations.Key("wordlist_file").ValueWithShadows() {
list, err := GetListFromFile(wordlist)
if err != nil {
return fmt.Errorf("Unable to load the file in the alterations wordlist_file setting: %s: %v", wordlist, err)
}
c.AltWordlist = utils.UniqueAppend(c.AltWordlist, list...)
}
}
}
}
return nil
}
// LoadSettings parses settings from an .ini file and assigns them to the Config.
func (c *Config) LoadSettings(path string) error {
cfg, err := ini.LoadSources(ini.LoadOptions{
Insensitive: true,
AllowShadows: true,
}, path)
if err != nil {
return fmt.Errorf("Failed to load the configuration file: %v", err)
}
// Get the easy ones out of the way using mapping
if err = cfg.MapTo(c); err != nil {
return fmt.Errorf("Error mapping configuration settings to internal values: %v", err)
}
// Attempt to load a special mode of operation specified by the user
if cfg.Section(ini.DEFAULT_SECTION).HasKey("mode") {
mode := cfg.Section(ini.DEFAULT_SECTION).Key("mode").String()
if mode == "passive" {
c.Passive = true
} else if mode == "active" {
c.Active = true
}
}
// Load up all the DNS domain names
if domains, err := cfg.GetSection("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("blacklisted"); err == nil {
c.Blacklist = utils.UniqueAppend(c.Blacklist,
blacklisted.Key("subdomain").ValueWithShadows()...)
}
// Load up all the disabled data source names
if disabled, err := cfg.GetSection("disabled_data_sources"); err == nil {
c.DisabledDataSources = utils.UniqueAppend(
c.DisabledDataSources, disabled.Key("data_source").ValueWithShadows()...)
}
// Load up all the Gremlin Server settings
if gremlin, err := cfg.GetSection("gremlin"); err == nil {
c.GremlinURL = gremlin.Key("url").String()
c.GremlinUser = gremlin.Key("username").String()
c.GremlinPass = gremlin.Key("password").String()
}
if err := c.loadNetworkSettings(cfg); err != nil {
return err
}
if err := c.loadAlterationSettings(cfg); err != nil {
return err
}
if err := c.loadBruteForceSettings(cfg); err != nil {
return err
}
// Load up all API key information from data source sections
nonAPISections := map[string]struct{}{
"alterations": struct{}{},
"bruteforce": struct{}{},
"default": struct{}{},
"domains": struct{}{},
"resolvers": struct{}{},
"blacklisted": struct{}{},
"disabled_data_sources": struct{}{},
"gremlin": struct{}{},
}
for _, section := range cfg.Sections() {
name := section.Name()
if _, skip := nonAPISections[name]; skip {
continue
}
key := new(APIKey)
// Parse the API key information and assign to the Config
if err := section.MapTo(key); err == nil {
c.AddAPIKey(name, key)
}
}
return nil
}
// AcquireConfig populates the Config struct provided by the config argument.
// The configuration file path and a bool indicating the settings were
// successfully loaded are returned.
func AcquireConfig(dir, file string, config *Config) (string, bool) {
if file != "" {
if err := config.LoadSettings(file); err == nil {
return file, true
}
}
if dir = OutputDirectory(dir); dir != "" {
if finfo, err := os.Stat(dir); !os.IsNotExist(err) && finfo.IsDir() {
file := filepath.Join(dir, "config.ini")
if err := config.LoadSettings(file); err == nil {
return file, true
}
}
}
return "", false
}
// OutputDirectory returns the file path of the Amass output directory. A suitable
// path provided will be used as the output directory instead.
func OutputDirectory(dir string) string {
if dir == "" {
if path, err := homedir.Dir(); err == nil {
dir = filepath.Join(path, DefaultOutputDirectory)
}
}
return dir
}
// GetResolversFromSettings loads the configuration file and returns all resolvers found.
func GetResolversFromSettings(path string) ([]string, error) {
cfg, err := ini.LoadSources(ini.LoadOptions{
Insensitive: true,
AllowShadows: true,
}, path)
if err != nil {
return nil, fmt.Errorf("Failed to load the configuration file: %v", err)
}
// Check that the resolvers section exists in the config file
sec, err := cfg.GetSection("resolvers")
if err != nil {
return nil, fmt.Errorf("The config file does not contain a resolvers section: %v", err)
}
resolvers := sec.Key("resolver").ValueWithShadows()
if len(resolvers) == 0 {
return nil, errors.New("No resolver keys were found in the resolvers section")
}
return resolvers, nil
}
// GetListFromFile reads a wordlist text or gzip file
// and returns the slice of words.
func GetListFromFile(path string) ([]string, error) {
var reader io.Reader
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("Error opening the file %s: %v", path, err)
}
defer file.Close()
reader = file
// We need to determine if this is a gzipped file or a plain text file, so we
// first read the first 512 bytes to pass them down to http.DetectContentType
// for mime detection. The file is rewinded before passing it along to the
// next reader
head := make([]byte, 512)
if _, err = file.Read(head); err != nil {
return nil, fmt.Errorf("Error reading the first 512 bytes from %s: %s", path, err)
}
if _, err = file.Seek(0, 0); err != nil {
return nil, fmt.Errorf("Error rewinding the file %s: %s", path, err)
}
// Read the file as gzip if it's actually compressed
if mt := http.DetectContentType(head); mt == "application/gzip" || mt == "application/x-gzip" {
gzReader, err := gzip.NewReader(file)
if err != nil {
return nil, fmt.Errorf("Error gz-reading the file %s: %v", path, err)
}
defer gzReader.Close()
reader = gzReader
}
return getWordList(reader)
}
func getWordlistByURL(url string) ([]string, error) {
page, err := utils.RequestWebPage(url, nil, nil, "", "")
if err != nil {
return nil, fmt.Errorf("Failed to obtain the wordlist at %s: %v", url, err)
}
return getWordList(strings.NewReader(page))
}
func getWordList(reader io.Reader) ([]string, error) {
var words []string
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
// Get the next word in the list
w := strings.TrimSpace(scanner.Text())
if err := scanner.Err(); err == nil && w != "" {
words = append(words, w)
}
}
return words, nil
}
func uniqueIntAppend(s []int, e string) []int {
if a1, err := strconv.Atoi(e); err == nil {
var found bool
for _, a2 := range s {
if a1 == a2 {
found = true
break
}
}
if !found {
s = append(s, a1)
}
}
return s
}