Path: blob/dev/pkg/protocols/network/operators.go
2070 views
package network12import (3"time"45"github.com/projectdiscovery/nuclei/v3/pkg/model"6"github.com/projectdiscovery/nuclei/v3/pkg/operators"7"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"8"github.com/projectdiscovery/nuclei/v3/pkg/operators/matchers"9"github.com/projectdiscovery/nuclei/v3/pkg/output"10"github.com/projectdiscovery/nuclei/v3/pkg/protocols"11protocolutils "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"12"github.com/projectdiscovery/nuclei/v3/pkg/types"13)1415// Match matches a generic data response again a given matcher16func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {17itemStr, ok := request.getMatchPart(matcher.Part, data)18if !ok && matcher.Type.MatcherType != matchers.DSLMatcher {19return false, []string{}20}2122switch matcher.GetType() {23case matchers.SizeMatcher:24return matcher.Result(matcher.MatchSize(len(itemStr))), []string{}25case matchers.WordsMatcher:26return matcher.ResultWithMatchedSnippet(matcher.MatchWords(itemStr, data))27case matchers.RegexMatcher:28return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(itemStr))29case matchers.BinaryMatcher:30return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(itemStr))31case matchers.DSLMatcher:32return matcher.Result(matcher.MatchDSL(data)), []string{}33case matchers.XPathMatcher:34return matcher.Result(matcher.MatchXPath(itemStr)), []string{}35}36return false, []string{}37}3839// Extract performs extracting operation for an extractor on model and returns true or false.40func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {41itemStr, ok := request.getMatchPart(extractor.Part, data)42if !ok && !extractors.SupportsMap(extractor) {43return nil44}4546switch extractor.GetType() {47case extractors.RegexExtractor:48return extractor.ExtractRegex(itemStr)49case extractors.KValExtractor:50return extractor.ExtractKval(data)51case extractors.DSLExtractor:52return extractor.ExtractDSL(data)53}54return nil55}5657func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {58switch part {59case "body", "all", "":60part = "data"61}6263item, ok := data[part]64if !ok {65return "", false66}67itemStr := types.ToString(item)6869return itemStr, true70}7172// responseToDSLMap converts a network response to a map for use in DSL matching73func (request *Request) responseToDSLMap(req, resp, raw, host, matched string) output.InternalEvent {74return output.InternalEvent{75"host": host,76"matched": matched,77"request": req,78"data": resp, // Data is the last bytes read79"raw": raw, // Raw is the full transaction data for network80"type": request.Type().String(),81"template-id": request.options.TemplateID,82"template-info": request.options.TemplateInfo,83"template-path": request.options.TemplatePath,84}85}8687// MakeResultEvent creates a result event from internal wrapped event88func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {89return protocols.MakeDefaultResultEvent(request, wrapped)90}9192func (request *Request) GetCompiledOperators() []*operators.Operators {93return []*operators.Operators{request.CompiledOperators}94}9596func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {97fields := protocolutils.GetJsonFieldsFromURL(types.ToString(wrapped.InternalEvent["host"]))98if types.ToString(wrapped.InternalEvent["ip"]) != "" {99fields.Ip = types.ToString(wrapped.InternalEvent["ip"])100}101data := &output.ResultEvent{102TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),103TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),104Info: wrapped.InternalEvent["template-info"].(model.Info),105TemplateVerifier: request.options.TemplateVerifier,106Type: types.ToString(wrapped.InternalEvent["type"]),107Host: fields.Host,108Port: fields.Port,109URL: fields.URL,110Matched: types.ToString(wrapped.InternalEvent["matched"]),111ExtractedResults: wrapped.OperatorsResult.OutputExtracts,112Metadata: wrapped.OperatorsResult.PayloadValues,113Timestamp: time.Now(),114MatcherStatus: true,115IP: fields.Ip,116Request: types.ToString(wrapped.InternalEvent["request"]),117Response: types.ToString(wrapped.InternalEvent["data"]),118TemplateEncoded: request.options.EncodeTemplate(),119Error: types.ToString(wrapped.InternalEvent["error"]),120}121return data122}123124125