-
Notifications
You must be signed in to change notification settings - Fork 28
/
mkpath.go
298 lines (260 loc) · 6.24 KB
/
mkpath.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
roundChan "github.com/trickest/mkpath/round"
)
const (
fileRegex = "[^?*:;{}]+\\.[^/?*:;{}]+"
bufferSizeMB = 100
maxWorkingThreads = 100000
numberOfFiles = 1
)
var (
domain string
inputDomains []string
domainFile string
wordlist string
toLowercase bool
regex string
dirWordSet map[string]bool
fileWordSet map[string]bool
depth int
onlyDirs bool
onlyFiles bool
outputFileName string
silent bool
workers int
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 {
fmt.Println("Could not open file to read domains:", err)
os.Exit(1)
}
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("Could not open file to read wordlist:", err)
os.Exit(1)
}
defer wordlistFile.Close()
fileReg, err := regexp.Compile(fileRegex)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dirWordSet = make(map[string]bool)
fileWordSet = make(map[string]bool)
scanner := bufio.NewScanner(wordlistFile)
for scanner.Scan() {
word := scanner.Text()
if toLowercase {
word = strings.ToLower(word)
}
word = strings.Trim(word, "/")
if word != "" {
if reg != nil {
if !reg.Match([]byte(word)) {
continue
}
}
if fileReg.Match([]byte(word)) {
fileWordSet[word] = true
} else {
dirWordSet[word] = true
}
}
}
}
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 := outputFileName
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("Couldn't open file to write output:", err)
os.Exit(1)
}
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, "/") > 0 {
processOutput(_comb, robin.Next())
}
var nextLevelWaitGroup sync.WaitGroup
if level > 1 {
nextLevelWt := make(chan struct{}, workers)
for dw := range dirWordSet {
nextLevelWaitGroup.Add(1)
nextLevelWt <- struct{}{}
go combo(_comb+"/"+dw, level-1, &nextLevelWaitGroup, &nextLevelWt)
}
} else {
for dw := range dirWordSet {
processOutput(_comb+"/"+dw, robin.Next())
}
}
nextLevelWaitGroup.Wait()
<-workerThreadMax
<-*wt
}
func processOutput(out string, outChan *chan string) {
if onlyDirs || onlyFiles == onlyDirs {
if !silent {
fmt.Print(out + "\n")
}
*outChan <- out + "\n"
}
if onlyFiles || onlyFiles == onlyDirs {
for file := range fileWordSet {
if !silent {
fmt.Print(out + "/" + file + "\n")
}
*outChan <- out + "/" + file + "\n"
}
}
}
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.BoolVar(&toLowercase, "lower", false, "Convert wordlist file content to lowercase (default false)")
flag.StringVar(®ex, "r", "", "Regex to filter words from wordlist file")
flag.IntVar(&depth, "l", 1, "URL path depth to generate")
flag.StringVar(&outputFileName, "o", "", "Output file (optional)")
flag.BoolVar(&onlyDirs, "only-dirs", false, "Generate directories only, files are filtered out (default false)")
flag.BoolVar(&onlyFiles, "only-files", false, "Generate files only, file names are appended to given domains (default false)")
flag.IntVar(&workers, "t", 100, "Number of threads for every path depth")
flag.BoolVar(&silent, "silent", true, "Skip writing generated paths 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 depth <= 0 || workers <= 0 {
fmt.Println("Path depth and number of threads must be positive integers!")
os.Exit(0)
}
prepareDomains()
readWordlistFile()
spawnWriters(numberOfFiles)
if outputFileName == "" {
silent = false
}
for _, d := range inputDomains {
wg.Add(1)
wt := make(chan struct{}, 1)
wt <- struct{}{}
go combo(d, depth, &wg, &wt)
}
wg.Wait()
closeWriters(numberOfFiles)
wgWrite.Wait()
}