2021-02-21 16:31:34 +05:30
|
|
|
package headless
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-07-19 21:04:08 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2021-10-01 16:52:38 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
|
2021-02-21 16:31:34 +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"
|
2021-10-06 21:53:03 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-02-21 16:31:34 +05:30
|
|
|
"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) {
|
2021-10-29 19:10:17 +03:00
|
|
|
itemStr, ok := request.getMatchPart(matcher.Part, data)
|
2022-01-13 13:22:43 +05:30
|
|
|
if !ok && matcher.Type.MatcherType != matchers.DSLMatcher {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch matcher.GetType() {
|
|
|
|
|
case matchers.SizeMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchSize(len(itemStr))), []string{}
|
2021-02-21 16:31:34 +05:30
|
|
|
case matchers.WordsMatcher:
|
2022-04-02 02:14:00 +05:30
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(itemStr, data))
|
2021-02-21 16:31:34 +05:30
|
|
|
case matchers.RegexMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(itemStr))
|
2021-02-21 16:31:34 +05:30
|
|
|
case matchers.BinaryMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(itemStr))
|
2021-02-21 16:31:34 +05:30
|
|
|
case matchers.DSLMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchDSL(data)), []string{}
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2021-02-21 16:31:34 +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-10-29 19:10:17 +03:00
|
|
|
itemStr, ok := request.getMatchPart(extractor.Part, data)
|
2022-04-20 11:32:13 +02:00
|
|
|
if !ok && !extractors.SupportsMap(extractor) {
|
2021-02-21 16:31:34 +05:30
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch extractor.GetType() {
|
|
|
|
|
case extractors.RegexExtractor:
|
|
|
|
|
return extractor.ExtractRegex(itemStr)
|
|
|
|
|
case extractors.KValExtractor:
|
|
|
|
|
return extractor.ExtractKval(data)
|
2022-04-20 11:32:13 +02:00
|
|
|
case extractors.DSLExtractor:
|
|
|
|
|
return extractor.ExtractDSL(data)
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 19:10:17 +03:00
|
|
|
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
|
|
|
|
|
switch part {
|
|
|
|
|
case "body", "resp", "":
|
|
|
|
|
part = "data"
|
2021-12-29 09:48:46 +01:00
|
|
|
case "history":
|
|
|
|
|
part = "history"
|
2021-10-29 19:10:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item, ok := data[part]
|
|
|
|
|
if !ok {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
itemStr := types.ToString(item)
|
|
|
|
|
|
|
|
|
|
return itemStr, true
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 20:06:55 +03:00
|
|
|
// responseToDSLMap converts a headless response to a map for use in DSL matching
|
2021-12-29 09:48:46 +01:00
|
|
|
func (request *Request) responseToDSLMap(resp, req, host, matched string, history string) output.InternalEvent {
|
2021-10-12 20:06:55 +03:00
|
|
|
return output.InternalEvent{
|
|
|
|
|
"host": host,
|
|
|
|
|
"matched": matched,
|
|
|
|
|
"req": req,
|
|
|
|
|
"data": resp,
|
2021-12-29 09:48:46 +01:00
|
|
|
"history": history,
|
2021-11-22 17:53:25 +05:30
|
|
|
"type": request.Type().String(),
|
2021-10-12 20:06:55 +03:00
|
|
|
"template-id": request.options.TemplateID,
|
|
|
|
|
"template-info": request.options.TemplateInfo,
|
|
|
|
|
"template-path": request.options.TemplatePath,
|
|
|
|
|
}
|
2021-02-21 16:31:34 +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-10-06 21:53:03 +03:00
|
|
|
return protocols.MakeDefaultResultEvent(request, wrapped)
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-01 16:52:38 +03:00
|
|
|
func (request *Request) GetCompiledOperators() []*operators.Operators {
|
|
|
|
|
return []*operators.Operators{request.CompiledOperators}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 21:53:03 +03:00
|
|
|
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
|
2021-02-21 16:31:34 +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"]),
|
2021-07-12 17:20:01 +03:00
|
|
|
Info: wrapped.InternalEvent["template-info"].(model.Info),
|
2021-11-22 17:53:25 +05:30
|
|
|
Type: types.ToString(wrapped.InternalEvent["type"]),
|
2021-02-21 16:31:34 +05:30
|
|
|
Host: types.ToString(wrapped.InternalEvent["host"]),
|
|
|
|
|
Matched: types.ToString(wrapped.InternalEvent["matched"]),
|
|
|
|
|
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
|
|
|
|
|
Timestamp: time.Now(),
|
2021-11-22 17:53:25 +05:30
|
|
|
MatcherStatus: true,
|
2021-02-21 16:31:34 +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"]),
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
|
|
|
|
return data
|
|
|
|
|
}
|