2020-04-04 02:50:32 +05:30
|
|
|
package templates
|
|
|
|
|
|
|
|
|
|
import (
|
2020-05-05 21:42:28 +02:00
|
|
|
"fmt"
|
2021-02-16 15:13:01 +05:30
|
|
|
"io/ioutil"
|
2020-04-04 02:50:32 +05:30
|
|
|
"os"
|
2021-08-28 00:15:28 +05:30
|
|
|
"regexp"
|
2021-02-04 01:09:29 +05:30
|
|
|
"strings"
|
2020-04-04 02:50:32 +05:30
|
|
|
|
2020-12-29 15:38:14 +05:30
|
|
|
"github.com/pkg/errors"
|
2021-07-19 21:04:08 +03:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
|
2021-08-28 00:15:28 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
2021-02-06 00:36:43 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
|
2020-12-29 15:38:14 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-01-02 02:39:27 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/executer"
|
2021-02-06 00:36:43 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/offlinehttp"
|
2021-08-28 00:15:28 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates/cache"
|
2021-07-19 21:04:08 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
|
2020-04-04 02:50:32 +05:30
|
|
|
)
|
|
|
|
|
|
2021-08-28 00:21:07 +05:30
|
|
|
var (
|
|
|
|
|
ErrCreateTemplateExecutor = errors.New("cannot create template executer")
|
|
|
|
|
fieldErrorRegexp = regexp.MustCompile(`not found in`)
|
|
|
|
|
)
|
2021-08-27 17:06:06 +03:00
|
|
|
|
2021-08-28 00:27:37 +05:30
|
|
|
var parsedTemplatesCache *cache.Templates
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
parsedTemplatesCache = cache.New()
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 17:43:08 +05:30
|
|
|
// Parse parses a yaml request template file
|
2021-02-26 13:13:11 +05:30
|
|
|
//nolint:gocritic // this cannot be passed by pointer
|
2021-08-27 17:06:06 +03:00
|
|
|
// TODO make sure reading from the disk the template parsing happens once: see parsers.ParseTemplate vs templates.Parse
|
2021-07-20 22:32:44 -07:00
|
|
|
func Parse(filePath string, preprocessor Preprocessor, options protocols.ExecuterOptions) (*Template, error) {
|
2021-08-28 00:15:28 +05:30
|
|
|
if value, err := parsedTemplatesCache.Has(filePath); value != nil {
|
|
|
|
|
return value.(*Template), err
|
2021-08-27 17:06:06 +03:00
|
|
|
}
|
|
|
|
|
|
2020-04-04 02:50:32 +05:30
|
|
|
template := &Template{}
|
|
|
|
|
|
2021-01-01 15:28:28 +05:30
|
|
|
f, err := os.Open(filePath)
|
2020-04-04 02:50:32 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-02-16 15:13:01 +05:30
|
|
|
defer f.Close()
|
2020-04-04 02:50:32 +05:30
|
|
|
|
2021-02-16 15:13:01 +05:30
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = template.expandPreprocessors(data)
|
2021-07-20 22:32:44 -07:00
|
|
|
if preprocessor != nil {
|
|
|
|
|
data = preprocessor.Process(data)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:55:52 +03:00
|
|
|
if err := yaml.UnmarshalStrict(data, template); err != nil {
|
2021-08-28 00:15:28 +05:30
|
|
|
if !fieldErrorRegexp.MatchString(err.Error()) {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
gologger.Warning().Msgf("Unrecognized fields in template %s: %s", filePath, err)
|
2020-04-04 02:50:32 +05:30
|
|
|
}
|
2020-06-26 14:37:55 +02:00
|
|
|
|
2021-08-03 14:51:34 +03:00
|
|
|
if utils.IsBlank(template.Info.Name) {
|
2021-02-04 01:09:29 +05:30
|
|
|
return nil, errors.New("no template name field provided")
|
|
|
|
|
}
|
2021-08-03 14:51:34 +03:00
|
|
|
if template.Info.Authors.IsEmpty() {
|
2021-02-04 01:09:29 +05:30
|
|
|
return nil, errors.New("no template author field provided")
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 15:38:14 +05:30
|
|
|
// Setting up variables regarding template metadata
|
|
|
|
|
options.TemplateID = template.ID
|
|
|
|
|
options.TemplateInfo = template.Info
|
2021-01-01 15:28:28 +05:30
|
|
|
options.TemplatePath = filePath
|
2020-07-31 17:13:51 +02:00
|
|
|
|
2020-06-29 17:43:08 +05:30
|
|
|
// If no requests, and it is also not a workflow, return error.
|
2021-08-03 14:51:34 +03:00
|
|
|
if len(template.RequestsDNS)+len(template.RequestsHTTP)+len(template.RequestsFile)+len(template.RequestsNetwork)+len(template.RequestsHeadless)+len(template.Workflows) == 0 {
|
2020-08-26 20:05:31 +02:00
|
|
|
return nil, fmt.Errorf("no requests defined for %s", template.ID)
|
2020-06-26 14:37:55 +02:00
|
|
|
}
|
2020-04-04 02:50:32 +05:30
|
|
|
|
2020-12-29 18:02:45 +05:30
|
|
|
// Compile the workflow request
|
2021-08-03 14:51:34 +03:00
|
|
|
if len(template.Workflows) > 0 {
|
2020-12-30 13:26:55 +05:30
|
|
|
compiled := &template.Workflow
|
2021-07-05 04:35:53 +05:30
|
|
|
|
2021-08-30 16:58:11 +05:30
|
|
|
compileWorkflow(filePath, preprocessor, &options, compiled, options.WorkflowLoader)
|
2020-12-30 13:26:55 +05:30
|
|
|
template.CompiledWorkflow = compiled
|
2021-02-23 22:55:29 +05:30
|
|
|
template.CompiledWorkflow.Options = &options
|
2020-12-29 18:02:45 +05:30
|
|
|
}
|
|
|
|
|
|
2020-12-29 15:38:14 +05:30
|
|
|
// Compile the requests found
|
2021-01-02 02:39:27 +05:30
|
|
|
requests := []protocols.Request{}
|
2021-02-07 15:12:38 +05:30
|
|
|
if len(template.RequestsDNS) > 0 && !options.Options.OfflineHTTP {
|
2021-01-02 02:39:27 +05:30
|
|
|
for _, req := range template.RequestsDNS {
|
|
|
|
|
requests = append(requests, req)
|
|
|
|
|
}
|
2021-02-06 00:36:43 +05:30
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
2020-12-29 16:33:25 +05:30
|
|
|
}
|
|
|
|
|
if len(template.RequestsHTTP) > 0 {
|
2021-02-06 00:36:43 +05:30
|
|
|
if options.Options.OfflineHTTP {
|
2021-02-26 13:13:11 +05:30
|
|
|
operatorsList := []*operators.Operators{}
|
2021-02-06 00:36:43 +05:30
|
|
|
|
2021-03-04 19:14:53 +05:30
|
|
|
mainLoop:
|
2021-02-06 00:36:43 +05:30
|
|
|
for _, req := range template.RequestsHTTP {
|
2021-03-04 19:14:53 +05:30
|
|
|
for _, path := range req.Path {
|
|
|
|
|
if !(strings.EqualFold(path, "{{BaseURL}}") || strings.EqualFold(path, "{{BaseURL}}/")) {
|
|
|
|
|
break mainLoop
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-26 13:13:11 +05:30
|
|
|
operatorsList = append(operatorsList, &req.Operators)
|
2021-02-06 00:36:43 +05:30
|
|
|
}
|
2021-03-04 19:14:53 +05:30
|
|
|
if len(operatorsList) > 0 {
|
|
|
|
|
options.Operators = operatorsList
|
|
|
|
|
template.Executer = executer.NewExecuter([]protocols.Request{&offlinehttp.Request{}}, &options)
|
|
|
|
|
}
|
2021-02-06 00:36:43 +05:30
|
|
|
} else {
|
|
|
|
|
for _, req := range template.RequestsHTTP {
|
|
|
|
|
requests = append(requests, req)
|
|
|
|
|
}
|
|
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
2021-01-02 02:39:27 +05:30
|
|
|
}
|
2021-01-01 15:28:28 +05:30
|
|
|
}
|
2021-02-07 15:12:38 +05:30
|
|
|
if len(template.RequestsFile) > 0 && !options.Options.OfflineHTTP {
|
2021-01-02 02:39:27 +05:30
|
|
|
for _, req := range template.RequestsFile {
|
|
|
|
|
requests = append(requests, req)
|
|
|
|
|
}
|
2021-02-06 00:36:43 +05:30
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
2020-12-29 16:33:25 +05:30
|
|
|
}
|
2021-02-07 15:12:38 +05:30
|
|
|
if len(template.RequestsNetwork) > 0 && !options.Options.OfflineHTTP {
|
2021-01-02 02:39:27 +05:30
|
|
|
for _, req := range template.RequestsNetwork {
|
|
|
|
|
requests = append(requests, req)
|
|
|
|
|
}
|
2021-02-06 00:36:43 +05:30
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
2021-02-21 16:31:34 +05:30
|
|
|
if len(template.RequestsHeadless) > 0 && !options.Options.OfflineHTTP && options.Options.Headless {
|
|
|
|
|
for _, req := range template.RequestsHeadless {
|
|
|
|
|
requests = append(requests, req)
|
|
|
|
|
}
|
|
|
|
|
template.Executer = executer.NewExecuter(requests, &options)
|
|
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
if template.Executer != nil {
|
2021-08-31 12:55:52 +03:00
|
|
|
if err := template.Executer.Compile(); err != nil {
|
2021-01-01 19:36:21 +05:30
|
|
|
return nil, errors.Wrap(err, "could not compile request")
|
|
|
|
|
}
|
2021-01-12 02:00:11 +05:30
|
|
|
template.TotalRequests += template.Executer.Requests()
|
2020-12-29 16:33:25 +05:30
|
|
|
}
|
2021-02-22 13:04:42 +05:30
|
|
|
if template.Executer == nil && template.CompiledWorkflow == nil {
|
2021-08-28 00:21:07 +05:30
|
|
|
return nil, ErrCreateTemplateExecutor
|
2021-02-07 15:12:38 +05:30
|
|
|
}
|
2021-06-05 23:00:59 +05:30
|
|
|
template.Path = filePath
|
2021-08-27 17:06:06 +03:00
|
|
|
|
2021-08-28 00:15:28 +05:30
|
|
|
parsedTemplatesCache.Store(filePath, template, err)
|
2020-04-04 02:50:32 +05:30
|
|
|
return template, nil
|
|
|
|
|
}
|