2020-06-25 03:53:37 +05:30
|
|
|
package runner
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-24 00:16:18 +05:30
|
|
|
"bufio"
|
2020-06-25 03:53:37 +05:30
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"regexp"
|
2020-06-25 21:26:44 +05:30
|
|
|
"strings"
|
2020-07-07 18:24:00 +05:30
|
|
|
"time"
|
2020-06-25 03:53:37 +05:30
|
|
|
|
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// nucleiConfig contains some configuration options for nuclei
|
|
|
|
|
type nucleiConfig struct {
|
2020-07-07 18:24:00 +05:30
|
|
|
TemplatesDirectory string `json:"templates-directory,omitempty"`
|
|
|
|
|
CurrentVersion string `json:"current-version,omitempty"`
|
|
|
|
|
LastChecked time.Time `json:"last-checked,omitempty"`
|
2021-03-22 15:48:27 +05:30
|
|
|
IgnoreURL string `json:"ignore-url,omitempty"`
|
|
|
|
|
NucleiVersion string `json:"nuclei-version,omitempty"`
|
|
|
|
|
LastCheckedIgnore time.Time `json:"last-checked-ignore,omitempty"`
|
2020-08-25 23:24:31 +02:00
|
|
|
// IgnorePaths ignores all the paths listed unless specified manually
|
|
|
|
|
IgnorePaths []string `json:"ignore-paths,omitempty"`
|
2020-06-25 03:53:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// nucleiConfigFilename is the filename of nuclei configuration file.
|
2021-03-22 15:48:27 +05:30
|
|
|
const nucleiConfigFilename = ".templates-config.json"
|
2020-06-25 03:53:37 +05:30
|
|
|
|
|
|
|
|
var reVersion = regexp.MustCompile(`\d+\.\d+\.\d+`)
|
|
|
|
|
|
|
|
|
|
// readConfiguration reads the nuclei configuration file from disk.
|
2020-10-20 02:14:44 +05:30
|
|
|
func readConfiguration() (*nucleiConfig, error) {
|
2020-06-25 03:53:37 +05:30
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-03-22 15:48:27 +05:30
|
|
|
configDir := path.Join(home, "/.config", "/nuclei")
|
|
|
|
|
_ = os.MkdirAll(configDir, os.ModePerm)
|
2020-06-25 03:53:37 +05:30
|
|
|
|
2021-03-22 15:48:27 +05:30
|
|
|
templatesConfigFile := path.Join(configDir, nucleiConfigFilename)
|
2020-06-25 03:53:37 +05:30
|
|
|
file, err := os.Open(templatesConfigFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
config := &nucleiConfig{}
|
2020-08-25 23:24:31 +02:00
|
|
|
err = jsoniter.NewDecoder(file).Decode(config)
|
|
|
|
|
if err != nil {
|
2020-06-25 03:53:37 +05:30
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// readConfiguration reads the nuclei configuration file from disk.
|
|
|
|
|
func (r *Runner) writeConfiguration(config *nucleiConfig) error {
|
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-03-22 15:48:27 +05:30
|
|
|
configDir := path.Join(home, "/.config", "/nuclei")
|
|
|
|
|
_ = os.MkdirAll(configDir, os.ModePerm)
|
2020-06-25 03:53:37 +05:30
|
|
|
|
2021-03-22 15:48:27 +05:30
|
|
|
if config.IgnoreURL == "" {
|
|
|
|
|
config.IgnoreURL = "https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/master/.nuclei-ignore"
|
|
|
|
|
}
|
2020-07-08 17:25:29 +05:30
|
|
|
config.LastChecked = time.Now()
|
2021-03-22 15:48:27 +05:30
|
|
|
config.LastCheckedIgnore = time.Now()
|
|
|
|
|
config.NucleiVersion = Version
|
|
|
|
|
templatesConfigFile := path.Join(configDir, nucleiConfigFilename)
|
2020-06-25 21:26:44 +05:30
|
|
|
file, err := os.OpenFile(templatesConfigFile, os.O_WRONLY|os.O_CREATE, 0777)
|
2020-06-25 03:53:37 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
2020-08-25 23:24:31 +02:00
|
|
|
err = jsoniter.NewEncoder(file).Encode(config)
|
|
|
|
|
if err != nil {
|
2020-06-25 21:26:44 +05:30
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2020-06-25 03:53:37 +05:30
|
|
|
}
|
|
|
|
|
|
2020-08-24 00:16:18 +05:30
|
|
|
const nucleiIgnoreFile = ".nuclei-ignore"
|
|
|
|
|
|
|
|
|
|
// readNucleiIgnoreFile reads the nuclei ignore file marking it in map
|
|
|
|
|
func (r *Runner) readNucleiIgnoreFile() {
|
2020-10-11 11:02:24 +02:00
|
|
|
file, err := os.Open(r.getIgnoreFilePath())
|
2020-08-24 00:16:18 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
text := scanner.Text()
|
|
|
|
|
if text == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-10-02 15:02:11 +05:30
|
|
|
if strings.HasPrefix(text, "#") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
r.templatesConfig.IgnorePaths = append(r.templatesConfig.IgnorePaths, text)
|
2020-08-24 00:16:18 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 15:38:14 +05:30
|
|
|
// getIgnoreFilePath returns the ignore file path for the runner
|
2020-10-11 11:02:24 +02:00
|
|
|
func (r *Runner) getIgnoreFilePath() string {
|
2021-03-22 15:48:27 +05:30
|
|
|
var defIgnoreFilePath string
|
|
|
|
|
|
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
|
if err == nil {
|
|
|
|
|
configDir := path.Join(home, "/.config", "/nuclei")
|
|
|
|
|
_ = os.MkdirAll(configDir, os.ModePerm)
|
|
|
|
|
|
|
|
|
|
defIgnoreFilePath = path.Join(configDir, nucleiIgnoreFile)
|
2021-03-31 21:27:40 +05:30
|
|
|
return defIgnoreFilePath
|
2021-03-22 15:48:27 +05:30
|
|
|
}
|
2020-10-11 11:02:24 +02:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return defIgnoreFilePath
|
|
|
|
|
}
|
|
|
|
|
cwdIgnoreFilePath := path.Join(cwd, nucleiIgnoreFile)
|
|
|
|
|
return cwdIgnoreFilePath
|
|
|
|
|
}
|