-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (110 loc) · 2.62 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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
)
// Set up command line arguments
var (
domain string
output string
baseURL string = "https://crt.sh/?q=%s&output=json"
)
func init() {
flag.StringVar(&domain, "d", "", "Domain to search for domains")
flag.StringVar(&output, "o", "", "Output file to write domains to")
flag.Parse()
}
// Get domains from crt.sh
func getDomains() ([]map[string]interface{}, error) {
resp, err := http.Get(fmt.Sprintf(baseURL, domain))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var domains []map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&domains)
if err != nil {
return nil, err
}
return domains, nil
}
// Filter domains to remove duplicates and unwanted domains
func filterDomains(domains []map[string]interface{}) []string {
var domainCollection []string
filters := []string{"*", "\\", "-", "@"}
for _, d := range domains {
domainName := strings.TrimSpace(d["name_value"].(string))
domainSplit := strings.Split(domainName, "\n")
if len(domainSplit) == 1 && !containsAny(domainName, filters) {
domainCollection = append(domainCollection, domainName)
} else {
for _, d2 := range domainSplit {
if !containsAny(d2, filters) {
domainCollection = append(domainCollection, d2)
}
}
}
}
return uniqueStrings(domainCollection)
}
// Write domains to file
func writeDomains(domains []string) error {
file, err := os.Create(output)
if err != nil {
return err
}
defer file.Close()
for _, domain := range domains {
file.WriteString(domain + "\n")
}
return nil
}
// Check if a string contains any of the substrings in a list
func containsAny(str string, substrings []string) bool {
for _, s := range substrings {
if strings.Contains(str, s) {
return true
}
}
return false
}
// Remove duplicates from a string slice
func uniqueStrings(strings []string) []string {
encountered := map[string]bool{}
result := []string{}
for _, s := range strings {
if !encountered[s] {
encountered[s] = true
result = append(result, s)
}
}
return result
}
// Main function
func main() {
if domain == "" {
log.Fatalln("Domain is required.")
}
domains, err := getDomains()
if err != nil {
log.Fatalln("Error getting domains from crt.sh:", err)
}
domainCollection := filterDomains(domains)
for _, d := range domainCollection {
fmt.Println(d)
}
if output != "" {
err = writeDomains(domainCollection)
if err != nil {
log.Fatalln("Error writing domains to file:", err)
}
}
}