nuclei/pkg/core/workflow_execute.go

174 lines
5.2 KiB
Go
Raw Normal View History

2021-10-27 11:20:36 +00:00
package core
import (
"fmt"
"net/http/cookiejar"
"sync/atomic"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/output"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"
"github.com/projectdiscovery/nuclei/v3/pkg/scan"
"github.com/projectdiscovery/nuclei/v3/pkg/workflows"
2024-04-03 15:50:57 +00:00
syncutil "github.com/projectdiscovery/utils/sync"
)
const workflowStepExecutionError = "[%s] Could not execute workflow step: %s\n"
// executeWorkflow runs a workflow on an input and returns true or false
func (e *Engine) executeWorkflow(ctx *scan.ScanContext, w *workflows.Workflow) bool {
results := &atomic.Bool{}
// at this point we should be at the start root execution of a workflow tree, hence we create global shared instances
workflowCookieJar, _ := cookiejar.New(nil)
ctxArgs := contextargs.New()
ctxArgs.MetaInput = ctx.Input.MetaInput
ctxArgs.CookieJar = workflowCookieJar
// we can know the nesting level only at runtime, so the best we can do here is increase template threads by one unit in case it's equal to 1 to allow
// at least one subtemplate to go through, which it's idempotent to one in-flight template as the parent one is in an idle state
templateThreads := w.Options.Options.TemplateThreads
if templateThreads == 1 {
templateThreads++
}
2024-04-03 15:50:57 +00:00
swg, _ := syncutil.New(syncutil.WithSize(templateThreads))
for _, template := range w.Workflows {
2021-02-22 12:19:02 +00:00
swg.Add()
func(template *workflows.WorkflowTemplate) {
defer swg.Done()
2024-04-03 15:50:57 +00:00
if err := e.runWorkflowStep(template, ctx, results, swg, w); err != nil {
gologger.Warning().Msgf(workflowStepExecutionError, template.Template, err)
2021-02-22 12:19:02 +00:00
}
}(template)
}
2021-02-22 12:19:02 +00:00
swg.Wait()
return results.Load()
}
// runWorkflowStep runs a workflow step for the workflow. It executes the workflow
// in a recursive manner running all subtemplates and matchers.
2024-04-03 15:50:57 +00:00
func (e *Engine) runWorkflowStep(template *workflows.WorkflowTemplate, ctx *scan.ScanContext, results *atomic.Bool, swg *syncutil.AdaptiveWaitGroup, w *workflows.Workflow) error {
var firstMatched bool
var err error
2021-01-17 07:26:29 +00:00
var mainErr error
if len(template.Matchers) == 0 {
2021-01-17 07:26:29 +00:00
for _, executer := range template.Executers {
executer.Options.Progress.AddToTotal(int64(executer.Executer.Requests()))
// Don't print results with subtemplates, only print results on template.
if len(template.Subtemplates) > 0 {
ctx.OnResult = func(result *output.InternalWrappedEvent) {
if result.OperatorsResult == nil {
return
}
if len(result.Results) > 0 {
firstMatched = true
}
if result.OperatorsResult != nil && result.OperatorsResult.Extracts != nil {
for k, v := range result.OperatorsResult.Extracts {
// normalize items:
switch len(v) {
case 0, 1:
// - key:[item] => key: item
ctx.Input.Set(k, v[0])
default:
// - key:[item_0, ..., item_n] => key0:item_0, keyn:item_n
for vIdx, vVal := range v {
normalizedKIdx := fmt.Sprintf("%s%d", k, vIdx)
ctx.Input.Set(normalizedKIdx, vVal)
}
// also add the original name with full slice
ctx.Input.Set(k, v)
}
}
}
}
_, err = executer.Executer.ExecuteWithResults(ctx)
} else {
var matched bool
matched, err = executer.Executer.Execute(ctx)
if matched {
firstMatched = true
}
}
2021-01-17 07:26:29 +00:00
if err != nil {
if w.Options.HostErrorsCache != nil {
w.Options.HostErrorsCache.MarkFailed(ctx.Input.MetaInput.ID(), err)
}
2021-01-17 07:26:29 +00:00
if len(template.Executers) == 1 {
mainErr = err
} else {
gologger.Warning().Msgf(workflowStepExecutionError, template.Template, err)
2021-01-17 07:26:29 +00:00
}
continue
}
2020-12-29 15:02:04 +00:00
}
}
if len(template.Subtemplates) == 0 {
2022-09-19 06:38:52 +00:00
results.CompareAndSwap(false, firstMatched)
}
if len(template.Matchers) > 0 {
2021-01-17 07:26:29 +00:00
for _, executer := range template.Executers {
executer.Options.Progress.AddToTotal(int64(executer.Executer.Requests()))
ctx.OnResult = func(event *output.InternalWrappedEvent) {
2021-01-17 07:26:29 +00:00
if event.OperatorsResult == nil {
return
}
if event.OperatorsResult.Extracts != nil {
for k, v := range event.OperatorsResult.Extracts {
ctx.Input.Set(k, v)
}
}
2021-01-17 07:26:29 +00:00
for _, matcher := range template.Matchers {
if !matcher.Match(event.OperatorsResult) {
2021-01-17 07:26:29 +00:00
continue
}
for _, subtemplate := range matcher.Subtemplates {
2021-02-22 12:19:02 +00:00
swg.Add()
go func(subtemplate *workflows.WorkflowTemplate) {
defer swg.Done()
if err := e.runWorkflowStep(subtemplate, ctx, results, swg, w); err != nil {
gologger.Warning().Msgf(workflowStepExecutionError, subtemplate.Template, err)
2021-02-22 12:19:02 +00:00
}
}(subtemplate)
}
}
}
_, err := executer.Executer.ExecuteWithResults(ctx)
2021-01-17 07:26:29 +00:00
if err != nil {
if len(template.Executers) == 1 {
mainErr = err
} else {
gologger.Warning().Msgf(workflowStepExecutionError, template.Template, err)
2021-01-17 07:26:29 +00:00
}
continue
}
}
2021-01-17 07:26:29 +00:00
return mainErr
}
if len(template.Subtemplates) > 0 && firstMatched {
for _, subtemplate := range template.Subtemplates {
2021-02-22 12:19:02 +00:00
swg.Add()
go func(template *workflows.WorkflowTemplate) {
if err := e.runWorkflowStep(template, ctx, results, swg, w); err != nil {
gologger.Warning().Msgf(workflowStepExecutionError, template.Template, err)
2021-02-22 12:19:02 +00:00
}
swg.Done()
}(subtemplate)
}
}
2021-01-17 07:26:29 +00:00
return mainErr
}