forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (133 loc) · 3.34 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
// 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 (
"flag"
"fmt"
"net"
"os"
"os/signal"
"path"
"sync"
"syscall"
"time"
"github.com/OWASP/Amass/amass"
"github.com/OWASP/Amass/amass/utils"
)
var (
filter map[string]struct{}
filterLock sync.Mutex
)
func main() {
var addrs parseIPs
var cidrs parseCIDRs
var asns, ports parseInts
filter = make(map[string]struct{})
help := flag.Bool("h", false, "Show the program usage message")
flag.Var(&addrs, "addr", "IPs and ranges (192.168.1.1-254) separated by commas")
flag.Var(&cidrs, "cidr", "CIDRs separated by commas (can be used multiple times)")
flag.Var(&asns, "asn", "ASNs separated by commas (can be used multiple times)")
flag.Var(&ports, "p", "Ports separated by commas (default: 443)")
flag.Parse()
if *help {
fmt.Printf("Usage: %s [--addr IP] [--cidr CIDR] [--asn number] [-p number]\n", path.Base(os.Args[0]))
flag.PrintDefaults()
return
}
done := make(chan struct{})
results := make(chan string, 10)
// Execute the signal handler
go CatchSignals(results, done)
if len(ports) == 0 {
ports = []int{443}
}
if ips := AllIPsInScope(addrs, cidrs, asns); len(ips) > 0 {
go PullAllCertificates(ips, ports, results)
for domain := range results {
fmt.Println(domain)
}
} else {
fmt.Println("The parameters identified no hosts")
}
close(done)
}
func AllIPsInScope(addrs parseIPs, cidrs parseCIDRs, asns parseInts) []net.IP {
var ips []net.IP
ips = append(ips, addrs...)
for _, cidr := range cidrs {
ips = append(ips, utils.NetHosts(cidr)...)
}
for _, asn := range asns {
record, err := amass.ASNRequest(asn)
if err != nil {
continue
}
for _, cidr := range record.Netblocks {
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
ips = append(ips, utils.NetHosts(ipnet)...)
}
}
return ips
}
func PullAllCertificates(ips []net.IP, ports parseInts, output chan string) {
var running int
done := make(chan struct{}, 100)
t := time.NewTicker(100 * time.Millisecond)
defer t.Stop()
loop:
for {
select {
case <-t.C:
if running >= 100 || len(ips) <= 0 {
continue
}
running++
addr := ips[0]
if len(ips) == 1 {
ips = []net.IP{}
} else {
ips = ips[1:]
}
go ObtainCert(addr.String(), ports, output, done)
case <-done:
running--
if running == 0 && len(ips) <= 0 {
close(output)
break loop
}
}
}
}
func ObtainCert(addr string, ports parseInts, output chan string, done chan struct{}) {
var domains []string
for _, r := range amass.PullCertificateNames(addr, ports) {
domains = utils.UniqueAppend(domains, r.Domain)
}
for _, domain := range domains {
FilteredSend(domain, output)
}
done <- struct{}{}
}
func FilteredSend(domain string, output chan string) {
filterLock.Lock()
defer filterLock.Unlock()
if _, found := filter[domain]; !found {
output <- domain
filter[domain] = struct{}{}
}
}
// If the user interrupts the program, print the summary information
func CatchSignals(output chan string, 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
close(output)
// Wait for the broadcast indicating completion
<-done
os.Exit(1)
}