2021-07-04 23:05:53 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"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
|
|
|
|
func compileWorkflow(options *protocols.ExecuterOptions, workflow *workflows.Workflow, loader compile.WorkflowLoader) error {
|
|
|
|
for _, workflow := range workflow.Workflows {
|
2021-07-05 15:31:51 +00:00
|
|
|
if err := parseWorkflow(workflow, options, loader); err != nil {
|
2021-07-04 23:05:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWorkflow parses and compiles all templates in a workflow recursively
|
2021-07-05 15:31:51 +00:00
|
|
|
func parseWorkflow(workflow *workflows.WorkflowTemplate, options *protocols.ExecuterOptions, loader compile.WorkflowLoader) error {
|
|
|
|
shouldNotValidate := false
|
|
|
|
|
|
|
|
if len(workflow.Subtemplates) > 0 || len(workflow.Matchers) > 0 {
|
|
|
|
shouldNotValidate = true
|
|
|
|
}
|
|
|
|
if err := parseWorkflowTemplate(workflow, options, loader, shouldNotValidate); err != nil {
|
2021-07-04 23:05:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, subtemplates := range workflow.Subtemplates {
|
2021-07-05 15:31:51 +00:00
|
|
|
if err := parseWorkflow(subtemplates, options, loader); err != nil {
|
2021-07-04 23:05:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, matcher := range workflow.Matchers {
|
|
|
|
for _, subtemplates := range matcher.Subtemplates {
|
2021-07-05 15:31:51 +00:00
|
|
|
if err := parseWorkflow(subtemplates, options, loader); err != nil {
|
2021-07-04 23:05:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWorkflowTemplate parses a workflow template creating an executer
|
2021-07-05 12:11:39 +00:00
|
|
|
func parseWorkflowTemplate(workflow *workflows.WorkflowTemplate, options *protocols.ExecuterOptions, loader compile.WorkflowLoader, noValidate bool) error {
|
2021-07-04 23:05:53 +00:00
|
|
|
var paths []string
|
|
|
|
|
|
|
|
if len(workflow.Tags) > 0 {
|
2021-07-05 15:32:38 +00:00
|
|
|
paths = loader.ListTags([]string{workflow.Tags})
|
2021-07-04 23:05:53 +00:00
|
|
|
} else {
|
2021-07-05 12:11:39 +00:00
|
|
|
paths = loader.ListTemplates([]string{workflow.Template}, noValidate)
|
2021-07-04 23:05:53 +00:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
template, err := Parse(path, opts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not parse workflow template")
|
|
|
|
}
|
|
|
|
if template.Executer == nil {
|
|
|
|
return errors.New("no executer found for template")
|
|
|
|
}
|
|
|
|
workflow.Executers = append(workflow.Executers, &workflows.ProtocolExecuterPair{
|
|
|
|
Executer: template.Executer,
|
|
|
|
Options: options,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|