ffuff/pkg/output/file_csv.go
Joona Hoikkala f97c2f7600
Interactive mode and recursion-strategy (#426)
* Add new feature: recursion-strategy

* Implementation of interactive mode (#8)

* Add interactive mode documentation (#9)

* Prepare for release 1.3.0 (#11)
2021-04-18 12:54:17 +03:00

75 lines
1.7 KiB
Go

package output
import (
"encoding/base64"
"encoding/csv"
"os"
"strconv"
"github.com/ffuf/ffuf/pkg/ffuf"
)
var staticheaders = []string{"url", "redirectlocation", "position", "status_code", "content_length", "content_words", "content_lines", "content_type", "resultfile"}
func writeCSV(filename string, config *ffuf.Config, res []ffuf.Result, encode bool) error {
if config.OutputCreateEmptyFile && (len(res) == 0) {
return nil
}
header := make([]string, 0)
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
w := csv.NewWriter(f)
defer w.Flush()
for _, inputprovider := range config.InputProviders {
header = append(header, inputprovider.Keyword)
}
header = append(header, staticheaders...)
if err := w.Write(header); err != nil {
return err
}
for _, r := range res {
if encode {
inputs := make(map[string][]byte, len(r.Input))
for k, v := range r.Input {
inputs[k] = []byte(base64encode(v))
}
r.Input = inputs
}
err := w.Write(toCSV(r))
if err != nil {
return err
}
}
return nil
}
func base64encode(in []byte) string {
return base64.StdEncoding.EncodeToString(in)
}
func toCSV(r ffuf.Result) []string {
res := make([]string, 0)
for _, v := range r.Input {
res = append(res, string(v))
}
res = append(res, r.Url)
res = append(res, r.RedirectLocation)
res = append(res, strconv.Itoa(r.Position))
res = append(res, strconv.FormatInt(r.StatusCode, 10))
res = append(res, strconv.FormatInt(r.ContentLength, 10))
res = append(res, strconv.FormatInt(r.ContentWords, 10))
res = append(res, strconv.FormatInt(r.ContentLines, 10))
res = append(res, r.ContentType)
res = append(res, r.ResultFile)
return res
}