-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
274 lines (247 loc) · 6.63 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
package main
import (
"bufio"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"flag"
"fmt"
"math/rand"
"net"
"os"
"strings"
"sync"
"time"
)
const (
defaultTLSConnectTimeout = 1 * time.Second
defaultHandshakeDeadline = 3 * time.Second
)
var (
// Public & free DNS servers
PublicResolvers = []string{
"1.1.1.1:53", // Cloudflare
"8.8.8.8:53", // Google
"64.6.64.6:53", // Verisign
"77.88.8.8:53", // Yandex.DNS
"74.82.42.42:53", // Hurricane Electric
"1.0.0.1:53", // Cloudflare Secondary
"8.8.4.4:53", // Google Secondary
"77.88.8.1:53", // Yandex.DNS Secondary
// The following servers have shown to be unreliable
//"64.6.65.6:53", // Verisign Secondary
//"9.9.9.9:53", // Quad9
//"149.112.112.112:53", // Quad9 Secondary
//"84.200.69.80:53", // DNS.WATCH
//"84.200.70.40:53", // DNS.WATCH Secondary
//"8.26.56.26:53", // Comodo Secure DNS
//"8.20.247.20:53", // Comodo Secure DNS Secondary
//"195.46.39.39:53", // SafeDNS
//"195.46.39.40:53", // SafeDNS Secondary
//"69.195.152.204:53", // OpenNIC
//"216.146.35.35:53", // Dyn
//"216.146.36.36:53", // Dyn Secondary
//"37.235.1.174:53", // FreeDNS
//"37.235.1.177:53", // FreeDNS Secondary
//"156.154.70.1:53", // Neustar
//"156.154.71.1:53", // Neustar Secondary
//"91.239.100.100:53", // UncensoredDNS
//"89.233.43.71:53", // UncensoredDNS Secondary
// Thought to falsely accuse researchers of malicious activity
// "208.67.222.222:53", // OpenDNS Home
// "208.67.220.220:53", // OpenDNS Home Secondary
// These DNS servers have shown to send back fake answers
//"198.101.242.72:53", // Alternate DNS
//"23.253.163.53:53", // Alternate DNS Secondary
}
)
func main() {
var connPort string
var threads int
var matchDomain string
flag.StringVar(&connPort, "p", "443", "Ports to connect, separate with comma.")
flag.IntVar(&threads, "t", 5, "Threads to use.")
flag.StringVar(&matchDomain, "d", "", "Only get subdomains end with this domain.")
flag.Parse()
if len(connPort) == 0 {
fmt.Println("Please check your ports input")
os.Exit(0)
}
var ports []string
if strings.Contains(connPort, ",") {
ports = strings.Split(connPort, ",")
} else {
ports = []string{connPort}
}
var wg sync.WaitGroup
jobsChan := make(chan string, threads*2)
seen := make(map[string]bool)
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for addr := range jobsChan {
certs := getCert(addr, ports)
for _, c := range certs {
// Check is it duplicated or not
if _, ok := seen[c]; !ok {
seen[c] = true
// Check is it endswith matchDomain
if len(matchDomain) > 0 {
if strings.HasSuffix(c, matchDomain) {
fmt.Println(c)
}
} else {
fmt.Println(c)
}
} else {
continue
}
}
}
}()
}
if flag.NArg() > 0 {
// fetch for a single domain
jobsChan <- flag.Arg(0)
} else {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
line := strings.TrimSpace(strings.ToLower(sc.Text()))
jobsChan <- line
}
}
close(jobsChan)
wg.Wait()
}
func getCert(addr string, ports []string) []string {
var certs []string
for _, port := range ports {
cfg := &tls.Config{InsecureSkipVerify: true}
// Set the maximum time allowed for making the connection
ctx, cancel := context.WithTimeout(context.Background(), defaultTLSConnectTimeout)
defer cancel()
// Obtain the connection
conn, err := dialContext(ctx, "tcp", addr+":"+port)
if err != nil {
continue
}
defer conn.Close()
c := tls.Client(conn, cfg)
// Attempt to acquire the certificate chain
errChan := make(chan error, 2)
// This goroutine will break us out of the handshake
time.AfterFunc(defaultHandshakeDeadline, func() {
errChan <- errors.New("Handshake timeout")
})
// Be sure we do not wait too long in this attempt
c.SetDeadline(time.Now().Add(defaultHandshakeDeadline))
// The handshake is performed in the goroutine
go func() {
errChan <- c.Handshake()
}()
// The error channel returns handshake or timeout error
if err = <-errChan; err != nil {
continue
}
// Get the correct certificate in the chain
certChain := c.ConnectionState().PeerCertificates
cert := certChain[0]
// Create the new requests from names found within the cert
certs = append(certs, namesFromCert(cert)...)
}
return certs
}
func namesFromCert(cert *x509.Certificate) []string {
var cn string
for _, name := range cert.Subject.Names {
oid := name.Type
if len(oid) == 4 && oid[0] == 2 && oid[1] == 5 && oid[2] == 4 {
if oid[3] == 3 {
cn = fmt.Sprintf("%s", name.Value)
break
}
}
}
var subdomains []string
// Add the subject common name to the list of subdomain names
commonName := removeAsteriskLabel(cn)
if commonName != "" {
subdomains = append(subdomains, commonName)
}
// Add the cert DNS names to the list of subdomain names
for _, name := range cert.DNSNames {
n := removeAsteriskLabel(name)
if n != "" {
subdomains = uniqAppend(subdomains, n)
}
}
return subdomains
}
func removeAsteriskLabel(s string) string {
var index int
labels := strings.Split(s, ".")
for i := len(labels) - 1; i >= 0; i-- {
if strings.TrimSpace(labels[i]) == "*" {
break
}
index = i
}
if index == len(labels)-1 {
return ""
}
return strings.Join(labels[index:], ".")
}
func dialContext(ctx context.Context, network, address string) (net.Conn, error) {
d := &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: DNSDialContext,
},
}
return d.DialContext(ctx, network, address)
}
func DNSDialContext(ctx context.Context, network, address string) (net.Conn, error) {
d := &net.Dialer{}
return d.DialContext(ctx, network, nextResolverAddress())
}
// NextResolverAddress - Requests the next server
func nextResolverAddress() string {
resolvers := PublicResolvers
rnd := rand.Int()
idx := rnd % len(resolvers)
return resolvers[idx]
}
func uniqAppend(orig []string, add ...string) []string {
return append(orig, newUniqueElements(orig, add...)...)
}
// NewUniqueElements - Removes elements that have duplicates in the original or new elements
func newUniqueElements(orig []string, add ...string) []string {
var n []string
for _, av := range add {
found := false
s := strings.ToLower(av)
// Check the original slice for duplicates
for _, ov := range orig {
if s == strings.ToLower(ov) {
found = true
break
}
}
// Check that we didn't already add it in
if !found {
for _, nv := range n {
if s == nv {
found = true
break
}
}
}
// If no duplicates were found, add the entry in
if !found {
n = append(n, s)
}
}
return n
}