nuclei/v2/pkg/templates/workflows.go

87 lines
2.9 KiB
Go
Raw Normal View History

package templates
import (
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/workflows"
"github.com/projectdiscovery/nuclei/v2/pkg/workflows/compile"
)
// compileWorkflow compiles the workflow for execution
2021-07-21 05:32:44 +00:00
func compileWorkflow(preprocessor Preprocessor, options *protocols.ExecuterOptions, workflow *workflows.Workflow, loader compile.WorkflowLoader) {
for _, workflow := range workflow.Workflows {
2021-07-21 05:32:44 +00:00
if err := parseWorkflow(preprocessor, workflow, options, loader); err != nil {
gologger.Warning().Msgf("Could not parse workflow: %v\n", err)
continue
}
}
}
// parseWorkflow parses and compiles all templates in a workflow recursively
2021-07-21 05:32:44 +00:00
func parseWorkflow(preprocessor Preprocessor, workflow *workflows.WorkflowTemplate, options *protocols.ExecuterOptions, loader compile.WorkflowLoader) error {
shouldNotValidate := false
if len(workflow.Subtemplates) > 0 || len(workflow.Matchers) > 0 {
shouldNotValidate = true
}
2021-07-21 05:32:44 +00:00
if err := parseWorkflowTemplate(workflow, preprocessor, options, loader, shouldNotValidate); err != nil {
return err
}
for _, subtemplates := range workflow.Subtemplates {
2021-07-21 05:32:44 +00:00
if err := parseWorkflow(preprocessor, subtemplates, options, loader); err != nil {
gologger.Warning().Msgf("Could not parse workflow: %v\n", err)
continue
}
}
for _, matcher := range workflow.Matchers {
for _, subtemplates := range matcher.Subtemplates {
2021-07-21 05:32:44 +00:00
if err := parseWorkflow(preprocessor, subtemplates, options, loader); err != nil {
gologger.Warning().Msgf("Could not parse workflow: %v\n", err)
continue
}
}
}
return nil
}
// parseWorkflowTemplate parses a workflow template creating an executer
2021-07-21 05:32:44 +00:00
func parseWorkflowTemplate(workflow *workflows.WorkflowTemplate, preprocessor Preprocessor, options *protocols.ExecuterOptions, loader compile.WorkflowLoader, noValidate bool) error {
var paths []string
if len(workflow.Tags) > 0 {
2021-07-05 15:32:38 +00:00
paths = loader.ListTags([]string{workflow.Tags})
} else {
2021-07-05 12:11:39 +00:00
paths = loader.ListTemplates([]string{workflow.Template}, noValidate)
}
if len(paths) == 0 {
return nil
}
for _, path := range paths {
opts := protocols.ExecuterOptions{
Output: options.Output,
Options: options.Options,
Progress: options.Progress,
Catalog: options.Catalog,
Browser: options.Browser,
RateLimiter: options.RateLimiter,
IssuesClient: options.IssuesClient,
Interactsh: options.Interactsh,
ProjectFile: options.ProjectFile,
}
2021-07-21 05:32:44 +00:00
template, err := Parse(path, preprocessor, opts)
if err != nil {
gologger.Warning().Msgf("Could not parse workflow template %s: %v\n", path, err)
continue
}
if template.Executer == nil {
gologger.Warning().Msgf("Could not parse workflow template %s: no executer found\n", path)
continue
}
workflow.Executers = append(workflow.Executers, &workflows.ProtocolExecuterPair{
Executer: template.Executer,
Options: options,
})
}
return nil
}