-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
214 lines (164 loc) · 4.21 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
/*
dirlstr
- given a list of urls from stdin, dirlstr will traverse the url paths and look for directory listing.
- where directory listing is found, results are output to the console.
- also checks for an open S3 bucket.
e.g.
$ cat urls.txt | dirlstr
options:
-c int = Concurrency (default 20; 50 is quick)
-v = Verbose (for added info)
written by @cybercdh
heavily inspired by @tomnomnom. In the immortal words of Russ Hanneman....."that guy f**ks"
*/
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
)
func main() {
// concurrency flag
var concurrency int
flag.IntVar(&concurrency, "c", 20, "set the concurrency level")
// timeout flag
var to int
flag.IntVar(&to, "t", 10000, "timeout (milliseconds)")
// verbose flag
var verbose bool
flag.BoolVar(&verbose, "v", false, "Get more info on URL attempts")
flag.Parse()
// make an actual time.Duration out of the timeout
timeout := time.Duration(to * 1000000)
var tr = &http.Transport{
MaxIdleConns: 30,
IdleConnTimeout: time.Second,
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: time.Second,
}).DialContext,
}
re := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
client := &http.Client{
Transport: tr,
CheckRedirect: re,
Timeout: timeout,
}
// make a urls channel
urls := make(chan string)
// spin up a bunch of workers
var wg sync.WaitGroup
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
for url := range urls {
// if Directory Listing is found, print the URL
if isDirectoryListing(client, url) {
if verbose {
fmt.Printf("[*] Directory Listing Found at %s\n", url)
} else {
fmt.Printf("%s\n",url)
}
continue
}
}
wg.Done()
}()
}
var input_urls io.Reader
input_urls = os.Stdin
arg_url := flag.Arg(0)
if arg_url != "" {
input_urls = strings.NewReader(arg_url)
}
sc := bufio.NewScanner(input_urls)
// keep track of urls we've seen
seen := make(map[string]bool)
for sc.Scan() {
// parse each url
_url := sc.Text()
// check if the subdomain is prefixed correctly
if !strings.HasPrefix(sc.Text(), "http") {
_url = "http://" + _url
}
u,err := url.Parse(_url)
if err != nil {
if verbose {
fmt.Printf("[!] Error processing %s\n", _url)
}
continue
}
// split the paths from the parsed url
paths := strings.Split(u.Path, "/")
// iterate over the paths slice to traverse and send to urls channel
for i := 0; i < len(paths); i++ {
path := paths[:len(paths)-i]
tmp_url := fmt.Sprintf(u.Scheme +"://" + u.Host + strings.Join(path,"/"))
// if we've seen the url already, keep moving
if _, ok := seen[tmp_url]; ok {
if verbose {
fmt.Printf("[-] Already seen %s\n", tmp_url)
}
continue
}
// add to seen
seen[tmp_url] = true
if verbose{
fmt.Printf("[+] Attempting: %s\n",tmp_url)
}
// feed the channel
urls <- tmp_url
}
}
// once all urls are sent, close the channel
close(urls)
// check there were no errors reading stdin (unlikely)
if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "[!] failed to read input: %s\n", err)
}
// wait until all the workers have finished
wg.Wait()
}
func isDirectoryListing (client *http.Client, url string) bool {
// perform the GET request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false
}
// set custom UA coz I'm 1337
req.Header.Set("User-Agent", "dirlstr/1.0")
req.Header.Add("Connection", "close")
req.Close = true
resp, err := client.Do(req)
// assuming a response, read the body
if resp != nil {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false
}
bodyString := string(bodyBytes)
// look for Directory Listing or an open S3 Bucket, if found return true
if ( strings.Contains(bodyString, "Index of") || strings.Contains(bodyString, "ListBucketResult xmlns=") ) {
return true
}
}
if err != nil {
return false
}
// default return false
return false
}