2020-04-03 22:15:39 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2021-02-08 07:36:11 +00:00
|
|
|
"bufio"
|
2020-08-29 13:26:11 +00:00
|
|
|
"errors"
|
|
|
|
"net/url"
|
2020-04-03 22:15:39 +00:00
|
|
|
"os"
|
2021-08-27 14:06:06 +00:00
|
|
|
"path/filepath"
|
2021-02-08 07:36:11 +00:00
|
|
|
"strings"
|
2020-04-03 22:15:39 +00:00
|
|
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
2020-12-29 10:08:14 +00:00
|
|
|
"github.com/projectdiscovery/gologger/formatter"
|
|
|
|
"github.com/projectdiscovery/gologger/levels"
|
2021-06-30 13:09:01 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
|
2021-01-14 07:51:21 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolinit"
|
2020-12-29 10:08:14 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
2020-04-03 22:15:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ParseOptions parses the command line flags provided by a user
|
2021-01-12 09:44:49 +00:00
|
|
|
func ParseOptions(options *types.Options) {
|
2020-04-03 22:15:39 +00:00
|
|
|
// Check if stdin pipe was given
|
|
|
|
options.Stdin = hasStdin()
|
|
|
|
|
2021-09-13 19:18:43 +00:00
|
|
|
// if VerboseVerbose is set, it implicitly enables the Verbose option as well
|
|
|
|
if options.VerboseVerbose {
|
|
|
|
options.Verbose = true
|
|
|
|
}
|
|
|
|
|
2020-04-03 22:15:39 +00:00
|
|
|
// Read the inputs and configure the logging
|
2020-12-29 10:08:14 +00:00
|
|
|
configureOutput(options)
|
2020-04-03 22:15:39 +00:00
|
|
|
|
|
|
|
// Show the user the banner
|
|
|
|
showBanner()
|
|
|
|
|
|
|
|
if options.Version {
|
2021-06-30 13:09:01 +00:00
|
|
|
gologger.Info().Msgf("Current Version: %s\n", config.Version)
|
2020-04-03 22:15:39 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2020-10-19 20:44:44 +00:00
|
|
|
if options.TemplatesVersion {
|
2021-07-05 11:59:45 +00:00
|
|
|
configuration, err := config.ReadConfiguration()
|
2020-10-19 20:44:44 +00:00
|
|
|
if err != nil {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Fatal().Msgf("Could not read template configuration: %s\n", err)
|
2020-10-19 20:44:44 +00:00
|
|
|
}
|
2021-09-14 22:31:40 +00:00
|
|
|
gologger.Info().Msgf("Current nuclei-templates version: %s (%s)\n", configuration.TemplateVersion, configuration.TemplatesDirectory)
|
2020-10-19 20:44:44 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2020-04-26 01:18:10 +00:00
|
|
|
|
2020-04-03 22:15:39 +00:00
|
|
|
// Validate the options passed by the user and if any
|
|
|
|
// invalid options have been used, exit.
|
2021-02-09 12:43:42 +00:00
|
|
|
if err := validateOptions(options); err != nil {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.Fatal().Msgf("Program exiting: %s\n", err)
|
2020-04-03 22:15:39 +00:00
|
|
|
}
|
2021-02-08 07:36:11 +00:00
|
|
|
|
2021-02-21 11:01:34 +00:00
|
|
|
// Auto adjust rate limits when using headless mode if the user
|
|
|
|
// hasn't specified any custom limits.
|
|
|
|
if options.Headless && options.BulkSize == 25 && options.TemplateThreads == 10 {
|
|
|
|
options.BulkSize = 2
|
|
|
|
options.TemplateThreads = 2
|
|
|
|
}
|
|
|
|
|
2021-02-08 07:36:11 +00:00
|
|
|
// Load the resolvers if user asked for them
|
|
|
|
loadResolvers(options)
|
2021-02-09 12:43:42 +00:00
|
|
|
|
|
|
|
err := protocolinit.Init(options)
|
|
|
|
if err != nil {
|
|
|
|
gologger.Fatal().Msgf("Could not initialize protocols: %s\n", err)
|
|
|
|
}
|
2020-04-03 22:15:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:08:14 +00:00
|
|
|
// hasStdin returns true if we have stdin input
|
2020-04-03 22:15:39 +00:00
|
|
|
func hasStdin() bool {
|
2020-09-28 10:50:00 +00:00
|
|
|
stat, err := os.Stdin.Stat()
|
2020-04-03 22:15:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-08-25 21:24:31 +00:00
|
|
|
|
2020-09-28 10:50:00 +00:00
|
|
|
isPipedFromChrDev := (stat.Mode() & os.ModeCharDevice) == 0
|
|
|
|
isPipedFromFIFO := (stat.Mode() & os.ModeNamedPipe) != 0
|
2020-08-25 21:24:31 +00:00
|
|
|
|
2020-09-28 10:50:00 +00:00
|
|
|
return isPipedFromChrDev || isPipedFromFIFO
|
2020-04-03 22:15:39 +00:00
|
|
|
}
|
2020-08-29 13:26:11 +00:00
|
|
|
|
|
|
|
// validateOptions validates the configuration options passed
|
2020-12-29 10:08:14 +00:00
|
|
|
func validateOptions(options *types.Options) error {
|
2020-08-29 13:26:11 +00:00
|
|
|
if options.Verbose && options.Silent {
|
|
|
|
return errors.New("both verbose and silent mode specified")
|
|
|
|
}
|
|
|
|
|
2021-08-31 09:55:52 +00:00
|
|
|
if err := validateProxyURL(options.ProxyURL, "invalid http proxy format (It should be http://username:password@host:port)"); err != nil {
|
2020-08-29 14:25:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-31 09:55:52 +00:00
|
|
|
if err := validateProxyURL(options.ProxySocksURL, "invalid socks proxy format (It should be socks5://username:password@host:port)"); err != nil {
|
2020-08-29 14:25:30 +00:00
|
|
|
return err
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
2021-08-27 14:06:06 +00:00
|
|
|
|
|
|
|
if options.Validate {
|
2021-09-30 16:07:59 +00:00
|
|
|
options.Headless = true // required for correct validation of headless templates
|
2021-08-27 14:06:06 +00:00
|
|
|
validateTemplatePaths(options.TemplatesDirectory, options.Templates, options.Workflows)
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:25:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateProxyURL(proxyURL, message string) error {
|
|
|
|
if proxyURL != "" && !isValidURL(proxyURL) {
|
|
|
|
return errors.New(message)
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:25:30 +00:00
|
|
|
func isValidURL(urlString string) bool {
|
|
|
|
_, err := url.Parse(urlString)
|
2020-08-29 13:26:11 +00:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-09-01 14:34:51 +00:00
|
|
|
// configureOutput configures the output logging levels to be displayed on the screen
|
2020-12-29 10:08:14 +00:00
|
|
|
func configureOutput(options *types.Options) {
|
2020-08-29 13:26:11 +00:00
|
|
|
// If the user desires verbose output, show verbose output
|
2021-09-13 19:18:43 +00:00
|
|
|
if options.Verbose || options.VerboseVerbose {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.DefaultLogger.SetMaxLevel(levels.LevelVerbose)
|
|
|
|
}
|
|
|
|
if options.Debug {
|
|
|
|
gologger.DefaultLogger.SetMaxLevel(levels.LevelDebug)
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
if options.NoColor {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.DefaultLogger.SetFormatter(formatter.NewCLI(true))
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
if options.Silent {
|
2020-12-29 10:08:14 +00:00
|
|
|
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-08 07:36:11 +00:00
|
|
|
|
|
|
|
// loadResolvers loads resolvers from both user provided flag and file
|
|
|
|
func loadResolvers(options *types.Options) {
|
|
|
|
if options.ResolversFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(options.ResolversFile)
|
|
|
|
if err != nil {
|
|
|
|
gologger.Fatal().Msgf("Could not open resolvers file: %s\n", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
part := scanner.Text()
|
|
|
|
if part == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(part, ":") {
|
|
|
|
options.InternalResolversList = append(options.InternalResolversList, part)
|
|
|
|
} else {
|
|
|
|
options.InternalResolversList = append(options.InternalResolversList, part+":53")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 14:06:06 +00:00
|
|
|
|
|
|
|
func validateTemplatePaths(templatesDirectory string, templatePaths, workflowPaths []string) {
|
|
|
|
allGivenTemplatePaths := append(templatePaths, workflowPaths...)
|
|
|
|
|
|
|
|
for _, templatePath := range allGivenTemplatePaths {
|
|
|
|
if templatesDirectory != templatePath && filepath.IsAbs(templatePath) {
|
|
|
|
fileInfo, err := os.Stat(templatePath)
|
|
|
|
if err == nil && fileInfo.IsDir() {
|
|
|
|
relativizedPath, err2 := filepath.Rel(templatesDirectory, templatePath)
|
|
|
|
if err2 != nil || (len(relativizedPath) >= 2 && relativizedPath[:2] == "..") {
|
|
|
|
gologger.Warning().Msgf("The given path (%s) is outside the default template directory path (%s)! "+
|
|
|
|
"Referenced sub-templates with relative paths in workflows will be resolved against the default template directory.", templatePath, templatesDirectory)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|