2020-04-03 18:46:27 +00:00
|
|
|
package matchers
|
|
|
|
|
|
|
|
import (
|
2020-04-21 18:50:35 +00:00
|
|
|
"encoding/hex"
|
2020-04-03 18:46:27 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
// MatchStatusCode matches a status code check against a corpus
|
|
|
|
func (m *Matcher) MatchStatusCode(statusCode int) bool {
|
2020-04-03 18:46:27 +00:00
|
|
|
// Iterate over all the status codes accepted as valid
|
2020-04-03 19:02:03 +00:00
|
|
|
//
|
|
|
|
// Status codes don't support AND conditions.
|
2020-04-03 18:46:27 +00:00
|
|
|
for _, status := range m.Status {
|
|
|
|
// Continue if the status codes don't match
|
|
|
|
if statusCode != status {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-03 19:02:03 +00:00
|
|
|
// Return on the first match.
|
|
|
|
return true
|
2020-04-03 18:46:27 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
// MatchSize matches a size check against a corpus
|
|
|
|
func (m *Matcher) MatchSize(length int) bool {
|
2020-04-03 18:46:27 +00:00
|
|
|
// Iterate over all the sizes accepted as valid
|
2020-04-03 19:02:03 +00:00
|
|
|
//
|
|
|
|
// Sizes codes don't support AND conditions.
|
2020-04-03 18:46:27 +00:00
|
|
|
for _, size := range m.Size {
|
|
|
|
// Continue if the size doesn't match
|
|
|
|
if length != size {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-03 19:02:03 +00:00
|
|
|
// Return on the first match.
|
|
|
|
return true
|
2020-04-03 18:46:27 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
// MatchWords matches a word check against a corpus.
|
|
|
|
func (m *Matcher) MatchWords(corpus string) bool {
|
2020-04-03 18:46:27 +00:00
|
|
|
// Iterate over all the words accepted as valid
|
2020-04-03 19:02:03 +00:00
|
|
|
for i, word := range m.Words {
|
2020-04-03 18:46:27 +00:00
|
|
|
// Continue if the word doesn't match
|
2020-08-25 21:24:31 +00:00
|
|
|
if !strings.Contains(corpus, word) {
|
2020-04-03 18:46:27 +00:00
|
|
|
// If we are in an AND request and a match failed,
|
|
|
|
// return false as the AND condition fails on any single mismatch.
|
|
|
|
if m.condition == ANDCondition {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Continue with the flow since its an OR Condition.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the condition was an OR, return on the first match.
|
|
|
|
if m.condition == ORCondition {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-03 19:02:03 +00:00
|
|
|
|
|
|
|
// If we are at the end of the words, return with true
|
|
|
|
if len(m.Words)-1 == i {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-03 18:46:27 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
// MatchRegex matches a regex check against a corpus
|
|
|
|
func (m *Matcher) MatchRegex(corpus string) bool {
|
2020-04-03 18:46:27 +00:00
|
|
|
// Iterate over all the regexes accepted as valid
|
2020-04-03 19:02:03 +00:00
|
|
|
for i, regex := range m.regexCompiled {
|
2020-04-03 18:46:27 +00:00
|
|
|
// Continue if the regex doesn't match
|
|
|
|
if !regex.MatchString(corpus) {
|
|
|
|
// If we are in an AND request and a match failed,
|
|
|
|
// return false as the AND condition fails on any single mismatch.
|
|
|
|
if m.condition == ANDCondition {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Continue with the flow since its an OR Condition.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the condition was an OR, return on the first match.
|
|
|
|
if m.condition == ORCondition {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-03 19:02:03 +00:00
|
|
|
|
|
|
|
// If we are at the end of the regex, return with true
|
|
|
|
if len(m.regexCompiled)-1 == i {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-03 18:46:27 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-04-21 18:50:35 +00:00
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
// MatchBinary matches a binary check against a corpus
|
|
|
|
func (m *Matcher) MatchBinary(corpus string) bool {
|
2020-04-21 18:50:35 +00:00
|
|
|
// Iterate over all the words accepted as valid
|
|
|
|
for i, binary := range m.Binary {
|
|
|
|
// Continue if the word doesn't match
|
|
|
|
hexa, _ := hex.DecodeString(binary)
|
|
|
|
if !strings.Contains(corpus, string(hexa)) {
|
|
|
|
// If we are in an AND request and a match failed,
|
|
|
|
// return false as the AND condition fails on any single mismatch.
|
|
|
|
if m.condition == ANDCondition {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Continue with the flow since its an OR Condition.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the condition was an OR, return on the first match.
|
|
|
|
if m.condition == ORCondition {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are at the end of the words, return with true
|
|
|
|
if len(m.Binary)-1 == i {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-04-26 21:32:58 +00:00
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
// MatchDSL matches on a generic map result
|
|
|
|
func (m *Matcher) MatchDSL(data map[string]interface{}) bool {
|
2020-11-25 17:27:14 +00:00
|
|
|
// Iterate over all the expressions accepted as valid
|
2020-04-26 21:32:58 +00:00
|
|
|
for i, expression := range m.dslCompiled {
|
2020-12-24 15:17:41 +00:00
|
|
|
result, err := expression.Evaluate(data)
|
2020-04-26 21:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-25 21:24:31 +00:00
|
|
|
|
2020-04-26 21:32:58 +00:00
|
|
|
var bResult bool
|
|
|
|
bResult, ok := result.(bool)
|
|
|
|
|
|
|
|
// Continue if the regex doesn't match
|
|
|
|
if !ok || !bResult {
|
|
|
|
// If we are in an AND request and a match failed,
|
|
|
|
// return false as the AND condition fails on any single mismatch.
|
|
|
|
if m.condition == ANDCondition {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Continue with the flow since its an OR Condition.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the condition was an OR, return on the first match.
|
|
|
|
if m.condition == ORCondition {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are at the end of the dsl, return with true
|
|
|
|
if len(m.dslCompiled)-1 == i {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|