forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
430 lines (382 loc) · 10.6 KB
/
main.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
// 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 main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"os/signal"
"path"
//"runtime/pprof"
"strconv"
"strings"
"syscall"
"time"
"github.com/caffix/amass/amass"
"github.com/caffix/recon"
)
var AsciiArt string = `
.+++:. : .+++.
+W@@@@@@8 &+W@# o8W8: +W@@@@@@#. oW@@@W#+
&@#+ .o@##. .@@@o@W.o@@o :@@#&W8o .@#: .:oW+ .@#+++&#&
+@& &@& #@8 +@W@&8@+ :@W. +@8 +@: .@8
8@ @@ 8@o 8@8 WW .@W W@+ .@W. o@#:
WW &@o &@: o@+ o@+ #@. 8@o +W@#+. +W@8:
#@ :@W &@+ &@+ @8 :@o o@o oW@@W+ oW@8
o@+ @@& &@+ &@+ #@ &@. .W@W .+#@& o@W.
WW +@W@8. &@+ :& o@+ #@ :@W&@& &@: .. :@o
:@W: o@# +Wo &@+ :W: +@W&o++o@W. &@& 8@#o+&@W. #@: o@+
:W@@WWWW@@8 + :&W@@@@& &W .o#@@W&. :W@WWW@@&
+o&&&&+. +oooo.
Subdomain Enumeration Tool
Coded By Jeff Foley (@jeff_foley)
`
type outputParams struct {
Verbose bool
Sources bool
PrintIPs bool
FileOut string
Results chan *amass.AmassRequest
Finish chan struct{}
Done chan struct{}
}
type IPs struct {
Addrs []net.IP
Ranges []*amass.IPRange
}
func main() {
var freq int64
var ASNs, Ports []int
var CIDRs []*net.IPNet
var wordlist, outfile, domainsfile, asns, addrs, cidrs, ports, proxy string
var fwd, verbose, extra, ips, brute, recursive, alts, whois, list, help bool
flag.BoolVar(&help, "h", false, "Show the program usage message")
flag.BoolVar(&ips, "ip", false, "Show the IP addresses for discovered names")
flag.BoolVar(&brute, "brute", false, "Execute brute forcing after searches")
flag.BoolVar(&recursive, "norecursive", true, "Turn off recursive brute forcing")
flag.BoolVar(&alts, "noalts", true, "Disable generation of altered names")
flag.BoolVar(&verbose, "v", false, "Print the summary information")
flag.BoolVar(&extra, "vv", false, "Print the data source information")
flag.BoolVar(&whois, "whois", false, "Include domains discoverd with reverse whois")
flag.BoolVar(&list, "l", false, "List all domains to be used in an enumeration")
flag.Int64Var(&freq, "freq", 0, "Sets the number of max DNS queries per minute")
flag.StringVar(&wordlist, "w", "", "Path to a different wordlist file")
flag.StringVar(&outfile, "o", "", "Path to the output file")
flag.StringVar(&domainsfile, "df", "", "Path to a file providing root domain names")
flag.StringVar(&asns, "asn", "", "ASNs to be probed for certificates")
flag.StringVar(&addrs, "addr", "", "IPs and ranges to be probed for certificates")
flag.StringVar(&cidrs, "net", "", "CIDRs to be probed for certificates")
flag.StringVar(&ports, "p", "", "Ports to be checked for certificates")
flag.StringVar(&proxy, "proxy", "", "The URL used to reach the proxy")
flag.Parse()
if extra {
verbose = true
}
if asns != "" {
ASNs = parseInts(asns)
fwd = true
}
ipAddresses := &IPs{}
if addrs != "" {
ipAddresses = parseIPs(addrs)
fwd = true
}
if cidrs != "" {
CIDRs = parseCIDRs(cidrs)
fwd = true
}
if ports != "" {
Ports = parseInts(ports)
}
// Get root domain names provided from the command-line
domains := flag.Args()
// Now, get domains provided by a file
if domainsfile != "" {
domains = amass.UniqueAppend(domains, getLinesFromFile(domainsfile)...)
}
if len(domains) > 0 {
fwd = true
}
// Should the help output be provided?
if help || !fwd {
fmt.Println(AsciiArt)
fmt.Printf("Usage: %s [options] domain domain2 domain3... (e.g. example.com)\n", path.Base(os.Args[0]))
flag.PrintDefaults()
return
}
if whois {
// Add the domains discovered by whois
domains = amass.UniqueAppend(domains, recon.ReverseWhois(flag.Arg(0))...)
}
if list {
// Just show the domains and quit
for _, d := range domains {
fmt.Println(d)
}
return
}
// Seed the default pseudo-random number generator
rand.Seed(time.Now().UTC().UnixNano())
finish := make(chan struct{})
done := make(chan struct{})
results := make(chan *amass.AmassRequest, 100)
go manageOutput(&outputParams{
Verbose: verbose,
Sources: extra,
PrintIPs: ips,
FileOut: outfile,
Results: results,
Finish: finish,
Done: done,
})
// Execute the signal handler
go catchSignals(finish, done)
// Grab the words from an identified wordlist
var words []string
if wordlist != "" {
words = getLinesFromFile(wordlist)
}
// Setup the amass configuration
config := amass.CustomConfig(&amass.AmassConfig{
ASNs: ASNs,
CIDRs: CIDRs,
IPs: ipAddresses.Addrs,
Ranges: ipAddresses.Ranges,
Ports: Ports,
Wordlist: words,
BruteForcing: brute,
Recursive: recursive,
Alterations: alts,
Frequency: freqToDuration(freq),
Output: results,
})
config.AddDomains(domains)
// If no domains were provided, allow amass to discover them
if len(domains) == 0 {
config.AdditionalDomains = true
}
// Check if a proxy connection should be setup
if proxy != "" {
err := config.SetupProxyConnection(proxy)
if err != nil {
fmt.Println("The proxy address provided failed to make a connection")
return
}
}
//profFile, _ := os.Create("amass_debug.prof")
//pprof.StartCPUProfile(profFile)
//defer pprof.StopCPUProfile()
amass.StartEnumeration(config)
// Signal for output to finish
finish <- struct{}{}
<-done
}
type asnData struct {
Name string
Netblocks map[string]int
}
func manageOutput(params *outputParams) {
var total int
var allLines string
tags := make(map[string]int)
asns := make(map[int]*asnData)
loop:
for {
select {
case result := <-params.Results: // Collect all the names returned by the enumeration
total++
updateData(result, tags, asns)
var line string
if params.Sources {
line += fmt.Sprintf("%-14s", "["+result.Source+"] ")
}
if params.PrintIPs {
line += fmt.Sprintf("%s\n", result.Name+","+result.Address)
} else {
line += fmt.Sprintf("%s\n", result.Name)
}
// Add line to the others and print it out
allLines += line
fmt.Print(line)
case <-params.Finish:
break loop
}
}
// Check to print the summary information
if params.Verbose {
printSummary(total, tags, asns)
}
// Check to output the results to a file
if params.FileOut != "" {
ioutil.WriteFile(params.FileOut, []byte(allLines), 0644)
}
// Signal that output is complete
close(params.Done)
}
func updateData(req *amass.AmassRequest, tags map[string]int, asns map[int]*asnData) {
tags[req.Tag]++
// Update the ASN information
data, found := asns[req.ASN]
if !found {
asns[req.ASN] = &asnData{
Name: req.ISP,
Netblocks: make(map[string]int),
}
data = asns[req.ASN]
}
// Increment how many IPs were in this netblock
data.Netblocks[req.Netblock.String()]++
}
func printSummary(total int, tags map[string]int, asns map[int]*asnData) {
if total == 0 {
fmt.Println("No names were discovered")
return
}
fmt.Printf("\n%d names discovered - ", total)
// Print the stats using tag information
num, length := 1, len(tags)
for k, v := range tags {
fmt.Printf("%s: %d", k, v)
if num < length {
fmt.Print(", ")
}
num++
}
fmt.Println("")
// Print a line across the terminal
for i := 0; i < 8; i++ {
fmt.Print("----------")
}
fmt.Println("")
// Print the ASN and netblock information
for asn, data := range asns {
fmt.Printf("ASN: %d - %s\n", asn, data.Name)
for cidr, ips := range data.Netblocks {
s := strconv.Itoa(ips)
fmt.Printf("\t%-18s\t%-3s Subdomain Name(s)\n", cidr, s)
}
}
}
// If the user interrupts the program, print the summary information
func catchSignals(output, done chan struct{}) {
sigs := make(chan os.Signal, 2)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
// Wait for a signal
<-sigs
// Start final output operations
output <- struct{}{}
// Wait for the broadcast indicating completion
<-done
os.Exit(0)
}
func getLinesFromFile(path string) []string {
var lines []string
// Open the file
file, err := os.Open(path)
if err != nil {
fmt.Printf("Error opening the file %s: %v\n", path, err)
return lines
}
defer file.Close()
// Get each line from the file
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Get the next line
text := scanner.Text()
if text != "" {
lines = append(lines, strings.TrimSpace(text))
}
}
return lines
}
func freqToDuration(freq int64) time.Duration {
if freq > 0 {
d := time.Duration(freq)
if d < 60 {
// We are dealing with number of seconds
return (60 / d) * time.Second
}
// Make it times per second
d = d / 60
m := 1000 / d
if d < 1000 && m > 1 {
return m * time.Millisecond
}
}
// Use the default rate
return amass.DefaultConfig().Frequency
}
func parseInts(s string) []int {
var results []int
nums := strings.Split(s, ",")
for _, n := range nums {
i, err := strconv.Atoi(n)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
results = append(results, i)
}
return results
}
func parseIPs(s string) *IPs {
results := new(IPs)
ips := strings.Split(s, ",")
for _, ip := range ips {
// Is this an IP range?
rng := parseRange(ip)
if rng != nil {
results.Ranges = append(results.Ranges, rng)
continue
}
addr := net.ParseIP(ip)
if addr == nil {
fmt.Printf("%s is not a valid IP address\n", ip)
os.Exit(1)
}
results.Addrs = append(results.Addrs, addr)
}
return results
}
func parseRange(s string) *amass.IPRange {
twoIPs := strings.Split(s, "-")
if twoIPs[0] == s {
// This is not an IP range
return nil
}
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 {
// These should have parsed properly
fmt.Printf("%s is not a valid IP range\n", s)
os.Exit(1)
}
return &amass.IPRange{
Start: start,
End: end,
}
}
func parseCIDRs(s string) []*net.IPNet {
var results []*net.IPNet
cidrs := strings.Split(s, ",")
for _, cidr := range cidrs {
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
results = append(results, ipnet)
}
return results
}