| 1 | package output |
| 2 | |
| 3 | import ( |
| 4 | "crypto/md5" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path" |
| 9 | "sort" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/ffuf/ffuf/v2/pkg/ffuf" |
| 15 | ) |
| 16 | |
| 17 | const ( |
| 18 | BANNER_HEADER = ` |
| 19 | /'___\ /'___\ /'___\ |
| 20 | /\ \__/ /\ \__/ __ __ /\ \__/ |
| 21 | \ \ ,__\\ \ ,__\/\ \/\ \ \ \ ,__\ |
| 22 | \ \ \_/ \ \ \_/\ \ \_\ \ \ \ \_/ |
| 23 | \ \_\ \ \_\ \ \____/ \ \_\ |
| 24 | \/_/ \/_/ \/___/ \/_/ |
| 25 | ` |
| 26 | BANNER_SEP = "________________________________________________" |
| 27 | ) |
| 28 | |
| 29 | type Stdoutput struct { |
| 30 | config *ffuf.Config |
| 31 | fuzzkeywords []string |
| 32 | Results []ffuf.Result |
| 33 | CurrentResults []ffuf.Result |
| 34 | } |
| 35 | |
| 36 | func NewStdoutput(conf *ffuf.Config) *Stdoutput { |
| 37 | var outp Stdoutput |
| 38 | outp.config = conf |
| 39 | outp.Results = make([]ffuf.Result, 0) |
| 40 | outp.CurrentResults = make([]ffuf.Result, 0) |
| 41 | outp.fuzzkeywords = make([]string, 0) |
| 42 | for _, ip := range conf.InputProviders { |
| 43 | outp.fuzzkeywords = append(outp.fuzzkeywords, ip.Keyword) |
| 44 | } |
| 45 | sort.Strings(outp.fuzzkeywords) |
| 46 | return &outp |
| 47 | } |
| 48 | |
| 49 | func (s *Stdoutput) Banner() { |
| 50 | version := strings.ReplaceAll(ffuf.FormattedVersion(), "<3", fmt.Sprintf("%s<3%s", ANSI_RED, ANSI_CLEAR)) |
| 51 | fmt.Fprintf(os.Stderr, "%s\n %s\n%s\n\n", BANNER_HEADER, version, BANNER_SEP) |
| 52 | printOption([]byte("Method"), []byte(s.config.Method)) |
| 53 | printOption([]byte("URL"), []byte(s.config.Url)) |
| 54 | |
| 55 | // Print wordlists |
| 56 | for _, provider := range s.config.InputProviders { |
| 57 | if provider.Name == "wordlist" { |
| 58 | printOption([]byte("Wordlist"), []byte(provider.Keyword+": "+provider.Value)) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Print headers |
| 63 | if len(s.config.Headers) > 0 { |
| 64 | for k, v := range s.config.Headers { |
| 65 | printOption([]byte("Header"), []byte(fmt.Sprintf("%s: %s", k, v))) |
| 66 | } |
| 67 | } |
| 68 | // Print POST data |
| 69 | if len(s.config.Data) > 0 { |
| 70 | printOption([]byte("Data"), []byte(s.config.Data)) |
| 71 | } |
| 72 | |
| 73 | // Print extensions |
| 74 | if len(s.config.Extensions) > 0 { |
| 75 | exts := "" |
| 76 | for _, ext := range s.config.Extensions { |
| 77 | exts = fmt.Sprintf("%s%s ", exts, ext) |
| 78 | } |
| 79 | printOption([]byte("Extensions"), []byte(exts)) |
| 80 | } |
| 81 | |
| 82 | // Output file info |
| 83 | if len(s.config.OutputFile) > 0 { |
| 84 | |
| 85 | // Use filename as specified by user |
| 86 | OutputFile := s.config.OutputFile |
| 87 | |
| 88 | if s.config.OutputFormat == "all" { |
| 89 | // Actually... append all extensions |
| 90 | OutputFile += ".{json,ejson,html,md,csv,ecsv}" |
| 91 | } |
| 92 | |
| 93 | printOption([]byte("Output file"), []byte(OutputFile)) |
| 94 | printOption([]byte("File format"), []byte(s.config.OutputFormat)) |
| 95 | } |
| 96 | |
| 97 | // Follow redirects? |
| 98 | follow := fmt.Sprintf("%t", s.config.FollowRedirects) |
| 99 | printOption([]byte("Follow redirects"), []byte(follow)) |
| 100 | |
| 101 | // Autocalibration |
| 102 | autocalib := fmt.Sprintf("%t", s.config.AutoCalibration) |
| 103 | printOption([]byte("Calibration"), []byte(autocalib)) |
| 104 | |
| 105 | // Proxies |
| 106 | if len(s.config.ProxyURL) > 0 { |
| 107 | printOption([]byte("Proxy"), []byte(s.config.ProxyURL)) |
| 108 | } |
| 109 | if len(s.config.ReplayProxyURL) > 0 { |
| 110 | printOption([]byte("ReplayProxy"), []byte(s.config.ReplayProxyURL)) |
| 111 | } |
| 112 | |
| 113 | // Timeout |
| 114 | timeout := fmt.Sprintf("%d", s.config.Timeout) |
| 115 | printOption([]byte("Timeout"), []byte(timeout)) |
| 116 | |
| 117 | // Threads |
| 118 | threads := fmt.Sprintf("%d", s.config.Threads) |
| 119 | printOption([]byte("Threads"), []byte(threads)) |
| 120 | |
| 121 | // Delay? |
| 122 | if s.config.Delay.HasDelay { |
| 123 | delay := "" |
| 124 | if s.config.Delay.IsRange { |
| 125 | delay = fmt.Sprintf("%.2f - %.2f seconds", s.config.Delay.Min, s.config.Delay.Max) |
| 126 | } else { |
| 127 | delay = fmt.Sprintf("%.2f seconds", s.config.Delay.Min) |
| 128 | } |
| 129 | printOption([]byte("Delay"), []byte(delay)) |
| 130 | } |
| 131 | |
| 132 | // Print matchers |
| 133 | for _, f := range s.config.MatcherManager.GetMatchers() { |
| 134 | printOption([]byte("Matcher"), []byte(f.ReprVerbose())) |
| 135 | } |
| 136 | // Print filters |
| 137 | for _, f := range s.config.MatcherManager.GetFilters() { |
| 138 | printOption([]byte("Filter"), []byte(f.ReprVerbose())) |
| 139 | } |
| 140 | fmt.Fprintf(os.Stderr, "%s\n\n", BANNER_SEP) |
| 141 | } |
| 142 | |
| 143 | // Reset resets the result slice |
| 144 | func (s *Stdoutput) Reset() { |
| 145 | s.CurrentResults = make([]ffuf.Result, 0) |
| 146 | } |
| 147 | |
| 148 | // Cycle moves the CurrentResults to Results and resets the results slice |
| 149 | func (s *Stdoutput) Cycle() { |
| 150 | s.Results = append(s.Results, s.CurrentResults...) |
| 151 | s.Reset() |
| 152 | } |
| 153 | |
| 154 | // GetResults returns the result slice |
| 155 | func (s *Stdoutput) GetCurrentResults() []ffuf.Result { |
| 156 | return s.CurrentResults |
| 157 | } |
| 158 | |
| 159 | // SetResults sets the result slice |
| 160 | func (s *Stdoutput) SetCurrentResults(results []ffuf.Result) { |
| 161 | s.CurrentResults = results |
| 162 | } |
| 163 | |
| 164 | func (s *Stdoutput) Progress(status ffuf.Progress) { |
| 165 | if s.config.Quiet { |
| 166 | // No progress for quiet mode |
| 167 | return |
| 168 | } |
| 169 | |
| 170 | dur := time.Since(status.StartedAt) |
| 171 | runningSecs := int(dur / time.Second) |
| 172 | var reqRate int64 |
| 173 | if runningSecs > 0 { |
| 174 | reqRate = status.ReqSec |
| 175 | } else { |
| 176 | reqRate = 0 |
| 177 | } |
| 178 | |
| 179 | hours := dur / time.Hour |
| 180 | dur -= hours * time.Hour |
| 181 | mins := dur / time.Minute |
| 182 | dur -= mins * time.Minute |
| 183 | secs := dur / time.Second |
| 184 | |
| 185 | fmt.Fprintf(os.Stderr, "%s:: Progress: [%d/%d] :: Job [%d/%d] :: %d req/sec :: Duration: [%d:%02d:%02d] :: Errors: %d ::", TERMINAL_CLEAR_LINE, status.ReqCount, status.ReqTotal, status.QueuePos, status.QueueTotal, reqRate, hours, mins, secs, status.ErrorCount) |
| 186 | } |
| 187 | |
| 188 | func (s *Stdoutput) Info(infostring string) { |
| 189 | if s.config.Quiet { |
| 190 | fmt.Fprintf(os.Stderr, "%s", infostring) |
| 191 | } else { |
| 192 | if !s.config.Colors { |
| 193 | fmt.Fprintf(os.Stderr, "%s[INFO] %s\n\n", TERMINAL_CLEAR_LINE, infostring) |
| 194 | } else { |
| 195 | fmt.Fprintf(os.Stderr, "%s[%sINFO%s] %s\n\n", TERMINAL_CLEAR_LINE, ANSI_BLUE, ANSI_CLEAR, infostring) |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func (s *Stdoutput) Error(errstring string) { |
| 201 | if s.config.Quiet { |
| 202 | fmt.Fprintf(os.Stderr, "%s", errstring) |
| 203 | } else { |
| 204 | if !s.config.Colors { |
| 205 | fmt.Fprintf(os.Stderr, "%s[ERR]Â %s\n", TERMINAL_CLEAR_LINE, errstring) |
| 206 | } else { |
| 207 | fmt.Fprintf(os.Stderr, "%s[%sERR%s] %s\n", TERMINAL_CLEAR_LINE, ANSI_RED, ANSI_CLEAR, errstring) |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func (s *Stdoutput) Warning(warnstring string) { |
| 213 | if s.config.Quiet { |
| 214 | fmt.Fprintf(os.Stderr, "%s", warnstring) |
| 215 | } else { |
| 216 | if !s.config.Colors { |
| 217 | fmt.Fprintf(os.Stderr, "%s[WARN] %s\n", TERMINAL_CLEAR_LINE, warnstring) |
| 218 | } else { |
| 219 | fmt.Fprintf(os.Stderr, "%s[%sWARN%s] %s\n", TERMINAL_CLEAR_LINE, ANSI_RED, ANSI_CLEAR, warnstring) |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | func (s *Stdoutput) Raw(output string) { |
| 225 | fmt.Fprintf(os.Stderr, "%s%s", TERMINAL_CLEAR_LINE, output) |
| 226 | } |
| 227 | |
| 228 | func (s *Stdoutput) writeToAll(filename string, config *ffuf.Config, res []ffuf.Result) error { |
| 229 | var err error |
| 230 | var BaseFilename string = s.config.OutputFile |
| 231 | |
| 232 | // Go through each type of write, adding |
| 233 | // the suffix to each output file. |
| 234 | |
| 235 | s.config.OutputFile = BaseFilename + ".json" |
| 236 | err = writeJSON(s.config.OutputFile, s.config, res) |
| 237 | if err != nil { |
| 238 | s.Error(err.Error()) |
| 239 | } |
| 240 | |
| 241 | s.config.OutputFile = BaseFilename + ".ejson" |
| 242 | err = writeEJSON(s.config.OutputFile, s.config, res) |
| 243 | if err != nil { |
| 244 | s.Error(err.Error()) |
| 245 | } |
| 246 | |
| 247 | s.config.OutputFile = BaseFilename + ".html" |
| 248 | err = writeHTML(s.config.OutputFile, s.config, res) |
| 249 | if err != nil { |
| 250 | s.Error(err.Error()) |
| 251 | } |
| 252 | |
| 253 | s.config.OutputFile = BaseFilename + ".md" |
| 254 | err = writeMarkdown(s.config.OutputFile, s.config, res) |
| 255 | if err != nil { |
| 256 | s.Error(err.Error()) |
| 257 | } |
| 258 | |
| 259 | s.config.OutputFile = BaseFilename + ".csv" |
| 260 | err = writeCSV(s.config.OutputFile, s.config, res, false) |
| 261 | if err != nil { |
| 262 | s.Error(err.Error()) |
| 263 | } |
| 264 | |
| 265 | s.config.OutputFile = BaseFilename + ".ecsv" |
| 266 | err = writeCSV(s.config.OutputFile, s.config, res, true) |
| 267 | if err != nil { |
| 268 | s.Error(err.Error()) |
| 269 | } |
| 270 | |
| 271 | return nil |
| 272 | |
| 273 | } |
| 274 | |
| 275 | // SaveFile saves the current results to a file of a given type |
| 276 | func (s *Stdoutput) SaveFile(filename, format string) error { |
| 277 | var err error |
| 278 | if s.config.OutputSkipEmptyFile && len(s.Results) == 0 && len(s.CurrentResults) == 0 { |
| 279 | s.Info("No results and -or defined, output file not written.") |
| 280 | return err |
| 281 | } |
| 282 | switch format { |
| 283 | case "all": |
| 284 | err = s.writeToAll(filename, s.config, append(s.Results, s.CurrentResults...)) |
| 285 | case "json": |
| 286 | err = writeJSON(filename, s.config, append(s.Results, s.CurrentResults...)) |
| 287 | case "ejson": |
| 288 | err = writeEJSON(filename, s.config, append(s.Results, s.CurrentResults...)) |
| 289 | case "html": |
| 290 | err = writeHTML(filename, s.config, append(s.Results, s.CurrentResults...)) |
| 291 | case "md": |
| 292 | err = writeMarkdown(filename, s.config, append(s.Results, s.CurrentResults...)) |
| 293 | case "csv": |
| 294 | err = writeCSV(filename, s.config, append(s.Results, s.CurrentResults...), false) |
| 295 | case "ecsv": |
| 296 | err = writeCSV(filename, s.config, append(s.Results, s.CurrentResults...), true) |
| 297 | } |
| 298 | return err |
| 299 | } |
| 300 | |
| 301 | // Finalize gets run after all the ffuf jobs are completed |
| 302 | func (s *Stdoutput) Finalize() error { |
| 303 | var err error |
| 304 | if s.config.OutputFile != "" { |
| 305 | err = s.SaveFile(s.config.OutputFile, s.config.OutputFormat) |
| 306 | if err != nil { |
| 307 | s.Error(err.Error()) |
| 308 | } |
| 309 | } |
| 310 | if !s.config.Quiet { |
| 311 | fmt.Fprintf(os.Stderr, "\n") |
| 312 | } |
| 313 | return nil |
| 314 | } |
| 315 | |
| 316 | func (s *Stdoutput) Result(resp ffuf.Response) { |
| 317 | // Do we want to write request and response to a file |
| 318 | if len(s.config.OutputDirectory) > 0 { |
| 319 | resp.ResultFile = s.writeResultToFile(resp) |
| 320 | } |
| 321 | |
| 322 | inputs := make(map[string][]byte, len(resp.Request.Input)) |
| 323 | for k, v := range resp.Request.Input { |
| 324 | inputs[k] = v |
| 325 | } |
| 326 | sResult := ffuf.Result{ |
| 327 | Input: inputs, |
| 328 | Position: resp.Request.Position, |
| 329 | StatusCode: resp.StatusCode, |
| 330 | ContentLength: resp.ContentLength, |
| 331 | ContentWords: resp.ContentWords, |
| 332 | ContentLines: resp.ContentLines, |
| 333 | ContentType: resp.ContentType, |
| 334 | RedirectLocation: resp.GetRedirectLocation(false), |
| 335 | ScraperData: resp.ScraperData, |
| 336 | Url: resp.Request.Url, |
| 337 | Duration: resp.Duration, |
| 338 | ResultFile: resp.ResultFile, |
| 339 | Host: resp.Request.Host, |
| 340 | } |
| 341 | s.CurrentResults = append(s.CurrentResults, sResult) |
| 342 | // Output the result |
| 343 | s.PrintResult(sResult) |
| 344 | } |
| 345 | |
| 346 | func (s *Stdoutput) writeResultToFile(resp ffuf.Response) string { |
| 347 | var fileContent, fileName, filePath string |
| 348 | // Create directory if needed |
| 349 | if s.config.OutputDirectory != "" { |
| 350 | err := os.MkdirAll(s.config.OutputDirectory, 0750) |
| 351 | if err != nil { |
| 352 | if !os.IsExist(err) { |
| 353 | s.Error(err.Error()) |
| 354 | return "" |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | fileContent = fmt.Sprintf("%s\n---- ↑ Request ---- Response ↓ ----\n\n%s", resp.Request.Raw, resp.Raw) |
| 359 | |
| 360 | // Create file name |
| 361 | fileName = fmt.Sprintf("%x", md5.Sum([]byte(fileContent))) |
| 362 | |
| 363 | filePath = path.Join(s.config.OutputDirectory, fileName) |
| 364 | err := os.WriteFile(filePath, []byte(fileContent), 0640) |
| 365 | if err != nil { |
| 366 | s.Error(err.Error()) |
| 367 | } |
| 368 | return fileName |
| 369 | } |
| 370 | |
| 371 | func (s *Stdoutput) PrintResult(res ffuf.Result) { |
| 372 | switch { |
| 373 | case s.config.Json: |
| 374 | s.resultJson(res) |
| 375 | case s.config.Quiet: |
| 376 | s.resultQuiet(res) |
| 377 | case len(s.fuzzkeywords) > 1 || s.config.Verbose || len(s.config.OutputDirectory) > 0 || len(res.ScraperData) > 0: |
| 378 | // Print a multi-line result (when using multiple input keywords and wordlists) |
| 379 | s.resultMultiline(res) |
| 380 | default: |
| 381 | s.resultNormal(res) |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | func (s *Stdoutput) prepareInputsOneLine(res ffuf.Result) string { |
| 386 | inputs := "" |
| 387 | if len(s.fuzzkeywords) > 1 { |
| 388 | for _, k := range s.fuzzkeywords { |
| 389 | if ffuf.StrInSlice(k, s.config.CommandKeywords) { |
| 390 | // If we're using external command for input, display the position instead of input |
| 391 | inputs = fmt.Sprintf("%s%s : %s ", inputs, k, strconv.Itoa(res.Position)) |
| 392 | } else { |
| 393 | inputs = fmt.Sprintf("%s%s : %s ", inputs, k, res.Input[k]) |
| 394 | } |
| 395 | } |
| 396 | } else { |
| 397 | for _, k := range s.fuzzkeywords { |
| 398 | if ffuf.StrInSlice(k, s.config.CommandKeywords) { |
| 399 | // If we're using external command for input, display the position instead of input |
| 400 | inputs = strconv.Itoa(res.Position) |
| 401 | } else { |
| 402 | inputs = string(res.Input[k]) |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | return inputs |
| 407 | } |
| 408 | |
| 409 | func (s *Stdoutput) resultQuiet(res ffuf.Result) { |
| 410 | fmt.Println(s.prepareInputsOneLine(res)) |
| 411 | } |
| 412 | |
| 413 | func (s *Stdoutput) resultMultiline(res ffuf.Result) { |
| 414 | var res_hdr, res_str string |
| 415 | res_str = "%s%s * %s: %s\n" |
| 416 | res_hdr = fmt.Sprintf("%s%s[Status: %d, Size: %d, Words: %d, Lines: %d, Duration: %dms]%s", TERMINAL_CLEAR_LINE, s.colorize(res.StatusCode), res.StatusCode, res.ContentLength, res.ContentWords, res.ContentLines, res.Duration.Milliseconds(), ANSI_CLEAR) |
| 417 | reslines := "" |
| 418 | if s.config.Verbose { |
| 419 | reslines = fmt.Sprintf("%s%s| URL | %s\n", reslines, TERMINAL_CLEAR_LINE, res.Url) |
| 420 | redirectLocation := res.RedirectLocation |
| 421 | if redirectLocation != "" { |
| 422 | reslines = fmt.Sprintf("%s%s| --> | %s\n", reslines, TERMINAL_CLEAR_LINE, redirectLocation) |
| 423 | } |
| 424 | } |
| 425 | if res.ResultFile != "" { |
| 426 | reslines = fmt.Sprintf("%s%s| RES | %s\n", reslines, TERMINAL_CLEAR_LINE, res.ResultFile) |
| 427 | } |
| 428 | for _, k := range s.fuzzkeywords { |
| 429 | if ffuf.StrInSlice(k, s.config.CommandKeywords) { |
| 430 | // If we're using external command for input, display the position instead of input |
| 431 | reslines = fmt.Sprintf(res_str, reslines, TERMINAL_CLEAR_LINE, k, strconv.Itoa(res.Position)) |
| 432 | } else { |
| 433 | // Wordlist input |
| 434 | reslines = fmt.Sprintf(res_str, reslines, TERMINAL_CLEAR_LINE, k, res.Input[k]) |
| 435 | } |
| 436 | } |
| 437 | if len(res.ScraperData) > 0 { |
| 438 | reslines = fmt.Sprintf("%s%s| SCR |\n", reslines, TERMINAL_CLEAR_LINE) |
| 439 | for k, vslice := range res.ScraperData { |
| 440 | for _, v := range vslice { |
| 441 | reslines = fmt.Sprintf(res_str, reslines, TERMINAL_CLEAR_LINE, k, v) |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | fmt.Printf("%s\n%s\n", res_hdr, reslines) |
| 446 | } |
| 447 | |
| 448 | func (s *Stdoutput) resultNormal(res ffuf.Result) { |
| 449 | resnormal := fmt.Sprintf("%s%s%-23s [Status: %d, Size: %d, Words: %d, Lines: %d, Duration: %dms]%s", TERMINAL_CLEAR_LINE, s.colorize(res.StatusCode), s.prepareInputsOneLine(res), res.StatusCode, res.ContentLength, res.ContentWords, res.ContentLines, res.Duration.Milliseconds(), ANSI_CLEAR) |
| 450 | fmt.Println(resnormal) |
| 451 | } |
| 452 | |
| 453 | func (s *Stdoutput) resultJson(res ffuf.Result) { |
| 454 | resBytes, err := json.Marshal(res) |
| 455 | if err != nil { |
| 456 | s.Error(err.Error()) |
| 457 | } else { |
| 458 | fmt.Fprint(os.Stderr, TERMINAL_CLEAR_LINE) |
| 459 | fmt.Println(string(resBytes)) |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | func (s *Stdoutput) colorize(status int64) string { |
| 464 | if !s.config.Colors { |
| 465 | return "" |
| 466 | } |
| 467 | colorCode := ANSI_CLEAR |
| 468 | if status >= 200 && status < 300 { |
| 469 | colorCode = ANSI_GREEN |
| 470 | } |
| 471 | if status >= 300 && status < 400 { |
| 472 | colorCode = ANSI_BLUE |
| 473 | } |
| 474 | if status >= 400 && status < 500 { |
| 475 | colorCode = ANSI_YELLOW |
| 476 | } |
| 477 | if status >= 500 && status < 600 { |
| 478 | colorCode = ANSI_RED |
| 479 | } |
| 480 | return colorCode |
| 481 | } |
| 482 | |
| 483 | func printOption(name []byte, value []byte) { |
| 484 | fmt.Fprintf(os.Stderr, " :: %-16s : %s\n", name, value) |
| 485 | } |