forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
182 lines (155 loc) · 4.92 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package config
import (
"os"
"path/filepath"
jsoniter "github.com/json-iterator/go"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/projectdiscovery/gologger"
fileutil "github.com/projectdiscovery/utils/file"
)
// Config contains the internal nuclei engine configuration
type Config struct {
TemplatesDirectory string `json:"nuclei-templates-directory,omitempty"`
CustomS3TemplatesDirectory string `json:"custom-s3-templates-directory"`
CustomGithubTemplatesDirectory string `json:"custom-github-templates-directory"`
TemplateVersion string `json:"nuclei-templates-version,omitempty"`
NucleiVersion string `json:"nuclei-version,omitempty"`
NucleiIgnoreHash string `json:"nuclei-ignore-hash,omitempty"`
NucleiLatestVersion string `json:"nuclei-latest-version"`
NucleiTemplatesLatestVersion string `json:"nuclei-templates-latest-version"`
}
// nucleiConfigFilename is the filename of nuclei configuration file.
const nucleiConfigFilename = ".templates-config.json"
// Version is the current version of nuclei
const Version = `2.8.0`
var customConfigDirectory string
func SetCustomConfigDirectory(dir string) {
customConfigDirectory = dir
if !fileutil.FolderExists(dir) {
_ = fileutil.CreateFolder(dir)
}
}
func getConfigDetails() (string, error) {
configDir, err := GetConfigDir()
if err != nil {
return "", errors.Wrap(err, "could not get home directory")
}
_ = os.MkdirAll(configDir, 0755)
templatesConfigFile := filepath.Join(configDir, nucleiConfigFilename)
return templatesConfigFile, nil
}
// GetConfigDir returns the nuclei configuration directory
func GetConfigDir() (string, error) {
var (
home string
err error
)
if customConfigDirectory != "" {
home = customConfigDirectory
return home, nil
}
home, err = homedir.Dir()
if err != nil {
return "", err
}
return filepath.Join(home, ".config", "nuclei"), nil
}
// ReadConfiguration reads the nuclei configuration file from disk.
func ReadConfiguration() (*Config, error) {
templatesConfigFile, err := getConfigDetails()
if err != nil {
return nil, err
}
file, err := os.Open(templatesConfigFile)
if err != nil {
return nil, err
}
defer file.Close()
config := &Config{}
if err := jsoniter.NewDecoder(file).Decode(config); err != nil {
return nil, err
}
return config, nil
}
// WriteConfiguration writes the updated nuclei configuration to disk
func WriteConfiguration(config *Config) error {
config.NucleiVersion = Version
templatesConfigFile, err := getConfigDetails()
if err != nil {
return err
}
file, err := os.OpenFile(templatesConfigFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
err = jsoniter.NewEncoder(file).Encode(config)
if err != nil {
return err
}
return nil
}
const nucleiIgnoreFile = ".nuclei-ignore"
// IgnoreFile is an internal nuclei template blocking configuration file
type IgnoreFile struct {
Tags []string `yaml:"tags"`
Files []string `yaml:"files"`
}
// ReadIgnoreFile reads the nuclei ignore file returning blocked tags and paths
func ReadIgnoreFile() IgnoreFile {
file, err := os.Open(GetIgnoreFilePath())
if err != nil {
gologger.Error().Msgf("Could not read nuclei-ignore file: %s\n", err)
return IgnoreFile{}
}
defer file.Close()
ignore := IgnoreFile{}
if err := yaml.NewDecoder(file).Decode(&ignore); err != nil {
gologger.Error().Msgf("Could not parse nuclei-ignore file: %s\n", err)
return IgnoreFile{}
}
return ignore
}
var (
// customIgnoreFilePath contains a custom path for the ignore file
customIgnoreFilePath string
// ErrCustomIgnoreFilePathNotExist is raised when the ignore file doesn't exist in the custom path
ErrCustomIgnoreFilePathNotExist = errors.New("Ignore file doesn't exist in custom path")
// ErrCustomFolderNotExist is raised when the custom ignore folder doesn't exist
ErrCustomFolderNotExist = errors.New("The custom ignore path doesn't exist")
)
// OverrideIgnoreFilePath with a custom existing folder
func OverrideIgnoreFilePath(customPath string) error {
// custom path does not exist
if !fileutil.FolderExists(customPath) {
return ErrCustomFolderNotExist
}
// ignore file within the custom path does not exist
if !fileutil.FileExists(filepath.Join(customPath, nucleiIgnoreFile)) {
return ErrCustomIgnoreFilePathNotExist
}
customIgnoreFilePath = customPath
return nil
}
// GetIgnoreFilePath returns the ignore file path for the runner
func GetIgnoreFilePath() string {
var defIgnoreFilePath string
if customIgnoreFilePath != "" {
defIgnoreFilePath = filepath.Join(customIgnoreFilePath, nucleiIgnoreFile)
return defIgnoreFilePath
}
configDir, err := GetConfigDir()
if err == nil {
_ = os.MkdirAll(configDir, 0755)
defIgnoreFilePath = filepath.Join(configDir, nucleiIgnoreFile)
return defIgnoreFilePath
}
cwd, err := os.Getwd()
if err != nil {
return defIgnoreFilePath
}
cwdIgnoreFilePath := filepath.Join(cwd, nucleiIgnoreFile)
return cwdIgnoreFilePath
}