2020-12-30 14:54:20 +05:30
|
|
|
package network
|
|
|
|
|
|
|
|
|
|
import (
|
2021-02-02 12:10:47 +05:30
|
|
|
"time"
|
|
|
|
|
|
2021-07-19 21:04:08 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2020-12-30 14:54:20 +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/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Match matches a generic data response again a given matcher
|
2021-09-29 19:43:46 +03:00
|
|
|
func (r *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
|
2020-12-30 14:54:20 +05:30
|
|
|
partString := matcher.Part
|
|
|
|
|
switch partString {
|
2021-02-16 15:29:14 +05:30
|
|
|
case "body", "all", "":
|
2020-12-30 14:54:20 +05:30
|
|
|
partString = "data"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item, ok := data[partString]
|
|
|
|
|
if !ok {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
|
|
|
|
itemStr := types.ToString(item)
|
|
|
|
|
|
|
|
|
|
switch matcher.GetType() {
|
|
|
|
|
case matchers.SizeMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchSize(len(itemStr))), []string{}
|
2020-12-30 14:54:20 +05:30
|
|
|
case matchers.WordsMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(itemStr, r.dynamicValues))
|
2020-12-30 14:54:20 +05:30
|
|
|
case matchers.RegexMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(itemStr))
|
2020-12-30 14:54:20 +05:30
|
|
|
case matchers.BinaryMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(itemStr))
|
2020-12-30 14:54:20 +05:30
|
|
|
case matchers.DSLMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchDSL(data)), []string{}
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:31:46 +03:00
|
|
|
// Extract performs extracting operation for an extractor on model and returns true or false.
|
2020-12-30 14:54:20 +05:30
|
|
|
func (r *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
|
2021-02-03 13:07:24 +05:30
|
|
|
partString := extractor.Part
|
2020-12-30 14:54:20 +05:30
|
|
|
switch partString {
|
2021-02-16 15:28:55 +05:30
|
|
|
case "body", "all", "":
|
2020-12-30 14:54:20 +05:30
|
|
|
partString = "data"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2021-02-26 13:13:11 +05:30
|
|
|
func (r *Request) responseToDSLMap(req, resp, raw, host, matched string) output.InternalEvent {
|
2021-02-03 13:07:24 +05:30
|
|
|
data := make(output.InternalEvent, 6)
|
2020-12-30 14:54:20 +05:30
|
|
|
|
|
|
|
|
// Some data regarding the request metadata
|
|
|
|
|
data["host"] = host
|
|
|
|
|
data["matched"] = matched
|
2021-02-03 13:07:24 +05:30
|
|
|
data["request"] = req
|
2021-02-27 12:31:17 +05:30
|
|
|
data["data"] = resp // Data is the last bytes read
|
|
|
|
|
data["raw"] = raw // Raw is the full transaction data for network
|
2021-01-13 12:18:56 +05:30
|
|
|
data["template-id"] = r.options.TemplateID
|
|
|
|
|
data["template-info"] = r.options.TemplateInfo
|
2021-06-05 18:01:08 +05:30
|
|
|
data["template-path"] = r.options.TemplatePath
|
2020-12-30 14:54:20 +05:30
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 12:18:56 +05:30
|
|
|
// MakeResultEvent creates a result event from internal wrapped event
|
|
|
|
|
func (r *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
|
2021-01-17 12:26:45 +05:30
|
|
|
if len(wrapped.OperatorsResult.DynamicValues) > 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-12-30 14:54:20 +05:30
|
|
|
results := make([]*output.ResultEvent, 0, len(wrapped.OperatorsResult.Matches)+1)
|
|
|
|
|
|
2021-01-11 21:11:35 +05:30
|
|
|
// If we have multiple matchers with names, write each of them separately.
|
|
|
|
|
if len(wrapped.OperatorsResult.Matches) > 0 {
|
|
|
|
|
for k := range wrapped.OperatorsResult.Matches {
|
|
|
|
|
data := r.makeResultEventItem(wrapped)
|
|
|
|
|
data.MatcherName = k
|
|
|
|
|
results = append(results, data)
|
|
|
|
|
}
|
|
|
|
|
} else if len(wrapped.OperatorsResult.Extracts) > 0 {
|
|
|
|
|
for k, v := range wrapped.OperatorsResult.Extracts {
|
|
|
|
|
data := r.makeResultEventItem(wrapped)
|
|
|
|
|
data.ExtractedResults = v
|
|
|
|
|
data.ExtractorName = k
|
|
|
|
|
results = append(results, data)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
data := r.makeResultEventItem(wrapped)
|
|
|
|
|
results = append(results, data)
|
|
|
|
|
}
|
|
|
|
|
return results
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Request) makeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
|
|
|
|
|
data := &output.ResultEvent{
|
2021-02-04 18:29:28 +05:30
|
|
|
TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),
|
2021-06-05 18:01:08 +05:30
|
|
|
TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),
|
2021-07-12 17:20:01 +03:00
|
|
|
Info: wrapped.InternalEvent["template-info"].(model.Info),
|
2020-12-30 14:54:20 +05:30
|
|
|
Type: "network",
|
2021-02-04 18:29:28 +05:30
|
|
|
Host: types.ToString(wrapped.InternalEvent["host"]),
|
|
|
|
|
Matched: types.ToString(wrapped.InternalEvent["matched"]),
|
2020-12-30 14:54:20 +05:30
|
|
|
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
|
2021-07-06 21:11:30 +05:30
|
|
|
Metadata: wrapped.OperatorsResult.PayloadValues,
|
2021-02-02 12:10:47 +05:30
|
|
|
Timestamp: time.Now(),
|
2021-02-04 18:29:28 +05:30
|
|
|
IP: types.ToString(wrapped.InternalEvent["ip"]),
|
2021-09-09 18:53:55 +05:30
|
|
|
Request: types.ToString(wrapped.InternalEvent["request"]),
|
|
|
|
|
Response: types.ToString(wrapped.InternalEvent["data"]),
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
2021-01-11 21:11:35 +05:30
|
|
|
return data
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|