-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
173 lines (163 loc) · 4.13 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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
func main() {
target := flag.String("u", "", "target url")
workers := flag.Uint("c", uint(runtime.NumCPU()), "number of concurrent quests")
timeout := flag.Uint("t", 5, "timeout in seconds")
wordList := flag.String("w", "", "wordlist")
hostHeader := flag.String("h", "", "host header")
customKey := flag.String("ck", "", "add a custom header to all requests (key)")
customVal := flag.String("cv", "", "add a custom header to all requests (value)")
extension := flag.String("e", "", "file extension to add (without dot prefix)")
success := flag.String("s", "", "status codes indicating success, seperated by commas")
failure := flag.String("f", "", "status codes indicating failure, seperated by commas")
userAgent := flag.String("a", "discover: https://github.com/bruston/discover", "user-agent to use")
cookieFile := flag.String("cookies", "", "file containing cookies")
prefix := flag.String("p", "", "prefix to add to word/directory")
insecure := flag.Bool("k", false, "ignore HTTPS errors")
flag.Parse()
if *target == "" {
fmt.Println("You must specify a target URL with the -u parameter.")
return
}
if *wordList == "" {
fmt.Println("You must specify a word list with the -w parameter.")
return
}
cookies := ""
if *cookieFile != "" {
b, err := ioutil.ReadFile(*cookieFile)
if err != nil {
log.Fatal(err)
}
cookies = string(b)
cookies = strings.TrimSpace(cookies)
}
f, err := os.Open(*wordList)
if err != nil {
log.Fatal(err)
}
defer f.Close()
work := make(chan string)
s := bufio.NewScanner(f)
go func() {
for s.Scan() {
work <- s.Text()
}
close(work)
}()
successCodes, err := cleanCodes(*success)
if err != nil {
log.Fatal(err)
}
failureCodes, _ := cleanCodes(*failure)
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: time.Second * time.Duration(*timeout),
}
if *insecure {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
if !strings.HasSuffix(*target, "/") {
*target = *target + "/"
}
fmt.Println("Discovering assets on", *target)
wg := &sync.WaitGroup{}
for i := 0; i < int(*workers); i++ {
wg.Add(1)
go func() {
for v := range work {
if *extension != "" {
v = v + "." + *extension
}
code, word, size, err := doReq(client, *target, *hostHeader, *prefix+v, *customKey, *customVal, *userAgent, cookies)
if err != nil {
continue
}
if len(failureCodes) != 0 {
fail := false
for _, v := range failureCodes {
if v == code {
fail = true
break
}
}
if !fail {
fmt.Printf("/%s %d %d\n", word, code, size)
}
continue
}
if len(successCodes) == 0 {
fmt.Printf("/%s %d %d\n", word, code, size)
}
for _, v := range successCodes {
if v == code {
fmt.Printf("/%s %d %d\n", word, code, size)
}
}
}
wg.Done()
}()
}
wg.Wait()
}
func cleanCodes(successCodes string) ([]int, error) {
if successCodes == "" {
return nil, nil
}
split := strings.Split(successCodes, ",")
codes := []int{}
for i := range split {
split[i] = strings.TrimSpace(split[i])
d, err := strconv.Atoi(split[i])
if err != nil {
return nil, fmt.Errorf("%s is not a valid status code", split[i])
}
codes = append(codes, d)
}
return codes, nil
}
func doReq(client *http.Client, url, host, word, customKey, customVal, userAgent, cookies string) (int, string, int64, error) {
req, err := http.NewRequest("GET", url+word, nil)
if err != nil {
return 0, "", 0, err
}
if host != "" {
req.Host = host
}
if customKey != "" {
req.Header.Set(customKey, customVal)
}
if cookies != "" {
req.Header.Set("Cookie", cookies)
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Connection", "close")
resp, err := client.Do(req)
if err != nil {
log.Print(err)
return 0, "", 0, err
}
defer resp.Body.Close()
n, _ := io.Copy(ioutil.Discard, resp.Body)
return resp.StatusCode, word, n, nil
}