2020-08-29 13:26:11 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/karrick/godirwalk"
|
|
|
|
"github.com/projectdiscovery/gologger"
|
2020-12-29 10:08:14 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2020-08-29 13:26:11 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-01-13 07:28:23 +00:00
|
|
|
func (r *Runner) getParsedTemplatesFor(templatePaths []string, severities []string) (map[string]*templates.Template, int) {
|
|
|
|
workflowCount := 0
|
2020-08-29 13:26:11 +00:00
|
|
|
filterBySeverity := len(severities) > 0
|
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Info().Msgf("Loading templates...")
|
2020-08-29 13:26:11 +00:00
|
|
|
|
2021-01-13 07:28:23 +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)
|
2020-12-29 10:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
gologger.Error().Msgf("Could not parse file '%s': %s\n", match, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-12-29 14:16:52 +00:00
|
|
|
if len(t.Workflows) > 0 {
|
|
|
|
workflowCount++
|
|
|
|
}
|
2020-12-29 10:08:14 +00:00
|
|
|
sev := strings.ToLower(t.Info["severity"])
|
2021-01-12 09:44:49 +00:00
|
|
|
if !filterBySeverity || hasMatchingSeverity(sev, severities) {
|
2021-01-13 07:28:23 +00:00
|
|
|
parsedTemplates[t.ID] = t
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Info().Msgf("%s\n", r.templateLogMsg(t.ID, t.Info["name"], t.Info["author"], t.Info["severity"]))
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
// parseTemplateFile returns the parsed template file
|
|
|
|
func (r *Runner) parseTemplateFile(file string) (*templates.Template, error) {
|
|
|
|
executerOpts := &protocols.ExecuterOptions{
|
|
|
|
Output: r.output,
|
|
|
|
Options: r.options,
|
|
|
|
Progress: r.progress,
|
2020-12-29 12:32:45 +00:00
|
|
|
Catalogue: r.catalogue,
|
2020-12-29 10:08:14 +00:00
|
|
|
RateLimiter: r.ratelimiter,
|
|
|
|
ProjectFile: r.projectFile,
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
2020-12-29 10:08:14 +00:00
|
|
|
template, err := templates.Parse(file, executerOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
2020-12-29 10:08:14 +00:00
|
|
|
return template, nil
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 21:02:45 +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)",
|
2020-12-29 10:08:14 +00:00
|
|
|
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 != "" {
|
2020-12-29 10:08:14 +00:00
|
|
|
message += " [" + r.severityColors.Data[severity] + "]"
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
2020-08-29 21:02:45 +00:00
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) logAvailableTemplate(tplPath string) {
|
|
|
|
t, err := r.parseTemplateFile(tplPath)
|
2020-12-29 10:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
gologger.Error().Msgf("Could not parse file '%s': %s\n", tplPath, err)
|
2020-08-29 21:02:45 +00:00
|
|
|
}
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Print().Msgf("%s\n", r.templateLogMsg(t.ID, t.Info["name"], t.Info["author"], t.Info["severity"]))
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListAvailableTemplates prints available templates to stdout
|
2020-08-29 21:02:45 +00:00
|
|
|
func (r *Runner) listAvailableTemplates() {
|
|
|
|
if r.templatesConfig == nil {
|
|
|
|
return
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 11:25:34 +00:00
|
|
|
if _, err := os.Stat(r.templatesConfig.TemplatesDirectory); os.IsNotExist(err) {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Error().Msgf("%s does not exists", r.templatesConfig.TemplatesDirectory)
|
2020-08-30 11:25:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Print().Msgf(
|
2020-08-30 11:25:34 +00:00
|
|
|
"\nListing available v.%s nuclei templates for %s",
|
|
|
|
r.templatesConfig.CurrentVersion,
|
|
|
|
r.templatesConfig.TemplatesDirectory,
|
|
|
|
)
|
2020-08-29 21:02:45 +00:00
|
|
|
err := directoryWalker(
|
|
|
|
r.templatesConfig.TemplatesDirectory,
|
|
|
|
func(path string, d *godirwalk.Dirent) error {
|
|
|
|
if d.IsDir() && path != r.templatesConfig.TemplatesDirectory {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Print().Msgf("\n%s:\n\n", r.colorizer.Bold(r.colorizer.BgBrightBlue(d.Name())).String())
|
2020-08-29 21:02:45 +00:00
|
|
|
} else if strings.HasSuffix(path, ".yaml") {
|
|
|
|
r.logAvailableTemplate(path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
// directory couldn't be walked
|
|
|
|
if err != nil {
|
2020-12-29 10:08:14 +00:00
|
|
|
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 {
|
2021-01-12 09:44:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFilePath(filePath string) (bool, error) {
|
|
|
|
info, err := os.Stat(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return info.Mode().IsRegular(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNewPath(filePath string, pathMap map[string]bool) bool {
|
|
|
|
if _, already := pathMap[filePath]; already {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Warning().Msgf("Skipping already specified path '%s'", filePath)
|
2020-08-29 13:26:11 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|