Path: blob/dev/pkg/protocols/common/globalmatchers/globalmatchers.go
2879 views
package globalmatchers12import (3"maps"45"github.com/projectdiscovery/nuclei/v3/pkg/model"6"github.com/projectdiscovery/nuclei/v3/pkg/operators"7"github.com/projectdiscovery/nuclei/v3/pkg/output"8)910// Storage is a struct that holds the global matchers11type Storage struct {12requests []*Item13}1415// Callback is called when a global matcher is matched.16// It receives internal event & result of the operator execution.17type Callback func(event output.InternalEvent, result *operators.Result)1819// Item is a struct that holds the global matchers20// details for a template21type Item struct {22TemplateID string23TemplatePath string24TemplateInfo model.Info25Operators []*operators.Operators26}2728// New creates a new storage for global matchers29func New() *Storage {30return &Storage{requests: make([]*Item, 0)}31}3233// hasStorage checks if the Storage is initialized34func (s *Storage) hasStorage() bool {35return s != nil36}3738// AddOperator adds a new operator to the global matchers39func (s *Storage) AddOperator(item *Item) {40if !s.hasStorage() {41return42}4344s.requests = append(s.requests, item)45}4647// HasMatchers returns true if we have global matchers48func (s *Storage) HasMatchers() bool {49if !s.hasStorage() {50return false51}5253return len(s.requests) > 054}5556// Match matches the global matchers against the response57func (s *Storage) Match(58event output.InternalEvent,59matchFunc operators.MatchFunc,60extractFunc operators.ExtractFunc,61isDebug bool,62callback Callback,63) {64for _, item := range s.requests {65for _, operator := range item.Operators {66newEvent := maps.Clone(event)67newEvent.Set("origin-template-id", event["template-id"])68newEvent.Set("origin-template-info", event["template-info"])69newEvent.Set("origin-template-path", event["template-path"])70newEvent.Set("template-id", item.TemplateID)71newEvent.Set("template-info", item.TemplateInfo)72newEvent.Set("template-path", item.TemplatePath)73newEvent.Set("global-matchers", true)7475result, matched := operator.Execute(newEvent, matchFunc, extractFunc, isDebug)76if !matched {77continue78}7980callback(newEvent, result)81}82}83}848586