Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/common/globalmatchers/globalmatchers.go
2879 views
1
package globalmatchers
2
3
import (
4
"maps"
5
6
"github.com/projectdiscovery/nuclei/v3/pkg/model"
7
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
8
"github.com/projectdiscovery/nuclei/v3/pkg/output"
9
)
10
11
// Storage is a struct that holds the global matchers
12
type Storage struct {
13
requests []*Item
14
}
15
16
// Callback is called when a global matcher is matched.
17
// It receives internal event & result of the operator execution.
18
type Callback func(event output.InternalEvent, result *operators.Result)
19
20
// Item is a struct that holds the global matchers
21
// details for a template
22
type Item struct {
23
TemplateID string
24
TemplatePath string
25
TemplateInfo model.Info
26
Operators []*operators.Operators
27
}
28
29
// New creates a new storage for global matchers
30
func New() *Storage {
31
return &Storage{requests: make([]*Item, 0)}
32
}
33
34
// hasStorage checks if the Storage is initialized
35
func (s *Storage) hasStorage() bool {
36
return s != nil
37
}
38
39
// AddOperator adds a new operator to the global matchers
40
func (s *Storage) AddOperator(item *Item) {
41
if !s.hasStorage() {
42
return
43
}
44
45
s.requests = append(s.requests, item)
46
}
47
48
// HasMatchers returns true if we have global matchers
49
func (s *Storage) HasMatchers() bool {
50
if !s.hasStorage() {
51
return false
52
}
53
54
return len(s.requests) > 0
55
}
56
57
// Match matches the global matchers against the response
58
func (s *Storage) Match(
59
event output.InternalEvent,
60
matchFunc operators.MatchFunc,
61
extractFunc operators.ExtractFunc,
62
isDebug bool,
63
callback Callback,
64
) {
65
for _, item := range s.requests {
66
for _, operator := range item.Operators {
67
newEvent := maps.Clone(event)
68
newEvent.Set("origin-template-id", event["template-id"])
69
newEvent.Set("origin-template-info", event["template-info"])
70
newEvent.Set("origin-template-path", event["template-path"])
71
newEvent.Set("template-id", item.TemplateID)
72
newEvent.Set("template-info", item.TemplateInfo)
73
newEvent.Set("template-path", item.TemplatePath)
74
newEvent.Set("global-matchers", true)
75
76
result, matched := operator.Execute(newEvent, matchFunc, extractFunc, isDebug)
77
if !matched {
78
continue
79
}
80
81
callback(newEvent, result)
82
}
83
}
84
}
85
86