package matchers
import (
"os"
"strings"
"github.com/Knetic/govaluate"
"github.com/antchfx/htmlquery"
"github.com/antchfx/xmlquery"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/operators/common/dsl"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/expressions"
stringsutil "github.com/projectdiscovery/utils/strings"
)
var (
showDSLErr = strings.EqualFold(os.Getenv("SHOW_DSL_ERRORS"), "true")
)
func (matcher *Matcher) MatchStatusCode(statusCode int) bool {
for _, status := range matcher.Status {
if statusCode != status {
continue
}
return true
}
return false
}
func (matcher *Matcher) MatchSize(length int) bool {
for _, size := range matcher.Size {
if length != size {
continue
}
return true
}
return false
}
func (matcher *Matcher) MatchWords(corpus string, data map[string]interface{}) (bool, []string) {
if matcher.CaseInsensitive {
corpus = strings.ToLower(corpus)
}
var matchedWords []string
for i, word := range matcher.Words {
if data == nil {
data = make(map[string]interface{})
}
var err error
word, err = expressions.Evaluate(word, data)
if err != nil {
gologger.Warning().Msgf("Error while evaluating word matcher: %q", word)
if matcher.condition == ANDCondition {
return false, []string{}
}
}
if !strings.Contains(corpus, word) {
switch matcher.condition {
case ANDCondition:
return false, []string{}
case ORCondition:
continue
}
}
if matcher.condition == ORCondition && !matcher.MatchAll {
return true, []string{word}
}
matchedWords = append(matchedWords, word)
if len(matcher.Words)-1 == i && !matcher.MatchAll {
return true, matchedWords
}
}
if len(matchedWords) > 0 && matcher.MatchAll {
return true, matchedWords
}
return false, []string{}
}
func (matcher *Matcher) MatchRegex(corpus string) (bool, []string) {
var matchedRegexes []string
for i, regex := range matcher.regexCompiled {
if !regex.MatchString(corpus) {
switch matcher.condition {
case ANDCondition:
return false, []string{}
case ORCondition:
continue
}
}
currentMatches := regex.FindAllString(corpus, -1)
if matcher.condition == ORCondition && !matcher.MatchAll {
return true, currentMatches
}
matchedRegexes = append(matchedRegexes, currentMatches...)
if len(matcher.regexCompiled)-1 == i && !matcher.MatchAll {
return true, matchedRegexes
}
}
if len(matchedRegexes) > 0 && matcher.MatchAll {
return true, matchedRegexes
}
return false, []string{}
}
func (matcher *Matcher) MatchBinary(corpus string) (bool, []string) {
var matchedBinary []string
for i, binary := range matcher.binaryDecoded {
if !strings.Contains(corpus, binary) {
switch matcher.condition {
case ANDCondition:
return false, []string{}
case ORCondition:
continue
}
}
if matcher.condition == ORCondition {
return true, []string{binary}
}
matchedBinary = append(matchedBinary, binary)
if len(matcher.Binary)-1 == i {
return true, matchedBinary
}
}
return false, []string{}
}
func (matcher *Matcher) MatchDSL(data map[string]interface{}) bool {
logExpressionEvaluationFailure := func(matcherName string, err error) {
gologger.Warning().Msgf("Could not evaluate expression: %s, error: %s", matcherName, err.Error())
}
for i, expression := range matcher.dslCompiled {
if varErr := expressions.ContainsUnresolvedVariables(expression.String()); varErr != nil {
resolvedExpression, err := expressions.Evaluate(expression.String(), data)
if err != nil {
logExpressionEvaluationFailure(matcher.Name, err)
return false
}
expression, err = govaluate.NewEvaluableExpressionWithFunctions(resolvedExpression, dsl.HelperFunctions)
if err != nil {
logExpressionEvaluationFailure(matcher.Name, err)
return false
}
}
result, err := expression.Evaluate(data)
if err != nil {
if matcher.condition == ANDCondition {
return false
}
if !matcher.ignoreErr(err) {
gologger.Warning().Msgf("[%s] %s", data["template-id"], err.Error())
}
continue
}
if boolResult, ok := result.(bool); !ok {
gologger.Error().Label("WRN").Msgf("[%s] The return value of a DSL statement must return a boolean value.", data["template-id"])
continue
} else if !boolResult {
switch matcher.condition {
case ANDCondition:
return false
case ORCondition:
continue
}
}
if matcher.condition == ORCondition {
return true
}
if len(matcher.dslCompiled)-1 == i {
return true
}
}
return false
}
func (matcher *Matcher) MatchXPath(corpus string) bool {
if strings.HasPrefix(corpus, "<?xml") {
return matcher.MatchXML(corpus)
}
return matcher.MatchHTML(corpus)
}
func (matcher *Matcher) MatchHTML(corpus string) bool {
doc, err := htmlquery.Parse(strings.NewReader(corpus))
if err != nil {
return false
}
matches := 0
for _, k := range matcher.XPath {
nodes, err := htmlquery.QueryAll(doc, k)
if err != nil {
continue
}
if len(nodes) == 0 {
switch matcher.condition {
case ANDCondition:
return false
case ORCondition:
continue
}
}
if matcher.condition == ORCondition && !matcher.MatchAll {
return true
}
matches = matches + len(nodes)
}
return matches > 0
}
func (matcher *Matcher) MatchXML(corpus string) bool {
doc, err := xmlquery.Parse(strings.NewReader(corpus))
if err != nil {
return false
}
matches := 0
for _, k := range matcher.XPath {
nodes, err := xmlquery.QueryAll(doc, k)
if err != nil {
continue
}
if len(nodes) == 0 {
switch matcher.condition {
case ANDCondition:
return false
case ORCondition:
continue
}
}
if matcher.condition == ORCondition && !matcher.MatchAll {
return true
}
matches = matches + len(nodes)
}
return matches > 0
}
func (m *Matcher) ignoreErr(err error) bool {
if showDSLErr {
return false
}
if stringsutil.ContainsAny(err.Error(), "No parameter", "error parsing argument value") {
return true
}
return false
}