nuclei/v2/internal/runner/templates.go

96 lines
2.7 KiB
Go
Raw Normal View History

2020-08-29 13:26:11 +00:00
package runner
import (
"fmt"
"os"
"strings"
"github.com/karrick/godirwalk"
2020-08-29 13:26:11 +00:00
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity"
"github.com/projectdiscovery/nuclei/v2/pkg/parsers"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
2020-08-29 13:26:11 +00:00
)
2021-09-01 14:36:07 +00:00
func (r *Runner) templateLogMsg(id, name string, authors []string, templateSeverity severity.Severity) string {
2020-08-29 13:26:11 +00:00
// Display the message for the template
return fmt.Sprintf("[%s] %s (%s) [%s]",
r.colorizer.BrightBlue(id).String(),
r.colorizer.Bold(name).String(),
2021-09-01 14:36:07 +00:00
r.colorizer.BrightYellow(appendAtSignToAuthors(authors)).String(),
r.addColor(templateSeverity))
}
2021-09-01 14:36:07 +00:00
// appendAtSignToAuthors appends @ before each author and returns the final string
func appendAtSignToAuthors(authors []string) string {
2021-07-08 09:45:26 +00:00
if len(authors) == 0 {
return "@none"
}
2021-09-01 14:36:07 +00:00
2021-07-08 09:45:26 +00:00
values := make([]string, 0, len(authors))
for _, k := range authors {
2021-09-01 14:36:07 +00:00
if !strings.HasPrefix(k, "@") {
2021-07-08 09:45:26 +00:00
values = append(values, fmt.Sprintf("@%s", k))
} else {
values = append(values, k)
}
}
return strings.Join(values, ",")
}
func (r *Runner) logAvailableTemplate(tplPath string) {
t, err := parsers.ParseTemplate(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),
2021-09-01 14:36:07 +00:00
t.Info.Authors.ToSlice(),
t.Info.SeverityHolder.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.TemplateVersion,
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 directoryWalker(fsPath string, callback func(fsPath string, d *godirwalk.Dirent) error) error {
2021-07-01 09:06:40 +00:00
return godirwalk.Walk(fsPath, &godirwalk.Options{
2020-08-29 13:26:11 +00:00
Callback: callback,
ErrorCallback: func(fsPath string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
Unsorted: true,
})
}