-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
209 lines (159 loc) · 3.93 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
package main
import (
"fmt"
"strings"
"time"
"github.com/CaliDog/certstream-go"
"github.com/joeguo/tldextract"
"golang.org/x/net/idna"
log "github.com/Sirupsen/logrus"
"github.com/Workiva/go-datastructures/queue"
)
var exit bool
var dQ *queue.Queue
var dbQ *queue.Queue
var permutatedQ *queue.Queue
var extract *tldextract.TLDExtract
var checked int64
var sem chan int
var action string
type Domain struct {
CN string
Domain string
Suffix string
Raw string
}
type PermutatedDomain struct {
Permutation string
Domain Domain
}
// StreamCerts takes input from certstream and stores it in the queue
func StreamCerts() {
// The false flag specifies that we don't want heartbeat messages.
stream, errStream := certstream.CertStreamEventStream(false)
for {
select {
case jq := <-stream:
domain, err2 := jq.String("data", "leaf_cert", "subject", "CN")
if err2 != nil {
if !strings.Contains(err2.Error(), "Error decoding jq string") {
continue
}
log.Error(err2)
}
//log.Infof("Domain: %s", domain)
//log.Info(jq)
dQ.Put(domain)
case err := <-errStream:
log.Error(err)
}
}
}
// ProcessQueue processes data stored in the queue
func ProcessQueue() {
for {
cn, err := dQ.Get(1)
if err != nil {
log.Error(err)
continue
}
//log.Infof("Domain: %s", cn[0].(string))
if !strings.Contains(cn[0].(string), "cloudflaressl") && !strings.Contains(cn[0].(string), "xn--") && len(cn[0].(string)) > 0 && !strings.HasPrefix(cn[0].(string), "*.") && !strings.HasPrefix(cn[0].(string), ".") {
punyCfgDomain, err := idna.ToASCII(cn[0].(string))
if err != nil {
log.Error(err)
}
result := extract.Extract(punyCfgDomain)
//domain := fmt.Sprintf("%s.%s", result.Root, result.Tld)
d := Domain{
CN: punyCfgDomain,
Domain: result.Root,
Suffix: result.Tld,
Raw: cn[0].(string),
}
if punyCfgDomain != cn[0].(string) {
log.Infof("%s is %s (punycode); AWS does not support internationalized buckets", cn[0].(string), punyCfgDomain)
continue
}
dbQ.Put(d)
}
//log.Infof("CN: %s\tDomain: %s", cn[0].(string), domain)
}
}
// StoreInDB stores the dbQ results into the database
func StoreInDB() {
for {
dstruct, err := dbQ.Get(1)
if err != nil {
log.Error(err)
continue
}
var d Domain = dstruct[0].(Domain)
//log.Infof("CN: %s\tDomain: %s.%s", d.CN, d.Domain, d.Suffix)
permutatedQ.Put(fmt.Sprintf("%s.%s", d.Domain, d.Suffix))
}
}
// CheckPermutations runs through all permutations checking them for PUBLIC/FORBIDDEN buckets
func CheckPermutations() {
//var max = runtime.NumCPU() * 1
var max = 1
sem = make(chan int, max)
for {
sem <- 1
dom, err := permutatedQ.Get(1)
domain := dom[0].(string)
//log.Infof("Performing zone transfer on %s", domain)
if err != nil {
log.Error(err)
}
go func(d string) {
results := ZoneTransfer(d)
//results := sonar.ZoneTransfer(d)
if len(results) > 0 {
log.Infof("\033[32m\033[1mSUCCESS\033[39m\033[0m %s", d)
}
checked = checked + 1
<-sem
}(domain)
}
}
// Init does low level initialization before we can run
func Init() {
var err error
dQ = queue.New(1000)
dbQ = queue.New(1000)
permutatedQ = queue.New(1000)
extract, err = tldextract.New("./tld.cache", false)
if err != nil {
log.Fatal(err)
}
}
// PrintJob prints the queue sizes
func PrintJob() {
for {
log.Infof("dQ size: %d", dQ.Len())
log.Infof("dbQ size: %d", dbQ.Len())
log.Infof("permutatedQ size: %d", permutatedQ.Len())
log.Infof("Checked: %d", checked)
time.Sleep(10 * time.Second)
}
}
func main() {
log.Info("Initializing....")
Init()
//go PrintJob()
log.Info("Starting to stream certs....")
go StreamCerts()
log.Info("Starting to process queue....")
go ProcessQueue()
//log.Info("Starting to stream certs....")
go StoreInDB()
log.Info("Starting to process permutations....")
go CheckPermutations()
for {
if exit {
break
}
time.Sleep(1 * time.Second)
}
}