Path: blob/dev/pkg/protocols/network/operators_test.go
2070 views
package network12import (3"testing"45"github.com/stretchr/testify/require"67"github.com/projectdiscovery/nuclei/v3/pkg/model"8"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"9"github.com/projectdiscovery/nuclei/v3/pkg/operators"10"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"11"github.com/projectdiscovery/nuclei/v3/pkg/operators/matchers"12"github.com/projectdiscovery/nuclei/v3/pkg/output"13"github.com/projectdiscovery/nuclei/v3/pkg/testutils"14)1516func TestResponseToDSLMap(t *testing.T) {17options := testutils.DefaultOptions1819testutils.Init(options)20templateID := "testing-network"21request := &Request{22ID: templateID,23Address: []string{"{{Hostname}}"},24ReadSize: 1024,25Inputs: []*Input{{Data: "test-data\r\n"}},26}27executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{28ID: templateID,29Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},30})31err := request.Compile(executerOpts)32require.Nil(t, err, "could not compile network request")3334req := "test-data\r\n"35resp := "resp-data\r\n"36event := request.responseToDSLMap(req, resp, "test", "one.one.one.one", "one.one.one.one")37require.Len(t, event, 9, "could not get correct number of items in dsl map")38require.Equal(t, resp, event["data"], "could not get correct resp")39}4041func TestNetworkOperatorMatch(t *testing.T) {42options := testutils.DefaultOptions4344testutils.Init(options)45templateID := "testing-network"46request := &Request{47ID: templateID,48Address: []string{"{{Hostname}}"},49ReadSize: 1024,50Inputs: []*Input{{Data: "test-data\r\n"}},51}52executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{53ID: templateID,54Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},55})56err := request.Compile(executerOpts)57require.Nil(t, err, "could not compile network request")5859req := "test-data\r\n"60resp := "resp-data\r\nSTAT \r\n"61event := request.responseToDSLMap(req, resp, "one.one.one.one", "one.one.one.one", "test")6263t.Run("valid", func(t *testing.T) {64matcher := &matchers.Matcher{65Part: "body",66Type: matchers.MatcherTypeHolder{MatcherType: matchers.WordsMatcher},67Words: []string{"STAT "},68}69err = matcher.CompileMatchers()70require.Nil(t, err, "could not compile matcher")7172isMatched, matched := request.Match(event, matcher)73require.True(t, isMatched, "could not match valid response")74require.Equal(t, matcher.Words, matched)75})7677t.Run("negative", func(t *testing.T) {78matcher := &matchers.Matcher{79Part: "data",80Type: matchers.MatcherTypeHolder{MatcherType: matchers.WordsMatcher},81Negative: true,82Words: []string{"random"},83}84err := matcher.CompileMatchers()85require.Nil(t, err, "could not compile negative matcher")8687isMatched, matched := request.Match(event, matcher)88require.True(t, isMatched, "could not match valid negative response matcher")89require.Equal(t, []string{}, matched)90})9192t.Run("invalid", func(t *testing.T) {93matcher := &matchers.Matcher{94Part: "data",95Type: matchers.MatcherTypeHolder{MatcherType: matchers.WordsMatcher},96Words: []string{"random"},97}98err := matcher.CompileMatchers()99require.Nil(t, err, "could not compile matcher")100101isMatched, matched := request.Match(event, matcher)102require.False(t, isMatched, "could match invalid response matcher")103require.Equal(t, []string{}, matched)104})105106t.Run("caseInsensitive", func(t *testing.T) {107matcher := &matchers.Matcher{108Part: "body",109Type: matchers.MatcherTypeHolder{MatcherType: matchers.WordsMatcher},110Words: []string{"rESp-DAta"},111CaseInsensitive: true,112}113err = matcher.CompileMatchers()114require.Nil(t, err, "could not compile matcher")115116req := "TEST-DATA\r\n"117resp := "RESP-DATA\r\nSTAT \r\n"118event := request.responseToDSLMap(req, resp, "one.one.one.one", "one.one.one.one", "TEST")119120isMatched, matched := request.Match(event, matcher)121require.True(t, isMatched, "could not match valid response")122require.Equal(t, []string{"resp-data"}, matched)123})124}125126func TestNetworkOperatorExtract(t *testing.T) {127options := testutils.DefaultOptions128129testutils.Init(options)130templateID := "testing-network"131request := &Request{132ID: templateID,133Address: []string{"{{Hostname}}"},134ReadSize: 1024,135Inputs: []*Input{{Data: "test-data\r\n"}},136}137executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{138ID: templateID,139Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},140})141err := request.Compile(executerOpts)142require.Nil(t, err, "could not compile network request")143144req := "test-data\r\n"145resp := "resp-data\r\nSTAT \r\n1.1.1.1\r\n"146event := request.responseToDSLMap(req, resp, "one.one.one.one", "one.one.one.one", "test")147148t.Run("extract", func(t *testing.T) {149extractor := &extractors.Extractor{150Part: "data",151Type: extractors.ExtractorTypeHolder{ExtractorType: extractors.RegexExtractor},152Regex: []string{"[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"},153}154err = extractor.CompileExtractors()155require.Nil(t, err, "could not compile extractor")156157data := request.Extract(event, extractor)158require.Greater(t, len(data), 0, "could not extractor valid response")159require.Equal(t, map[string]struct{}{"1.1.1.1": {}}, data, "could not extract correct data")160})161162t.Run("kval", func(t *testing.T) {163extractor := &extractors.Extractor{164Type: extractors.ExtractorTypeHolder{ExtractorType: extractors.KValExtractor},165KVal: []string{"request"},166}167err = extractor.CompileExtractors()168require.Nil(t, err, "could not compile kval extractor")169170data := request.Extract(event, extractor)171require.Greater(t, len(data), 0, "could not extractor kval valid response")172require.Equal(t, map[string]struct{}{req: {}}, data, "could not extract correct kval data")173})174}175176func TestNetworkMakeResult(t *testing.T) {177options := testutils.DefaultOptions178179testutils.Init(options)180templateID := "testing-network"181request := &Request{182ID: templateID,183Address: []string{"{{Hostname}}"},184ReadSize: 1024,185Inputs: []*Input{{Data: "test-data\r\n"}},186Operators: operators.Operators{187Matchers: []*matchers.Matcher{{188Name: "test",189Part: "data",190Type: matchers.MatcherTypeHolder{MatcherType: matchers.WordsMatcher},191Words: []string{"STAT "},192}},193Extractors: []*extractors.Extractor{{194Part: "data",195Type: extractors.ExtractorTypeHolder{ExtractorType: extractors.RegexExtractor},196Regex: []string{"[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"},197}},198},199}200executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{201ID: templateID,202Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},203})204err := request.Compile(executerOpts)205require.Nil(t, err, "could not compile network request")206207req := "test-data\r\n"208resp := "resp-data\rSTAT \r\n1.1.1.1\n"209event := request.responseToDSLMap(req, resp, "one.one.one.one", "one.one.one.one", "test")210finalEvent := &output.InternalWrappedEvent{InternalEvent: event}211event["ip"] = "192.168.1.1"212if request.CompiledOperators != nil {213result, ok := request.CompiledOperators.Execute(event, request.Match, request.Extract, false)214if ok && result != nil {215finalEvent.OperatorsResult = result216finalEvent.Results = request.MakeResultEvent(finalEvent)217}218}219require.Equal(t, 1, len(finalEvent.Results), "could not get correct number of results")220require.Equal(t, "test", finalEvent.Results[0].MatcherName, "could not get correct matcher name of results")221require.Equal(t, "1.1.1.1", finalEvent.Results[0].ExtractedResults[0], "could not get correct extracted results")222}223224225