nuclei/v2/pkg/protocols/file/operators.go

137 lines
4.2 KiB
Go
Raw Normal View History

2021-01-01 15:28:28 +05:30
package file
import (
"bufio"
"strings"
"time"
"github.com/projectdiscovery/nuclei/v2/pkg/model"
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
2021-01-01 15:28:28 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/operators/extractors"
"github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers"
"github.com/projectdiscovery/nuclei/v2/pkg/output"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
2021-01-01 15:28:28 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/types"
)
// Match matches a generic data response again a given matcher
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
2021-01-01 15:28:28 +05:30
partString := matcher.Part
switch partString {
2021-02-03 17:49:10 +05:30
case "body", "all", "data", "":
2021-01-01 15:28:28 +05:30
partString = "raw"
}
item, ok := data[partString]
if !ok {
return false, []string{}
2021-01-01 15:28:28 +05:30
}
itemStr := types.ToString(item)
switch matcher.GetType() {
case matchers.SizeMatcher:
return matcher.Result(matcher.MatchSize(len(itemStr))), []string{}
2021-01-01 15:28:28 +05:30
case matchers.WordsMatcher:
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(itemStr, nil))
2021-01-01 15:28:28 +05:30
case matchers.RegexMatcher:
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(itemStr))
2021-01-01 15:28:28 +05:30
case matchers.BinaryMatcher:
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(itemStr))
2021-01-01 15:28:28 +05:30
case matchers.DSLMatcher:
return matcher.Result(matcher.MatchDSL(data)), []string{}
2021-01-01 15:28:28 +05:30
}
return false, []string{}
2021-01-01 15:28:28 +05:30
}
2021-09-07 17:31:46 +03:00
// Extract performs extracting operation for an extractor on model and returns true or false.
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
2021-02-03 17:49:10 +05:30
partString := extractor.Part
2021-01-01 15:28:28 +05:30
switch partString {
2021-02-03 17:49:10 +05:30
case "body", "all", "data", "":
2021-01-01 15:28:28 +05:30
partString = "raw"
}
item, ok := data[partString]
if !ok {
return nil
}
itemStr := types.ToString(item)
switch extractor.GetType() {
case extractors.RegexExtractor:
return extractor.ExtractRegex(itemStr)
case extractors.KValExtractor:
return extractor.ExtractKval(data)
}
return nil
}
// responseToDSLMap converts a DNS response to a map for use in DSL matching
func (request *Request) responseToDSLMap(raw, host, matched string) output.InternalEvent {
2021-02-03 17:49:10 +05:30
data := make(output.InternalEvent, 5)
2021-01-01 15:28:28 +05:30
// Some data regarding the request metadata
2021-03-05 12:14:46 +05:30
data["path"] = host
2021-01-01 15:28:28 +05:30
data["matched"] = matched
data["raw"] = raw
data["template-id"] = request.options.TemplateID
data["template-info"] = request.options.TemplateInfo
data["template-path"] = request.options.TemplatePath
2021-01-01 15:28:28 +05:30
return data
}
// MakeResultEvent creates a result event from internal wrapped event
func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
results := protocols.MakeDefaultResultEvent(request, wrapped)
raw, ok := wrapped.InternalEvent["raw"]
if !ok {
return results
}
rawStr, ok := raw.(string)
if !ok {
return results
}
// Identify the position of match in file using a dirty hack.
for _, result := range results {
for _, extraction := range result.ExtractedResults {
scanner := bufio.NewScanner(strings.NewReader(rawStr))
line := 1
for scanner.Scan() {
if strings.Contains(scanner.Text(), extraction) {
2021-06-06 15:57:22 +05:30
if result.FileToIndexPosition == nil {
result.FileToIndexPosition = make(map[string]int)
}
result.FileToIndexPosition[result.Matched] = line
continue
}
line++
}
}
}
2021-01-11 21:11:35 +05:30
return results
}
func (request *Request) GetCompiledOperators() []*operators.Operators {
return []*operators.Operators{request.CompiledOperators}
}
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
2021-01-11 21:11:35 +05:30
data := &output.ResultEvent{
TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),
2021-06-05 18:01:08 +05:30
TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),
Info: wrapped.InternalEvent["template-info"].(model.Info),
2021-01-01 15:28:28 +05:30
Type: "file",
2021-03-05 19:25:09 +05:30
Path: types.ToString(wrapped.InternalEvent["path"]),
Matched: types.ToString(wrapped.InternalEvent["matched"]),
Host: types.ToString(wrapped.InternalEvent["host"]),
2021-01-01 15:28:28 +05:30
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
Response: types.ToString(wrapped.InternalEvent["raw"]),
Timestamp: time.Now(),
2021-01-01 15:28:28 +05:30
}
2021-01-11 21:11:35 +05:30
return data
2021-01-01 15:28:28 +05:30
}