github.com/projectdiscovery/nuclei/v2@v2.9.15/internal/runner/templates.go (about) 1 package runner 2 3 import ( 4 "bytes" 5 "path/filepath" 6 "strings" 7 8 "github.com/alecthomas/chroma/quick" 9 "github.com/logrusorgru/aurora" 10 "github.com/projectdiscovery/nuclei/v2/pkg/catalog/config" 11 "github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader" 12 13 "github.com/projectdiscovery/gologger" 14 "github.com/projectdiscovery/nuclei/v2/pkg/parsers" 15 "github.com/projectdiscovery/nuclei/v2/pkg/templates" 16 "github.com/projectdiscovery/nuclei/v2/pkg/types" 17 ) 18 19 // log available templates for verbose (-vv) 20 func (r *Runner) logAvailableTemplate(tplPath string) { 21 t, err := parsers.ParseTemplate(tplPath, r.catalog) 22 if err != nil { 23 gologger.Error().Msgf("Could not parse file '%s': %s\n", tplPath, err) 24 } else { 25 r.verboseTemplate(t) 26 } 27 } 28 29 // log available templates for verbose (-vv) 30 func (r *Runner) verboseTemplate(tpl *templates.Template) { 31 gologger.Print().Msgf("%s\n", templates.TemplateLogMessage(tpl.ID, 32 types.ToString(tpl.Info.Name), 33 tpl.Info.Authors.ToSlice(), 34 tpl.Info.SeverityHolder.Severity)) 35 } 36 37 func (r *Runner) listAvailableStoreTemplates(store *loader.Store) { 38 gologger.Print().Msgf( 39 "\nListing available %v nuclei templates for %v", 40 config.DefaultConfig.TemplateVersion, 41 config.DefaultConfig.TemplatesDirectory, 42 ) 43 for _, tpl := range store.Templates() { 44 if hasExtraFlags(r.options) { 45 if r.options.TemplateDisplay { 46 colorize := !r.options.NoColor 47 path := tpl.Path 48 tplBody, err := store.ReadTemplateFromURI(path, true) 49 if err != nil { 50 gologger.Error().Msgf("Could not read the template %s: %s", path, err) 51 continue 52 } 53 if colorize { 54 path = aurora.Cyan(tpl.Path).String() 55 tplBody, err = r.highlightTemplate(&tplBody) 56 if err != nil { 57 gologger.Error().Msgf("Could not highlight the template %s: %s", tpl.Path, err) 58 continue 59 } 60 } 61 gologger.Silent().Msgf("Template: %s\n\n%s", path, tplBody) 62 } else { 63 gologger.Silent().Msgf("%s\n", strings.TrimPrefix(tpl.Path, config.DefaultConfig.TemplatesDirectory+string(filepath.Separator))) 64 } 65 } else { 66 r.verboseTemplate(tpl) 67 } 68 } 69 } 70 71 func (r *Runner) highlightTemplate(body *[]byte) ([]byte, error) { 72 var buf bytes.Buffer 73 // YAML lexer, true color terminal formatter and monokai style 74 err := quick.Highlight(&buf, string(*body), "yaml", "terminal16m", "monokai") 75 if err != nil { 76 return nil, err 77 } 78 79 return buf.Bytes(), nil 80 } 81 82 func hasExtraFlags(options *types.Options) bool { 83 return options.Templates != nil || options.Authors != nil || 84 options.Tags != nil || len(options.ExcludeTags) > 3 || 85 options.IncludeTags != nil || options.IncludeIds != nil || 86 options.ExcludeIds != nil || options.IncludeTemplates != nil || 87 options.ExcludedTemplates != nil || options.ExcludeMatchers != nil || 88 options.Severities != nil || options.ExcludeSeverities != nil || 89 options.Protocols != nil || options.ExcludeProtocols != nil || 90 options.IncludeConditions != nil || options.TemplateList 91 }