include request and response when matcher status is false (#3986)

* include request and response when matcher status is false

* use failed result event to write failure

---------

Co-authored-by: Collins Huff <collins.huff@Collinss-MacBook-Pro-2.local>
Co-authored-by: Tarun Koyalwar <tarun@projectdiscovery.io>
dev
seeyarh 2023-08-17 13:35:15 -04:00 committed by GitHub
parent d07c273ab4
commit 1ee108ed13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
"go.uber.org/multierr"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
@ -36,7 +37,7 @@ type Writer interface {
// Write writes the event to file and/or screen. // Write writes the event to file and/or screen.
Write(*ResultEvent) error Write(*ResultEvent) error
// WriteFailure writes the optional failure event for template to file and/or screen. // WriteFailure writes the optional failure event for template to file and/or screen.
WriteFailure(event InternalEvent) error WriteFailure(*InternalWrappedEvent) error
// Request logs a request in the trace log // Request logs a request in the trace log
Request(templateID, url, requestType string, err error) Request(templateID, url, requestType string, err error)
// WriteStoreDebugData writes the request/response debug data to file // WriteStoreDebugData writes the request/response debug data to file
@ -302,10 +303,26 @@ func (w *StandardWriter) Close() {
} }
// WriteFailure writes the failure event for template to file and/or screen. // WriteFailure writes the failure event for template to file and/or screen.
func (w *StandardWriter) WriteFailure(event InternalEvent) error { func (w *StandardWriter) WriteFailure(wrappedEvent *InternalWrappedEvent) error {
if !w.matcherStatus { if !w.matcherStatus {
return nil return nil
} }
if len(wrappedEvent.Results) > 0 {
errs := []error{}
for _, result := range wrappedEvent.Results {
result.MatcherStatus = false // just in case
if err := w.Write(result); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return multierr.Combine(errs...)
}
return nil
}
// if no results were found, manually create a failure event
event := wrappedEvent.InternalEvent
templatePath, templateURL := utils.TemplatePathURL(types.ToString(event["template-path"])) templatePath, templateURL := utils.TemplatePathURL(types.ToString(event["template-path"]))
var templateInfo model.Info var templateInfo model.Info
if event["template-info"] != nil { if event["template-info"] != nil {
@ -319,6 +336,8 @@ func (w *StandardWriter) WriteFailure(event InternalEvent) error {
Info: templateInfo, Info: templateInfo,
Type: types.ToString(event["type"]), Type: types.ToString(event["type"]),
Host: types.ToString(event["host"]), Host: types.ToString(event["host"]),
Request: types.ToString(event["request"]),
Response: types.ToString(event["response"]),
MatcherStatus: false, MatcherStatus: false,
Timestamp: time.Now(), Timestamp: time.Now(),
} }

View File

@ -74,7 +74,7 @@ func (e *Executer) Execute(input *contextargs.Context) (bool, error) {
var lastMatcherEvent *output.InternalWrappedEvent var lastMatcherEvent *output.InternalWrappedEvent
writeFailureCallback := func(event *output.InternalWrappedEvent, matcherStatus bool) { writeFailureCallback := func(event *output.InternalWrappedEvent, matcherStatus bool) {
if !results.Load() && matcherStatus { if !results.Load() && matcherStatus {
if err := e.options.Output.WriteFailure(event.InternalEvent); err != nil { if err := e.options.Output.WriteFailure(event); err != nil {
gologger.Warning().Msgf("Could not write failure event to output: %s\n", err) gologger.Warning().Msgf("Could not write failure event to output: %s\n", err)
} }
results.CompareAndSwap(false, true) results.CompareAndSwap(false, true)

View File

@ -250,7 +250,7 @@ func (e *ClusterExecuter) Execute(input *contextargs.Context) (bool, error) {
event.InternalEvent["template-info"] = operator.templateInfo event.InternalEvent["template-info"] = operator.templateInfo
if result == nil && !matched { if result == nil && !matched {
if err := e.options.Output.WriteFailure(event.InternalEvent); err != nil { if err := e.options.Output.WriteFailure(event); err != nil {
gologger.Warning().Msgf("Could not write failure event to output: %s\n", err) gologger.Warning().Msgf("Could not write failure event to output: %s\n", err)
} }
continue continue

View File

@ -136,7 +136,7 @@ func (m *MockOutputWriter) Request(templateID, url, requestType string, err erro
} }
// WriteFailure writes the event to file and/or screen. // WriteFailure writes the event to file and/or screen.
func (m *MockOutputWriter) WriteFailure(result output.InternalEvent) error { func (m *MockOutputWriter) WriteFailure(*output.InternalWrappedEvent) error {
return nil return nil
} }
func (m *MockOutputWriter) WriteStoreDebugData(host, templateID, eventType string, data string) { func (m *MockOutputWriter) WriteStoreDebugData(host, templateID, eventType string, data string) {