ffuff/pkg/output/file_json.go
layton 0c991947a7
Adding Content-Type to all output formats (#336) (#341)
* adding content-type to csv and json output (#336)

* added to contributors and changelog

* changed 'type' to 'content-type'

* added content-type for html and md output

* updated changelog

Co-authored-by: layton <layton@desktop-manjaro.fritz.box>
Co-authored-by: Joona Hoikkala <joohoi@users.noreply.github.com>
2021-02-21 15:52:41 +02:00

101 lines
2.5 KiB
Go

package output
import (
"encoding/json"
"io/ioutil"
"time"
"github.com/ffuf/ffuf/pkg/ffuf"
)
type ejsonFileOutput struct {
CommandLine string `json:"commandline"`
Time string `json:"time"`
Results []Result `json:"results"`
Config *ffuf.Config `json:"config"`
}
type JsonResult struct {
Input map[string]string `json:"input"`
Position int `json:"position"`
StatusCode int64 `json:"status"`
ContentLength int64 `json:"length"`
ContentWords int64 `json:"words"`
ContentLines int64 `json:"lines"`
ContentType string `json:"content-type"`
RedirectLocation string `json:"redirectlocation"`
ResultFile string `json:"resultfile"`
Url string `json:"url"`
Host string `json:"host"`
}
type jsonFileOutput struct {
CommandLine string `json:"commandline"`
Time string `json:"time"`
Results []JsonResult `json:"results"`
Config *ffuf.Config `json:"config"`
}
func writeEJSON(config *ffuf.Config, res []Result) error {
if(config.OutputCreateEmptyFile && (len(res) == 0)){
return nil
}
t := time.Now()
outJSON := ejsonFileOutput{
CommandLine: config.CommandLine,
Time: t.Format(time.RFC3339),
Results: res,
}
outBytes, err := json.Marshal(outJSON)
if err != nil {
return err
}
err = ioutil.WriteFile(config.OutputFile, outBytes, 0644)
if err != nil {
return err
}
return nil
}
func writeJSON(config *ffuf.Config, res []Result) error {
t := time.Now()
jsonRes := make([]JsonResult, 0)
for _, r := range res {
strinput := make(map[string]string)
for k, v := range r.Input {
strinput[k] = string(v)
}
jsonRes = append(jsonRes, JsonResult{
Input: strinput,
Position: r.Position,
StatusCode: r.StatusCode,
ContentLength: r.ContentLength,
ContentWords: r.ContentWords,
ContentLines: r.ContentLines,
ContentType: r.ContentType,
RedirectLocation: r.RedirectLocation,
ResultFile: r.ResultFile,
Url: r.Url,
Host: r.Host,
})
}
outJSON := jsonFileOutput{
CommandLine: config.CommandLine,
Time: t.Format(time.RFC3339),
Results: jsonRes,
Config: config,
}
outBytes, err := json.Marshal(outJSON)
if err != nil {
return err
}
err = ioutil.WriteFile(config.OutputFile, outBytes, 0644)
if err != nil {
return err
}
return nil
}