-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmain.go
335 lines (271 loc) · 8.41 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"bufio"
"bytes"
"crypto/sha1"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
func init() {
flag.Usage = func() {
h := []string{
"Request URLs provided on stdin fairly frickin' fast",
"",
"Options:",
" -b, --body <data> Request body",
" -d, --delay <delay> Delay between issuing requests (ms)",
" -H, --header <header> Add a header to the request (can be specified multiple times)",
" --ignore-html Don't save HTML files; useful when looking non-HTML files only",
" --ignore-empty Don't save empty files",
" -k, --keep-alive Use HTTP Keep-Alive",
" -m, --method HTTP method to use (default: GET, or POST if body is specified)",
" -M, --match <string> Save responses that include <string> in the body",
" -o, --output <dir> Directory to save responses in (will be created)",
" -s, --save-status <code> Save responses with given status code (can be specified multiple times)",
" -S, --save Save all responses",
" -x, --proxy <proxyURL> Use the provided HTTP proxy",
"",
}
fmt.Fprintf(os.Stderr, strings.Join(h, "\n"))
}
}
func main() {
var requestBody string
flag.StringVar(&requestBody, "body", "", "")
flag.StringVar(&requestBody, "b", "", "")
var keepAlives bool
flag.BoolVar(&keepAlives, "keep-alive", false, "")
flag.BoolVar(&keepAlives, "keep-alives", false, "")
flag.BoolVar(&keepAlives, "k", false, "")
var saveResponses bool
flag.BoolVar(&saveResponses, "save", false, "")
flag.BoolVar(&saveResponses, "S", false, "")
var delayMs int
flag.IntVar(&delayMs, "delay", 100, "")
flag.IntVar(&delayMs, "d", 100, "")
var method string
flag.StringVar(&method, "method", "GET", "")
flag.StringVar(&method, "m", "GET", "")
var match string
flag.StringVar(&match, "match", "", "")
flag.StringVar(&match, "M", "", "")
var outputDir string
flag.StringVar(&outputDir, "output", "out", "")
flag.StringVar(&outputDir, "o", "out", "")
var headers headerArgs
flag.Var(&headers, "header", "")
flag.Var(&headers, "H", "")
var saveStatus saveStatusArgs
flag.Var(&saveStatus, "save-status", "")
flag.Var(&saveStatus, "s", "")
var proxy string
flag.StringVar(&proxy, "proxy", "", "")
flag.StringVar(&proxy, "x", "", "")
var ignoreHTMLFiles bool
flag.BoolVar(&ignoreHTMLFiles, "ignore-html", false, "")
var ignoreEmpty bool
flag.BoolVar(&ignoreEmpty, "ignore-empty", false, "")
flag.Parse()
delay := time.Duration(delayMs * 1000000)
client := newClient(keepAlives, proxy)
prefix := outputDir
// regex for determining if something is probably HTML. You might
// think that checking the content-type response header would be a better
// idea, and you might be right - but if there's one thing I've learnt
// about webservers it's that they are dirty, rotten, filthy liars.
isHTML := regexp.MustCompile(`(?i)<html`)
var wg sync.WaitGroup
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
rawURL := sc.Text()
wg.Add(1)
time.Sleep(delay)
go func() {
defer wg.Done()
// create the request
var b io.Reader
if requestBody != "" {
b = strings.NewReader(requestBody)
// Can't send a body with a GET request
if method == "GET" {
method = "POST"
}
}
_, err := url.ParseRequestURI(rawURL)
if err != nil {
return
}
req, err := http.NewRequest(method, rawURL, b)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create request: %s\n", err)
return
}
// add headers to the request
for _, h := range headers {
parts := strings.SplitN(h, ":", 2)
if len(parts) != 2 {
continue
}
req.Header.Set(parts[0], parts[1])
}
// send the request
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "request failed: %s\n", err)
return
}
defer resp.Body.Close()
// we want to read the body into a string or something like that so we can provide options to
// not save content based on a pattern or something like that
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read body: %s\n", err)
return
}
shouldSave := saveResponses || len(saveStatus) > 0 && saveStatus.Includes(resp.StatusCode)
// If we've been asked to ignore HTML files then we should really do that.
// But why would you want to ignore HTML files? Sometimes you're looking at
// a ton of hosts for config files and that sort of thing, and they lie to you
// by sending a 200 response code instead of a 404. Those pages are *usually*
// HTML so providing a way to ignore them cuts down on clutter a little bit,
// even if it is a niche use-case.
if ignoreHTMLFiles {
shouldSave = shouldSave && !isHTML.Match(responseBody)
}
// sometimes we don't about the response at all if it's empty
if ignoreEmpty {
shouldSave = shouldSave && len(bytes.TrimSpace(responseBody)) != 0
}
// if a -M/--match option has been used, we always want to save if it matches
if match != "" {
if bytes.Contains(responseBody, []byte(match)) {
shouldSave = true
}
}
if !shouldSave {
fmt.Printf("%s %d\n", rawURL, resp.StatusCode)
return
}
// output files are stored in prefix/domain/normalisedpath/hash.(body|headers)
normalisedPath := normalisePath(req.URL)
hash := sha1.Sum([]byte(method + rawURL + requestBody + headers.String()))
p := path.Join(prefix, req.URL.Hostname(), normalisedPath, fmt.Sprintf("%x.body", hash))
err = os.MkdirAll(path.Dir(p), 0750)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create dir: %s\n", err)
return
}
// write the response body to a file
err = ioutil.WriteFile(p, responseBody, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write file contents: %s\n", err)
return
}
// create the headers file
headersPath := path.Join(prefix, req.URL.Hostname(), normalisedPath, fmt.Sprintf("%x.headers", hash))
headersFile, err := os.Create(headersPath)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create file: %s\n", err)
return
}
defer headersFile.Close()
var buf strings.Builder
// put the request URL and method at the top
buf.WriteString(fmt.Sprintf("%s %s\n\n", method, rawURL))
// add the request headers
for _, h := range headers {
buf.WriteString(fmt.Sprintf("> %s\n", h))
}
buf.WriteRune('\n')
// add the request body
if requestBody != "" {
buf.WriteString(requestBody)
buf.WriteString("\n\n")
}
// add the proto and status
buf.WriteString(fmt.Sprintf("< %s %s\n", resp.Proto, resp.Status))
// add the response headers
for k, vs := range resp.Header {
for _, v := range vs {
buf.WriteString(fmt.Sprintf("< %s: %s\n", k, v))
}
}
// add the response body
_, err = io.Copy(headersFile, strings.NewReader(buf.String()))
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write file contents: %s\n", err)
return
}
// output the body filename for each URL
fmt.Printf("%s: %s %d\n", p, rawURL, resp.StatusCode)
}()
}
wg.Wait()
}
func newClient(keepAlives bool, proxy string) *http.Client {
tr := &http.Transport{
MaxIdleConns: 30,
IdleConnTimeout: time.Second,
DisableKeepAlives: !keepAlives,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{
Timeout: time.Second * 10,
KeepAlive: time.Second,
}).DialContext,
}
if proxy != "" {
if p, err := url.Parse(proxy); err == nil {
tr.Proxy = http.ProxyURL(p)
}
}
re := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
return &http.Client{
Transport: tr,
CheckRedirect: re,
Timeout: time.Second * 10,
}
}
type headerArgs []string
func (h *headerArgs) Set(val string) error {
*h = append(*h, val)
return nil
}
func (h headerArgs) String() string {
return strings.Join(h, ", ")
}
type saveStatusArgs []int
func (s *saveStatusArgs) Set(val string) error {
i, _ := strconv.Atoi(val)
*s = append(*s, i)
return nil
}
func (s saveStatusArgs) String() string {
return "string"
}
func (s saveStatusArgs) Includes(search int) bool {
for _, status := range s {
if status == search {
return true
}
}
return false
}
func normalisePath(u *url.URL) string {
re := regexp.MustCompile(`[^a-zA-Z0-9/._-]+`)
return re.ReplaceAllString(u.Path, "-")
}