2020-04-04 00:32:03 +05:30
|
|
|
package matchers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2021-11-30 20:20:43 +05:30
|
|
|
"github.com/Knetic/govaluate"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators/common/dsl"
|
2020-04-04 00:32:03 +05:30
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-30 20:36:39 +03:00
|
|
|
func TestWordANDCondition(t *testing.T) {
|
2020-04-04 00:32:03 +05:30
|
|
|
m := &Matcher{condition: ANDCondition, Words: []string{"a", "b"}}
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched := m.MatchWords("a b", nil)
|
2021-09-30 20:36:39 +03:00
|
|
|
require.True(t, isMatched, "Could not match words with valid AND condition")
|
2021-09-29 19:43:46 +03:00
|
|
|
require.Equal(t, m.Words, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched = m.MatchWords("b", nil)
|
2021-09-30 20:36:39 +03:00
|
|
|
require.False(t, isMatched, "Could match words with invalid AND condition")
|
|
|
|
|
require.Equal(t, []string{}, matched)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRegexANDCondition(t *testing.T) {
|
2021-11-19 04:54:09 -06:00
|
|
|
m := &Matcher{Type: MatcherTypeHolder{MatcherType: RegexMatcher}, Condition: "and", Regex: []string{"[a-z]{3}", "\\d{2}"}}
|
2021-09-30 20:36:39 +03:00
|
|
|
err := m.CompileMatchers()
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
isMatched, matched := m.MatchRegex("abc abcd 123")
|
|
|
|
|
require.True(t, isMatched, "Could not match regex with valid AND condition")
|
|
|
|
|
require.Equal(t, []string{"abc", "abc", "12"}, matched)
|
|
|
|
|
|
|
|
|
|
isMatched, matched = m.MatchRegex("bc 1")
|
|
|
|
|
require.False(t, isMatched, "Could match regex with invalid AND condition")
|
2021-09-29 19:43:46 +03:00
|
|
|
require.Equal(t, []string{}, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestORCondition(t *testing.T) {
|
|
|
|
|
m := &Matcher{condition: ORCondition, Words: []string{"a", "b"}}
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched := m.MatchWords("a b", nil)
|
2021-09-30 20:36:39 +03:00
|
|
|
require.True(t, isMatched, "Could not match valid word OR condition")
|
2021-09-29 19:43:46 +03:00
|
|
|
require.Equal(t, []string{"a"}, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched = m.MatchWords("b", nil)
|
2021-09-30 20:36:39 +03:00
|
|
|
require.True(t, isMatched, "Could not match valid word OR condition")
|
2021-09-29 19:43:46 +03:00
|
|
|
require.Equal(t, []string{"b"}, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched = m.MatchWords("c", nil)
|
2021-09-30 20:36:39 +03:00
|
|
|
require.False(t, isMatched, "Could match invalid word OR condition")
|
|
|
|
|
require.Equal(t, []string{}, matched)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRegexOrCondition(t *testing.T) {
|
2021-11-19 04:54:09 -06:00
|
|
|
m := &Matcher{Type: MatcherTypeHolder{MatcherType: RegexMatcher}, Condition: "or", Regex: []string{"[a-z]{3}", "\\d{2}"}}
|
2021-09-30 20:36:39 +03:00
|
|
|
err := m.CompileMatchers()
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
isMatched, matched := m.MatchRegex("ab 123")
|
|
|
|
|
require.True(t, isMatched, "Could not match valid regex OR condition")
|
|
|
|
|
require.Equal(t, []string{"12"}, matched)
|
|
|
|
|
|
|
|
|
|
isMatched, matched = m.MatchRegex("bc 1")
|
|
|
|
|
require.False(t, isMatched, "Could match invalid regex OR condition")
|
2021-09-29 19:43:46 +03:00
|
|
|
require.Equal(t, []string{}, matched)
|
2020-04-04 00:32:03 +05:30
|
|
|
}
|
2021-02-24 11:23:22 +05:30
|
|
|
|
|
|
|
|
func TestHexEncoding(t *testing.T) {
|
2021-11-19 04:54:09 -06:00
|
|
|
m := &Matcher{Encoding: "hex", Type: MatcherTypeHolder{MatcherType: WordsMatcher}, Part: "body", Words: []string{"50494e47"}}
|
2021-02-24 11:23:22 +05:30
|
|
|
err := m.CompileMatchers()
|
|
|
|
|
require.Nil(t, err, "could not compile matcher")
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
isMatched, matched := m.MatchWords("PING", nil)
|
|
|
|
|
require.True(t, isMatched, "Could not match valid Hex condition")
|
|
|
|
|
require.Equal(t, m.Words, matched)
|
2021-02-24 11:23:22 +05:30
|
|
|
}
|
2021-11-30 20:20:43 +05:30
|
|
|
|
|
|
|
|
func TestMatcher_MatchDSL(t *testing.T) {
|
2022-08-23 13:16:41 +05:30
|
|
|
compiled, err := govaluate.NewEvaluableExpressionWithFunctions("contains(body, \"{{VARIABLE}}\")", dsl.HelperFunctions)
|
2021-11-30 20:20:43 +05:30
|
|
|
require.Nil(t, err, "couldn't compile expression")
|
|
|
|
|
|
|
|
|
|
m := &Matcher{Type: MatcherTypeHolder{MatcherType: DSLMatcher}, dslCompiled: []*govaluate.EvaluableExpression{compiled}}
|
|
|
|
|
err = m.CompileMatchers()
|
|
|
|
|
require.Nil(t, err, "could not compile matcher")
|
|
|
|
|
|
|
|
|
|
values := []string{"PING", "pong"}
|
|
|
|
|
|
|
|
|
|
for value := range values {
|
|
|
|
|
isMatched := m.MatchDSL(map[string]interface{}{"body": value, "VARIABLE": value})
|
|
|
|
|
require.True(t, isMatched)
|
|
|
|
|
}
|
|
|
|
|
}
|