Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/internal/runner/templates.go
2852 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
// order templates alphabetically by path
48
templates := store.Templates()
49
sort.Slice(templates, func(i, j int) bool {
50
return templates[i].Path < templates[j].Path
51
})
52
53
for _, tpl := range templates {
54
if hasExtraFlags(r.options) {
55
if r.options.TemplateDisplay {
56
colorize := !r.options.NoColor
57
path := tpl.Path
58
tplBody, err := store.ReadTemplateFromURI(path, true)
59
if err != nil {
60
r.Logger.Error().Msgf("Could not read the template %s: %s", path, err)
61
continue
62
}
63
if colorize {
64
path = aurora.Cyan(tpl.Path).String()
65
tplBody, err = r.highlightTemplate(&tplBody)
66
if err != nil {
67
r.Logger.Error().Msgf("Could not highlight the template %s: %s", tpl.Path, err)
68
continue
69
}
70
}
71
r.Logger.Print().Msgf("Template: %s\n\n%s", path, tplBody)
72
} else {
73
r.Logger.Print().Msgf("%s\n", strings.TrimPrefix(tpl.Path, config.DefaultConfig.TemplatesDirectory+string(filepath.Separator)))
74
}
75
} else {
76
r.verboseTemplate(tpl)
77
}
78
}
79
}
80
81
func (r *Runner) listAvailableStoreTags(store *loader.Store) {
82
r.Logger.Print().Msgf(
83
"\nListing available %v nuclei tags for %v",
84
config.DefaultConfig.TemplateVersion,
85
config.DefaultConfig.TemplatesDirectory,
86
)
87
tagsMap := make(map[string]int)
88
for _, tpl := range store.Templates() {
89
for _, tag := range tpl.Info.Tags.ToSlice() {
90
tagsMap[tag]++
91
}
92
}
93
type kv struct {
94
Key string `json:"tag"`
95
Value int `json:"count"`
96
}
97
var tagsList []kv
98
for k, v := range tagsMap {
99
tagsList = append(tagsList, kv{k, v})
100
}
101
sort.Slice(tagsList, func(i, j int) bool {
102
return tagsList[i].Value > tagsList[j].Value
103
})
104
105
for _, tag := range tagsList {
106
if r.options.JSONL {
107
marshalled, _ := jsoniter.Marshal(tag)
108
r.Logger.Debug().Msgf("%s", string(marshalled))
109
} else {
110
r.Logger.Debug().Msgf("%s (%d)", tag.Key, tag.Value)
111
}
112
}
113
}
114
115
func (r *Runner) highlightTemplate(body *[]byte) ([]byte, error) {
116
var buf bytes.Buffer
117
// YAML lexer, true color terminal formatter and monokai style
118
err := quick.Highlight(&buf, string(*body), "yaml", "terminal16m", "monokai")
119
if err != nil {
120
return nil, err
121
}
122
123
return buf.Bytes(), nil
124
}
125
126
func hasExtraFlags(options *types.Options) bool {
127
return options.Templates != nil || options.Authors != nil ||
128
options.Tags != nil || len(options.ExcludeTags) > 3 ||
129
options.IncludeTags != nil || options.IncludeIds != nil ||
130
options.ExcludeIds != nil || options.IncludeTemplates != nil ||
131
options.ExcludedTemplates != nil || options.ExcludeMatchers != nil ||
132
options.Severities != nil || options.ExcludeSeverities != nil ||
133
options.Protocols != nil || options.ExcludeProtocols != nil ||
134
options.IncludeConditions != nil || options.TemplateList
135
}
136
137