diff --git a/pkg/output/output.go b/pkg/output/output.go index dc298d0699..e37f9decda 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -354,6 +354,7 @@ func (w *StandardWriter) WriteFailure(wrappedEvent *InternalWrappedEvent) error Timestamp: time.Now(), //FIXME: this is workaround to encode the template when no results were found TemplateEncoded: w.encodeTemplate(types.ToString(event["template-path"])), + Error: types.ToString(event["error"]), } return w.Write(data) } diff --git a/pkg/protocols/code/code.go b/pkg/protocols/code/code.go index 5977702e5e..bb8c06e549 100644 --- a/pkg/protocols/code/code.go +++ b/pkg/protocols/code/code.go @@ -255,6 +255,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Timestamp: time.Now(), MatcherStatus: true, TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/dns/operators.go b/pkg/protocols/dns/operators.go index 7aeafe5837..7b8467646b 100644 --- a/pkg/protocols/dns/operators.go +++ b/pkg/protocols/dns/operators.go @@ -123,6 +123,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Request: types.ToString(wrapped.InternalEvent["request"]), Response: types.ToString(wrapped.InternalEvent["raw"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/file/operators.go b/pkg/protocols/file/operators.go index 347b5846c5..aee2abdfda 100644 --- a/pkg/protocols/file/operators.go +++ b/pkg/protocols/file/operators.go @@ -108,6 +108,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Response: types.ToString(wrapped.InternalEvent["raw"]), Timestamp: time.Now(), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/headless/operators.go b/pkg/protocols/headless/operators.go index c1bc0f2164..fd50ddaddb 100644 --- a/pkg/protocols/headless/operators.go +++ b/pkg/protocols/headless/operators.go @@ -139,6 +139,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Request: types.ToString(wrapped.InternalEvent["request"]), Response: types.ToString(wrapped.InternalEvent["data"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/http/operators.go b/pkg/protocols/http/operators.go index 475bdf81b4..1e0f680399 100644 --- a/pkg/protocols/http/operators.go +++ b/pkg/protocols/http/operators.go @@ -165,6 +165,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Response: request.truncateResponse(wrapped.InternalEvent["response"]), CURLCommand: types.ToString(wrapped.InternalEvent["curl-command"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/http/request.go b/pkg/protocols/http/request.go index aac2bad71d..2328c8f30c 100644 --- a/pkg/protocols/http/request.go +++ b/pkg/protocols/http/request.go @@ -655,26 +655,25 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ request.options.Output.Request(request.options.TemplatePath, formedURL, request.Type().String(), err) request.options.Progress.IncrementErrorsBy(1) - // If we have interactsh markers and request times out, still send + // In case of interactsh markers and request times out, still send // a callback event so in case we receive an interaction, correlation is possible. - if hasInteractMatchers { - outputEvent := request.responseToDSLMap(&http.Response{}, input.MetaInput.Input, formedURL, tostring.UnsafeToString(dumpedRequest), "", "", "", 0, generatedRequest.meta) - if i := strings.LastIndex(hostname, ":"); i != -1 { - hostname = hostname[:i] - } + // Also, to log failed use-cases. + outputEvent := request.responseToDSLMap(&http.Response{}, input.MetaInput.Input, formedURL, tostring.UnsafeToString(dumpedRequest), "", "", "", 0, generatedRequest.meta) + if i := strings.LastIndex(hostname, ":"); i != -1 { + hostname = hostname[:i] + } - if input.MetaInput.CustomIP != "" { - outputEvent["ip"] = input.MetaInput.CustomIP - } else { - outputEvent["ip"] = httpclientpool.Dialer.GetDialedIP(hostname) - } + if input.MetaInput.CustomIP != "" { + outputEvent["ip"] = input.MetaInput.CustomIP + } else { + outputEvent["ip"] = httpclientpool.Dialer.GetDialedIP(hostname) + } - event := &output.InternalWrappedEvent{InternalEvent: outputEvent} - if request.CompiledOperators != nil { - event.InternalEvent = outputEvent - } - callback(event) + event := &output.InternalWrappedEvent{InternalEvent: outputEvent} + if request.CompiledOperators != nil { + event.InternalEvent = outputEvent } + callback(event) return err } defer func() { diff --git a/pkg/protocols/javascript/js.go b/pkg/protocols/javascript/js.go index 3736bf034c..d6c8917970 100644 --- a/pkg/protocols/javascript/js.go +++ b/pkg/protocols/javascript/js.go @@ -635,6 +635,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Response: types.ToString(wrapped.InternalEvent["response"]), IP: types.ToString(wrapped.InternalEvent["ip"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/network/operators.go b/pkg/protocols/network/operators.go index 74bee27980..8988496408 100644 --- a/pkg/protocols/network/operators.go +++ b/pkg/protocols/network/operators.go @@ -109,6 +109,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Request: types.ToString(wrapped.InternalEvent["request"]), Response: types.ToString(wrapped.InternalEvent["data"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/offlinehttp/operators.go b/pkg/protocols/offlinehttp/operators.go index 0bd7a30ff9..0d906ff514 100644 --- a/pkg/protocols/offlinehttp/operators.go +++ b/pkg/protocols/offlinehttp/operators.go @@ -152,6 +152,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Request: types.ToString(wrapped.InternalEvent["request"]), Response: types.ToString(wrapped.InternalEvent["raw"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/ssl/ssl.go b/pkg/protocols/ssl/ssl.go index 63b7cfe77c..4fe556b3b9 100644 --- a/pkg/protocols/ssl/ssl.go +++ b/pkg/protocols/ssl/ssl.go @@ -376,6 +376,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent MatcherStatus: true, IP: types.ToString(wrapped.InternalEvent["ip"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/websocket/websocket.go b/pkg/protocols/websocket/websocket.go index fdaebad356..4cedbc765e 100644 --- a/pkg/protocols/websocket/websocket.go +++ b/pkg/protocols/websocket/websocket.go @@ -410,6 +410,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Request: types.ToString(wrapped.InternalEvent["request"]), Response: types.ToString(wrapped.InternalEvent["response"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/protocols/whois/whois.go b/pkg/protocols/whois/whois.go index a7c02a9740..ce0bafe56c 100644 --- a/pkg/protocols/whois/whois.go +++ b/pkg/protocols/whois/whois.go @@ -186,6 +186,7 @@ func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent Request: types.ToString(wrapped.InternalEvent["request"]), Response: types.ToString(wrapped.InternalEvent["response"]), TemplateEncoded: request.options.EncodeTemplate(), + Error: types.ToString(wrapped.InternalEvent["error"]), } return data } diff --git a/pkg/scan/scan_context.go b/pkg/scan/scan_context.go index c75a2b464b..a7624b8ab9 100644 --- a/pkg/scan/scan_context.go +++ b/pkg/scan/scan_context.go @@ -23,12 +23,7 @@ func NewScanContext(input *contextargs.Context) *ScanContext { } func (s *ScanContext) GenerateResult() []*output.ResultEvent { - errorMessage := joinErrors(s.errors) - results := aggregateResults(s.events) - for _, result := range results { - result.Error = errorMessage - } - return results + return aggregateResults(s.events) } func aggregateResults(events []*output.InternalWrappedEvent) []*output.ResultEvent { @@ -59,5 +54,15 @@ func (s *ScanContext) LogError(err error) error { s.OnError(err) } s.errors = append(s.errors, err) + + errorMessage := joinErrors(s.errors) + results := aggregateResults(s.events) + for _, result := range results { + result.Error = errorMessage + } + for _, e := range s.events { + e.InternalEvent["error"] = errorMessage + } + return err } diff --git a/pkg/templates/cluster.go b/pkg/templates/cluster.go index fda477f083..e8eb435a8d 100644 --- a/pkg/templates/cluster.go +++ b/pkg/templates/cluster.go @@ -303,6 +303,10 @@ func (e *ClusterExecuter) ExecuteWithResults(ctx *scan.ScanContext) ([]*output.R } } }) + if err != nil { + ctx.LogError(err) + } + if err != nil && e.options.HostErrorsCache != nil { e.options.HostErrorsCache.MarkFailed(ctx.Input.MetaInput.Input, err) } diff --git a/pkg/testutils/testutils.go b/pkg/testutils/testutils.go index 3b916e5166..f212f935f7 100644 --- a/pkg/testutils/testutils.go +++ b/pkg/testutils/testutils.go @@ -180,6 +180,7 @@ func (m *MockOutputWriter) WriteFailure(wrappedEvent *output.InternalWrappedEven Timestamp: time.Now(), //FIXME: this is workaround to encode the template when no results were found TemplateEncoded: m.encodeTemplate(types.ToString(event["template-path"])), + Error: types.ToString(event["error"]), } return m.Write(data) } diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index a3d4a3b08a..45f5058779 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -156,10 +156,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { // ExecuteWithResults executes the protocol requests and returns results instead of writing them. func (e *TemplateExecuter) ExecuteWithResults(ctx *scan.ScanContext) ([]*output.ResultEvent, error) { - // scanCtx := scan.NewScanContext(input) err := e.engine.ExecuteWithResults(ctx) - if err != nil && !e.options.Options.MatcherStatus { - return nil, err - } + ctx.LogError(err) return ctx.GenerateResult(), err } diff --git a/pkg/tmplexec/flow/flow_executor.go b/pkg/tmplexec/flow/flow_executor.go index 4aba797850..690a4be991 100644 --- a/pkg/tmplexec/flow/flow_executor.go +++ b/pkg/tmplexec/flow/flow_executor.go @@ -184,10 +184,12 @@ func (f *FlowExecutor) ExecuteWithResults(ctx *scan.ScanContext) error { // pass flow and execute the js vm and handle errors value, err := f.jsVM.RunProgram(f.program) if err != nil { + ctx.LogError(err) return errorutil.NewWithErr(err).Msgf("failed to execute flow\n%v\n", f.options.Flow) } runtimeErr := f.GetRuntimeErrors() if runtimeErr != nil { + ctx.LogError(runtimeErr) return errorutil.NewWithErr(runtimeErr).Msgf("got following errors while executing flow") } // this is where final result is generated/created diff --git a/pkg/tmplexec/generic/exec.go b/pkg/tmplexec/generic/exec.go index 606331e047..c5d25795cf 100644 --- a/pkg/tmplexec/generic/exec.go +++ b/pkg/tmplexec/generic/exec.go @@ -75,6 +75,7 @@ func (g *Generic) ExecuteWithResults(ctx *scan.ScanContext) error { ctx.LogEvent(event) }) if err != nil { + ctx.LogError(err) if g.options.HostErrorsCache != nil { g.options.HostErrorsCache.MarkFailed(ctx.Input.MetaInput.ID(), err) } diff --git a/pkg/tmplexec/multiproto/multi.go b/pkg/tmplexec/multiproto/multi.go index dec575d2e8..021c83534d 100644 --- a/pkg/tmplexec/multiproto/multi.go +++ b/pkg/tmplexec/multiproto/multi.go @@ -93,6 +93,7 @@ func (m *MultiProtocol) ExecuteWithResults(ctx *scan.ScanContext) error { err := req.ExecuteWithResults(ctx.Input, output.InternalEvent(values), nil, multiProtoCallback) // if error skip execution of next protocols if err != nil { + ctx.LogError(err) return err } }