| 1 | package output |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "encoding/csv" |
| 6 | "os" |
| 7 | "strconv" |
| 8 | |
| 9 | "github.com/ffuf/ffuf/v2/pkg/ffuf" |
| 10 | ) |
| 11 | |
| 12 | var staticheaders = []string{"url", "redirectlocation", "position", "status_code", "content_length", "content_words", "content_lines", "content_type", "duration", "resultfile", "Ffufhash"} |
| 13 | |
| 14 | func writeCSV(filename string, config *ffuf.Config, res []ffuf.Result, encode bool) error { |
| 15 | header := make([]string, 0) |
| 16 | f, err := os.Create(filename) |
| 17 | if err != nil { |
| 18 | return err |
| 19 | } |
| 20 | defer f.Close() |
| 21 | |
| 22 | w := csv.NewWriter(f) |
| 23 | defer w.Flush() |
| 24 | |
| 25 | for _, inputprovider := range config.InputProviders { |
| 26 | header = append(header, inputprovider.Keyword) |
| 27 | } |
| 28 | header = append(header, staticheaders...) |
| 29 | |
| 30 | if err := w.Write(header); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | for _, r := range res { |
| 34 | if encode { |
| 35 | inputs := make(map[string][]byte, len(r.Input)) |
| 36 | for k, v := range r.Input { |
| 37 | inputs[k] = []byte(base64encode(v)) |
| 38 | } |
| 39 | r.Input = inputs |
| 40 | } |
| 41 | |
| 42 | err := w.Write(toCSV(r)) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | } |
| 47 | return nil |
| 48 | } |
| 49 | |
| 50 | func base64encode(in []byte) string { |
| 51 | return base64.StdEncoding.EncodeToString(in) |
| 52 | } |
| 53 | |
| 54 | func toCSV(r ffuf.Result) []string { |
| 55 | res := make([]string, 0) |
| 56 | ffufhash := "" |
| 57 | for k, v := range r.Input { |
| 58 | if k == "FFUFHASH" { |
| 59 | ffufhash = string(v) |
| 60 | } else { |
| 61 | res = append(res, string(v)) |
| 62 | } |
| 63 | } |
| 64 | res = append(res, r.Url) |
| 65 | res = append(res, r.RedirectLocation) |
| 66 | res = append(res, strconv.Itoa(r.Position)) |
| 67 | res = append(res, strconv.FormatInt(r.StatusCode, 10)) |
| 68 | res = append(res, strconv.FormatInt(r.ContentLength, 10)) |
| 69 | res = append(res, strconv.FormatInt(r.ContentWords, 10)) |
| 70 | res = append(res, strconv.FormatInt(r.ContentLines, 10)) |
| 71 | res = append(res, r.ContentType) |
| 72 | res = append(res, r.Duration.String()) |
| 73 | res = append(res, r.ResultFile) |
| 74 | res = append(res, ffufhash) |
| 75 | return res |
| 76 | } |