Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/network/operators.go
2070 views
1
package network
2
3
import (
4
"time"
5
6
"github.com/projectdiscovery/nuclei/v3/pkg/model"
7
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
8
"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"
9
"github.com/projectdiscovery/nuclei/v3/pkg/operators/matchers"
10
"github.com/projectdiscovery/nuclei/v3/pkg/output"
11
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
12
protocolutils "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"
13
"github.com/projectdiscovery/nuclei/v3/pkg/types"
14
)
15
16
// Match matches a generic data response again a given matcher
17
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
18
itemStr, ok := request.getMatchPart(matcher.Part, data)
19
if !ok && matcher.Type.MatcherType != matchers.DSLMatcher {
20
return false, []string{}
21
}
22
23
switch matcher.GetType() {
24
case matchers.SizeMatcher:
25
return matcher.Result(matcher.MatchSize(len(itemStr))), []string{}
26
case matchers.WordsMatcher:
27
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(itemStr, data))
28
case matchers.RegexMatcher:
29
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(itemStr))
30
case matchers.BinaryMatcher:
31
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(itemStr))
32
case matchers.DSLMatcher:
33
return matcher.Result(matcher.MatchDSL(data)), []string{}
34
case matchers.XPathMatcher:
35
return matcher.Result(matcher.MatchXPath(itemStr)), []string{}
36
}
37
return false, []string{}
38
}
39
40
// Extract performs extracting operation for an extractor on model and returns true or false.
41
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
42
itemStr, ok := request.getMatchPart(extractor.Part, data)
43
if !ok && !extractors.SupportsMap(extractor) {
44
return nil
45
}
46
47
switch extractor.GetType() {
48
case extractors.RegexExtractor:
49
return extractor.ExtractRegex(itemStr)
50
case extractors.KValExtractor:
51
return extractor.ExtractKval(data)
52
case extractors.DSLExtractor:
53
return extractor.ExtractDSL(data)
54
}
55
return nil
56
}
57
58
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
59
switch part {
60
case "body", "all", "":
61
part = "data"
62
}
63
64
item, ok := data[part]
65
if !ok {
66
return "", false
67
}
68
itemStr := types.ToString(item)
69
70
return itemStr, true
71
}
72
73
// responseToDSLMap converts a network response to a map for use in DSL matching
74
func (request *Request) responseToDSLMap(req, resp, raw, host, matched string) output.InternalEvent {
75
return output.InternalEvent{
76
"host": host,
77
"matched": matched,
78
"request": req,
79
"data": resp, // Data is the last bytes read
80
"raw": raw, // Raw is the full transaction data for network
81
"type": request.Type().String(),
82
"template-id": request.options.TemplateID,
83
"template-info": request.options.TemplateInfo,
84
"template-path": request.options.TemplatePath,
85
}
86
}
87
88
// MakeResultEvent creates a result event from internal wrapped event
89
func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
90
return protocols.MakeDefaultResultEvent(request, wrapped)
91
}
92
93
func (request *Request) GetCompiledOperators() []*operators.Operators {
94
return []*operators.Operators{request.CompiledOperators}
95
}
96
97
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
98
fields := protocolutils.GetJsonFieldsFromURL(types.ToString(wrapped.InternalEvent["host"]))
99
if types.ToString(wrapped.InternalEvent["ip"]) != "" {
100
fields.Ip = types.ToString(wrapped.InternalEvent["ip"])
101
}
102
data := &output.ResultEvent{
103
TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),
104
TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),
105
Info: wrapped.InternalEvent["template-info"].(model.Info),
106
TemplateVerifier: request.options.TemplateVerifier,
107
Type: types.ToString(wrapped.InternalEvent["type"]),
108
Host: fields.Host,
109
Port: fields.Port,
110
URL: fields.URL,
111
Matched: types.ToString(wrapped.InternalEvent["matched"]),
112
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
113
Metadata: wrapped.OperatorsResult.PayloadValues,
114
Timestamp: time.Now(),
115
MatcherStatus: true,
116
IP: fields.Ip,
117
Request: types.ToString(wrapped.InternalEvent["request"]),
118
Response: types.ToString(wrapped.InternalEvent["data"]),
119
TemplateEncoded: request.options.EncodeTemplate(),
120
Error: types.ToString(wrapped.InternalEvent["error"]),
121
}
122
return data
123
}
124
125