From 3b523b1d87a737deba6444a89ed2cdf061df9368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 26 Aug 2024 12:23:50 +0300 Subject: [PATCH 01/16] fail if OnResult callback is not called --- pkg/tmplexec/exec.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index 3d09f5e7a0..8b447b930c 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -10,6 +10,7 @@ import ( "github.com/dop251/goja" "github.com/projectdiscovery/gologger" "github.com/projectdiscovery/nuclei/v3/pkg/js/compiler" + "github.com/projectdiscovery/nuclei/v3/pkg/operators" "github.com/projectdiscovery/nuclei/v3/pkg/operators/common/dsl" "github.com/projectdiscovery/nuclei/v3/pkg/output" "github.com/projectdiscovery/nuclei/v3/pkg/protocols" @@ -126,6 +127,8 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { executed := &atomic.Bool{} // matched in this case means something was exported / written to output matched := &atomic.Bool{} + // callbackCalled tracks if the callback was called or not + callbackCalled := &atomic.Bool{} defer func() { // it is essential to remove template context of `Scan i.e template x input pair` // since it is of no use after scan is completed (regardless of success or failure) @@ -143,6 +146,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { } ctx.OnResult = func(event *output.InternalWrappedEvent) { + callbackCalled.Store(true) if event == nil { // something went wrong return @@ -202,6 +206,35 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { if lastMatcherEvent != nil { writeFailureCallback(lastMatcherEvent, e.options.Options.MatcherStatus) } + + //TODO: this is a hacky way to handle the case where the callback is not called and matcher-status is true. + // This is a workaround and needs to be refactored. + // Check if callback was never called and matcher-status is true + if !callbackCalled.Load() && e.options.Options.MatcherStatus { + fakeEvent := &output.InternalWrappedEvent{ + Results: []*output.ResultEvent{ + { + TemplateID: e.options.TemplateID, + Info: e.options.TemplateInfo, + Type: e.getTemplateType(), + Host: ctx.Input.MetaInput.Input, + Error: func(err error) string { + if err == nil { + return "" + } + return err.Error() + }(errx), + }, + }, + OperatorsResult: &operators.Result{ + Matched: false, + }, + } + if err := e.options.Output.WriteFailure(fakeEvent); err != nil { + gologger.Warning().Msgf("Could not write failure event to output: %s\n", err) + } + } + return executed.Load() || matched.Load(), errx } From e92f6ce2c75517b5f12e9e1f4ab63a340317045d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 26 Aug 2024 16:59:59 +0300 Subject: [PATCH 02/16] generate error message from error logs --- pkg/scan/scan_context.go | 9 ++++++++- pkg/tmplexec/exec.go | 7 +------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/scan/scan_context.go b/pkg/scan/scan_context.go index b8f59ac7d4..77c1bdc24f 100644 --- a/pkg/scan/scan_context.go +++ b/pkg/scan/scan_context.go @@ -52,6 +52,13 @@ func (s *ScanContext) Context() context.Context { return s.ctx } +func (s *ScanContext) GenerateErrorMessage() string { + s.m.Lock() + defer s.m.Unlock() + + return joinErrors(s.errors) +} + // GenerateResult returns final results slice from all events func (s *ScanContext) GenerateResult() []*output.ResultEvent { s.m.Lock() @@ -96,7 +103,7 @@ func (s *ScanContext) LogError(err error) { } s.errors = append(s.errors, err) - errorMessage := joinErrors(s.errors) + errorMessage := s.GenerateErrorMessage() for _, result := range s.results { result.Error = errorMessage diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index 8b447b930c..8836cf2221 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -218,12 +218,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { Info: e.options.TemplateInfo, Type: e.getTemplateType(), Host: ctx.Input.MetaInput.Input, - Error: func(err error) string { - if err == nil { - return "" - } - return err.Error() - }(errx), + Error: ctx.GenerateErrorMessage(), }, }, OperatorsResult: &operators.Result{ From 921daa67c61ddb5fe0d2a2c80b4d9b94b52b9c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Mon, 26 Aug 2024 18:03:10 +0300 Subject: [PATCH 03/16] try..parse.. --- pkg/scan/scan_context.go | 3 --- pkg/tmplexec/exec.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/scan/scan_context.go b/pkg/scan/scan_context.go index 77c1bdc24f..45456ddcac 100644 --- a/pkg/scan/scan_context.go +++ b/pkg/scan/scan_context.go @@ -53,9 +53,6 @@ func (s *ScanContext) Context() context.Context { } func (s *ScanContext) GenerateErrorMessage() string { - s.m.Lock() - defer s.m.Unlock() - return joinErrors(s.errors) } diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index 8836cf2221..6c0bf51f0c 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -20,6 +20,8 @@ import ( "github.com/projectdiscovery/nuclei/v3/pkg/tmplexec/flow" "github.com/projectdiscovery/nuclei/v3/pkg/tmplexec/generic" "github.com/projectdiscovery/nuclei/v3/pkg/tmplexec/multiproto" + "github.com/projectdiscovery/nuclei/v3/pkg/types/nucleierr" + "github.com/projectdiscovery/utils/errkit" ) // TemplateExecutor is an executor for a template @@ -218,7 +220,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { Info: e.options.TemplateInfo, Type: e.getTemplateType(), Host: ctx.Input.MetaInput.Input, - Error: ctx.GenerateErrorMessage(), + Error: tryParseCause(fmt.Errorf(ctx.GenerateErrorMessage())), }, }, OperatorsResult: &operators.Result{ @@ -233,6 +235,36 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { return executed.Load() || matched.Load(), errx } +// tryParseCause tries to parse the cause of given error +// this is legacy support due to use of errorutil in existing libraries +// but this should not be required once all libraries are updated +func tryParseCause(err error) string { + errStr := "" + errX := errkit.FromError(err) + if errX != nil { + var errCause error + + if len(errX.Errors()) > 1 { + errCause = errX.Errors()[0] + } + if errCause == nil { + errCause = errX + } + + msg := errCause.Error() + parts := strings.Split(msg, ":") + if len(parts) == 1 { + return "" + } + errCause = errkit.New("%s", strings.TrimSpace(parts[len(parts)-1])) + errKind := errkit.GetErrorKind(err, nucleierr.ErrTemplateLogic).String() + errStr = errCause.Error() + errStr = strings.TrimSpace(strings.Replace(errStr, "errKind="+errKind, "", -1)) + } + + return errStr +} + // ExecuteWithResults executes the protocol requests and returns results instead of writing them. func (e *TemplateExecuter) ExecuteWithResults(ctx *scan.ScanContext) ([]*output.ResultEvent, error) { var errx error From cca0fb0203807762a059233d7a18de8abfbb219d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 09:57:22 +0300 Subject: [PATCH 04/16] fix lint --- pkg/tmplexec/exec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index 6c0bf51f0c..79b9d96ea3 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -220,7 +220,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { Info: e.options.TemplateInfo, Type: e.getTemplateType(), Host: ctx.Input.MetaInput.Input, - Error: tryParseCause(fmt.Errorf(ctx.GenerateErrorMessage())), + Error: tryParseCause(fmt.Errorf("%s", ctx.GenerateErrorMessage())), }, }, OperatorsResult: &operators.Result{ From 2b31aae27f15bb00fdd1ab810b2ac86c24152f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 11:09:20 +0300 Subject: [PATCH 05/16] add error message to last matcher event --- pkg/tmplexec/exec.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index 79b9d96ea3..d4781042fc 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -206,6 +206,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { } if lastMatcherEvent != nil { + lastMatcherEvent.InternalEvent["error"] = tryParseCause(fmt.Errorf("%s", ctx.GenerateErrorMessage())) writeFailureCallback(lastMatcherEvent, e.options.Options.MatcherStatus) } @@ -227,9 +228,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { Matched: false, }, } - if err := e.options.Output.WriteFailure(fakeEvent); err != nil { - gologger.Warning().Msgf("Could not write failure event to output: %s\n", err) - } + writeFailureCallback(fakeEvent, e.options.Options.MatcherStatus) } return executed.Load() || matched.Load(), errx From 3d7dfc560061f4b9ca6f8873a4ca3fedd9c6e954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 12:44:21 +0300 Subject: [PATCH 06/16] fix network protocol error logging --- pkg/protocols/network/request.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/protocols/network/request.go b/pkg/protocols/network/request.go index 90390e53c3..f89d7acf4e 100644 --- a/pkg/protocols/network/request.go +++ b/pkg/protocols/network/request.go @@ -155,14 +155,13 @@ func (request *Request) executeOnTarget(input *contextargs.Context, visited maps } visited.Set(actualAddress, struct{}{}) - if err := request.executeAddress(variables, actualAddress, address, input, kv.tls, previous, callback); err != nil { + if err = request.executeAddress(variables, actualAddress, address, input, kv.tls, previous, callback); err != nil { outputEvent := request.responseToDSLMap("", "", "", address, "") callback(&output.InternalWrappedEvent{InternalEvent: outputEvent}) - gologger.Warning().Msgf("[%v] Could not make network request for (%s) : %s\n", request.options.TemplateID, actualAddress, err) continue } } - return nil + return err } // executeAddress executes the request for an address From a6f5089ad19d244eb58c649d47f8b40187e3d7f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 12:44:43 +0300 Subject: [PATCH 07/16] log returned log from ExecuteWithResults --- pkg/tmplexec/exec.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index d4781042fc..37c95731b9 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -204,6 +204,7 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { } else { errx = e.engine.ExecuteWithResults(ctx) } + ctx.LogError(errx) if lastMatcherEvent != nil { lastMatcherEvent.InternalEvent["error"] = tryParseCause(fmt.Errorf("%s", ctx.GenerateErrorMessage())) From 21ffe96467cd817bc6f09cbc5c9fd809bb09352e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 12:52:12 +0300 Subject: [PATCH 08/16] add back specific logging --- pkg/protocols/network/request.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/protocols/network/request.go b/pkg/protocols/network/request.go index f89d7acf4e..5fa8609d51 100644 --- a/pkg/protocols/network/request.go +++ b/pkg/protocols/network/request.go @@ -158,6 +158,7 @@ func (request *Request) executeOnTarget(input *contextargs.Context, visited maps if err = request.executeAddress(variables, actualAddress, address, input, kv.tls, previous, callback); err != nil { outputEvent := request.responseToDSLMap("", "", "", address, "") callback(&output.InternalWrappedEvent{InternalEvent: outputEvent}) + gologger.Warning().Msgf("[%v] Could not make network request for (%s) : %s\n", request.options.TemplateID, actualAddress, err) continue } } From 69ef4a2180585778aa035eb3d7439c66eeb94a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 13:11:36 +0300 Subject: [PATCH 09/16] clean up the msg --- pkg/tmplexec/exec.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index 37c95731b9..57b6ca8b70 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -252,9 +252,10 @@ func tryParseCause(err error) string { } msg := errCause.Error() + msg = strings.Trim(msg, "{} ") parts := strings.Split(msg, ":") if len(parts) == 1 { - return "" + return msg } errCause = errkit.New("%s", strings.TrimSpace(parts[len(parts)-1])) errKind := errkit.GetErrorKind(err, nucleierr.ErrTemplateLogic).String() From 7d846a3a92ea50d54edd6fed852f92332359924b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 13:18:56 +0300 Subject: [PATCH 10/16] minor --- pkg/tmplexec/exec.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index 57b6ca8b70..4ca9badf70 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -251,13 +251,9 @@ func tryParseCause(err error) string { errCause = errX } - msg := errCause.Error() - msg = strings.Trim(msg, "{} ") + msg := strings.Trim(errCause.Error(), "{} ") parts := strings.Split(msg, ":") - if len(parts) == 1 { - return msg - } - errCause = errkit.New("%s", strings.TrimSpace(parts[len(parts)-1])) + errCause = errkit.New("%s", parts[len(parts)-1]) errKind := errkit.GetErrorKind(err, nucleierr.ErrTemplateLogic).String() errStr = errCause.Error() errStr = strings.TrimSpace(strings.Replace(errStr, "errKind="+errKind, "", -1)) From e2bf781e5f86bc8f41cfad5098fd90cd6bd1dba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 13:29:43 +0300 Subject: [PATCH 11/16] init integration test for -ms --- cmd/integration-test/integration-test.go | 1 + cmd/integration-test/matcher-status.go | 1 + 2 files changed, 2 insertions(+) create mode 100644 cmd/integration-test/matcher-status.go diff --git a/cmd/integration-test/integration-test.go b/cmd/integration-test/integration-test.go index 180efcfc6e..84ec6790f8 100644 --- a/cmd/integration-test/integration-test.go +++ b/cmd/integration-test/integration-test.go @@ -55,6 +55,7 @@ var ( "dsl": dslTestcases, "flow": flowTestcases, "javascript": jsTestcases, + "matcher-status": matcherStatusTestcases, } // flakyTests are run with a retry count of 3 flakyTests = map[string]bool{ diff --git a/cmd/integration-test/matcher-status.go b/cmd/integration-test/matcher-status.go new file mode 100644 index 0000000000..06ab7d0f9a --- /dev/null +++ b/cmd/integration-test/matcher-status.go @@ -0,0 +1 @@ +package main From 1fc1f77d6fe7b0c8edd7ad98f7788b5f046b0a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 13:30:36 +0300 Subject: [PATCH 12/16] add tests for http,network,js,ws protocols --- cmd/integration-test/matcher-status.go | 100 +++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/cmd/integration-test/matcher-status.go b/cmd/integration-test/matcher-status.go index 06ab7d0f9a..76cb376f8e 100644 --- a/cmd/integration-test/matcher-status.go +++ b/cmd/integration-test/matcher-status.go @@ -1 +1,101 @@ package main + +import ( + "encoding/json" + "fmt" + + "github.com/projectdiscovery/nuclei/v3/pkg/output" + "github.com/projectdiscovery/nuclei/v3/pkg/testutils" +) + +var matcherStatusTestcases = []TestCaseInfo{ + {Path: "protocols/http/get.yaml", TestCase: &httpNoAccess{}}, + {Path: "protocols/network/net-https.yaml", TestCase: &networkNoAccess{}}, + {Path: "protocols/headless/headless-basic.yaml", TestCase: &headlessNoAccess{}}, + {Path: "protocols/javascript/net-https.yaml", TestCase: &javascriptNoAccess{}}, + {Path: "protocols/websocket/basic.yaml", TestCase: &websocketNoAccess{}}, +} + +type httpNoAccess struct{} + +func (h *httpNoAccess) Execute(filePath string) error { + results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "trust_me_bro.real", debug, "-ms", "-j") + if err != nil { + return err + } + event := &output.ResultEvent{} + json.Unmarshal([]byte(results[0]), event) + + if event.Error != "no address found for host" { + return fmt.Errorf("unexpected result: expecting \"no address found for host\" error but got none") + } + return nil +} + +type networkNoAccess struct{} + +// Execute executes a test case and returns an error if occurred +func (h *networkNoAccess) Execute(filePath string) error { + results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "trust_me_bro.real", debug, "-ms", "-j") + if err != nil { + return err + } + event := &output.ResultEvent{} + json.Unmarshal([]byte(results[0]), event) + + if event.Error != "no address found for host" { + return fmt.Errorf("unexpected result: expecting \"no address found for host\" error but got \"%s\"", event.Error) + } + return nil +} + +type headlessNoAccess struct{} + +// Execute executes a test case and returns an error if occurred +func (h *headlessNoAccess) Execute(filePath string) error { + results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "trust_me_bro.real", debug, "-headless", "-ms", "-j") + if err != nil { + return err + } + event := &output.ResultEvent{} + json.Unmarshal([]byte(results[0]), event) + + if event.Error == "" { + return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error) + } + return nil +} + +type javascriptNoAccess struct{} + +// Execute executes a test case and returns an error if occurred +func (h *javascriptNoAccess) Execute(filePath string) error { + results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "trust_me_bro.real", debug, "-ms", "-j") + if err != nil { + return err + } + event := &output.ResultEvent{} + json.Unmarshal([]byte(results[0]), event) + + if event.Error == "" { + return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error) + } + return nil +} + +type websocketNoAccess struct{} + +// Execute executes a test case and returns an error if occurred +func (h *websocketNoAccess) Execute(filePath string) error { + results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "ws://trust_me_bro.real", debug, "-ms", "-j") + if err != nil { + return err + } + event := &output.ResultEvent{} + json.Unmarshal([]byte(results[0]), event) + + if event.Error == "" { + return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error) + } + return nil +} From b2872096c39477c7433189daa37657eeca804aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 13:33:29 +0300 Subject: [PATCH 13/16] fix lint --- cmd/integration-test/matcher-status.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/integration-test/matcher-status.go b/cmd/integration-test/matcher-status.go index 76cb376f8e..8fb5383edc 100644 --- a/cmd/integration-test/matcher-status.go +++ b/cmd/integration-test/matcher-status.go @@ -24,7 +24,7 @@ func (h *httpNoAccess) Execute(filePath string) error { return err } event := &output.ResultEvent{} - json.Unmarshal([]byte(results[0]), event) + _ = json.Unmarshal([]byte(results[0]), event) if event.Error != "no address found for host" { return fmt.Errorf("unexpected result: expecting \"no address found for host\" error but got none") @@ -41,7 +41,7 @@ func (h *networkNoAccess) Execute(filePath string) error { return err } event := &output.ResultEvent{} - json.Unmarshal([]byte(results[0]), event) + _ = json.Unmarshal([]byte(results[0]), event) if event.Error != "no address found for host" { return fmt.Errorf("unexpected result: expecting \"no address found for host\" error but got \"%s\"", event.Error) @@ -58,7 +58,7 @@ func (h *headlessNoAccess) Execute(filePath string) error { return err } event := &output.ResultEvent{} - json.Unmarshal([]byte(results[0]), event) + _ = json.Unmarshal([]byte(results[0]), event) if event.Error == "" { return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error) @@ -75,7 +75,7 @@ func (h *javascriptNoAccess) Execute(filePath string) error { return err } event := &output.ResultEvent{} - json.Unmarshal([]byte(results[0]), event) + _ = json.Unmarshal([]byte(results[0]), event) if event.Error == "" { return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error) @@ -92,7 +92,7 @@ func (h *websocketNoAccess) Execute(filePath string) error { return err } event := &output.ResultEvent{} - json.Unmarshal([]byte(results[0]), event) + _ = json.Unmarshal([]byte(results[0]), event) if event.Error == "" { return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error) From ec2614a5df58f62a09eb2218acda136bfb3324f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Tue, 27 Aug 2024 13:43:47 +0300 Subject: [PATCH 14/16] fix network test --- pkg/protocols/network/request_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/protocols/network/request_test.go b/pkg/protocols/network/request_test.go index 1945888e9b..7ff0f4882b 100644 --- a/pkg/protocols/network/request_test.go +++ b/pkg/protocols/network/request_test.go @@ -86,7 +86,7 @@ func TestNetworkExecuteWithResults(t *testing.T) { err := request.ExecuteWithResults(ctxArgs, metadata, previous, func(event *output.InternalWrappedEvent) { finalEvent = event }) - require.Nil(t, err, "could not execute network request") + require.NotNil(t, err, "could not execute network request") }) require.Nil(t, finalEvent.Results, "could not get event output from request") From 5c8ad8c5e2380d58c482abdd87eb78a77c500bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Wed, 28 Aug 2024 12:57:56 +0300 Subject: [PATCH 15/16] return err for dns protocol --- pkg/protocols/dns/request.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/protocols/dns/request.go b/pkg/protocols/dns/request.go index a16c2af88b..9457845270 100644 --- a/pkg/protocols/dns/request.go +++ b/pkg/protocols/dns/request.go @@ -106,7 +106,7 @@ func (request *Request) ExecuteWithResults(input *contextargs.Context, metadata, } func (request *Request) execute(input *contextargs.Context, domain string, metadata, previous output.InternalEvent, vars map[string]interface{}, callback protocols.OutputEventCallback) error { - + var err error if vardump.EnableVarDump { gologger.Debug().Msgf("DNS Protocol request variables: \n%s\n", vardump.DumpVariables(vars)) } @@ -199,7 +199,7 @@ func (request *Request) execute(input *contextargs.Context, domain string, metad } callback(event) - return nil + return err } func (request *Request) parseDNSInput(host string) (string, error) { From ff94581b1e83f67969e4d3f902cc0d0318870748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Wed, 28 Aug 2024 12:59:51 +0300 Subject: [PATCH 16/16] add integration test for dns protocol --- cmd/integration-test/matcher-status.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cmd/integration-test/matcher-status.go b/cmd/integration-test/matcher-status.go index 8fb5383edc..b88763720a 100644 --- a/cmd/integration-test/matcher-status.go +++ b/cmd/integration-test/matcher-status.go @@ -14,6 +14,7 @@ var matcherStatusTestcases = []TestCaseInfo{ {Path: "protocols/headless/headless-basic.yaml", TestCase: &headlessNoAccess{}}, {Path: "protocols/javascript/net-https.yaml", TestCase: &javascriptNoAccess{}}, {Path: "protocols/websocket/basic.yaml", TestCase: &websocketNoAccess{}}, + {Path: "protocols/dns/a.yaml", TestCase: &dnsNoAccess{}}, } type httpNoAccess struct{} @@ -99,3 +100,20 @@ func (h *websocketNoAccess) Execute(filePath string) error { } return nil } + +type dnsNoAccess struct{} + +// Execute executes a test case and returns an error if occurred +func (h *dnsNoAccess) Execute(filePath string) error { + results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "trust_me_bro.real", debug, "-ms", "-j") + if err != nil { + return err + } + event := &output.ResultEvent{} + _ = json.Unmarshal([]byte(results[0]), event) + + if event.Error == "" { + return fmt.Errorf("unexpected result: expecting an error but got \"%s\"", event.Error) + } + return nil +}