| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "compress/flate" |
| 6 | "compress/gzip" |
| 7 | "crypto/tls" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net" |
| 11 | "net/http" |
| 12 | "net/http/httptrace" |
| 13 | "net/http/httputil" |
| 14 | "net/textproto" |
| 15 | "net/url" |
| 16 | "strconv" |
| 17 | "strings" |
| 18 | "time" |
| 19 | |
| 20 | "github.com/ffuf/ffuf/v2/pkg/ffuf" |
| 21 | |
| 22 | "github.com/andybalholm/brotli" |
| 23 | ) |
| 24 | |
| 25 | // Download results < 5MB |
| 26 | const MAX_DOWNLOAD_SIZE = 5242880 |
| 27 | |
| 28 | type SimpleRunner struct { |
| 29 | config *ffuf.Config |
| 30 | client *http.Client |
| 31 | } |
| 32 | |
| 33 | func NewSimpleRunner(conf *ffuf.Config, replay bool) ffuf.RunnerProvider { |
| 34 | var simplerunner SimpleRunner |
| 35 | proxyURL := http.ProxyFromEnvironment |
| 36 | customProxy := "" |
| 37 | |
| 38 | if replay { |
| 39 | customProxy = conf.ReplayProxyURL |
| 40 | } else { |
| 41 | customProxy = conf.ProxyURL |
| 42 | } |
| 43 | if len(customProxy) > 0 { |
| 44 | pu, err := url.Parse(customProxy) |
| 45 | if err == nil { |
| 46 | proxyURL = http.ProxyURL(pu) |
| 47 | } |
| 48 | } |
| 49 | cert := []tls.Certificate{} |
| 50 | |
| 51 | if conf.ClientCert != "" && conf.ClientKey != "" { |
| 52 | tmp, _ := tls.LoadX509KeyPair(conf.ClientCert, conf.ClientKey) |
| 53 | cert = []tls.Certificate{tmp} |
| 54 | } |
| 55 | |
| 56 | simplerunner.config = conf |
| 57 | simplerunner.client = &http.Client{ |
| 58 | CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, |
| 59 | Timeout: time.Duration(time.Duration(conf.Timeout) * time.Second), |
| 60 | Transport: &http.Transport{ |
| 61 | ForceAttemptHTTP2: conf.Http2, |
| 62 | Proxy: proxyURL, |
| 63 | MaxIdleConns: 1000, |
| 64 | MaxIdleConnsPerHost: 500, |
| 65 | MaxConnsPerHost: 500, |
| 66 | DialContext: (&net.Dialer{ |
| 67 | Timeout: time.Duration(time.Duration(conf.Timeout) * time.Second), |
| 68 | }).DialContext, |
| 69 | TLSHandshakeTimeout: time.Duration(time.Duration(conf.Timeout) * time.Second), |
| 70 | TLSClientConfig: &tls.Config{ |
| 71 | InsecureSkipVerify: true, |
| 72 | MinVersion: tls.VersionTLS10, |
| 73 | Renegotiation: tls.RenegotiateOnceAsClient, |
| 74 | ServerName: conf.SNI, |
| 75 | Certificates: cert, |
| 76 | }, |
| 77 | }} |
| 78 | |
| 79 | if conf.FollowRedirects { |
| 80 | simplerunner.client.CheckRedirect = nil |
| 81 | } |
| 82 | return &simplerunner |
| 83 | } |
| 84 | |
| 85 | func (r *SimpleRunner) Prepare(input map[string][]byte, basereq *ffuf.Request) (ffuf.Request, error) { |
| 86 | req := ffuf.CopyRequest(basereq) |
| 87 | |
| 88 | for keyword, inputitem := range input { |
| 89 | req.Method = strings.ReplaceAll(req.Method, keyword, string(inputitem)) |
| 90 | headers := make(map[string]string, len(req.Headers)) |
| 91 | for h, v := range req.Headers { |
| 92 | var CanonicalHeader string = textproto.CanonicalMIMEHeaderKey(strings.ReplaceAll(h, keyword, string(inputitem))) |
| 93 | headers[CanonicalHeader] = strings.ReplaceAll(v, keyword, string(inputitem)) |
| 94 | } |
| 95 | req.Headers = headers |
| 96 | req.Url = strings.ReplaceAll(req.Url, keyword, string(inputitem)) |
| 97 | req.Data = []byte(strings.ReplaceAll(string(req.Data), keyword, string(inputitem))) |
| 98 | } |
| 99 | |
| 100 | req.Input = input |
| 101 | return req, nil |
| 102 | } |
| 103 | |
| 104 | func (r *SimpleRunner) Execute(req *ffuf.Request) (ffuf.Response, error) { |
| 105 | var httpreq *http.Request |
| 106 | var err error |
| 107 | var rawreq []byte |
| 108 | data := bytes.NewReader(req.Data) |
| 109 | |
| 110 | var start time.Time |
| 111 | var firstByteTime time.Duration |
| 112 | |
| 113 | trace := &httptrace.ClientTrace{ |
| 114 | WroteRequest: func(wri httptrace.WroteRequestInfo) { |
| 115 | start = time.Now() // begin the timer after the request is fully written |
| 116 | }, |
| 117 | GotFirstResponseByte: func() { |
| 118 | firstByteTime = time.Since(start) // record when the first byte of the response was received |
| 119 | }, |
| 120 | } |
| 121 | |
| 122 | httpreq, err = http.NewRequestWithContext(r.config.Context, req.Method, req.Url, data) |
| 123 | |
| 124 | if err != nil { |
| 125 | return ffuf.Response{}, err |
| 126 | } |
| 127 | |
| 128 | // set default User-Agent header if not present |
| 129 | if _, ok := req.Headers["User-Agent"]; !ok { |
| 130 | req.Headers["User-Agent"] = fmt.Sprintf("%s v%s", "Fuzz Faster U Fool", ffuf.Version()) |
| 131 | } |
| 132 | |
| 133 | // Handle Go http.Request special cases |
| 134 | if _, ok := req.Headers["Host"]; ok { |
| 135 | httpreq.Host = req.Headers["Host"] |
| 136 | } |
| 137 | |
| 138 | req.Host = httpreq.Host |
| 139 | httpreq = httpreq.WithContext(httptrace.WithClientTrace(r.config.Context, trace)) |
| 140 | |
| 141 | if r.config.Raw { |
| 142 | httpreq.URL.Opaque = req.Url |
| 143 | } |
| 144 | |
| 145 | for k, v := range req.Headers { |
| 146 | httpreq.Header.Set(k, v) |
| 147 | } |
| 148 | |
| 149 | if len(r.config.OutputDirectory) > 0 || len(r.config.AuditLog) > 0 { |
| 150 | rawreq, _ = httputil.DumpRequestOut(httpreq, true) |
| 151 | req.Raw = string(rawreq) |
| 152 | } |
| 153 | |
| 154 | httpresp, err := r.client.Do(httpreq) |
| 155 | if err != nil { |
| 156 | return ffuf.Response{}, err |
| 157 | } |
| 158 | |
| 159 | req.Timestamp = start |
| 160 | |
| 161 | resp := ffuf.NewResponse(httpresp, req) |
| 162 | defer httpresp.Body.Close() |
| 163 | |
| 164 | // Check if we should download the resource or not |
| 165 | size, err := strconv.Atoi(httpresp.Header.Get("Content-Length")) |
| 166 | if err == nil { |
| 167 | resp.ContentLength = int64(size) |
| 168 | if (r.config.IgnoreBody) || (size > MAX_DOWNLOAD_SIZE) { |
| 169 | resp.Cancelled = true |
| 170 | return resp, nil |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if len(r.config.OutputDirectory) > 0 || len(r.config.AuditLog) > 0 { |
| 175 | rawresp, _ := httputil.DumpResponse(httpresp, true) |
| 176 | resp.Request.Raw = string(rawreq) |
| 177 | resp.Raw = string(rawresp) |
| 178 | } |
| 179 | var bodyReader io.Reader = httpresp.Body |
| 180 | switch httpresp.Header.Get("Content-Encoding") { |
| 181 | case "gzip": |
| 182 | if zr, zerr := gzip.NewReader(httpresp.Body); zerr == nil { |
| 183 | bodyReader = zr |
| 184 | } // else: fall back to the raw (undecoded) body |
| 185 | case "br": |
| 186 | bodyReader = brotli.NewReader(httpresp.Body) |
| 187 | case "deflate": |
| 188 | bodyReader = flate.NewReader(httpresp.Body) |
| 189 | } |
| 190 | |
| 191 | // Bound the DECOMPRESSED read. The Content-Length guard above is not |
| 192 | // sufficient on its own: a malicious server can send a small compressed body |
| 193 | // (or a chunked response with no Content-Length, or one the transport |
| 194 | // transparently decoded and stripped the headers from) that expands to |
| 195 | // gigabytes. LimitReader caps the bytes we ever pull from the possibly- |
| 196 | // decompressing reader, so a decompression bomb only materialises |
| 197 | // MAX_DOWNLOAD_SIZE+1 bytes in memory instead of OOM-killing the process. |
| 198 | limited := io.LimitReader(bodyReader, int64(MAX_DOWNLOAD_SIZE)+1) |
| 199 | if respbody, rerr := io.ReadAll(limited); rerr == nil { |
| 200 | if len(respbody) > MAX_DOWNLOAD_SIZE { |
| 201 | resp.Cancelled = true |
| 202 | return resp, nil |
| 203 | } |
| 204 | resp.ContentLength = int64(len(respbody)) |
| 205 | resp.Data = respbody |
| 206 | } |
| 207 | |
| 208 | wordsSize := len(strings.Split(string(resp.Data), " ")) |
| 209 | linesSize := len(strings.Split(string(resp.Data), "\n")) |
| 210 | resp.ContentWords = int64(wordsSize) |
| 211 | resp.ContentLines = int64(linesSize) |
| 212 | resp.Duration = firstByteTime |
| 213 | resp.Timestamp = start.Add(firstByteTime) |
| 214 | |
| 215 | return resp, nil |
| 216 | } |
| 217 | |
| 218 | func (r *SimpleRunner) Dump(req *ffuf.Request) ([]byte, error) { |
| 219 | var httpreq *http.Request |
| 220 | var err error |
| 221 | data := bytes.NewReader(req.Data) |
| 222 | httpreq, err = http.NewRequestWithContext(r.config.Context, req.Method, req.Url, data) |
| 223 | if err != nil { |
| 224 | return []byte{}, err |
| 225 | } |
| 226 | |
| 227 | // set default User-Agent header if not present |
| 228 | if _, ok := req.Headers["User-Agent"]; !ok { |
| 229 | req.Headers["User-Agent"] = fmt.Sprintf("%s v%s", "Fuzz Faster U Fool", ffuf.Version()) |
| 230 | } |
| 231 | |
| 232 | // Handle Go http.Request special cases |
| 233 | if _, ok := req.Headers["Host"]; ok { |
| 234 | httpreq.Host = req.Headers["Host"] |
| 235 | } |
| 236 | |
| 237 | req.Host = httpreq.Host |
| 238 | for k, v := range req.Headers { |
| 239 | httpreq.Header.Set(k, v) |
| 240 | } |
| 241 | return httputil.DumpRequestOut(httpreq, true) |
| 242 | } |