2020-04-04 00:16:27 +05:30
|
|
|
package matchers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
2021-09-15 18:02:22 +05:30
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/expressions"
|
2020-04-04 00:16:27 +05:30
|
|
|
)
|
|
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
// MatchStatusCode matches a status code check against a corpus
|
|
|
|
|
func (m *Matcher) MatchStatusCode(statusCode int) bool {
|
2020-04-04 00:16:27 +05:30
|
|
|
// Iterate over all the status codes accepted as valid
|
2020-04-04 00:32:03 +05:30
|
|
|
//
|
|
|
|
|
// Status codes don't support AND conditions.
|
2020-04-04 00:16:27 +05:30
|
|
|
for _, status := range m.Status {
|
|
|
|
|
// Continue if the status codes don't match
|
|
|
|
|
if statusCode != status {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-04-04 00:32:03 +05:30
|
|
|
// Return on the first match.
|
|
|
|
|
return true
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
// MatchSize matches a size check against a corpus
|
|
|
|
|
func (m *Matcher) MatchSize(length int) bool {
|
2020-04-04 00:16:27 +05:30
|
|
|
// Iterate over all the sizes accepted as valid
|
2020-04-04 00:32:03 +05:30
|
|
|
//
|
|
|
|
|
// Sizes codes don't support AND conditions.
|
2020-04-04 00:16:27 +05:30
|
|
|
for _, size := range m.Size {
|
|
|
|
|
// Continue if the size doesn't match
|
|
|
|
|
if length != size {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-04-04 00:32:03 +05:30
|
|
|
// Return on the first match.
|
|
|
|
|
return true
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
// MatchWords matches a word check against a corpus.
|
2021-09-29 19:43:46 +03:00
|
|
|
func (m *Matcher) MatchWords(corpus string, dynamicValues map[string]interface{}) (bool, []string) {
|
2021-10-29 19:08:18 +03:00
|
|
|
if m.CaseInsensitive {
|
|
|
|
|
corpus = strings.ToLower(corpus)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
var matchedWords []string
|
2020-04-04 00:16:27 +05:30
|
|
|
// Iterate over all the words accepted as valid
|
2020-04-04 00:32:03 +05:30
|
|
|
for i, word := range m.Words {
|
2021-09-15 18:02:22 +05:30
|
|
|
if dynamicValues == nil {
|
|
|
|
|
dynamicValues = make(map[string]interface{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
word, err = expressions.Evaluate(word, dynamicValues)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-04-04 00:16:27 +05:30
|
|
|
// Continue if the word doesn't match
|
2020-08-25 23:24:31 +02:00
|
|
|
if !strings.Contains(corpus, word) {
|
2020-04-04 00:16:27 +05:30
|
|
|
// 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 {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
2021-09-07 17:31:46 +03:00
|
|
|
// Continue with the flow since it's an OR Condition.
|
2020-04-04 00:16:27 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the condition was an OR, return on the first match.
|
|
|
|
|
if m.condition == ORCondition {
|
2021-09-29 19:43:46 +03:00
|
|
|
return true, []string{word}
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
2020-04-04 00:32:03 +05:30
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
matchedWords = append(matchedWords, word)
|
|
|
|
|
|
2020-04-04 00:32:03 +05:30
|
|
|
// If we are at the end of the words, return with true
|
|
|
|
|
if len(m.Words)-1 == i {
|
2021-09-29 19:43:46 +03:00
|
|
|
return true, matchedWords
|
2020-04-04 00:32:03 +05:30
|
|
|
}
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
|
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
// MatchRegex matches a regex check against a corpus
|
2021-09-29 19:43:46 +03:00
|
|
|
func (m *Matcher) MatchRegex(corpus string) (bool, []string) {
|
2021-09-30 20:36:39 +03:00
|
|
|
var matchedRegexes []string
|
2020-04-04 00:16:27 +05:30
|
|
|
// Iterate over all the regexes accepted as valid
|
2020-04-04 00:32:03 +05:30
|
|
|
for i, regex := range m.regexCompiled {
|
2020-04-04 00:16:27 +05:30
|
|
|
// 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 {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
2021-09-07 17:31:46 +03:00
|
|
|
// Continue with the flow since it's an OR Condition.
|
2020-04-04 00:16:27 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 20:36:39 +03:00
|
|
|
currentMatches := regex.FindAllString(corpus, -1)
|
2020-04-04 00:16:27 +05:30
|
|
|
// If the condition was an OR, return on the first match.
|
|
|
|
|
if m.condition == ORCondition {
|
2021-09-30 20:36:39 +03:00
|
|
|
return true, currentMatches
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
2020-04-04 00:32:03 +05:30
|
|
|
|
2021-09-30 20:36:39 +03:00
|
|
|
matchedRegexes = append(matchedRegexes, currentMatches...)
|
|
|
|
|
|
2020-04-04 00:32:03 +05:30
|
|
|
// If we are at the end of the regex, return with true
|
|
|
|
|
if len(m.regexCompiled)-1 == i {
|
2021-09-30 20:36:39 +03:00
|
|
|
return true, matchedRegexes
|
2020-04-04 00:32:03 +05:30
|
|
|
}
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-04-04 00:16:27 +05:30
|
|
|
}
|
2020-04-21 20:50:35 +02:00
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
// MatchBinary matches a binary check against a corpus
|
2021-09-29 19:43:46 +03:00
|
|
|
func (m *Matcher) MatchBinary(corpus string) (bool, []string) {
|
2021-09-30 20:36:39 +03:00
|
|
|
var matchedBinary []string
|
2020-04-21 20:50:35 +02:00
|
|
|
// Iterate over all the words accepted as valid
|
2021-11-17 02:04:27 +05:30
|
|
|
for i, binary := range m.binaryDecoded {
|
|
|
|
|
if !strings.Contains(corpus, binary) {
|
2020-04-21 20:50:35 +02: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 {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-04-21 20:50:35 +02:00
|
|
|
}
|
2021-09-07 17:31:46 +03:00
|
|
|
// Continue with the flow since it's an OR Condition.
|
2020-04-21 20:50:35 +02:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the condition was an OR, return on the first match.
|
|
|
|
|
if m.condition == ORCondition {
|
2021-11-17 02:04:27 +05:30
|
|
|
return true, []string{binary}
|
2020-04-21 20:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-17 02:04:27 +05:30
|
|
|
matchedBinary = append(matchedBinary, binary)
|
2021-09-30 20:36:39 +03:00
|
|
|
|
2020-04-21 20:50:35 +02:00
|
|
|
// If we are at the end of the words, return with true
|
|
|
|
|
if len(m.Binary)-1 == i {
|
2021-09-30 20:36:39 +03:00
|
|
|
return true, matchedBinary
|
2020-04-21 20:50:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-04-21 20:50:35 +02:00
|
|
|
}
|
2020-04-26 23:32:58 +02:00
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
// MatchDSL matches on a generic map result
|
|
|
|
|
func (m *Matcher) MatchDSL(data map[string]interface{}) bool {
|
2020-11-25 18:27:14 +01:00
|
|
|
// Iterate over all the expressions accepted as valid
|
2020-04-26 23:32:58 +02:00
|
|
|
for i, expression := range m.dslCompiled {
|
2020-12-24 20:47:41 +05:30
|
|
|
result, err := expression.Evaluate(data)
|
2020-04-26 23:32:58 +02:00
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-26 23:32:58 +02: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
|
|
|
|
|
}
|
2021-09-07 17:31:46 +03:00
|
|
|
// Continue with the flow since it's an OR Condition.
|
2020-04-26 23:32:58 +02:00
|
|
|
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
|
|
|
|
|
}
|