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"
|
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2020-04-03 21:20:32 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-06-29 12:13:08 +00:00
|
|
|
// Parse parses a yaml request template file
|
2020-12-29 10:08:14 +00:00
|
|
|
func Parse(file string, options *protocols.ExecuterOptions) (*Template, error) {
|
2020-04-03 21:20:32 +00:00
|
|
|
template := &Template{}
|
|
|
|
|
|
|
|
f, err := os.Open(file)
|
|
|
|
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()
|
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
// Setting up variables regarding template metadata
|
2020-07-31 15:13:51 +00:00
|
|
|
template.path = file
|
2020-12-29 10:08:14 +00:00
|
|
|
options.TemplateID = template.ID
|
|
|
|
options.TemplateInfo = template.Info
|
|
|
|
options.TemplatePath = file
|
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.
|
2020-12-29 10:08:14 +00:00
|
|
|
if len(template.RequestsDNS)+len(template.RequestsDNS)+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 10:08:14 +00:00
|
|
|
// Compile the requests found
|
|
|
|
for _, request := range template.RequestsDNS {
|
|
|
|
if err := request.Compile(options); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not compile dns request")
|
|
|
|
}
|
|
|
|
template.totalRequests += request.Requests()
|
|
|
|
}
|
|
|
|
for _, request := range template.RequestsHTTP {
|
|
|
|
if err := request.Compile(options); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not compile dns request")
|
|
|
|
}
|
|
|
|
template.totalRequests += request.Requests()
|
2020-04-03 21:20:32 +00:00
|
|
|
}
|
|
|
|
return template, nil
|
|
|
|
}
|