2021-07-04 23:05:53 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2021-08-30 11:28:11 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-07-05 16:26:40 +00:00
|
|
|
"github.com/projectdiscovery/gologger"
|
2021-07-15 10:41:41 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2021-07-04 23:05:53 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/workflows"
|
|
|
|
)
|
|
|
|
|
|
|
|
// compileWorkflow compiles the workflow for execution
|
2021-08-30 11:28:11 +00:00
|
|
|
func compileWorkflow(path string, preprocessor Preprocessor, options *protocols.ExecuterOptions, workflow *workflows.Workflow, loader model.WorkflowLoader) {
|
2021-07-04 23:05:53 +00:00
|
|
|
for _, workflow := range workflow.Workflows {
|
2021-07-21 05:32:44 +00:00
|
|
|
if err := parseWorkflow(preprocessor, workflow, options, loader); err != nil {
|
2021-08-30 11:28:11 +00:00
|
|
|
gologger.Warning().Msgf("Could not parse workflow %s: %v\n", path, err)
|
2021-07-05 16:26:40 +00:00
|
|
|
continue
|
2021-07-04 23:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWorkflow parses and compiles all templates in a workflow recursively
|
2021-08-03 12:26:45 +00:00
|
|
|
func parseWorkflow(preprocessor Preprocessor, workflow *workflows.WorkflowTemplate, options *protocols.ExecuterOptions, loader model.WorkflowLoader) error {
|
2021-07-05 15:31:51 +00:00
|
|
|
shouldNotValidate := false
|
|
|
|
|
2021-08-30 11:28:11 +00:00
|
|
|
if len(workflow.Template) == 0 && workflow.Tags.IsEmpty() {
|
|
|
|
return errors.New("invalid workflow with no templates or tags")
|
|
|
|
}
|
2021-07-05 15:31:51 +00:00
|
|
|
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 {
|
2021-07-04 23:05:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, subtemplates := range workflow.Subtemplates {
|
2021-07-21 05:32:44 +00:00
|
|
|
if err := parseWorkflow(preprocessor, subtemplates, options, loader); err != nil {
|
2021-07-05 16:26:40 +00:00
|
|
|
gologger.Warning().Msgf("Could not parse workflow: %v\n", err)
|
|
|
|
continue
|
2021-07-04 23:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2021-07-05 16:26:40 +00:00
|
|
|
gologger.Warning().Msgf("Could not parse workflow: %v\n", err)
|
|
|
|
continue
|
2021-07-04 23:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWorkflowTemplate parses a workflow template creating an executer
|
2021-08-03 12:26:45 +00:00
|
|
|
func parseWorkflowTemplate(workflow *workflows.WorkflowTemplate, preprocessor Preprocessor, options *protocols.ExecuterOptions, loader model.WorkflowLoader, noValidate bool) error {
|
2021-07-04 23:05:53 +00:00
|
|
|
var paths []string
|
|
|
|
|
2021-08-19 12:17:25 +00:00
|
|
|
subTemplateTags := workflow.Tags
|
|
|
|
if !subTemplateTags.IsEmpty() {
|
|
|
|
paths = loader.GetTemplatePathsByTags(subTemplateTags.ToSlice())
|
2021-07-04 23:05:53 +00:00
|
|
|
} else {
|
2021-08-19 12:17:25 +00:00
|
|
|
paths = loader.GetTemplatePaths([]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{
|
2021-08-16 15:54:37 +00:00
|
|
|
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,
|
|
|
|
HostErrorsCache: options.HostErrorsCache,
|
2021-07-04 23:05:53 +00:00
|
|
|
}
|
2021-07-21 05:32:44 +00:00
|
|
|
template, err := Parse(path, preprocessor, opts)
|
2021-07-04 23:05:53 +00:00
|
|
|
if err != nil {
|
2021-07-05 16:26:40 +00:00
|
|
|
gologger.Warning().Msgf("Could not parse workflow template %s: %v\n", path, err)
|
|
|
|
continue
|
2021-07-04 23:05:53 +00:00
|
|
|
}
|
|
|
|
if template.Executer == nil {
|
2021-07-05 16:26:40 +00:00
|
|
|
gologger.Warning().Msgf("Could not parse workflow template %s: no executer found\n", path)
|
|
|
|
continue
|
2021-07-04 23:05:53 +00:00
|
|
|
}
|
|
|
|
workflow.Executers = append(workflow.Executers, &workflows.ProtocolExecuterPair{
|
|
|
|
Executer: template.Executer,
|
|
|
|
Options: options,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|