Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/headless/operators.go
2844 views
1
package headless
2
3
import (
4
"strconv"
5
"time"
6
7
"github.com/projectdiscovery/nuclei/v3/pkg/model"
8
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
9
"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"
10
"github.com/projectdiscovery/nuclei/v3/pkg/operators/matchers"
11
"github.com/projectdiscovery/nuclei/v3/pkg/output"
12
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
13
protocolUtils "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"
14
"github.com/projectdiscovery/nuclei/v3/pkg/types"
15
)
16
17
// Match matches a generic data response again a given matcher
18
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
19
itemStr, ok := request.getMatchPart(matcher.Part, data)
20
if !ok && matcher.Type.MatcherType != matchers.DSLMatcher {
21
return false, []string{}
22
}
23
24
switch matcher.GetType() {
25
case matchers.StatusMatcher:
26
statusCode, ok := getStatusCode(data)
27
if !ok {
28
return false, []string{}
29
}
30
return matcher.Result(matcher.MatchStatusCode(statusCode)), []string{}
31
case matchers.SizeMatcher:
32
return matcher.Result(matcher.MatchSize(len(itemStr))), []string{}
33
case matchers.WordsMatcher:
34
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(itemStr, data))
35
case matchers.RegexMatcher:
36
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(itemStr))
37
case matchers.BinaryMatcher:
38
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(itemStr))
39
case matchers.DSLMatcher:
40
return matcher.Result(matcher.MatchDSL(data)), []string{}
41
case matchers.XPathMatcher:
42
return matcher.Result(matcher.MatchXPath(itemStr)), []string{}
43
}
44
return false, []string{}
45
}
46
47
func getStatusCode(data map[string]interface{}) (int, bool) {
48
statusCodeValue, ok := data["status_code"]
49
if !ok {
50
return 0, false
51
}
52
statusCodeStr, ok := statusCodeValue.(string)
53
if !ok {
54
return 0, false
55
}
56
57
statusCode, err := strconv.Atoi(statusCodeStr)
58
if err != nil {
59
return 0, false
60
}
61
62
return statusCode, true
63
}
64
65
// Extract performs extracting operation for an extractor on model and returns true or false.
66
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
67
itemStr, ok := request.getMatchPart(extractor.Part, data)
68
if !ok && !extractors.SupportsMap(extractor) {
69
return nil
70
}
71
72
switch extractor.GetType() {
73
case extractors.RegexExtractor:
74
return extractor.ExtractRegex(itemStr)
75
case extractors.KValExtractor:
76
return extractor.ExtractKval(data)
77
case extractors.DSLExtractor:
78
return extractor.ExtractDSL(data)
79
case extractors.XPathExtractor:
80
return extractor.ExtractXPath(itemStr)
81
case extractors.JSONExtractor:
82
return extractor.ExtractJSON(itemStr)
83
}
84
return nil
85
}
86
87
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
88
switch part {
89
case "body", "resp", "":
90
part = "data"
91
case "history":
92
part = "history"
93
case "header":
94
part = "header"
95
}
96
97
item, ok := data[part]
98
if !ok {
99
return "", false
100
}
101
itemStr := types.ToString(item)
102
103
return itemStr, true
104
}
105
106
// responseToDSLMap converts a headless response to a map for use in DSL matching
107
func (request *Request) responseToDSLMap(resp, headers, status_code, req, host, matched string, history string) output.InternalEvent {
108
return output.InternalEvent{
109
"host": host,
110
"matched": matched,
111
"req": req,
112
"data": resp,
113
"header": headers,
114
"status_code": status_code,
115
"history": history,
116
"type": request.Type().String(),
117
"template-id": request.options.TemplateID,
118
"template-info": request.options.TemplateInfo,
119
"template-path": request.options.TemplatePath,
120
}
121
}
122
123
// MakeResultEvent creates a result event from internal wrapped event
124
func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
125
return protocols.MakeDefaultResultEvent(request, wrapped)
126
}
127
128
func (request *Request) GetCompiledOperators() []*operators.Operators {
129
return []*operators.Operators{request.CompiledOperators}
130
}
131
132
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
133
fields := protocolUtils.GetJsonFieldsFromURL(types.ToString(wrapped.InternalEvent["host"]))
134
if types.ToString(wrapped.InternalEvent["ip"]) != "" {
135
fields.Ip = types.ToString(wrapped.InternalEvent["ip"])
136
}
137
if types.ToString(wrapped.InternalEvent["path"]) != "" {
138
fields.Path = types.ToString(wrapped.InternalEvent["path"])
139
}
140
data := &output.ResultEvent{
141
TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),
142
TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),
143
Info: wrapped.InternalEvent["template-info"].(model.Info),
144
TemplateVerifier: request.options.TemplateVerifier,
145
Type: types.ToString(wrapped.InternalEvent["type"]),
146
Host: fields.Host,
147
Path: fields.Path,
148
Port: fields.Port,
149
Scheme: fields.Scheme,
150
URL: fields.URL,
151
Matched: types.ToString(wrapped.InternalEvent["matched"]),
152
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
153
Timestamp: time.Now(),
154
MatcherStatus: true,
155
IP: fields.Ip,
156
Request: types.ToString(wrapped.InternalEvent["request"]),
157
Response: types.ToString(wrapped.InternalEvent["data"]),
158
TemplateEncoded: request.options.EncodeTemplate(),
159
Error: types.ToString(wrapped.InternalEvent["error"]),
160
}
161
return data
162
}
163
164