-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrt.go
68 lines (58 loc) · 1.43 KB
/
crt.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
type CRTSHEntry struct {
NameValue string `json:"name_value"`
}
func getSubdomainsFromCRT(domain string) ([]string, error) {
url := fmt.Sprintf("https://crt.sh/?q=%s&output=json", domain)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get data from crt.sh, status code %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var entries []CRTSHEntry
if err := json.Unmarshal(bodyBytes, &entries); err != nil {
return nil, err
}
subdomainsSet := make(map[string]struct{})
for _, entry := range entries {
names := strings.Split(entry.NameValue, "\n")
for _, name := range names {
name = strings.TrimSpace(name)
// tld sanity check
if isValidDomain(name, domain) {
subdomainsSet[name] = struct{}{}
}
}
}
var subdomains []string
for subdomain := range subdomainsSet {
subdomains = append(subdomains, subdomain)
}
return subdomains, nil
}
func isValidDomain(name, domain string) bool {
// only process domains from the tld domain
if !strings.HasSuffix(name, "."+domain) && name != domain {
return false
}
/* // explicitly exclude punycode domains -- probably not necessary as this would cause false negatives
if strings.HasPrefix(name, "xn--") {
return false
}
*/
return true
}