package workflows
import (
"fmt"
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/stringslice"
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
templateTypes "github.com/projectdiscovery/nuclei/v3/pkg/templates/types"
)
type Workflow struct {
Workflows []*WorkflowTemplate `yaml:"workflows,omitempty" json:"workflows,omitempty" jsonschema:"title=list of workflows to execute,description=List of workflows to execute for template"`
Options *protocols.ExecutorOptions `yaml:"-" json:"-"`
}
type WorkflowTemplate struct {
Template string `yaml:"template,omitempty" json:"template,omitempty" jsonschema:"title=template/directory to execute,description=Template or directory to execute as part of workflow"`
Tags stringslice.StringSlice `yaml:"tags,omitempty" json:"tags,omitempty" jsonschema:"title=tags to execute,description=Tags to run template based on"`
Matchers []*Matcher `yaml:"matchers,omitempty" json:"matchers,omitempty" jsonschema:"title=name based template result matchers,description=Matchers perform name based matching to run subtemplates for a workflow"`
Subtemplates []*WorkflowTemplate `yaml:"subtemplates,omitempty" json:"subtemplates,omitempty" jsonschema:"title=subtemplate based result matchers,description=Subtemplates are ran if the template field Template matches"`
Executers []*ProtocolExecuterPair `yaml:"-" json:"-"`
}
type ProtocolExecuterPair struct {
Executer protocols.Executer
Options *protocols.ExecutorOptions
TemplateType templateTypes.ProtocolType
}
type Matcher struct {
Name stringslice.StringSlice `yaml:"name,omitempty" json:"name,omitempty" jsonschema:"title=name of items to match,description=Name of items to match"`
Condition string `yaml:"condition,omitempty" json:"condition,omitempty" jsonschema:"title=condition between names,description=Condition between the names,enum=and,enum=or"`
Subtemplates []*WorkflowTemplate `yaml:"subtemplates,omitempty" json:"subtemplates,omitempty" jsonschema:"title=templates to run after match,description=Templates to run after match"`
condition ConditionType
}
type ConditionType int
const (
ANDCondition ConditionType = iota + 1
ORCondition
)
var ConditionTypes = map[string]ConditionType{
"and": ANDCondition,
"or": ORCondition,
}
func (matcher *Matcher) Compile() error {
var ok bool
if matcher.Condition != "" {
matcher.condition, ok = ConditionTypes[matcher.Condition]
if !ok {
return fmt.Errorf("unknown condition specified: %s", matcher.Condition)
}
} else {
matcher.condition = ORCondition
}
return nil
}
func (matcher *Matcher) Match(result *operators.Result) bool {
names := matcher.Name.ToSlice()
if len(names) == 0 {
return false
}
for i, name := range names {
matchOK := result.HasMatch(name)
extractOK := result.HasExtract(name)
if !matchOK && !extractOK {
if matcher.condition == ANDCondition {
return false
}
continue
}
if matcher.condition == ORCondition {
return true
} else if len(names)-1 == i {
return true
}
}
return false
}