65 lines
1.5 KiB
Go
Raw Normal View History

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"
2020-04-26 23:32:58 +02:00
"github.com/Knetic/govaluate"
"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.
switch m.Encoding {
case "hex":
for i, word := range m.Words {
if decoded, err := hex.DecodeString(word); err == nil && len(decoded) > 0 {
m.Words[i] = string(decoded)
}
}
}
2020-04-04 00:16:27 +05:30
// Setup the matcher type
m.matcherType, ok = MatcherTypes[m.Type]
if !ok {
return fmt.Errorf("unknown matcher type specified: %s", m.Type)
}
2020-12-24 12:13:18 +05:30
// By default, match on body if user hasn't provided any specific items
if m.Part == "" {
2020-12-24 12:13:18 +05:30
m.Part = "body"
}
2020-04-04 00:16:27 +05:30
// 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)
}
2020-04-26 23:32:58 +02:00
// Compile the dsl expressions
for _, expr := range m.DSL {
compiled, err := govaluate.NewEvaluableExpressionWithFunctions(expr, dsl.HelperFunctions())
2020-04-26 23:32:58 +02:00
if err != nil {
return fmt.Errorf("could not compile dsl: %s", expr)
2020-04-26 23:32:58 +02:00
}
m.dslCompiled = append(m.dslCompiled, compiled)
}
2020-04-04 00:16:27 +05:30
// Setup the condition type, if any.
if m.Condition != "" {
m.condition, ok = ConditionTypes[m.Condition]
if !ok {
return fmt.Errorf("unknown condition specified: %s", m.Condition)
}
} else {
m.condition = ORCondition
}
return nil
}