nuclei/v2/internal/runner/templates.go

145 lines
4.4 KiB
Go
Raw Normal View History

2020-08-29 13:26:11 +00:00
package runner
import (
"fmt"
"os"
"strings"
"github.com/karrick/godirwalk"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
2020-08-29 13:26:11 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
2020-08-29 13:26:11 +00:00
)
// getParsedTemplatesFor parse the specified templates and returns a slice of the parsable ones, optionally filtered
// by severity, along with a flag indicating if workflows are present.
2021-02-26 07:43:11 +00:00
func (r *Runner) getParsedTemplatesFor(templatePaths, severities []string) (parsedTemplates map[string]*templates.Template, workflowCount int) {
2020-08-29 13:26:11 +00:00
filterBySeverity := len(severities) > 0
gologger.Info().Msgf("Loading templates...")
2020-08-29 13:26:11 +00:00
2021-02-26 07:43:11 +00:00
parsedTemplates = make(map[string]*templates.Template)
2020-08-29 13:26:11 +00:00
for _, match := range templatePaths {
t, err := r.parseTemplateFile(match)
if err != nil {
2021-02-04 16:41:53 +00:00
gologger.Warning().Msgf("Could not parse file '%s': %s\n", match, err)
continue
}
2021-02-22 07:34:42 +00:00
if len(t.Workflows) == 0 && r.options.Workflows {
continue // don't print if user only wants to run workflows
}
2020-12-29 14:16:52 +00:00
if len(t.Workflows) > 0 {
workflowCount++
}
sev := strings.ToLower(types.ToString(t.Info["severity"]))
if !filterBySeverity || hasMatchingSeverity(sev, severities) {
parsedTemplates[t.ID] = t
gologger.Info().Msgf("%s\n", r.templateLogMsg(t.ID, types.ToString(t.Info["name"]), types.ToString(t.Info["author"]), sev))
} else {
gologger.Error().Msgf("Excluding template %s due to severity filter (%s not in [%s])", t.ID, sev, severities)
2020-08-29 13:26:11 +00:00
}
}
return parsedTemplates, workflowCount
}
// parseTemplateFile returns the parsed template file
func (r *Runner) parseTemplateFile(file string) (*templates.Template, error) {
executerOpts := protocols.ExecuterOptions{
2021-02-07 20:37:19 +00:00
Output: r.output,
Options: r.options,
Progress: r.progress,
2021-02-26 07:43:11 +00:00
Catalog: r.catalog,
2021-02-07 20:37:19 +00:00
IssuesClient: r.issuesClient,
RateLimiter: r.ratelimiter,
ProjectFile: r.projectFile,
Browser: r.browser,
2020-08-29 13:26:11 +00:00
}
template, err := templates.Parse(file, executerOpts)
if err != nil {
return nil, err
2020-08-29 13:26:11 +00:00
}
return template, nil
2020-08-29 13:26:11 +00:00
}
func (r *Runner) templateLogMsg(id, name, author, severity string) string {
2020-08-29 13:26:11 +00:00
// Display the message for the template
message := fmt.Sprintf("[%s] %s (%s)",
r.colorizer.BrightBlue(id).String(),
r.colorizer.Bold(name).String(),
r.colorizer.BrightYellow("@"+author).String())
2020-08-29 13:26:11 +00:00
if severity != "" {
message += " [" + r.severityColors.Data[severity] + "]"
2020-08-29 13:26:11 +00:00
}
return message
}
func (r *Runner) logAvailableTemplate(tplPath string) {
t, err := r.parseTemplateFile(tplPath)
if err != nil {
gologger.Error().Msgf("Could not parse file '%s': %s\n", tplPath, err)
} else {
gologger.Print().Msgf("%s\n", r.templateLogMsg(t.ID, types.ToString(t.Info["name"]), types.ToString(t.Info["author"]), types.ToString(t.Info["severity"])))
}
2020-08-29 13:26:11 +00:00
}
// ListAvailableTemplates prints available templates to stdout
func (r *Runner) listAvailableTemplates() {
if r.templatesConfig == nil {
return
2020-08-29 13:26:11 +00:00
}
if _, err := os.Stat(r.templatesConfig.TemplatesDirectory); os.IsNotExist(err) {
gologger.Error().Msgf("%s does not exists", r.templatesConfig.TemplatesDirectory)
return
}
gologger.Print().Msgf(
"\nListing available v.%s nuclei templates for %s",
r.templatesConfig.CurrentVersion,
r.templatesConfig.TemplatesDirectory,
)
err := directoryWalker(
r.templatesConfig.TemplatesDirectory,
func(path string, d *godirwalk.Dirent) error {
if d.IsDir() && path != r.templatesConfig.TemplatesDirectory {
gologger.Print().Msgf("\n%s:\n\n", r.colorizer.Bold(r.colorizer.BgBrightBlue(d.Name())).String())
} else if strings.HasSuffix(path, ".yaml") {
r.logAvailableTemplate(path)
}
return nil
},
)
// directory couldn't be walked
if err != nil {
gologger.Error().Msgf("Could not find templates in directory '%s': %s\n", r.templatesConfig.TemplatesDirectory, err)
2020-08-29 13:26:11 +00:00
}
}
func hasMatchingSeverity(templateSeverity string, allowedSeverities []string) bool {
for _, s := range allowedSeverities {
s = strings.ToLower(s)
2020-08-29 13:26:11 +00:00
if s != "" && strings.HasPrefix(templateSeverity, s) {
return true
}
}
return false
}
func directoryWalker(fsPath string, callback func(fsPath string, d *godirwalk.Dirent) error) error {
err := godirwalk.Walk(fsPath, &godirwalk.Options{
Callback: callback,
ErrorCallback: func(fsPath string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
Unsorted: true,
})
// directory couldn't be walked
if err != nil {
return err
}
return nil
}