github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/network/operators.go (about) 1 package network 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, data)) 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", "": 60 part = "data" 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 network response to a map for use in DSL matching 73 func (request *Request) responseToDSLMap(req, resp, raw, host, matched string) output.InternalEvent { 74 return output.InternalEvent{ 75 "host": host, 76 "matched": matched, 77 "request": req, 78 "data": resp, // Data is the last bytes read 79 "raw": raw, // Raw is the full transaction data for network 80 "type": request.Type().String(), 81 "template-id": request.options.TemplateID, 82 "template-info": request.options.TemplateInfo, 83 "template-path": request.options.TemplatePath, 84 } 85 } 86 87 // MakeResultEvent creates a result event from internal wrapped event 88 func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent { 89 return protocols.MakeDefaultResultEvent(request, wrapped) 90 } 91 92 func (request *Request) GetCompiledOperators() []*operators.Operators { 93 return []*operators.Operators{request.CompiledOperators} 94 } 95 96 func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent { 97 data := &output.ResultEvent{ 98 TemplateID: types.ToString(wrapped.InternalEvent["template-id"]), 99 TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]), 100 Info: wrapped.InternalEvent["template-info"].(model.Info), 101 Type: types.ToString(wrapped.InternalEvent["type"]), 102 Host: types.ToString(wrapped.InternalEvent["host"]), 103 Matched: types.ToString(wrapped.InternalEvent["matched"]), 104 ExtractedResults: wrapped.OperatorsResult.OutputExtracts, 105 Metadata: wrapped.OperatorsResult.PayloadValues, 106 Timestamp: time.Now(), 107 MatcherStatus: true, 108 IP: types.ToString(wrapped.InternalEvent["ip"]), 109 Request: types.ToString(wrapped.InternalEvent["request"]), 110 Response: types.ToString(wrapped.InternalEvent["data"]), 111 } 112 return data 113 }