2020-04-03 21:20:32 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2020-05-05 19:42:28 +00:00
|
|
|
"fmt"
|
2020-04-03 21:20:32 +00:00
|
|
|
"os"
|
2021-02-03 19:39:29 +00:00
|
|
|
"strings"
|
2020-04-03 21:20:32 +00:00
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-02-05 19:06:43 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
|
2020-12-29 10:08:14 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-01-01 21:09:27 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/executer"
|
2021-02-05 19:06:43 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/offlinehttp"
|
2021-02-03 19:39:29 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
2020-12-29 12:32:45 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/workflows"
|
2020-12-30 07:56:55 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2020-04-03 21:20:32 +00:00
|
|
|
)
|
|
|
|
|
2020-06-29 12:13:08 +00:00
|
|
|
// Parse parses a yaml request template file
|
2021-02-05 19:06:43 +00:00
|
|
|
func Parse(filePath string, options protocols.ExecuterOptions) (*Template, error) {
|
2020-04-03 21:20:32 +00:00
|
|
|
template := &Template{}
|
|
|
|
|
2021-01-01 09:58:28 +00:00
|
|
|
f, err := os.Open(filePath)
|
2020-04-03 21:20:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.NewDecoder(f).Decode(template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-26 12:37:55 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2021-02-03 19:39:29 +00:00
|
|
|
if _, ok := template.Info["name"]; !ok {
|
|
|
|
return nil, errors.New("no template name field provided")
|
|
|
|
}
|
|
|
|
if _, ok := template.Info["author"]; !ok {
|
|
|
|
return nil, errors.New("no template author field provided")
|
|
|
|
}
|
2021-02-04 12:59:28 +00:00
|
|
|
if len(options.Options.Tags) > 0 {
|
|
|
|
templateTags, ok := template.Info["tags"]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("no tags found for template")
|
|
|
|
}
|
|
|
|
if err := matchTemplateWithTags(types.ToString(templateTags), options.Options); err != nil {
|
2021-02-03 19:39:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
// Setting up variables regarding template metadata
|
|
|
|
options.TemplateID = template.ID
|
|
|
|
options.TemplateInfo = template.Info
|
2021-01-01 09:58:28 +00:00
|
|
|
options.TemplatePath = filePath
|
2020-07-31 15:13:51 +00:00
|
|
|
|
2020-06-29 12:13:08 +00:00
|
|
|
// If no requests, and it is also not a workflow, return error.
|
2021-01-01 13:25:35 +00:00
|
|
|
if len(template.RequestsDNS)+len(template.RequestsHTTP)+len(template.RequestsFile)+len(template.RequestsNetwork)+len(template.Workflows) == 0 {
|
2020-08-26 18:05:31 +00:00
|
|
|
return nil, fmt.Errorf("no requests defined for %s", template.ID)
|
2020-06-26 12:37:55 +00:00
|
|
|
}
|
2020-04-03 21:20:32 +00:00
|
|
|
|
2020-12-29 12:32:45 +00:00
|
|
|
// Compile the workflow request
|
2020-12-29 13:18:13 +00:00
|
|
|
if len(template.Workflows) > 0 {
|
2020-12-30 07:56:55 +00:00
|
|
|
compiled := &template.Workflow
|
2021-02-05 19:06:43 +00:00
|
|
|
if err := template.compileWorkflow(&options, compiled); err != nil {
|
2020-12-29 12:32:45 +00:00
|
|
|
return nil, errors.Wrap(err, "could not compile workflow")
|
|
|
|
}
|
2020-12-30 07:56:55 +00:00
|
|
|
template.CompiledWorkflow = compiled
|
2020-12-29 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
// Compile the requests found
|
2021-01-01 21:09:27 +00:00
|
|
|
requests := []protocols.Request{}
|
2020-12-29 11:03:25 +00:00
|
|
|
if len(template.RequestsDNS) > 0 {
|
2021-01-01 21:09:27 +00:00
|
|
|
for _, req := range template.RequestsDNS {
|
|
|
|
requests = append(requests, req)
|
|
|
|
}
|
2021-02-05 19:06:43 +00:00
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
2020-12-29 11:03:25 +00:00
|
|
|
}
|
|
|
|
if len(template.RequestsHTTP) > 0 {
|
2021-02-05 19:06:43 +00:00
|
|
|
if options.Options.OfflineHTTP {
|
|
|
|
operators := []*operators.Operators{}
|
|
|
|
|
|
|
|
for _, req := range template.RequestsHTTP {
|
|
|
|
operators = append(operators, &req.Operators)
|
|
|
|
}
|
|
|
|
options.Operators = operators
|
|
|
|
template.Executer = executer.NewExecuter([]protocols.Request{&offlinehttp.Request{}}, &options)
|
|
|
|
} else {
|
|
|
|
for _, req := range template.RequestsHTTP {
|
|
|
|
requests = append(requests, req)
|
|
|
|
}
|
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
2021-01-01 21:09:27 +00:00
|
|
|
}
|
2021-01-01 09:58:28 +00:00
|
|
|
}
|
|
|
|
if len(template.RequestsFile) > 0 {
|
2021-01-01 21:09:27 +00:00
|
|
|
for _, req := range template.RequestsFile {
|
|
|
|
requests = append(requests, req)
|
|
|
|
}
|
2021-02-05 19:06:43 +00:00
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
2020-12-29 11:03:25 +00:00
|
|
|
}
|
2020-12-30 09:24:20 +00:00
|
|
|
if len(template.RequestsNetwork) > 0 {
|
2021-01-01 21:09:27 +00:00
|
|
|
for _, req := range template.RequestsNetwork {
|
|
|
|
requests = append(requests, req)
|
|
|
|
}
|
2021-02-05 19:06:43 +00:00
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
2021-01-01 14:06:21 +00:00
|
|
|
if template.Executer != nil {
|
|
|
|
err := template.Executer.Compile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not compile request")
|
|
|
|
}
|
2021-01-11 20:30:11 +00:00
|
|
|
template.TotalRequests += template.Executer.Requests()
|
2020-12-29 11:03:25 +00:00
|
|
|
}
|
2020-04-03 21:20:32 +00:00
|
|
|
return template, nil
|
|
|
|
}
|
2020-12-29 12:32:45 +00:00
|
|
|
|
|
|
|
// compileWorkflow compiles the workflow for execution
|
2020-12-30 07:56:55 +00:00
|
|
|
func (t *Template) compileWorkflow(options *protocols.ExecuterOptions, workflows *workflows.Workflow) error {
|
|
|
|
for _, workflow := range workflows.Workflows {
|
2020-12-29 12:32:45 +00:00
|
|
|
if err := t.parseWorkflow(workflow, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWorkflow parses and compiles all templates in a workflow recursively
|
|
|
|
func (t *Template) parseWorkflow(workflow *workflows.WorkflowTemplate, options *protocols.ExecuterOptions) error {
|
|
|
|
if err := t.parseWorkflowTemplate(workflow, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, subtemplates := range workflow.Subtemplates {
|
|
|
|
if err := t.parseWorkflow(subtemplates, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, matcher := range workflow.Matchers {
|
|
|
|
for _, subtemplates := range matcher.Subtemplates {
|
|
|
|
if err := t.parseWorkflow(subtemplates, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWorkflowTemplate parses a workflow template creating an executer
|
|
|
|
func (t *Template) parseWorkflowTemplate(workflow *workflows.WorkflowTemplate, options *protocols.ExecuterOptions) error {
|
|
|
|
paths, err := options.Catalogue.GetTemplatePath(workflow.Template)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not get workflow template")
|
|
|
|
}
|
2021-01-17 07:26:29 +00:00
|
|
|
for _, path := range paths {
|
2021-02-05 19:06:43 +00:00
|
|
|
opts := protocols.ExecuterOptions{
|
2021-01-17 07:26:29 +00:00
|
|
|
Output: options.Output,
|
|
|
|
Options: options.Options,
|
|
|
|
Progress: options.Progress,
|
|
|
|
Catalogue: options.Catalogue,
|
|
|
|
RateLimiter: options.RateLimiter,
|
|
|
|
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,
|
|
|
|
})
|
2020-12-29 12:32:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-03 19:39:29 +00:00
|
|
|
|
|
|
|
// matchTemplateWithTags matches if the template matches a tag
|
2021-02-04 09:28:34 +00:00
|
|
|
func matchTemplateWithTags(tags string, options *types.Options) error {
|
|
|
|
actualTags := strings.Split(tags, ",")
|
|
|
|
|
2021-02-03 19:39:29 +00:00
|
|
|
matched := false
|
|
|
|
mainLoop:
|
2021-02-05 09:45:41 +00:00
|
|
|
for _, t := range options.Tags {
|
|
|
|
commaTags := strings.Split(t, ",")
|
|
|
|
for _, tag := range commaTags {
|
2021-02-05 09:50:01 +00:00
|
|
|
tag = strings.TrimSpace(tag)
|
2021-02-05 09:45:41 +00:00
|
|
|
key, value := getKeyValue(tag)
|
|
|
|
|
|
|
|
for _, templTag := range actualTags {
|
|
|
|
templTag = strings.TrimSpace(templTag)
|
|
|
|
tKey, tValue := getKeyValue(templTag)
|
|
|
|
|
|
|
|
if strings.EqualFold(key, tKey) && strings.EqualFold(value, tValue) {
|
|
|
|
matched = true
|
|
|
|
break mainLoop
|
|
|
|
}
|
2021-02-03 19:39:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !matched {
|
|
|
|
return errors.New("could not match template tags with input")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getKeyValue returns key value pair for a data string
|
|
|
|
func getKeyValue(data string) (string, string) {
|
|
|
|
var key, value string
|
2021-02-04 09:28:34 +00:00
|
|
|
|
2021-02-03 19:39:29 +00:00
|
|
|
if strings.Contains(data, ":") {
|
2021-02-04 09:28:34 +00:00
|
|
|
parts := strings.SplitN(data, ":", 2)
|
|
|
|
if len(parts) == 2 {
|
2021-02-03 19:39:29 +00:00
|
|
|
key, value = parts[0], parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if value == "" {
|
|
|
|
value = data
|
|
|
|
}
|
|
|
|
return key, value
|
|
|
|
}
|