Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/catalog/config/template.go
2070 views
1
package config
2
3
import (
4
"encoding/csv"
5
"io"
6
"os"
7
"path/filepath"
8
"strings"
9
10
"github.com/projectdiscovery/nuclei/v3/pkg/templates/extensions"
11
fileutil "github.com/projectdiscovery/utils/file"
12
stringsutil "github.com/projectdiscovery/utils/strings"
13
)
14
15
var knownConfigFiles = []string{"cves.json", "contributors.json", "TEMPLATES-STATS.json"}
16
17
// TemplateFormat
18
type TemplateFormat uint8
19
20
const (
21
YAML TemplateFormat = iota
22
JSON
23
Unknown
24
)
25
26
// GetTemplateFormatFromExt returns template format
27
func GetTemplateFormatFromExt(filePath string) TemplateFormat {
28
fileExt := strings.ToLower(filepath.Ext(filePath))
29
switch fileExt {
30
case extensions.JSON:
31
return JSON
32
case extensions.YAML:
33
return YAML
34
default:
35
return Unknown
36
}
37
}
38
39
// GetSupportTemplateFileExtensions returns all supported template file extensions
40
func GetSupportTemplateFileExtensions() []string {
41
return []string{extensions.YAML, extensions.JSON}
42
}
43
44
// IsTemplate is a callback function used by goflags to decide if given file should be read
45
// if it is not a nuclei-template file only then file is read
46
func IsTemplate(filename string) bool {
47
if stringsutil.ContainsAny(filename, knownConfigFiles...) {
48
return false
49
}
50
return stringsutil.EqualFoldAny(filepath.Ext(filename), GetSupportTemplateFileExtensions()...)
51
}
52
53
type template struct {
54
ID string `json:"id" yaml:"id"`
55
}
56
57
// GetTemplateIDFromReader returns template id from reader
58
func GetTemplateIDFromReader(data io.Reader, filename string) (string, error) {
59
var t template
60
var err error
61
switch GetTemplateFormatFromExt(filename) {
62
case YAML:
63
err = fileutil.UnmarshalFromReader(fileutil.YAML, data, &t)
64
case JSON:
65
err = fileutil.UnmarshalFromReader(fileutil.JSON, data, &t)
66
}
67
return t.ID, err
68
}
69
70
func getTemplateID(filePath string) (string, error) {
71
file, err := os.Open(filePath)
72
if err != nil {
73
return "", err
74
}
75
76
defer func() {
77
_ = file.Close()
78
}()
79
return GetTemplateIDFromReader(file, filePath)
80
}
81
82
// GetTemplatesIndexFile returns map[template-id]: template-file-path
83
func GetNucleiTemplatesIndex() (map[string]string, error) {
84
indexFile := DefaultConfig.GetTemplateIndexFilePath()
85
index := map[string]string{}
86
if fileutil.FileExists(indexFile) {
87
f, err := os.Open(indexFile)
88
if err == nil {
89
csvReader := csv.NewReader(f)
90
records, err := csvReader.ReadAll()
91
if err == nil {
92
for _, v := range records {
93
if len(v) >= 2 {
94
index[v[0]] = v[1]
95
}
96
}
97
return index, nil
98
}
99
}
100
DefaultConfig.Logger.Error().Msgf("failed to read index file creating new one: %v", err)
101
}
102
103
ignoreDirs := DefaultConfig.GetAllCustomTemplateDirs()
104
105
// empty index if templates are not installed
106
if !fileutil.FolderExists(DefaultConfig.TemplatesDirectory) {
107
return index, nil
108
}
109
err := filepath.WalkDir(DefaultConfig.TemplatesDirectory, func(path string, d os.DirEntry, err error) error {
110
if err != nil {
111
DefaultConfig.Logger.Verbose().Msgf("failed to walk path=%v err=%v", path, err)
112
return nil
113
}
114
if d.IsDir() || !IsTemplate(path) || stringsutil.ContainsAny(path, ignoreDirs...) {
115
return nil
116
}
117
// get template id from file
118
id, err := getTemplateID(path)
119
if err != nil || id == "" {
120
DefaultConfig.Logger.Verbose().Msgf("failed to get template id from file=%v got id=%v err=%v", path, id, err)
121
return nil
122
}
123
index[id] = path
124
return nil
125
})
126
return index, err
127
}
128
129