Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/internal/runner/templates.go
2070 views
1
package runner
2
3
import (
4
"bytes"
5
"path/filepath"
6
"sort"
7
"strings"
8
9
"github.com/alecthomas/chroma/quick"
10
jsoniter "github.com/json-iterator/go"
11
"github.com/logrusorgru/aurora"
12
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
13
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/loader"
14
15
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
16
"github.com/projectdiscovery/nuclei/v3/pkg/types"
17
)
18
19
// log available templates for verbose (-vv)
20
func (r *Runner) logAvailableTemplate(tplPath string) {
21
t, err := r.parser.ParseTemplate(tplPath, r.catalog)
22
tpl, ok := t.(*templates.Template)
23
if !ok {
24
panic("not a template")
25
}
26
if err != nil {
27
r.Logger.Error().Msgf("Could not parse file '%s': %s\n", tplPath, err)
28
} else {
29
r.verboseTemplate(tpl)
30
}
31
}
32
33
// log available templates for verbose (-vv)
34
func (r *Runner) verboseTemplate(tpl *templates.Template) {
35
r.Logger.Print().Msgf("%s\n", templates.TemplateLogMessage(tpl.ID,
36
types.ToString(tpl.Info.Name),
37
tpl.Info.Authors.ToSlice(),
38
tpl.Info.SeverityHolder.Severity))
39
}
40
41
func (r *Runner) listAvailableStoreTemplates(store *loader.Store) {
42
r.Logger.Print().Msgf(
43
"\nListing available %v nuclei templates for %v",
44
config.DefaultConfig.TemplateVersion,
45
config.DefaultConfig.TemplatesDirectory,
46
)
47
for _, tpl := range store.Templates() {
48
if hasExtraFlags(r.options) {
49
if r.options.TemplateDisplay {
50
colorize := !r.options.NoColor
51
path := tpl.Path
52
tplBody, err := store.ReadTemplateFromURI(path, true)
53
if err != nil {
54
r.Logger.Error().Msgf("Could not read the template %s: %s", path, err)
55
continue
56
}
57
if colorize {
58
path = aurora.Cyan(tpl.Path).String()
59
tplBody, err = r.highlightTemplate(&tplBody)
60
if err != nil {
61
r.Logger.Error().Msgf("Could not highlight the template %s: %s", tpl.Path, err)
62
continue
63
}
64
}
65
r.Logger.Print().Msgf("Template: %s\n\n%s", path, tplBody)
66
} else {
67
r.Logger.Print().Msgf("%s\n", strings.TrimPrefix(tpl.Path, config.DefaultConfig.TemplatesDirectory+string(filepath.Separator)))
68
}
69
} else {
70
r.verboseTemplate(tpl)
71
}
72
}
73
}
74
75
func (r *Runner) listAvailableStoreTags(store *loader.Store) {
76
r.Logger.Print().Msgf(
77
"\nListing available %v nuclei tags for %v",
78
config.DefaultConfig.TemplateVersion,
79
config.DefaultConfig.TemplatesDirectory,
80
)
81
tagsMap := make(map[string]int)
82
for _, tpl := range store.Templates() {
83
for _, tag := range tpl.Info.Tags.ToSlice() {
84
tagsMap[tag]++
85
}
86
}
87
type kv struct {
88
Key string `json:"tag"`
89
Value int `json:"count"`
90
}
91
var tagsList []kv
92
for k, v := range tagsMap {
93
tagsList = append(tagsList, kv{k, v})
94
}
95
sort.Slice(tagsList, func(i, j int) bool {
96
return tagsList[i].Value > tagsList[j].Value
97
})
98
99
for _, tag := range tagsList {
100
if r.options.JSONL {
101
marshalled, _ := jsoniter.Marshal(tag)
102
r.Logger.Debug().Msgf("%s", string(marshalled))
103
} else {
104
r.Logger.Debug().Msgf("%s (%d)", tag.Key, tag.Value)
105
}
106
}
107
}
108
109
func (r *Runner) highlightTemplate(body *[]byte) ([]byte, error) {
110
var buf bytes.Buffer
111
// YAML lexer, true color terminal formatter and monokai style
112
err := quick.Highlight(&buf, string(*body), "yaml", "terminal16m", "monokai")
113
if err != nil {
114
return nil, err
115
}
116
117
return buf.Bytes(), nil
118
}
119
120
func hasExtraFlags(options *types.Options) bool {
121
return options.Templates != nil || options.Authors != nil ||
122
options.Tags != nil || len(options.ExcludeTags) > 3 ||
123
options.IncludeTags != nil || options.IncludeIds != nil ||
124
options.ExcludeIds != nil || options.IncludeTemplates != nil ||
125
options.ExcludedTemplates != nil || options.ExcludeMatchers != nil ||
126
options.Severities != nil || options.ExcludeSeverities != nil ||
127
options.Protocols != nil || options.ExcludeProtocols != nil ||
128
options.IncludeConditions != nil || options.TemplateList
129
}
130
131