github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/file/operators.go (about) 1 package file 2 3 import ( 4 "time" 5 6 "github.com/projectdiscovery/nuclei/v2/pkg/model" 7 "github.com/projectdiscovery/nuclei/v2/pkg/operators" 8 "github.com/projectdiscovery/nuclei/v2/pkg/operators/extractors" 9 "github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers" 10 "github.com/projectdiscovery/nuclei/v2/pkg/output" 11 "github.com/projectdiscovery/nuclei/v2/pkg/protocols" 12 "github.com/projectdiscovery/nuclei/v2/pkg/types" 13 ) 14 15 // Match matches a generic data response again a given matcher 16 func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) { 17 itemStr, ok := request.getMatchPart(matcher.Part, data) 18 if !ok && matcher.Type.MatcherType != matchers.DSLMatcher { 19 return false, []string{} 20 } 21 22 switch matcher.GetType() { 23 case matchers.SizeMatcher: 24 return matcher.Result(matcher.MatchSize(len(itemStr))), []string{} 25 case matchers.WordsMatcher: 26 return matcher.ResultWithMatchedSnippet(matcher.MatchWords(itemStr, nil)) 27 case matchers.RegexMatcher: 28 return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(itemStr)) 29 case matchers.BinaryMatcher: 30 return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(itemStr)) 31 case matchers.DSLMatcher: 32 return matcher.Result(matcher.MatchDSL(data)), []string{} 33 case matchers.XPathMatcher: 34 return matcher.Result(matcher.MatchXPath(itemStr)), []string{} 35 } 36 return false, []string{} 37 } 38 39 // Extract performs extracting operation for an extractor on model and returns true or false. 40 func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} { 41 itemStr, ok := request.getMatchPart(extractor.Part, data) 42 if !ok && !extractors.SupportsMap(extractor) { 43 return nil 44 } 45 46 switch extractor.GetType() { 47 case extractors.RegexExtractor: 48 return extractor.ExtractRegex(itemStr) 49 case extractors.KValExtractor: 50 return extractor.ExtractKval(data) 51 case extractors.DSLExtractor: 52 return extractor.ExtractDSL(data) 53 } 54 return nil 55 } 56 57 func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) { 58 switch part { 59 case "body", "all", "data", "": 60 part = "raw" 61 } 62 63 item, ok := data[part] 64 if !ok { 65 return "", false 66 } 67 itemStr := types.ToString(item) 68 69 return itemStr, true 70 } 71 72 // responseToDSLMap converts a file chunk elaboration to a map for use in DSL matching 73 func (request *Request) responseToDSLMap(raw, inputFilePath, matchedFileName string) output.InternalEvent { 74 return output.InternalEvent{ 75 "path": inputFilePath, 76 "matched": matchedFileName, 77 "raw": raw, 78 "type": request.Type().String(), 79 "template-id": request.options.TemplateID, 80 "template-info": request.options.TemplateInfo, 81 "template-path": request.options.TemplatePath, 82 } 83 } 84 85 // MakeResultEvent creates a result event from internal wrapped event 86 // Deprecated: unused in stream mode, must be present for interface compatibility 87 func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent { 88 return protocols.MakeDefaultResultEvent(request, wrapped) 89 } 90 91 func (request *Request) GetCompiledOperators() []*operators.Operators { 92 return []*operators.Operators{request.CompiledOperators} 93 } 94 95 // MakeResultEventItem 96 // Deprecated: unused in stream mode, must be present for interface compatibility 97 func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent { 98 data := &output.ResultEvent{ 99 MatcherStatus: true, 100 TemplateID: types.ToString(wrapped.InternalEvent["template-id"]), 101 TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]), 102 Info: wrapped.InternalEvent["template-info"].(model.Info), 103 Type: types.ToString(wrapped.InternalEvent["type"]), 104 Path: types.ToString(wrapped.InternalEvent["path"]), 105 Matched: types.ToString(wrapped.InternalEvent["matched"]), 106 Host: types.ToString(wrapped.InternalEvent["host"]), 107 ExtractedResults: wrapped.OperatorsResult.OutputExtracts, 108 Response: types.ToString(wrapped.InternalEvent["raw"]), 109 Timestamp: time.Now(), 110 } 111 return data 112 }