Merge pull request #248 from projectdiscovery/add-negative-matchers

Added negative matchers support closing #163
dev
bauthard 2020-08-23 23:22:25 +05:30 committed by GitHub
commit db5bd924a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 12 deletions

View File

@ -12,39 +12,39 @@ import (
func (m *Matcher) Match(resp *http.Response, body, headers string) bool {
switch m.matcherType {
case StatusMatcher:
return m.matchStatusCode(resp.StatusCode)
return m.isNegative(m.matchStatusCode(resp.StatusCode))
case SizeMatcher:
return m.matchSizeCode(len(body))
return m.isNegative(m.matchSizeCode(len(body)))
case WordsMatcher:
// Match the parts as required for word check
if m.part == BodyPart {
return m.matchWords(body)
return m.isNegative(m.matchWords(body))
} else if m.part == HeaderPart {
return m.matchWords(headers)
return m.isNegative(m.matchWords(headers))
} else {
return m.matchWords(headers) || m.matchWords(body)
return m.isNegative(m.matchWords(headers) || m.matchWords(body))
}
case RegexMatcher:
// Match the parts as required for regex check
if m.part == BodyPart {
return m.matchRegex(body)
return m.isNegative(m.matchRegex(body))
} else if m.part == HeaderPart {
return m.matchRegex(headers)
return m.isNegative(m.matchRegex(headers))
} else {
return m.matchRegex(headers) || m.matchRegex(body)
return m.isNegative(m.matchRegex(headers) || m.matchRegex(body))
}
case BinaryMatcher:
// Match the parts as required for binary characters check
if m.part == BodyPart {
return m.matchBinary(body)
return m.isNegative(m.matchBinary(body))
} else if m.part == HeaderPart {
return m.matchBinary(headers)
return m.isNegative(m.matchBinary(headers))
} else {
return m.matchBinary(headers) || m.matchBinary(body)
return m.isNegative(m.matchBinary(headers) || m.matchBinary(body))
}
case DSLMatcher:
// Match complex query
return m.matchDSL(httpToMap(resp, body, headers))
return m.isNegative(m.matchDSL(httpToMap(resp, body, headers)))
}
return false
}

View File

@ -45,6 +45,10 @@ type Matcher struct {
Part string `yaml:"part,omitempty"`
// part is the part of the request to match
part Part
// Negative specifies if the match should be reversed
// It will only match if the condition is not true.
Negative bool `yaml:"negative,omitempty"`
}
// MatcherType is the type of the matcher specified
@ -114,3 +118,12 @@ var PartTypes = map[string]Part{
func (m *Matcher) GetPart() Part {
return m.part
}
// isNegative reverts the results of the match if the matcher
// is of type negative.
func (m *Matcher) isNegative(data bool) bool {
if m.Negative {
return !data
}
return data
}