From 91ad44621257775443f5003e9faaad13a9f0f538 Mon Sep 17 00:00:00 2001 From: mzack Date: Thu, 24 Feb 2022 23:18:37 +0100 Subject: [PATCH] . --- v2/pkg/protocols/file/file_test.go | 35 ------------------------------ v2/pkg/protocols/file/operators.go | 29 +++++++++---------------- v2/pkg/protocols/file/request.go | 17 +++++++++++++++ 3 files changed, 27 insertions(+), 54 deletions(-) delete mode 100644 v2/pkg/protocols/file/file_test.go diff --git a/v2/pkg/protocols/file/file_test.go b/v2/pkg/protocols/file/file_test.go deleted file mode 100644 index 73f4fde9..00000000 --- a/v2/pkg/protocols/file/file_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package file - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/projectdiscovery/nuclei/v2/pkg/model" - "github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity" - "github.com/projectdiscovery/nuclei/v2/pkg/testutils" -) - -func TestFileCompile(t *testing.T) { - options := testutils.DefaultOptions - - testutils.Init(options) - templateID := "testing-file" - request := &Request{ - ID: templateID, - MaxSize: "1Gb", - NoRecursive: false, - Extensions: []string{"all", ".lock"}, - DenyList: []string{".go"}, - } - executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{ - ID: templateID, - Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"}, - }) - err := request.Compile(executerOpts) - require.Nil(t, err, "could not compile file request") - - require.Contains(t, request.denyList, ".go", "could not get .go in denylist") - require.NotContains(t, request.extensions, ".go", "could get .go in allowlist") - require.True(t, request.allExtensions, "could not get correct allExtensions") -} diff --git a/v2/pkg/protocols/file/operators.go b/v2/pkg/protocols/file/operators.go index 1c11ccd7..0dcbb22f 100644 --- a/v2/pkg/protocols/file/operators.go +++ b/v2/pkg/protocols/file/operators.go @@ -1,6 +1,7 @@ package file import ( + "log" "time" "github.com/projectdiscovery/nuclei/v2/pkg/model" @@ -118,25 +119,15 @@ func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) [] result.Lines = calculateLineFunc(allMatches, lineWords) } // Identify the position of match in file using a dirty hack. - // for _, result := range results { - // for _, extraction := range result.ExtractedResults { - // file, _ := os.Open(filePath) - // scanner := bufio.NewScanner(file) - - // line := 1 - // for scanner.Scan() { - // if strings.Contains(scanner.Text(), extraction) { - // if result.FileToIndexPosition == nil { - // result.FileToIndexPosition = make(map[string]int) - // } - // result.FileToIndexPosition[result.Matched] = line - // continue - // } - // line++ - // } - // file.Close() - // } - // } + for _, result := range results { + for _, extraction := range result.ExtractedResults { + if result.FileToIndexPosition == nil { + result.FileToIndexPosition = make(map[string]int) + } + result.FileToIndexPosition[result.Matched] = calculateFileIndexFunc(allMatches, extraction) + log.Fatalf("%s %#v\n", extraction, result.FileToIndexPosition) + } + } return results } diff --git a/v2/pkg/protocols/file/request.go b/v2/pkg/protocols/file/request.go index 4beb010a..adf6e31d 100644 --- a/v2/pkg/protocols/file/request.go +++ b/v2/pkg/protocols/file/request.go @@ -166,3 +166,20 @@ func calculateLineFunc(allMatches []*output.InternalEvent, words map[string]stru sort.Ints(lines) return lines } + +func calculateFileIndexFunc(allMatches []*output.InternalEvent, extraction string) int { + for _, match := range allMatches { + matchPt := *match + opResult := matchPt["results"].(operators.Result) + if opResult.Matched { + for _, extracts := range opResult.Extracts { + for _, extract := range extracts { + if extraction == extract { + return matchPt["results"].(int) + } + } + } + } + } + return -1 +}