2020-08-29 13:26:11 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2022-11-29 12:18:44 +00:00
|
|
|
"bytes"
|
2022-05-08 06:52:21 +00:00
|
|
|
"path/filepath"
|
2020-08-29 13:26:11 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-11-29 12:18:44 +00:00
|
|
|
"github.com/alecthomas/chroma/quick"
|
|
|
|
"github.com/logrusorgru/aurora"
|
2023-10-17 12:14:13 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/loader"
|
2022-11-29 12:18:44 +00:00
|
|
|
|
2020-08-29 13:26:11 +00:00
|
|
|
"github.com/projectdiscovery/gologger"
|
2023-10-17 12:14:13 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/parsers"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2020-08-29 13:26:11 +00:00
|
|
|
)
|
|
|
|
|
2022-09-20 21:33:57 +00:00
|
|
|
// log available templates for verbose (-vv)
|
2020-08-29 21:02:45 +00:00
|
|
|
func (r *Runner) logAvailableTemplate(tplPath string) {
|
2022-08-10 18:05:58 +00:00
|
|
|
t, err := parsers.ParseTemplate(tplPath, r.catalog)
|
2020-12-29 10:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
gologger.Error().Msgf("Could not parse file '%s': %s\n", tplPath, err)
|
2021-01-14 07:51:21 +00:00
|
|
|
} else {
|
2022-11-29 12:18:44 +00:00
|
|
|
r.verboseTemplate(t)
|
2020-08-29 21:02:45 +00:00
|
|
|
}
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 12:18:44 +00:00
|
|
|
// log available templates for verbose (-vv)
|
|
|
|
func (r *Runner) verboseTemplate(tpl *templates.Template) {
|
|
|
|
gologger.Print().Msgf("%s\n", templates.TemplateLogMessage(tpl.ID,
|
|
|
|
types.ToString(tpl.Info.Name),
|
|
|
|
tpl.Info.Authors.ToSlice(),
|
|
|
|
tpl.Info.SeverityHolder.Severity))
|
|
|
|
}
|
|
|
|
|
2022-09-20 21:33:57 +00:00
|
|
|
func (r *Runner) listAvailableStoreTemplates(store *loader.Store) {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Print().Msgf(
|
2023-04-19 16:28:48 +00:00
|
|
|
"\nListing available %v nuclei templates for %v",
|
|
|
|
config.DefaultConfig.TemplateVersion,
|
|
|
|
config.DefaultConfig.TemplatesDirectory,
|
2020-08-30 11:25:34 +00:00
|
|
|
)
|
2022-11-29 12:18:44 +00:00
|
|
|
for _, tpl := range store.Templates() {
|
|
|
|
if hasExtraFlags(r.options) {
|
|
|
|
if r.options.TemplateDisplay {
|
|
|
|
colorize := !r.options.NoColor
|
|
|
|
path := tpl.Path
|
2023-08-25 21:03:45 +00:00
|
|
|
tplBody, err := store.ReadTemplateFromURI(path, true)
|
2022-11-29 12:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
gologger.Error().Msgf("Could not read the template %s: %s", path, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if colorize {
|
|
|
|
path = aurora.Cyan(tpl.Path).String()
|
|
|
|
tplBody, err = r.highlightTemplate(&tplBody)
|
|
|
|
if err != nil {
|
2023-08-01 18:33:43 +00:00
|
|
|
gologger.Error().Msgf("Could not highlight the template %s: %s", tpl.Path, err)
|
2022-11-29 12:18:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gologger.Silent().Msgf("Template: %s\n\n%s", path, tplBody)
|
|
|
|
} else {
|
2023-04-19 16:28:48 +00:00
|
|
|
gologger.Silent().Msgf("%s\n", strings.TrimPrefix(tpl.Path, config.DefaultConfig.TemplatesDirectory+string(filepath.Separator)))
|
2022-11-29 12:18:44 +00:00
|
|
|
}
|
2022-09-20 21:33:57 +00:00
|
|
|
} else {
|
2022-11-29 12:18:44 +00:00
|
|
|
r.verboseTemplate(tpl)
|
2022-09-20 21:33:57 +00:00
|
|
|
}
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-29 12:18:44 +00:00
|
|
|
|
|
|
|
func (r *Runner) highlightTemplate(body *[]byte) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
2023-08-01 18:33:43 +00:00
|
|
|
// YAML lexer, true color terminal formatter and monokai style
|
2022-11-29 12:18:44 +00:00
|
|
|
err := quick.Highlight(&buf, string(*body), "yaml", "terminal16m", "monokai")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasExtraFlags(options *types.Options) bool {
|
|
|
|
return options.Templates != nil || options.Authors != nil ||
|
|
|
|
options.Tags != nil || len(options.ExcludeTags) > 3 ||
|
|
|
|
options.IncludeTags != nil || options.IncludeIds != nil ||
|
|
|
|
options.ExcludeIds != nil || options.IncludeTemplates != nil ||
|
|
|
|
options.ExcludedTemplates != nil || options.ExcludeMatchers != nil ||
|
|
|
|
options.Severities != nil || options.ExcludeSeverities != nil ||
|
|
|
|
options.Protocols != nil || options.ExcludeProtocols != nil ||
|
|
|
|
options.IncludeConditions != nil || options.TemplateList
|
|
|
|
}
|