package operators
import (
"fmt"
"maps"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"
"github.com/projectdiscovery/nuclei/v3/pkg/operators/matchers"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/generators"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/utils/excludematchers"
sliceutil "github.com/projectdiscovery/utils/slice"
)
type Operators struct {
Matchers []*matchers.Matcher `yaml:"matchers,omitempty" json:"matchers,omitempty" jsonschema:"title=matchers to run on response,description=Detection mechanism to identify whether the request was successful by doing pattern matching"`
Extractors []*extractors.Extractor `yaml:"extractors,omitempty" json:"extractors,omitempty" jsonschema:"title=extractors to run on response,description=Extractors contains the extraction mechanism for the request to identify and extract parts of the response"`
MatchersCondition string `yaml:"matchers-condition,omitempty" json:"matchers-condition,omitempty" jsonschema:"title=condition between the matchers,description=Conditions between the matchers,enum=and,enum=or"`
matchersCondition matchers.ConditionType
TemplateID string `json:"-" yaml:"-" jsonschema:"-"`
ExcludeMatchers *excludematchers.ExcludeMatchers `json:"-" yaml:"-" jsonschema:"-"`
}
func (operators *Operators) Compile() error {
if operators.MatchersCondition != "" {
operators.matchersCondition = matchers.ConditionTypes[operators.MatchersCondition]
} else {
operators.matchersCondition = matchers.ORCondition
}
for _, matcher := range operators.Matchers {
if err := matcher.CompileMatchers(); err != nil {
return errors.Wrap(err, "could not compile matcher")
}
}
for _, extractor := range operators.Extractors {
if err := extractor.CompileExtractors(); err != nil {
return errors.Wrap(err, "could not compile extractor")
}
}
return nil
}
func (operators *Operators) HasDSL() bool {
for _, matcher := range operators.Matchers {
if len(matcher.DSL) > 0 {
return true
}
}
for _, extractor := range operators.Extractors {
if len(extractor.DSL) > 0 {
return true
}
}
return false
}
func (operators *Operators) GetMatchersCondition() matchers.ConditionType {
return operators.matchersCondition
}
type Result struct {
Matched bool
Extracted bool
Matches map[string][]string
Extracts map[string][]string
OutputExtracts []string
outputUnique map[string]struct{}
DynamicValues map[string][]string
PayloadValues map[string]interface{}
LineCount string
Operators *Operators
}
func (result *Result) HasMatch(name string) bool {
return result.hasItem(name, result.Matches)
}
func (result *Result) HasExtract(name string) bool {
return result.hasItem(name, result.Extracts)
}
func (result *Result) hasItem(name string, m map[string][]string) bool {
for matchName := range m {
if strings.EqualFold(name, matchName) {
return true
}
}
return false
}
func MakeDynamicValuesCallback(input map[string][]string, iterateAllValues bool, callback func(map[string]interface{}) bool) {
output := make(map[string]interface{}, len(input))
if !iterateAllValues {
for k, v := range input {
if len(v) > 0 {
output[k] = v[0]
}
}
callback(output)
return
}
inputIndex := make(map[string]int, len(input))
var maxValue int
for _, v := range input {
if len(v) > maxValue {
maxValue = len(v)
}
}
for i := 0; i < maxValue; i++ {
for k, v := range input {
if len(v) == 0 {
continue
}
if len(v) == 1 {
output[k] = v[0]
continue
}
if gotIndex, ok := inputIndex[k]; !ok {
inputIndex[k] = 0
output[k] = v[0]
} else {
newIndex := gotIndex + 1
if newIndex >= len(v) {
output[k] = v[len(v)-1]
continue
}
output[k] = v[newIndex]
inputIndex[k] = newIndex
}
}
if callback(output) {
return
}
}
}
func (r *Result) Merge(result *Result) {
if !r.Matched && result.Matched {
r.Matched = result.Matched
}
if !r.Extracted && result.Extracted {
r.Extracted = result.Extracted
}
for k, v := range result.Matches {
r.Matches[k] = sliceutil.Dedupe(append(r.Matches[k], v...))
}
for k, v := range result.Extracts {
r.Extracts[k] = sliceutil.Dedupe(append(r.Extracts[k], v...))
}
r.outputUnique = make(map[string]struct{})
output := r.OutputExtracts
r.OutputExtracts = make([]string, 0, len(output))
for _, v := range output {
if _, ok := r.outputUnique[v]; !ok {
r.outputUnique[v] = struct{}{}
r.OutputExtracts = append(r.OutputExtracts, v)
}
}
for _, v := range result.OutputExtracts {
if _, ok := r.outputUnique[v]; !ok {
r.outputUnique[v] = struct{}{}
r.OutputExtracts = append(r.OutputExtracts, v)
}
}
for k, v := range result.DynamicValues {
if _, ok := r.DynamicValues[k]; !ok {
r.DynamicValues[k] = v
} else {
r.DynamicValues[k] = sliceutil.Dedupe(append(r.DynamicValues[k], v...))
}
}
maps.Copy(r.PayloadValues, result.PayloadValues)
}
type MatchFunc func(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string)
type ExtractFunc func(data map[string]interface{}, matcher *extractors.Extractor) map[string]struct{}
func (operators *Operators) Execute(data map[string]interface{}, match MatchFunc, extract ExtractFunc, isDebug bool) (*Result, bool) {
matcherCondition := operators.GetMatchersCondition()
var matches bool
result := &Result{
Matches: make(map[string][]string),
Extracts: make(map[string][]string),
DynamicValues: make(map[string][]string),
outputUnique: make(map[string]struct{}),
Operators: operators,
}
var allInternalExtractors = true
for _, extractor := range operators.Extractors {
if !extractor.Internal && allInternalExtractors {
allInternalExtractors = false
}
var extractorResults []string
for match := range extract(data, extractor) {
extractorResults = append(extractorResults, match)
if extractor.Internal {
if data, ok := result.DynamicValues[extractor.Name]; !ok {
result.DynamicValues[extractor.Name] = []string{match}
} else {
result.DynamicValues[extractor.Name] = append(data, match)
}
} else {
if _, ok := result.outputUnique[match]; !ok {
result.OutputExtracts = append(result.OutputExtracts, match)
result.outputUnique[match] = struct{}{}
}
}
}
if len(extractorResults) > 0 && !extractor.Internal && extractor.Name != "" {
result.Extracts[extractor.Name] = extractorResults
}
if len(extractorResults) > 0 {
data[extractor.Name] = getExtractedValue(extractorResults)
}
}
if len(result.DynamicValues) > 0 {
dataDynamicValues := make(map[string]interface{})
for dynName, dynValues := range result.DynamicValues {
if len(dynValues) > 1 {
for dynIndex, dynValue := range dynValues {
dynKeyName := fmt.Sprintf("%s%d", dynName, dynIndex)
dataDynamicValues[dynKeyName] = dynValue
}
dataDynamicValues[dynName] = dynValues
} else {
dataDynamicValues[dynName] = dynValues[0]
}
}
data = generators.MergeMaps(data, dataDynamicValues)
}
for matcherIndex, matcher := range operators.Matchers {
if operators.ExcludeMatchers != nil {
if operators.ExcludeMatchers.Match(operators.TemplateID, matcher.Name) {
continue
}
}
if isMatch, matched := match(data, matcher); isMatch {
if isDebug {
matcherName := GetMatcherName(matcher, matcherIndex)
result.Matches[matcherName] = matched
} else {
if matcherCondition == matchers.ORCondition && matcher.Name != "" {
result.Matches[matcher.Name] = matched
}
}
matches = true
} else if matcherCondition == matchers.ANDCondition {
if len(result.DynamicValues) > 0 {
return result, true
}
return result, false
}
}
result.Matched = matches
result.Extracted = len(result.OutputExtracts) > 0
if len(result.DynamicValues) > 0 && allInternalExtractors {
return result, true
}
if len(operators.Matchers) > 0 && !matches {
if len(result.DynamicValues) > 0 {
return result, true
}
return nil, false
}
if len(result.Extracts) > 0 || len(result.OutputExtracts) > 0 || matches {
return result, true
}
if len(result.DynamicValues) > 0 {
return result, true
}
return nil, false
}
func GetMatcherName(matcher *matchers.Matcher, matcherIndex int) string {
if matcher.Name != "" {
return matcher.Name
} else {
return matcher.Type.String() + "-" + strconv.Itoa(matcherIndex+1)
}
}
func (operators *Operators) ExecuteInternalExtractors(data map[string]interface{}, extract ExtractFunc) map[string]interface{} {
dynamicValues := make(map[string]interface{})
for _, extractor := range operators.Extractors {
if !extractor.Internal {
continue
}
for match := range extract(data, extractor) {
if _, ok := dynamicValues[extractor.Name]; !ok {
dynamicValues[extractor.Name] = match
}
}
}
return dynamicValues
}
func (operators *Operators) IsEmpty() bool {
return operators.Len() == 0
}
func (operators *Operators) Len() int {
return len(operators.Matchers) + len(operators.Extractors)
}
func getExtractedValue(values []string) any {
if len(values) == 1 {
return values[0]
} else {
return values
}
}
func EvalBoolSlice(slice []bool, isAnd bool) bool {
if len(slice) == 0 {
return false
}
result := slice[0]
for _, b := range slice[1:] {
if isAnd {
result = result && b
} else {
result = result || b
}
}
return result
}