2020-12-20 18:34:11 +00:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
2022-04-01 19:29:02 +00:00
|
|
|
"fmt"
|
2021-10-30 11:19:11 +00:00
|
|
|
"io"
|
2020-12-20 18:34:11 +00:00
|
|
|
"os"
|
2022-04-01 19:29:02 +00:00
|
|
|
"path/filepath"
|
2020-12-23 15:16:42 +00:00
|
|
|
"regexp"
|
2022-04-01 19:29:02 +00:00
|
|
|
"strings"
|
2022-05-10 13:17:22 +00:00
|
|
|
"sync"
|
2021-01-14 07:51:21 +00:00
|
|
|
"time"
|
2020-12-20 18:34:11 +00:00
|
|
|
|
2021-07-19 18:04:08 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2020-12-21 06:28:33 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2020-12-20 18:34:11 +00:00
|
|
|
"github.com/logrusorgru/aurora"
|
2021-07-19 18:04:08 +00:00
|
|
|
|
2022-04-01 19:29:02 +00:00
|
|
|
"github.com/projectdiscovery/fileutil"
|
|
|
|
"github.com/projectdiscovery/gologger"
|
2021-04-16 11:26:41 +00:00
|
|
|
"github.com/projectdiscovery/interactsh/pkg/server"
|
2020-12-29 10:08:14 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/internal/colorizer"
|
2021-07-19 18:04:08 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2021-09-03 13:48:39 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity"
|
2020-12-25 07:25:46 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
|
2021-11-22 12:23:25 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
2021-10-30 10:46:07 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
|
2020-12-20 18:34:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Writer is an interface which writes output to somewhere for nuclei events.
|
|
|
|
type Writer interface {
|
|
|
|
// Close closes the output writer interface
|
|
|
|
Close()
|
2020-12-21 06:34:33 +00:00
|
|
|
// Colorizer returns the colorizer instance for writer
|
|
|
|
Colorizer() aurora.Aurora
|
2020-12-20 18:34:11 +00:00
|
|
|
// Write writes the event to file and/or screen.
|
2020-12-25 15:03:52 +00:00
|
|
|
Write(*ResultEvent) error
|
2021-11-22 12:23:25 +00:00
|
|
|
// WriteFailure writes the optional failure event for template to file and/or screen.
|
|
|
|
WriteFailure(event InternalEvent) error
|
2021-02-02 20:39:45 +00:00
|
|
|
// Request logs a request in the trace log
|
2020-12-21 06:28:33 +00:00
|
|
|
Request(templateID, url, requestType string, err error)
|
2022-04-01 19:29:02 +00:00
|
|
|
// WriteStoreDebugData writes the request/response debug data to file
|
|
|
|
WriteStoreDebugData(host, templateID, eventType string, data string)
|
2020-12-20 18:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StandardWriter is a writer writing output to file and screen for results.
|
|
|
|
type StandardWriter struct {
|
2022-04-01 19:29:02 +00:00
|
|
|
json bool
|
|
|
|
jsonReqResp bool
|
|
|
|
noTimestamp bool
|
|
|
|
noMetadata bool
|
|
|
|
matcherStatus bool
|
2022-05-10 13:17:22 +00:00
|
|
|
mutex *sync.Mutex
|
2022-04-01 19:29:02 +00:00
|
|
|
aurora aurora.Aurora
|
|
|
|
outputFile io.WriteCloser
|
|
|
|
traceFile io.WriteCloser
|
|
|
|
errorFile io.WriteCloser
|
|
|
|
severityColors func(severity.Severity) string
|
|
|
|
storeResponse bool
|
|
|
|
storeResponseDir string
|
2020-12-20 18:34:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 15:16:42 +00:00
|
|
|
var decolorizerRegex = regexp.MustCompile(`\x1B\[[0-9;]*[a-zA-Z]`)
|
|
|
|
|
2020-12-25 07:25:46 +00:00
|
|
|
// InternalEvent is an internal output generation structure for nuclei.
|
|
|
|
type InternalEvent map[string]interface{}
|
|
|
|
|
|
|
|
// InternalWrappedEvent is a wrapped event with operators result added to it.
|
|
|
|
type InternalWrappedEvent struct {
|
|
|
|
InternalEvent InternalEvent
|
2020-12-25 20:39:16 +00:00
|
|
|
Results []*ResultEvent
|
2020-12-25 07:25:46 +00:00
|
|
|
OperatorsResult *operators.Result
|
2021-11-22 12:23:25 +00:00
|
|
|
UsesInteractsh bool
|
2020-12-25 07:25:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 15:03:52 +00:00
|
|
|
// ResultEvent is a wrapped result event for a single nuclei output.
|
|
|
|
type ResultEvent struct {
|
2021-11-22 12:23:25 +00:00
|
|
|
// Template is the relative filename for the template
|
|
|
|
Template string `json:"template,omitempty"`
|
|
|
|
// TemplateURL is the URL of the template for the result inside the nuclei
|
|
|
|
// templates repository if it belongs to the repository.
|
|
|
|
TemplateURL string `json:"template-url,omitempty"`
|
2020-12-25 07:25:46 +00:00
|
|
|
// TemplateID is the ID of the template for the result.
|
2021-10-18 19:56:21 +00:00
|
|
|
TemplateID string `json:"template-id"`
|
2021-06-05 12:31:08 +00:00
|
|
|
// TemplatePath is the path of template
|
|
|
|
TemplatePath string `json:"-"`
|
2020-12-25 07:25:46 +00:00
|
|
|
// Info contains information block of the template for the result.
|
2021-07-12 14:20:01 +00:00
|
|
|
Info model.Info `json:"info,inline"`
|
2020-12-25 07:25:46 +00:00
|
|
|
// MatcherName is the name of the matcher matched if any.
|
2021-10-18 19:56:21 +00:00
|
|
|
MatcherName string `json:"matcher-name,omitempty"`
|
2021-01-11 15:41:35 +00:00
|
|
|
// ExtractorName is the name of the extractor matched if any.
|
2021-10-18 19:56:21 +00:00
|
|
|
ExtractorName string `json:"extractor-name,omitempty"`
|
2020-12-25 07:25:46 +00:00
|
|
|
// Type is the type of the result event.
|
|
|
|
Type string `json:"type"`
|
|
|
|
// Host is the host input on which match was found.
|
|
|
|
Host string `json:"host,omitempty"`
|
2021-03-05 13:55:09 +00:00
|
|
|
// Path is the path input on which match was found.
|
|
|
|
Path string `json:"path,omitempty"`
|
2020-12-25 07:25:46 +00:00
|
|
|
// Matched contains the matched input in its transformed form.
|
2021-10-18 19:56:21 +00:00
|
|
|
Matched string `json:"matched-at,omitempty"`
|
2020-12-25 07:25:46 +00:00
|
|
|
// ExtractedResults contains the extraction result from the inputs.
|
2021-10-18 19:56:21 +00:00
|
|
|
ExtractedResults []string `json:"extracted-results,omitempty"`
|
2021-09-07 14:31:46 +00:00
|
|
|
// Request is the optional, dumped request for the match.
|
2020-12-25 07:25:46 +00:00
|
|
|
Request string `json:"request,omitempty"`
|
2021-09-07 14:31:46 +00:00
|
|
|
// Response is the optional, dumped response for the match.
|
2020-12-25 07:25:46 +00:00
|
|
|
Response string `json:"response,omitempty"`
|
|
|
|
// Metadata contains any optional metadata for the event
|
|
|
|
Metadata map[string]interface{} `json:"meta,omitempty"`
|
2021-01-16 06:36:27 +00:00
|
|
|
// IP is the IP address for the found result event.
|
|
|
|
IP string `json:"ip,omitempty"`
|
2021-01-14 07:51:21 +00:00
|
|
|
// Timestamp is the time the result was found at.
|
|
|
|
Timestamp time.Time `json:"timestamp"`
|
2021-04-16 11:26:41 +00:00
|
|
|
// Interaction is the full details of interactsh interaction.
|
|
|
|
Interaction *server.Interaction `json:"interaction,omitempty"`
|
2021-10-15 08:25:50 +00:00
|
|
|
// CURLCommand is an optional curl command to reproduce the request
|
|
|
|
// Only applicable if the report is for HTTP.
|
2021-11-22 12:23:25 +00:00
|
|
|
CURLCommand string `json:"curl-command,omitempty"`
|
|
|
|
// MatcherStatus is the status of the match
|
2022-02-10 10:29:05 +00:00
|
|
|
MatcherStatus bool `json:"matcher-status"`
|
2022-02-23 22:32:25 +00:00
|
|
|
// Lines is the line count for the specified match
|
|
|
|
Lines []int `json:"matched-line"`
|
2022-02-10 10:29:05 +00:00
|
|
|
|
2021-06-06 10:22:13 +00:00
|
|
|
FileToIndexPosition map[string]int `json:"-"`
|
2020-12-25 07:25:46 +00:00
|
|
|
}
|
2020-12-20 18:34:11 +00:00
|
|
|
|
|
|
|
// NewStandardWriter creates a new output writer based on user configurations
|
2022-04-01 19:29:02 +00:00
|
|
|
func NewStandardWriter(colors, noMetadata, noTimestamp, json, jsonReqResp, MatcherStatus, storeResponse bool, file, traceFile string, errorFile string, storeResponseDir string) (*StandardWriter, error) {
|
2020-12-29 10:08:14 +00:00
|
|
|
auroraColorizer := aurora.NewAurora(colors)
|
2020-12-20 18:34:11 +00:00
|
|
|
|
2021-10-30 11:19:11 +00:00
|
|
|
var outputFile io.WriteCloser
|
2020-12-20 18:34:11 +00:00
|
|
|
if file != "" {
|
|
|
|
output, err := newFileOutputWriter(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create output file")
|
|
|
|
}
|
|
|
|
outputFile = output
|
|
|
|
}
|
2021-10-30 11:19:11 +00:00
|
|
|
var traceOutput io.WriteCloser
|
2020-12-21 06:28:33 +00:00
|
|
|
if traceFile != "" {
|
|
|
|
output, err := newFileOutputWriter(traceFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create output file")
|
|
|
|
}
|
|
|
|
traceOutput = output
|
|
|
|
}
|
2021-10-30 11:19:11 +00:00
|
|
|
var errorOutput io.WriteCloser
|
2021-10-30 09:39:38 +00:00
|
|
|
if errorFile != "" {
|
|
|
|
output, err := newFileOutputWriter(errorFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create error file")
|
|
|
|
}
|
|
|
|
errorOutput = output
|
|
|
|
}
|
2022-04-01 19:29:02 +00:00
|
|
|
// Try to create output folder if it doesn't exist
|
|
|
|
if storeResponse && !fileutil.FolderExists(storeResponseDir) {
|
|
|
|
if err := fileutil.CreateFolder(storeResponseDir); err != nil {
|
|
|
|
gologger.Fatal().Msgf("Could not create output directory '%s': %s\n", storeResponseDir, err)
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 18:34:11 +00:00
|
|
|
writer := &StandardWriter{
|
2022-04-01 19:29:02 +00:00
|
|
|
json: json,
|
|
|
|
jsonReqResp: jsonReqResp,
|
|
|
|
noMetadata: noMetadata,
|
|
|
|
matcherStatus: MatcherStatus,
|
|
|
|
noTimestamp: noTimestamp,
|
|
|
|
aurora: auroraColorizer,
|
2022-05-10 13:17:22 +00:00
|
|
|
mutex: &sync.Mutex{},
|
2022-04-01 19:29:02 +00:00
|
|
|
outputFile: outputFile,
|
|
|
|
traceFile: traceOutput,
|
|
|
|
errorFile: errorOutput,
|
|
|
|
severityColors: colorizer.New(auroraColorizer),
|
|
|
|
storeResponse: storeResponse,
|
|
|
|
storeResponseDir: storeResponseDir,
|
2020-12-20 18:34:11 +00:00
|
|
|
}
|
|
|
|
return writer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes the event to file and/or screen.
|
2020-12-25 15:03:52 +00:00
|
|
|
func (w *StandardWriter) Write(event *ResultEvent) error {
|
2021-11-22 12:23:25 +00:00
|
|
|
// Enrich the result event with extra metadata on the template-path and url.
|
|
|
|
if event.TemplatePath != "" {
|
|
|
|
event.Template, event.TemplateURL = utils.TemplatePathURL(types.ToString(event.TemplatePath))
|
|
|
|
}
|
2021-03-08 06:13:23 +00:00
|
|
|
event.Timestamp = time.Now()
|
|
|
|
|
2020-12-20 18:34:11 +00:00
|
|
|
var data []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if w.json {
|
|
|
|
data, err = w.formatJSON(event)
|
|
|
|
} else {
|
2021-02-26 07:43:11 +00:00
|
|
|
data = w.formatScreen(event)
|
2020-12-20 18:34:11 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not format output")
|
|
|
|
}
|
2021-02-05 09:13:11 +00:00
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-10 13:17:22 +00:00
|
|
|
w.mutex.Lock()
|
|
|
|
defer w.mutex.Unlock()
|
|
|
|
|
2020-12-20 18:34:11 +00:00
|
|
|
_, _ = os.Stdout.Write(data)
|
2020-12-29 06:12:46 +00:00
|
|
|
_, _ = os.Stdout.Write([]byte("\n"))
|
2022-04-01 19:29:02 +00:00
|
|
|
|
2020-12-20 18:34:11 +00:00
|
|
|
if w.outputFile != nil {
|
2020-12-23 15:16:42 +00:00
|
|
|
if !w.json {
|
|
|
|
data = decolorizerRegex.ReplaceAll(data, []byte(""))
|
|
|
|
}
|
2021-10-30 11:19:11 +00:00
|
|
|
if _, writeErr := w.outputFile.Write(data); writeErr != nil {
|
2020-12-20 18:34:11 +00:00
|
|
|
return errors.Wrap(err, "could not write to output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-30 09:46:26 +00:00
|
|
|
// JSONLogRequest is a trace/error log request written to file
|
|
|
|
type JSONLogRequest struct {
|
|
|
|
Template string `json:"template"`
|
|
|
|
Input string `json:"input"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
Type string `json:"type"`
|
2020-12-21 06:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request writes a log the requests trace log
|
2021-10-30 09:46:26 +00:00
|
|
|
func (w *StandardWriter) Request(templatePath, input, requestType string, requestErr error) {
|
2021-10-30 09:39:38 +00:00
|
|
|
if w.traceFile == nil && w.errorFile == nil {
|
2020-12-21 06:28:33 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-30 09:46:26 +00:00
|
|
|
request := &JSONLogRequest{
|
|
|
|
Template: templatePath,
|
|
|
|
Input: input,
|
|
|
|
Type: requestType,
|
2020-12-21 06:28:33 +00:00
|
|
|
}
|
2021-10-30 10:46:07 +00:00
|
|
|
if unwrappedErr := utils.UnwrapError(requestErr); unwrappedErr != nil {
|
|
|
|
request.Error = unwrappedErr.Error()
|
2020-12-21 06:28:33 +00:00
|
|
|
} else {
|
|
|
|
request.Error = "none"
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := jsoniter.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-10-30 09:39:38 +00:00
|
|
|
|
|
|
|
if w.traceFile != nil {
|
2021-10-30 11:19:11 +00:00
|
|
|
_, _ = w.traceFile.Write(data)
|
2021-10-30 09:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if requestErr != nil && w.errorFile != nil {
|
2021-10-30 11:19:11 +00:00
|
|
|
_, _ = w.errorFile.Write(data)
|
2021-10-30 09:39:38 +00:00
|
|
|
}
|
2020-12-21 06:28:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 06:34:33 +00:00
|
|
|
// Colorizer returns the colorizer instance for writer
|
|
|
|
func (w *StandardWriter) Colorizer() aurora.Aurora {
|
|
|
|
return w.aurora
|
|
|
|
}
|
|
|
|
|
2020-12-20 18:34:11 +00:00
|
|
|
// Close closes the output writing interface
|
|
|
|
func (w *StandardWriter) Close() {
|
2020-12-21 06:28:33 +00:00
|
|
|
if w.outputFile != nil {
|
|
|
|
w.outputFile.Close()
|
|
|
|
}
|
|
|
|
if w.traceFile != nil {
|
|
|
|
w.traceFile.Close()
|
|
|
|
}
|
2021-10-30 09:39:38 +00:00
|
|
|
if w.errorFile != nil {
|
|
|
|
w.errorFile.Close()
|
|
|
|
}
|
2020-12-20 18:34:11 +00:00
|
|
|
}
|
2021-11-22 12:23:25 +00:00
|
|
|
|
|
|
|
// WriteFailure writes the failure event for template to file and/or screen.
|
|
|
|
func (w *StandardWriter) WriteFailure(event InternalEvent) error {
|
|
|
|
if !w.matcherStatus {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
templatePath, templateURL := utils.TemplatePathURL(types.ToString(event["template-path"]))
|
2022-06-03 08:02:45 +00:00
|
|
|
var templateInfo model.Info
|
|
|
|
if event["template-info"] != nil {
|
|
|
|
templateInfo = event["template-info"].(model.Info)
|
|
|
|
}
|
2021-11-22 12:23:25 +00:00
|
|
|
data := &ResultEvent{
|
|
|
|
Template: templatePath,
|
|
|
|
TemplateURL: templateURL,
|
|
|
|
TemplateID: types.ToString(event["template-id"]),
|
|
|
|
TemplatePath: types.ToString(event["template-path"]),
|
2022-06-03 08:02:45 +00:00
|
|
|
Info: templateInfo,
|
2021-11-22 12:23:25 +00:00
|
|
|
Type: types.ToString(event["type"]),
|
|
|
|
Host: types.ToString(event["host"]),
|
|
|
|
MatcherStatus: false,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
}
|
|
|
|
return w.Write(data)
|
|
|
|
}
|
2022-04-01 19:29:02 +00:00
|
|
|
func sanitizeFileName(fileName string) string {
|
|
|
|
fileName = strings.ReplaceAll(fileName, "http:", "")
|
|
|
|
fileName = strings.ReplaceAll(fileName, "https:", "")
|
|
|
|
fileName = strings.ReplaceAll(fileName, "/", "_")
|
|
|
|
fileName = strings.ReplaceAll(fileName, "\\", "_")
|
|
|
|
fileName = strings.ReplaceAll(fileName, "-", "_")
|
|
|
|
fileName = strings.ReplaceAll(fileName, ".", "_")
|
|
|
|
fileName = strings.TrimPrefix(fileName, "__")
|
|
|
|
return fileName
|
|
|
|
}
|
|
|
|
func (w *StandardWriter) WriteStoreDebugData(host, templateID, eventType string, data string) {
|
|
|
|
if w.storeResponse {
|
|
|
|
filename := sanitizeFileName(fmt.Sprintf("%s_%s", host, templateID))
|
|
|
|
subFolder := filepath.Join(w.storeResponseDir, sanitizeFileName(eventType))
|
|
|
|
if !fileutil.FolderExists(subFolder) {
|
|
|
|
_ = fileutil.CreateFolder(subFolder)
|
|
|
|
}
|
|
|
|
filename = filepath.Join(subFolder, fmt.Sprintf("%s.txt", filename))
|
2022-06-20 11:40:22 +00:00
|
|
|
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
2022-04-01 19:29:02 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, _ = f.WriteString(fmt.Sprintln(data))
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|