/
sancrawler.go
472 lines (388 loc) · 12.3 KB
/
sancrawler.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
package main
/* SANCrawler is a tool designed to enumerate linked x509 certificates based
* on shared metadata. Traditional approaches to using x509 data focused on
* linking based on shared apex domain, but in practice, many different fields
* exist and are actively used by corporations. SANCrawler implements two such
* approaches: strict organization search, and general keyword searches matching
* on any field.
*/
import (
"bufio"
"database/sql"
"flag"
"fmt"
"net/http"
"os"
"regexp"
"runtime"
"runtime/pprof"
"strings"
"time"
// Lets hope this one works better than psycopg2
_ "github.com/lib/pq"
log "github.com/sirupsen/logrus"
"golang.org/x/net/publicsuffix"
)
// Data format used by crawlers, tells them which CA they are working on and where the
// bounds of their search are. start and stop usually only come into effect when the
// company is large.
type crawlerData struct {
caID int
start int
stop int
}
/* getNames: Retrieves the common names and subject alternative names (SANs)
* from the postgres instance run by crt.sh, you can find details about their
* complicated database schema here: https://github.com/crtsh/certwatch_db
*/
func getNames(query string, org string, inChan chan crawlerData, outChan chan string, stopChan chan bool) {
// https://blog.marin.qa/posts/2016/04/07/pgbouncer-problems-with-go/
connStr := "host=crt.sh user=guest dbname=certwatch binary_parameters=yes"
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
panic(err)
}
for {
select {
case <-stopChan:
db.Close()
return
case tmpData := <-inChan:
// offset determines pagination of records from crt.sh.
// count is how many records we actually read each time.
for offset, count := tmpData.start, 0; ; offset += count {
count = 0
rows, err := db.Query(query, tmpData.caID, org, offset)
if err != nil {
log.Fatal(err)
panic(err)
}
// Scan through the records returned and keep track of the information we
// actually care about. We don't care about ID, but need it since doing an
// ORDER BY on strings is slow and we need an ORDER BY so we can use LIMIT
// and OFFSET. I also suck at SQL, so keep that in mind.
for rows.Next() {
var (
ID int
name string
)
// Note: Some of these results may not be actual domains, recall these are
// just common names and SANs. They only have to be resolvable/accessible for
// whatever system is using them. This means you may find internal domain names
// as SANs that aren't fully qualified. You are very likely to encounter wildcard
// entires too.
if err := rows.Scan(&ID, &name); err != nil {
log.Fatal(err)
}
count++
// Make sure to lowercase to avoid duplicates based on mixed cases
outChan <- strings.ToLower(name)
}
// Bail out if we're done
if count == 0 {
break
}
}
break
default:
continue
}
}
}
func loadCrawlerData(orgname string, sanChan chan crawlerData, cnChan chan crawlerData) int {
// We need to group all of the certificates by CA. Then we will partition those results
// into the blocks of crawler data that will get used by other functions.
numTotalCerts := 0
numCrawlers := 0
query := `
SELECT ci.ISSUER_CA_ID, count(DISTINCT ci.CERTIFICATE_ID)
FROM ca, certificate_identity ci
WHERE ci.ISSUER_CA_ID = ca.ID AND
lower(ci.NAME_VALUE) = lower($1)
GROUP BY ci.ISSUER_CA_ID;`
space := regexp.MustCompile(`\s+`)
query = strings.Replace(query, "\n", " ", -1)
query = space.ReplaceAllString(query, " ")
// Make database connection
connStr := "host=crt.sh user=guest dbname=certwatch"
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
panic(err)
}
// Pull the results
rows, err := db.Query(query, orgname)
if err != nil {
log.Fatal(err)
panic(err)
}
for rows.Next() {
var (
caID int
numCerts int
)
if err := rows.Scan(&caID, &numCerts); err != nil {
log.Fatal(err)
}
var tmpData crawlerData
tmpData.caID = caID
tmpData.start = 0
tmpData.stop = numCerts
sanChan <- tmpData
cnChan <- tmpData
numTotalCerts += numCerts
}
// How many crawlers will we need for this run? Note this will always
// be an even number since we have 1 crawler for each name type: SAN, CN.
if numTotalCerts < 10000 {
numCrawlers = 1
} else {
numCrawlers = (numTotalCerts / 10000)
}
db.Close()
return numCrawlers
}
/* getDomainsByKeyword: Get all the names belonging to a certain organization.
*/
func getDomainsByKeyword(orgname string) map[string]int {
ret := make(map[string]int)
// I have never liked SQL and these queries are probably shit, but they return
// results faster than any of the others I tried by *a lot* and I have no
// idea why.
// This is where this tool gets its name. The gorountines that read from the
// sanChan are called "SANCrawlers".
sanQuery := `
SELECT c.ID, x509_altNames(c.CERTIFICATE, 2, TRUE)
FROM certificate c WHERE c.ID IN (
SELECT DISTINCT ci.CERTIFICATE_ID
FROM certificate_identity ci
WHERE ci.ISSUER_CA_ID = $1 AND lower(ci.NAME_VALUE) = lower($2)
)
ORDER BY c.ID DESC OFFSET $3 LIMIT 2000;
`
cnQuery := `
SELECT c.ID, x509_nameAttributes(c.CERTIFICATE, 'commonName', TRUE)
FROM certificate c WHERE c.ID IN (
SELECT DISTINCT ci.CERTIFICATE_ID
FROM certificate_identity ci
WHERE ci.ISSUER_CA_ID = $1 AND lower(ci.NAME_VALUE) = lower($2)
)
ORDER BY c.ID DESC OFFSET $3 LIMIT 2000;
`
space := regexp.MustCompile(`\s+`)
sanQuery = strings.Replace(sanQuery, "\n", " ", -1)
sanQuery = space.ReplaceAllString(sanQuery, " ")
cnQuery = strings.Replace(cnQuery, "\n", " ", -1)
cnQuery = space.ReplaceAllString(cnQuery, " ")
// Channels for I/O between goroutines. Goroutines will read from either sanChan or
// cnChan and then put their discovered domains into domainChan. They will begin
// terminating when doneChan becomes populated.
sanChan := make(chan crawlerData, 10000)
cnChan := make(chan crawlerData, 10000)
domainChan := make(chan string, 10000)
numCrawlers := loadCrawlerData(orgname, sanChan, cnChan)
doneChan := make(chan bool, numCrawlers*2)
for i := 0; i < numCrawlers; i++ {
go getNames(sanQuery, orgname, sanChan, domainChan, doneChan)
go getNames(cnQuery, orgname, cnChan, domainChan, doneChan)
}
// Keep waiting until both input channels drain.
// Keep track of the values spewing out.
for len(sanChan) > 0 || len(cnChan) > 0 {
select {
case tmp := <-domainChan:
ret[tmp] = 0
break
default:
continue
}
}
// Allow for goroutines to start exiting
for i := 0; i < numCrawlers*2; i++ {
doneChan <- true
}
// Read until both of the other channels finish draining
for len(doneChan) > 0 || len(domainChan) > 0 {
select {
case tmp := <-domainChan:
ret[tmp] = 0
break
default:
continue
}
}
return ret
}
/* tryExtractOrg: Attempts to automatically extract the organization field from
* any x509 certificates detected from trying a TLS connection to the URL specified.
*/
func tryExtractOrg(url string) string {
org := ""
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
// Akamai and other WAFs will block our requests if we aren't using a standard
// User-Agent string.
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36")
res, err := client.Do(req)
if err != nil {
log.Fatal("Could not connect to URL provided. Quitting.")
}
if res.TLS != nil {
// 0th element is always the last certificate in the chain, which is the one that
// we want to examine.
cert := res.TLS.PeerCertificates[0]
orgs := cert.Subject.Organization
if len(orgs) < 1 {
log.Fatal("URL provided does not contain an organization. Quitting.")
} else {
// This may cause some bugs later on if there is more than 1 organization name
// within the certificate
org = orgs[0]
}
} else {
log.Fatal("URL provided does not use TLS. Quitting.")
}
return org
}
/* printStatistics: prints statistics about which top level domains occur the most
* frequently. Can be useful in helping to remove false positives, or gain insight
* into subdomain distribution. Probably will add more useful stats later.
*/
func printStatistics(subdomains *map[string]int) {
domains := make(map[string]int)
for k := range *subdomains {
d, err := publicsuffix.EffectiveTLDPlusOne(k)
if err != nil {
log.Warn("printStatistics: Failed to parse subdomain name")
} else {
if _, ok := domains[d]; ok {
domains[d]++
} else {
domains[d] = 1
}
}
}
for domain, occurances := range domains {
log.WithFields(log.Fields{
"Occurances": occurances,
"Domain": domain,
}).Info(" . . . ")
}
}
/* ayy */
func printASCIIArt(major int, minor int) {
art := `
__________
\\ | SAN CRAWLER v%d.%d: Uncle Rico's Time Machine
\\ | @cramppet
\\@@@@@@|
`
fmt.Printf(art+"\n", major, minor)
}
func main() {
var print = flag.Bool("p", false, "")
var debugMode = flag.Bool("d", false, "")
var keyword = flag.String("k", "", "")
var org = flag.String("s", "", "")
var outfile = flag.String("o", "", "")
var autoURL = flag.String("u", "", "")
var subdomains map[string]int
flag.Usage = func() {
out := flag.CommandLine.Output()
fmt.Fprintf(out, "SANCrawler: reverses x509 metadata using CT logs\n\n")
fmt.Fprintf(out, "Example: ./sancrawler -u https://example.com/ -o example.out\n\n")
fmt.Fprintf(out, "Discovery modes:\n")
fmt.Fprintf(out, " -k Keyword to match on.\n")
fmt.Fprintf(out, " -u URL; attempt auto-extraction of x509 Subject's Organization field.\n")
fmt.Fprintf(out, "Output:\n")
fmt.Fprintf(out, " -o Use this output file.\n")
fmt.Fprintf(out, "Auxiliary:\n")
fmt.Fprintf(out, " -p Print domain statistics (ie. subdomain distribution) to stdout.\n")
fmt.Fprintf(out, "Debugging:\n")
fmt.Fprintf(out, " -d Generate profiling files and debugging output\n")
}
start := time.Now()
flag.Parse()
printASCIIArt(2, 1)
log.Info("SANCrawler running")
// Check if we are running in debug mode, enable CPU profiling now if we are
if *debugMode {
cpuProfFile, err := os.Create("sancrawler2.cpu")
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(cpuProfFile); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
// If we want to try the auto extraction, then we are implictly choosing to
// use the organization mode.
if *autoURL != "" {
log.WithFields(log.Fields{
"URL": *autoURL,
}).Info("Attempting auto-extraction from URL")
*org = tryExtractOrg(*autoURL)
if *org != "" {
log.WithFields(log.Fields{
"Organization": *org,
}).Info("Using extracted organization as seed")
}
}
// Switch between the different possible modes, first one we see is the one
// we end up doing. Passing multiple modes doesn't make a lot of sense, unless
// we want to combine results or something.
if *keyword != "" {
subdomains = getDomainsByKeyword(*keyword)
} else if *org != "" {
subdomains = getDomainsByKeyword(*org)
}
// Why not show this bad motherfucker off?
elapsed := time.Since(start)
// Do we want to print the stats to standard out?
if *print {
log.Info("Printing domains statistics ...")
printStatistics(&subdomains)
}
// Do we want to write to an output file?
if *outfile != "" {
log.WithFields(log.Fields{
"Outfile": *outfile,
}).Info("Writing results to output file")
fHandle, err := os.Create(*outfile)
if err != nil {
panic(err)
}
bufWriter := bufio.NewWriter(fHandle)
newLine := []byte("\n")
defer fHandle.Close()
for k := range subdomains {
bufWriter.WriteString(k)
bufWriter.Write(newLine)
}
bufWriter.Flush()
}
// Before we finish, check if we need to output memory usage stats for debug mode
if *debugMode {
memProfFile, err := os.Create("sancrawler2.mem")
if err != nil {
log.Fatal("Could not create memory profile: ", err)
}
runtime.GC()
if err := pprof.WriteHeapProfile(memProfFile); err != nil {
log.Fatal("Could not write memory profile: ", err)
}
memProfFile.Close()
}
log.WithFields(log.Fields{
"Runtime": elapsed,
}).Info("SANCrawler shutting down")
}