nuclei/v2/pkg/templates/compile.go

113 lines
2.8 KiB
Go
Raw Normal View History

2020-04-03 21:20:32 +00:00
package templates
import (
2020-06-26 12:37:55 +00:00
"errors"
"fmt"
2020-04-03 21:20:32 +00:00
"os"
2020-07-10 20:29:49 +00:00
"strings"
2020-04-03 21:20:32 +00:00
2020-07-01 10:47:24 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/generators"
"github.com/projectdiscovery/nuclei/v2/pkg/matchers"
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
func Parse(file string) (*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-06-29 12:13:08 +00:00
// If no requests, and it is also not a workflow, return error.
2020-07-18 19:42:23 +00:00
if len(template.BulkRequestsHTTP)+len(template.RequestsDNS) <= 0 {
2020-06-26 12:37:55 +00:00
return nil, errors.New("No requests defined")
}
2020-04-03 21:20:32 +00:00
2020-04-22 20:45:02 +00:00
// Compile the matchers and the extractors for http requests
2020-07-18 19:42:23 +00:00
for _, request := range template.BulkRequestsHTTP {
// Get the condition between the matchers
condition, ok := matchers.ConditionTypes[request.MatchersCondition]
if !ok {
request.SetMatchersCondition(matchers.ORCondition)
} else {
request.SetMatchersCondition(condition)
}
// Set the attack type - used only in raw requests
attack, ok := generators.AttackTypes[request.AttackType]
if !ok {
request.SetAttackType(generators.Sniper)
} else {
request.SetAttackType(attack)
}
// Validate the payloads if any
2020-07-13 01:30:07 +00:00
for name, payload := range request.Payloads {
switch payload.(type) {
case string:
v := payload.(string)
// check if it's a multiline string list
if len(strings.Split(v, "\n")) <= 1 {
// check if it's a worldlist file
if !generators.FileExists(v) {
return nil, fmt.Errorf("The %s file for payload %s does not exist or does not contain enough elements", v, name)
}
2020-07-10 20:29:49 +00:00
}
2020-07-13 01:30:07 +00:00
case []string, []interface{}:
if len(payload.([]interface{})) <= 0 {
return nil, fmt.Errorf("The payload %s does not contain enough elements", name)
}
default:
return nil, fmt.Errorf("The payload %s has invalid type", name)
}
}
2020-04-03 21:20:32 +00:00
for _, matcher := range request.Matchers {
if err = matcher.CompileMatchers(); err != nil {
return nil, err
}
}
for _, extractor := range request.Extractors {
if err := extractor.CompileExtractors(); err != nil {
return nil, err
}
}
2020-07-24 16:12:16 +00:00
request.InitGenerator()
2020-04-03 21:20:32 +00:00
}
2020-04-22 20:45:02 +00:00
// Compile the matchers and the extractors for dns requests
for _, request := range template.RequestsDNS {
// Get the condition between the matchers
condition, ok := matchers.ConditionTypes[request.MatchersCondition]
if !ok {
request.SetMatchersCondition(matchers.ORCondition)
} else {
request.SetMatchersCondition(condition)
}
2020-04-22 20:45:02 +00:00
for _, matcher := range request.Matchers {
if err = matcher.CompileMatchers(); err != nil {
return nil, err
}
}
for _, extractor := range request.Extractors {
if err := extractor.CompileExtractors(); err != nil {
return nil, err
}
}
}
2020-04-23 16:44:34 +00:00
2020-04-03 21:20:32 +00:00
return template, nil
}