Repository navigation
Expand file tree
/
Copy pathworkflows.go
More file actions
167 lines (147 loc) · 5.71 KB
/
Copy pathworkflows.go
File metadata and controls
167 lines (147 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package templates
import (
"github.com/pkg/errors"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/keys"
"github.com/projectdiscovery/nuclei/v3/pkg/model"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/stats"
"github.com/projectdiscovery/nuclei/v3/pkg/workflows"
)
// compileWorkflow compiles the workflow for execution
func compileWorkflow(path string, preprocessor Preprocessor, options *protocols.ExecutorOptions, workflow *workflows.Workflow, loader model.WorkflowLoader) {
for _, workflow := range workflow.Workflows {
if err := parseWorkflow(path, preprocessor, workflow, options, loader); err != nil {
gologger.Warning().Msgf("Could not parse workflow %s: %v\n", path, err)
continue
}
}
}
// parseWorkflow parses and compiles all templates of the workflow at path
// recursively.
func parseWorkflow(path string, preprocessor Preprocessor, workflow *workflows.WorkflowTemplate, options *protocols.ExecutorOptions, loader model.WorkflowLoader) error {
if workflow.Template == "" && workflow.Tags.IsEmpty() {
return errors.New("invalid workflow with no templates or tags")
}
// subtemplates are parsed first: a template that does not pass the
// filters is loaded only when one of its subtemplates was loaded, as
// subtemplates run only after it
shouldNotValidate := false
for _, subtemplates := range workflow.Subtemplates {
if err := parseWorkflow(path, preprocessor, subtemplates, options, loader); err != nil {
gologger.Warning().Msgf("Could not parse workflow: %v\n", err)
continue
}
shouldNotValidate = shouldNotValidate || len(subtemplates.Executers) > 0
}
var matcherErr error
for _, matcher := range workflow.Matchers {
if len(matcher.Name.ToSlice()) > 0 {
if err := matcher.Compile(); err != nil {
matcherErr = errors.Wrap(err, "could not compile workflow matcher")
break
}
}
for _, subtemplates := range matcher.Subtemplates {
if err := parseWorkflow(path, preprocessor, subtemplates, options, loader); err != nil {
gologger.Warning().Msgf("Could not parse workflow: %v\n", err)
continue
}
shouldNotValidate = shouldNotValidate || len(subtemplates.Executers) > 0
}
}
if err := parseWorkflowTemplate(workflow, preprocessor, options, loader, shouldNotValidate); err != nil {
return err
}
// warnings show only with -v, so the count is shown after loading
if shouldNotValidate && len(workflow.Executers) == 0 {
stats.Increment(SkippedWorkflowStepStats)
gologger.Warning().Msgf("Workflow %s: step %s loaded no templates, so its subtemplates will not run",
path, describeWorkflowStep(workflow))
}
return matcherErr
}
// describeWorkflowStep names a workflow step by the templates it selects.
func describeWorkflowStep(workflow *workflows.WorkflowTemplate) string {
if !workflow.Tags.IsEmpty() {
return "tags " + workflow.Tags.String()
}
return workflow.Template
}
// parseWorkflowTemplate parses a workflow template creating an executer
func parseWorkflowTemplate(workflow *workflows.WorkflowTemplate, preprocessor Preprocessor, options *protocols.ExecutorOptions, loader model.WorkflowLoader, noValidate bool) error {
var paths []string
subTemplateTags := workflow.Tags
if !subTemplateTags.IsEmpty() {
paths = loader.GetTemplatePathsByTags(subTemplateTags.ToSlice(), noValidate)
} else {
paths = loader.GetTemplatePaths([]string{workflow.Template}, noValidate)
}
if len(paths) == 0 {
return nil
}
var workflowTemplates []*Template
caps := CapabilitiesFromOptions(options.Options)
for _, path := range paths {
template, err := Parse(path, preprocessor, options.Copy())
if err != nil {
gologger.Warning().Msgf("Could not parse workflow template %s: %v\n", path, err)
continue
}
if template == nil {
continue
}
if template.Executer == nil {
gologger.Warning().Msgf("Could not parse workflow template %s: no executer found\n", path)
continue
}
if options.Options.DisableUnsignedTemplates && !template.Verified {
// skip unverified templates when prompted to do so
stats.Increment(SkippedUnverifiedTemplateStats)
continue
}
if template.HasCodeRequest() && !template.Verified {
// unverified code templates are not allowed in workflows
stats.Increment(SkippedUnverifiedCodeTemplateStats)
gologger.Warning().Msgf("Skipping unverified code template(s) from workflow: %v\n", path)
continue
}
if template.IsUnsignedJavascriptTemplate() {
// unverified javascript templates are not allowed in workflows
stats.Increment(SkippedUnverifiedJavascriptTemplateStats)
gologger.Warning().Msgf("Skipping unverified javascript template(s) from workflow: %v\n", path)
continue
}
if missingCaps := template.MissingLoadCapabilities(caps); len(missingCaps) > 0 {
for _, capability := range missingCaps {
stats.Increment(capability.Stat())
gologger.Warning().Msgf("Skipping workflow subtemplate: %s", capability.MissingFlagMessage(path))
}
continue
}
if template.UsesRequestSignature() && !template.Verified {
stats.Increment(SkippedRequestSignatureTemplateStats)
continue
}
// increment signed/unsigned counters
if template.Verified {
if template.TemplateVerifier == "" {
SignatureStats[keys.PDVerifier].Add(1)
} else {
SignatureStats[template.TemplateVerifier].Add(1)
}
} else {
SignatureStats[Unsigned].Add(1)
}
workflowTemplates = append(workflowTemplates, template)
}
finalTemplates, _, _ := ClusterTemplates(workflowTemplates, options.Copy())
for _, template := range finalTemplates {
workflow.Executers = append(workflow.Executers, &workflows.ProtocolExecuterPair{
Executer: template.Executer,
Options: options,
TemplateType: template.Type(),
})
}
return nil
}