2020-12-25 02:24:55 +05:30
|
|
|
package dns
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2021-02-02 12:10:47 +05:30
|
|
|
"time"
|
2020-12-25 02:24:55 +05:30
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2021-07-19 21:04:08 +03:00
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2020-12-25 02:24:55 +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-10-01 14:30:04 +03:00
|
|
|
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
|
2020-12-29 11:42:46 +05:30
|
|
|
partString := matcher.Part
|
2020-12-25 02:24:55 +05:30
|
|
|
switch partString {
|
2020-12-29 11:42:46 +05:30
|
|
|
case "body", "all", "":
|
2020-12-25 02:24:55 +05:30
|
|
|
partString = "raw"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item, ok := data[partString]
|
|
|
|
|
if !ok {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch matcher.GetType() {
|
2021-10-01 16:52:38 +03:00
|
|
|
case matchers.StatusMatcher:
|
2021-10-05 13:56:02 +03:00
|
|
|
statusCode, ok := item.(int)
|
|
|
|
|
if !ok {
|
|
|
|
|
return false, []string{}
|
|
|
|
|
}
|
|
|
|
|
return matcher.Result(matcher.MatchStatusCode(statusCode)), []string{}
|
2020-12-25 02:24:55 +05:30
|
|
|
case matchers.SizeMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchSize(len(types.ToString(item)))), []string{}
|
2020-12-25 02:24:55 +05:30
|
|
|
case matchers.WordsMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(types.ToString(item), nil))
|
2020-12-25 02:24:55 +05:30
|
|
|
case matchers.RegexMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(types.ToString(item)))
|
2020-12-25 02:24:55 +05:30
|
|
|
case matchers.BinaryMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(types.ToString(item)))
|
2020-12-25 02:24:55 +05:30
|
|
|
case matchers.DSLMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchDSL(data)), []string{}
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:31:46 +03:00
|
|
|
// Extract performs extracting operation for an extractor on model and returns true or false.
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
|
2021-02-03 02:09:45 +05:30
|
|
|
part := extractor.Part
|
|
|
|
|
switch part {
|
2020-12-25 02:24:55 +05:30
|
|
|
case "body", "all":
|
2021-02-03 02:09:45 +05:30
|
|
|
part = "raw"
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
2021-02-03 02:09:45 +05:30
|
|
|
item, ok := data[part]
|
2020-12-25 02:24:55 +05:30
|
|
|
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-10-01 14:30:04 +03:00
|
|
|
func (request *Request) responseToDSLMap(req, resp *dns.Msg, host, matched string) output.InternalEvent {
|
2021-02-03 02:09:45 +05:30
|
|
|
data := make(output.InternalEvent, 11)
|
2020-12-25 20:33:52 +05:30
|
|
|
|
|
|
|
|
// Some data regarding the request metadata
|
|
|
|
|
data["host"] = host
|
|
|
|
|
data["matched"] = matched
|
2021-02-03 02:09:45 +05:30
|
|
|
data["request"] = req.String()
|
2020-12-25 02:24:55 +05:30
|
|
|
|
2020-12-25 20:33:52 +05:30
|
|
|
data["rcode"] = resp.Rcode
|
2020-12-25 02:24:55 +05:30
|
|
|
buffer := &bytes.Buffer{}
|
2020-12-25 20:33:52 +05:30
|
|
|
for _, question := range resp.Question {
|
2020-12-25 02:24:55 +05:30
|
|
|
buffer.WriteString(question.String())
|
|
|
|
|
}
|
|
|
|
|
data["question"] = buffer.String()
|
|
|
|
|
buffer.Reset()
|
|
|
|
|
|
2020-12-25 20:33:52 +05:30
|
|
|
for _, extra := range resp.Extra {
|
2020-12-25 02:24:55 +05:30
|
|
|
buffer.WriteString(extra.String())
|
|
|
|
|
}
|
|
|
|
|
data["extra"] = buffer.String()
|
|
|
|
|
buffer.Reset()
|
|
|
|
|
|
2020-12-25 20:33:52 +05:30
|
|
|
for _, answer := range resp.Answer {
|
2020-12-25 02:24:55 +05:30
|
|
|
buffer.WriteString(answer.String())
|
|
|
|
|
}
|
|
|
|
|
data["answer"] = buffer.String()
|
|
|
|
|
buffer.Reset()
|
|
|
|
|
|
2020-12-25 20:33:52 +05:30
|
|
|
for _, ns := range resp.Ns {
|
2020-12-25 02:24:55 +05:30
|
|
|
buffer.WriteString(ns.String())
|
|
|
|
|
}
|
|
|
|
|
data["ns"] = buffer.String()
|
|
|
|
|
buffer.Reset()
|
|
|
|
|
|
2020-12-25 20:33:52 +05:30
|
|
|
rawData := resp.String()
|
2020-12-25 02:24:55 +05:30
|
|
|
data["raw"] = rawData
|
2021-10-01 14:30:04 +03:00
|
|
|
data["template-id"] = request.options.TemplateID
|
|
|
|
|
data["template-info"] = request.options.TemplateInfo
|
|
|
|
|
data["template-path"] = request.options.TemplatePath
|
2020-12-25 02:24:55 +05:30
|
|
|
return data
|
|
|
|
|
}
|
2020-12-25 20:39:09 +05:30
|
|
|
|
2021-01-13 12:18:56 +05:30
|
|
|
// MakeResultEvent creates a result event from internal wrapped event
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *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-29 11:42:46 +05:30
|
|
|
results := make([]*output.ResultEvent, 0, len(wrapped.OperatorsResult.Matches)+1)
|
2020-12-25 20:39:09 +05:30
|
|
|
|
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 {
|
2021-10-01 14:30:04 +03:00
|
|
|
data := request.makeResultEventItem(wrapped)
|
2021-01-11 21:11:35 +05:30
|
|
|
data.MatcherName = k
|
|
|
|
|
results = append(results, data)
|
|
|
|
|
}
|
|
|
|
|
} else if len(wrapped.OperatorsResult.Extracts) > 0 {
|
|
|
|
|
for k, v := range wrapped.OperatorsResult.Extracts {
|
2021-10-01 14:30:04 +03:00
|
|
|
data := request.makeResultEventItem(wrapped)
|
2021-01-11 21:11:35 +05:30
|
|
|
data.ExtractedResults = v
|
|
|
|
|
data.ExtractorName = k
|
|
|
|
|
results = append(results, data)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-10-01 14:30:04 +03:00
|
|
|
data := request.makeResultEventItem(wrapped)
|
2021-01-11 21:11:35 +05:30
|
|
|
results = append(results, data)
|
|
|
|
|
}
|
|
|
|
|
return results
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) makeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
|
2021-01-11 21:11:35 +05:30
|
|
|
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-25 20:39:09 +05:30
|
|
|
Type: "dns",
|
2021-02-04 18:29:28 +05:30
|
|
|
Host: types.ToString(wrapped.InternalEvent["host"]),
|
|
|
|
|
Matched: types.ToString(wrapped.InternalEvent["matched"]),
|
2020-12-25 20:39:09 +05:30
|
|
|
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
|
2021-02-02 12:10:47 +05:30
|
|
|
Timestamp: time.Now(),
|
2021-09-09 18:53:55 +05:30
|
|
|
Request: types.ToString(wrapped.InternalEvent["request"]),
|
|
|
|
|
Response: types.ToString(wrapped.InternalEvent["raw"]),
|
2020-12-25 20:39:09 +05:30
|
|
|
}
|
2021-01-11 21:11:35 +05:30
|
|
|
return data
|
2020-12-25 20:39:09 +05:30
|
|
|
}
|