2020-11-12 23:28:24 +05:30
|
|
|
package workflows
|
|
|
|
|
|
2021-01-01 19:36:21 +05:30
|
|
|
import (
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
)
|
2020-11-12 23:28:24 +05:30
|
|
|
|
2020-12-26 02:08:48 +05:30
|
|
|
// RunWorkflow runs a workflow on an input and returns true or false
|
2020-12-29 18:02:45 +05:30
|
|
|
func (w *Workflow) RunWorkflow(input string) (bool, error) {
|
2020-12-26 02:08:48 +05:30
|
|
|
results := &atomic.Bool{}
|
2020-11-12 23:28:24 +05:30
|
|
|
|
|
|
|
|
for _, template := range w.Workflows {
|
2020-12-26 02:08:48 +05:30
|
|
|
err := w.runWorkflowStep(template, input, results)
|
|
|
|
|
if err != nil {
|
2020-12-29 20:32:04 +05:30
|
|
|
return results.Load(), err
|
2020-11-12 23:31:38 +05:30
|
|
|
}
|
2020-11-12 23:28:24 +05:30
|
|
|
}
|
2020-12-26 02:08:48 +05:30
|
|
|
return results.Load(), nil
|
2020-11-12 23:28:24 +05:30
|
|
|
}
|
|
|
|
|
|
2020-12-26 02:08:48 +05:30
|
|
|
// runWorkflowStep runs a workflow step for the workflow. It executes the workflow
|
|
|
|
|
// in a recursive manner running all subtemplates and matchers.
|
|
|
|
|
func (w *Workflow) runWorkflowStep(template *WorkflowTemplate, input string, results *atomic.Bool) error {
|
2020-12-26 13:20:56 +05:30
|
|
|
var firstMatched bool
|
|
|
|
|
if len(template.Matchers) == 0 {
|
2020-12-29 18:02:45 +05:30
|
|
|
w.options.Progress.AddToTotal(int64(template.Executer.Requests()))
|
|
|
|
|
|
|
|
|
|
matched, err := template.Executer.Execute(input)
|
2020-12-26 13:20:56 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-12-29 20:32:04 +05:30
|
|
|
if matched {
|
|
|
|
|
firstMatched = matched
|
|
|
|
|
results.CAS(false, matched)
|
|
|
|
|
}
|
2020-12-26 13:20:56 +05:30
|
|
|
}
|
|
|
|
|
|
2020-11-12 23:28:24 +05:30
|
|
|
if len(template.Matchers) > 0 {
|
2020-12-29 18:02:45 +05:30
|
|
|
w.options.Progress.AddToTotal(int64(template.Executer.Requests()))
|
|
|
|
|
|
2021-01-01 19:36:21 +05:30
|
|
|
var executionErr error
|
|
|
|
|
err := template.Executer.ExecuteWithResults(input, func(event *output.InternalWrappedEvent) {
|
|
|
|
|
if event.OperatorsResult == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-12-26 02:08:48 +05:30
|
|
|
|
2021-01-01 19:36:21 +05:30
|
|
|
for _, matcher := range template.Matchers {
|
|
|
|
|
_, matchOK := event.OperatorsResult.Matches[matcher.Name]
|
|
|
|
|
_, extractOK := event.OperatorsResult.Extracts[matcher.Name]
|
2020-12-26 02:08:48 +05:30
|
|
|
if !matchOK && !extractOK {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-11-12 23:28:24 +05:30
|
|
|
|
2020-12-26 02:08:48 +05:30
|
|
|
for _, subtemplate := range matcher.Subtemplates {
|
|
|
|
|
if err := w.runWorkflowStep(subtemplate, input, results); err != nil {
|
2021-01-01 19:36:21 +05:30
|
|
|
executionErr = err
|
|
|
|
|
break
|
2020-12-26 02:08:48 +05:30
|
|
|
}
|
2020-11-12 23:28:24 +05:30
|
|
|
}
|
|
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if executionErr != nil {
|
|
|
|
|
return executionErr
|
2020-11-12 23:28:24 +05:30
|
|
|
}
|
2020-12-26 13:20:56 +05:30
|
|
|
return nil
|
2020-11-12 23:28:24 +05:30
|
|
|
}
|
2020-12-26 13:20:56 +05:30
|
|
|
if len(template.Subtemplates) > 0 && firstMatched {
|
2020-11-12 23:28:24 +05:30
|
|
|
for _, subtemplate := range template.Subtemplates {
|
2020-12-26 02:08:48 +05:30
|
|
|
if err := w.runWorkflowStep(subtemplate, input, results); err != nil {
|
2020-11-12 23:28:24 +05:30
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|