2021-03-22 14:03:05 +05:30
|
|
|
package disk
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
2021-08-23 14:53:37 +03:00
|
|
|
"path/filepath"
|
2021-03-22 14:03:05 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/reporting/format"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Exporter struct {
|
|
|
|
|
directory string
|
|
|
|
|
options *Options
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:31:46 +03:00
|
|
|
// Options contains the configuration options for GitHub issue tracker client
|
2021-03-22 14:03:05 +05:30
|
|
|
type Options struct {
|
|
|
|
|
// Directory is the directory to export found results to
|
|
|
|
|
Directory string `yaml:"directory"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates a new disk exporter integration client based on options.
|
|
|
|
|
func New(options *Options) (*Exporter, error) {
|
|
|
|
|
directory := options.Directory
|
|
|
|
|
if options.Directory == "" {
|
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
directory = dir
|
|
|
|
|
}
|
|
|
|
|
_ = os.MkdirAll(directory, os.ModePerm)
|
|
|
|
|
return &Exporter{options: options, directory: directory}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Export exports a passed result event to disk
|
|
|
|
|
func (i *Exporter) Export(event *output.ResultEvent) error {
|
|
|
|
|
summary := format.Summary(event)
|
|
|
|
|
description := format.MarkdownDescription(event)
|
|
|
|
|
|
|
|
|
|
filenameBuilder := &strings.Builder{}
|
2021-03-22 14:28:29 +05:30
|
|
|
filenameBuilder.WriteString(event.TemplateID)
|
|
|
|
|
filenameBuilder.WriteString("-")
|
2021-03-22 14:48:11 +05:30
|
|
|
filenameBuilder.WriteString(strings.ReplaceAll(strings.ReplaceAll(event.Matched, "/", "_"), ":", "_"))
|
2021-03-22 14:03:05 +05:30
|
|
|
filenameBuilder.WriteString(".md")
|
2021-08-26 02:43:58 +05:30
|
|
|
finalFilename := sanitizeFilename(filenameBuilder.String())
|
2021-03-22 14:03:05 +05:30
|
|
|
|
|
|
|
|
dataBuilder := &bytes.Buffer{}
|
|
|
|
|
dataBuilder.WriteString("### ")
|
|
|
|
|
dataBuilder.WriteString(summary)
|
|
|
|
|
dataBuilder.WriteString("\n---\n")
|
|
|
|
|
dataBuilder.WriteString(description)
|
|
|
|
|
data := dataBuilder.Bytes()
|
|
|
|
|
|
2021-08-31 12:55:52 +03:00
|
|
|
return ioutil.WriteFile(filepath.Join(i.directory, finalFilename), data, 0644)
|
2021-03-22 14:03:05 +05:30
|
|
|
}
|
2021-06-05 18:01:08 +05:30
|
|
|
|
|
|
|
|
// Close closes the exporter after operation
|
|
|
|
|
func (i *Exporter) Close() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-08-26 02:43:58 +05:30
|
|
|
|
|
|
|
|
func sanitizeFilename(filename string) string {
|
|
|
|
|
if len(filename) > 256 {
|
|
|
|
|
filename = filename[0:255]
|
|
|
|
|
}
|
|
|
|
|
return filename
|
|
|
|
|
}
|