From 0635b9e54f09d15d07e33d8ccfebc22ea350d7ee Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Mon, 10 Jan 2022 10:16:48 +0100 Subject: [PATCH] Adding support for ignore file path override if custom config file is used (#1441) * Adding support for ignore file path override if custom config file is used --- v2/cmd/nuclei/main.go | 5 +++++ v2/pkg/catalog/config/config.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/v2/cmd/nuclei/main.go b/v2/cmd/nuclei/main.go index 2471c91a7..6c78ec5f6 100644 --- a/v2/cmd/nuclei/main.go +++ b/v2/cmd/nuclei/main.go @@ -10,6 +10,7 @@ import ( "github.com/projectdiscovery/goflags" "github.com/projectdiscovery/gologger" "github.com/projectdiscovery/nuclei/v2/internal/runner" + "github.com/projectdiscovery/nuclei/v2/pkg/catalog/config" "github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity" templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types" "github.com/projectdiscovery/nuclei/v2/pkg/types" @@ -203,6 +204,10 @@ on extensive configurability, massive extensibility and ease of use.`) if err := flagSet.MergeConfigFile(cfgFile); err != nil { gologger.Fatal().Msgf("Could not read config: %s\n", err) } + cfgFileFolder := filepath.Dir(cfgFile) + if err := config.OverrideIgnoreFilePath(cfgFileFolder); err != nil { + gologger.Warning().Msgf("Could not read ignore file from custom path: %s\n", err) + } } } diff --git a/v2/pkg/catalog/config/config.go b/v2/pkg/catalog/config/config.go index 7b6e6ab39..9861d8e18 100644 --- a/v2/pkg/catalog/config/config.go +++ b/v2/pkg/catalog/config/config.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" "gopkg.in/yaml.v2" + "github.com/projectdiscovery/fileutil" "github.com/projectdiscovery/gologger" ) @@ -105,10 +106,38 @@ func ReadIgnoreFile() 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 + } + home, err := os.UserHomeDir() if err == nil { configDir := filepath.Join(home, ".config", "nuclei")