2020-04-04 00:16:27 +05:30
|
|
|
package matchers
|
|
|
|
|
|
|
|
|
|
import (
|
2021-02-24 11:23:22 +05:30
|
|
|
"encoding/hex"
|
2020-04-04 00:16:27 +05:30
|
|
|
"fmt"
|
|
|
|
|
"regexp"
|
2021-10-29 19:08:18 +03:00
|
|
|
"strings"
|
2020-04-26 23:32:58 +02:00
|
|
|
|
|
|
|
|
"github.com/Knetic/govaluate"
|
2021-09-07 17:31:46 +03:00
|
|
|
|
2020-12-23 16:16:16 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators/common/dsl"
|
2020-04-04 00:16:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CompileMatchers performs the initial setup operation on a matcher
|
|
|
|
|
func (m *Matcher) CompileMatchers() error {
|
|
|
|
|
var ok bool
|
|
|
|
|
|
2021-02-24 11:23:22 +05:30
|
|
|
// Support hexadecimal encoding for matchers too.
|
2021-02-26 13:13:11 +05:30
|
|
|
if m.Encoding == "hex" {
|
2021-02-24 11:23:22 +05:30
|
|
|
for i, word := range m.Words {
|
|
|
|
|
if decoded, err := hex.DecodeString(word); err == nil && len(decoded) > 0 {
|
|
|
|
|
m.Words[i] = string(decoded)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:31:46 +03:00
|
|
|
// Set up the matcher type
|
2020-04-04 00:16:27 +05:30
|
|
|
m.matcherType, ok = MatcherTypes[m.Type]
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("unknown matcher type specified: %s", m.Type)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compile the regexes
|
|
|
|
|
for _, regex := range m.Regex {
|
|
|
|
|
compiled, err := regexp.Compile(regex)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("could not compile regex: %s", regex)
|
|
|
|
|
}
|
|
|
|
|
m.regexCompiled = append(m.regexCompiled, compiled)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 02:04:27 +05:30
|
|
|
// Compile and validate binary Values in matcher
|
|
|
|
|
for _, value := range m.Binary {
|
|
|
|
|
if decoded, err := hex.DecodeString(value); err != nil {
|
|
|
|
|
return fmt.Errorf("could not hex decode binary: %s", value)
|
|
|
|
|
} else {
|
|
|
|
|
m.binaryDecoded = append(m.binaryDecoded, string(decoded))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 23:32:58 +02:00
|
|
|
// Compile the dsl expressions
|
2020-12-23 16:16:16 +05:30
|
|
|
for _, expr := range m.DSL {
|
|
|
|
|
compiled, err := govaluate.NewEvaluableExpressionWithFunctions(expr, dsl.HelperFunctions())
|
2020-04-26 23:32:58 +02:00
|
|
|
if err != nil {
|
2020-12-23 16:16:16 +05:30
|
|
|
return fmt.Errorf("could not compile dsl: %s", expr)
|
2020-04-26 23:32:58 +02:00
|
|
|
}
|
|
|
|
|
m.dslCompiled = append(m.dslCompiled, compiled)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:31:46 +03:00
|
|
|
// Set up the condition type, if any.
|
2020-04-04 00:16:27 +05:30
|
|
|
if m.Condition != "" {
|
|
|
|
|
m.condition, ok = ConditionTypes[m.Condition]
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("unknown condition specified: %s", m.Condition)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m.condition = ORCondition
|
|
|
|
|
}
|
2021-10-29 19:08:18 +03:00
|
|
|
|
|
|
|
|
if m.CaseInsensitive {
|
|
|
|
|
if m.Type != "word" {
|
|
|
|
|
return fmt.Errorf("case-insensitive flag is supported only for 'word' matchers (not '%s')", m.Type)
|
|
|
|
|
}
|
|
|
|
|
for i := range m.Words {
|
|
|
|
|
m.Words[i] = strings.ToLower(m.Words[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-04 00:16:27 +05:30
|
|
|
return nil
|
|
|
|
|
}
|