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