-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
267 lines (234 loc) · 5.29 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
roundChan "github.com/trickest/mksub/round"
)
const (
bufferSizeMB = 100
maxWorkingThreads = 100000
numberOfFiles = 1
)
var (
inputDomains []string
wordSet map[string]bool
words []string
)
var (
domain string
domainFile string
wordlist string
regex string
level int
workers int
outputFile string
silent bool
workerThreadMax = make(chan struct{}, maxWorkingThreads)
done = make(chan struct{})
wg sync.WaitGroup
wgWrite sync.WaitGroup
robin roundChan.RoundRobin
)
func readDomainFile() {
inputFile, err := os.Open(domainFile)
if err != nil {
panic("Could not open file to read domains!")
}
defer inputFile.Close()
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
inputDomains = append(inputDomains, strings.TrimSpace(scanner.Text()))
}
}
func prepareDomains() {
if domain == "" && domainFile == "" {
fmt.Println("No domain input provided")
os.Exit(1)
}
inputDomains = make([]string, 0)
if domain != "" {
inputDomains = append(inputDomains, domain)
} else {
if domainFile != "" {
readDomainFile()
}
}
}
func readWordlistFile() {
var reg *regexp.Regexp
var err error
if regex != "" {
reg, err = regexp.Compile(regex)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
wordlistFile, err := os.Open(wordlist)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
defer wordlistFile.Close()
wordSet = make(map[string]bool)
scanner := bufio.NewScanner(wordlistFile)
for scanner.Scan() {
word := strings.ToLower(scanner.Text())
word = strings.Trim(word, ".")
if reg != nil {
if !reg.Match([]byte(word)) {
continue
}
}
if word != "" {
wordSet[word] = true
}
}
for w := range wordSet {
words = append(words, w)
}
}
func closeWriters(number int) {
for i := 0; i < number; i++ {
done <- struct{}{}
}
}
func spawnWriters(number int) {
for i := 0; i < number; i++ {
var bf bytes.Buffer
ch := make(chan string, 100000)
fileName := outputFile
fileSplit := strings.Split(fileName, ".")
if len(fileSplit) == 1 {
fileName += ".txt"
}
if number > 1 {
fileSplit = strings.Split(fileName, ".")
extension := "." + fileSplit[len(fileSplit)-1]
fileName = strings.TrimSuffix(fileName, extension) + "-" + strconv.Itoa(i) + extension
}
file, err := os.Create(fileName)
if err != nil {
fmt.Println(err)
fmt.Println("Couldn't open file to write output!")
}
wgWrite.Add(1)
go write(file, &bf, &ch)
if robin == nil {
robin = roundChan.New(&ch)
continue
}
robin.Add(&ch)
}
}
func write(file *os.File, buffer *bytes.Buffer, ch *chan string) {
mainLoop:
for {
select {
case <-done:
for {
if !writeOut(file, buffer, ch) {
break
}
}
if buffer.Len() > 0 {
if file != nil {
_, _ = file.WriteString(buffer.String())
buffer.Reset()
}
}
break mainLoop
default:
writeOut(file, buffer, ch)
}
}
wgWrite.Done()
}
func writeOut(file *os.File, buffer *bytes.Buffer, outputChannel *chan string) bool {
select {
case s := <-*outputChannel:
buffer.WriteString(s)
if buffer.Len() >= bufferSizeMB*1024*1024 {
_, _ = file.WriteString(buffer.String())
buffer.Reset()
}
return true
default:
return false
}
}
func combo(_comb string, level int, wg *sync.WaitGroup, wt *chan struct{}) {
defer wg.Done()
workerThreadMax <- struct{}{}
if strings.Count(_comb, ".") > 1 {
if !silent {
fmt.Print(_comb + "\n")
}
*robin.Next() <- _comb + "\n"
}
var nextLevelWaitGroup sync.WaitGroup
if level > 1 {
nextLevelWt := make(chan struct{}, workers)
for _, c := range words {
nextLevelWaitGroup.Add(1)
nextLevelWt <- struct{}{}
go combo(c+"."+_comb, level-1, &nextLevelWaitGroup, &nextLevelWt)
}
} else {
for _, c := range words {
if !silent {
fmt.Print(c + "." + _comb + "\n")
}
*robin.Next() <- c + "." + _comb + "\n"
}
}
nextLevelWaitGroup.Wait()
<-workerThreadMax
<-*wt
}
func main() {
flag.StringVar(&domain, "d", "", "Input domain")
flag.StringVar(&domainFile, "df", "", "Input domain file, one domain per line")
flag.StringVar(&wordlist, "w", "", "Wordlist file")
flag.StringVar(®ex, "r", "", "Regex to filter words from wordlist file")
flag.IntVar(&level, "l", 1, "Subdomain level to generate")
flag.StringVar(&outputFile, "o", "", "Output file (stdout will be used when omitted)")
flag.IntVar(&workers, "t", 100, "Number of threads for every subdomain level")
flag.BoolVar(&silent, "silent", true, "Skip writing generated subdomains to stdout (faster)")
flag.Parse()
go func() {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
<-signalChannel
fmt.Println("Program interrupted, exiting...")
os.Exit(0)
}()
if level <= 0 || workers <= 0 {
fmt.Println("Subdomain level and number of threads must be positive integers!")
os.Exit(0)
}
if outputFile == "" {
silent = false
}
prepareDomains()
readWordlistFile()
spawnWriters(numberOfFiles)
for _, d := range inputDomains {
wg.Add(1)
wt := make(chan struct{}, 1)
wt <- struct{}{}
go combo(d, level, &wg, &wt)
}
wg.Wait()
closeWriters(numberOfFiles)
wgWrite.Wait()
}