nuclei/v2/internal/runner/runner.go

316 lines
9.2 KiB
Go
Raw Normal View History

package runner
import (
"bufio"
"fmt"
"os"
"strings"
2020-08-23 18:46:18 +00:00
"github.com/logrusorgru/aurora"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/hmap/store/hybrid"
"github.com/projectdiscovery/nuclei/v2/internal/collaborator"
"github.com/projectdiscovery/nuclei/v2/internal/colorizer"
2020-07-23 18:19:19 +00:00
"github.com/projectdiscovery/nuclei/v2/internal/progress"
"github.com/projectdiscovery/nuclei/v2/pkg/catalogue"
"github.com/projectdiscovery/nuclei/v2/pkg/output"
2020-10-18 01:09:24 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/projectfile"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/clusterer"
"github.com/projectdiscovery/nuclei/v2/pkg/reporting/issues"
2020-07-01 10:47:24 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
2020-10-18 01:09:24 +00:00
"github.com/remeh/sizedwaitgroup"
"github.com/rs/xid"
"go.uber.org/atomic"
"go.uber.org/ratelimit"
)
// Runner is a client for running the enumeration process.
type Runner struct {
hostMap *hybrid.HybridMap
output output.Writer
inputCount int64
2020-06-24 22:23:37 +00:00
templatesConfig *nucleiConfig
options *types.Options
projectFile *projectfile.ProjectFile
catalogue *catalogue.Catalogue
progress *progress.Progress
colorizer aurora.Aurora
issuesClient *issues.Client
severityColors *colorizer.Colorizer
ratelimiter ratelimit.Limiter
}
// New creates a new client for running enumeration process.
func New(options *types.Options) (*Runner, error) {
runner := &Runner{
options: options,
}
2020-06-24 22:23:37 +00:00
if err := runner.updateTemplates(); err != nil {
gologger.Warning().Msgf("Could not update templates: %s\n", err)
2020-06-24 22:23:37 +00:00
}
2021-01-14 07:54:50 +00:00
// Read nucleiignore file if given a templateconfig
if runner.templatesConfig != nil {
runner.readNucleiIgnoreFile()
}
runner.catalogue = catalogue.New(runner.options.TemplatesDirectory)
if options.ReportingConfig != "" {
if client, err := issues.New(options.ReportingConfig, options.ReportingDirectory); err != nil {
gologger.Fatal().Msgf("Could not create issue reporting client: %s\n", err)
} else {
runner.issuesClient = client
}
}
// output coloring
useColor := !options.NoColor
runner.colorizer = aurora.NewAurora(useColor)
runner.severityColors = colorizer.New(runner.colorizer)
if options.TemplateList {
runner.listAvailableTemplates()
os.Exit(0)
}
if (len(options.Templates) == 0 || (options.Targets == "" && !options.Stdin && options.Target == "")) && options.UpdateTemplates {
2020-06-24 22:23:37 +00:00
os.Exit(0)
}
if hm, err := hybrid.New(hybrid.DefaultDiskOptions); err != nil {
gologger.Fatal().Msgf("Could not create temporary input file: %s\n", err)
} else {
runner.hostMap = hm
}
runner.inputCount = 0
dupeCount := 0
// Handle single target
if options.Target != "" {
runner.inputCount++
2020-11-20 10:12:06 +00:00
// nolint:errcheck // ignoring error
runner.hostMap.Set(options.Target, nil)
}
// Handle stdin
if options.Stdin {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
url := strings.TrimSpace(scanner.Text())
if url == "" {
continue
}
if _, ok := runner.hostMap.Get(url); ok {
dupeCount++
continue
}
runner.inputCount++
2020-11-20 10:12:06 +00:00
// nolint:errcheck // ignoring error
runner.hostMap.Set(url, nil)
}
2020-07-23 18:19:19 +00:00
}
// Handle taget file
if options.Targets != "" {
input, err := os.Open(options.Targets)
if err != nil {
gologger.Fatal().Msgf("Could not open targets file '%s': %s\n", options.Targets, err)
}
scanner := bufio.NewScanner(input)
for scanner.Scan() {
url := strings.TrimSpace(scanner.Text())
if url == "" {
continue
}
if _, ok := runner.hostMap.Get(url); ok {
dupeCount++
continue
}
runner.inputCount++
2020-11-20 10:12:06 +00:00
// nolint:errcheck // ignoring error
runner.hostMap.Set(url, nil)
}
input.Close()
}
if dupeCount > 0 {
gologger.Info().Msgf("Supplied input was automatically deduplicated (%d removed).", dupeCount)
2020-07-23 18:19:19 +00:00
}
2020-04-04 12:51:05 +00:00
// Create the output file if asked
2020-12-29 12:45:27 +00:00
output, err := output.NewStandardWriter(!options.NoColor, options.NoMeta, options.JSON, options.Output, options.TraceLogFile)
if err != nil {
gologger.Fatal().Msgf("Could not create output file '%s': %s\n", options.Output, err)
2020-04-04 12:51:05 +00:00
}
2020-12-29 12:45:27 +00:00
runner.output = output
2020-07-23 18:19:19 +00:00
// Creates the progress tracking object
var progressErr error
runner.progress, progressErr = progress.NewProgress(options.EnableProgressBar, options.Metrics, options.MetricsPort)
if progressErr != nil {
return nil, progressErr
}
2020-07-23 18:19:19 +00:00
2020-10-17 00:10:47 +00:00
// create project file if requested or load existing one
if options.Project {
2020-10-30 12:06:05 +00:00
var projectFileErr error
runner.projectFile, projectFileErr = projectfile.New(&projectfile.Options{Path: options.ProjectPath, Cleanup: options.ProjectPath == ""})
2020-10-30 12:06:05 +00:00
if projectFileErr != nil {
return nil, projectFileErr
2020-10-15 21:39:00 +00:00
}
}
2020-10-23 08:13:34 +00:00
// Enable Polling
if options.BurpCollaboratorBiid != "" {
collaborator.DefaultCollaborator.Collab.AddBIID(options.BurpCollaboratorBiid)
}
if options.RateLimit > 0 {
runner.ratelimiter = ratelimit.New(options.RateLimit)
} else {
runner.ratelimiter = ratelimit.NewUnlimited()
}
return runner, nil
}
// Close releases all the resources and cleans up
2020-04-04 12:51:05 +00:00
func (r *Runner) Close() {
2020-09-11 16:05:29 +00:00
if r.output != nil {
r.output.Close()
}
r.hostMap.Close()
if r.projectFile != nil {
r.projectFile.Close()
2020-10-15 21:39:00 +00:00
}
2020-04-04 12:51:05 +00:00
}
// RunEnumeration sets up the input layer for giving input nuclei.
// binary and runs the actual enumeration
func (r *Runner) RunEnumeration() {
2020-08-02 13:48:10 +00:00
// resolves input templates definitions and any optional exclusion
includedTemplates := r.catalogue.GetTemplatesPath(r.options.Templates)
excludedTemplates := r.catalogue.GetTemplatesPath(r.options.ExcludedTemplates)
2020-08-02 13:48:10 +00:00
// defaults to all templates
allTemplates := includedTemplates
2020-08-02 13:48:10 +00:00
if len(excludedTemplates) > 0 {
excludedMap := make(map[string]struct{}, len(excludedTemplates))
for _, excl := range excludedTemplates {
excludedMap[excl] = struct{}{}
}
// rebuild list with only non-excluded templates
allTemplates = []string{}
2020-08-02 13:48:10 +00:00
for _, incl := range includedTemplates {
if _, found := excludedMap[incl]; !found {
allTemplates = append(allTemplates, incl)
} else {
gologger.Warning().Msgf("Excluding '%s'", incl)
2020-08-02 13:48:10 +00:00
}
}
}
executerOpts := &protocols.ExecuterOptions{
Output: r.output,
Options: r.options,
Progress: r.progress,
Catalogue: r.catalogue,
IssuesClient: r.issuesClient,
RateLimiter: r.ratelimiter,
ProjectFile: r.projectFile,
}
2020-08-02 16:33:55 +00:00
// pre-parse all the templates, apply filters
finalTemplates := []*templates.Template{}
2020-08-02 16:33:55 +00:00
availableTemplates, workflowCount := r.getParsedTemplatesFor(allTemplates, r.options.Severity)
2021-01-15 08:47:34 +00:00
var unclusteredRequests int64 = 0
for _, template := range availableTemplates {
// workflows will dynamically adjust the totals while running, as
// it can't be know in advance which requests will be called
if len(template.Workflows) > 0 {
continue
}
unclusteredRequests += int64(template.TotalRequests) * r.inputCount
}
originalTemplatesCount := len(availableTemplates)
2021-01-16 06:56:38 +00:00
clusterCount := 0
clusters := clusterer.Cluster(availableTemplates)
for _, cluster := range clusters {
if len(cluster) > 1 {
clusterID := fmt.Sprintf("cluster-%s", xid.New().String())
finalTemplates = append(finalTemplates, &templates.Template{
ID: clusterID,
RequestsHTTP: cluster[0].RequestsHTTP,
Executer: clusterer.NewExecuter(cluster, executerOpts),
TotalRequests: len(cluster[0].RequestsHTTP),
})
2021-01-16 06:56:38 +00:00
clusterCount++
} else {
finalTemplates = append(finalTemplates, cluster[0])
}
}
2021-01-15 08:47:34 +00:00
var totalRequests int64 = 0
for _, t := range finalTemplates {
if len(t.Workflows) > 0 {
continue
}
totalRequests += int64(t.TotalRequests) * r.inputCount
}
if totalRequests < unclusteredRequests {
2021-01-16 06:56:38 +00:00
gologger.Info().Msgf("Reduced %d requests to %d (%d templates clustered)", unclusteredRequests, totalRequests, clusterCount)
2021-01-15 08:47:34 +00:00
}
templateCount := originalTemplatesCount
2020-08-02 16:33:55 +00:00
hasWorkflows := workflowCount > 0
// 0 matches means no templates were found in directory
2020-08-02 16:33:55 +00:00
if templateCount == 0 {
gologger.Fatal().Msgf("Error, no templates were found.\n")
}
gologger.Info().Msgf("Using %s rules (%s templates, %s workflows)",
r.colorizer.Bold(templateCount).String(),
r.colorizer.Bold(templateCount-workflowCount).String(),
r.colorizer.Bold(workflowCount).String())
2020-07-23 18:19:19 +00:00
results := &atomic.Bool{}
2020-10-17 00:10:47 +00:00
wgtemplates := sizedwaitgroup.New(r.options.TemplateThreads)
2020-10-23 08:13:34 +00:00
// Starts polling or ignore
collaborator.DefaultCollaborator.Poll()
2020-07-24 16:12:16 +00:00
if r.inputCount == 0 {
gologger.Error().Msgf("Could not find any valid input URLs.")
} else if totalRequests > 0 || hasWorkflows {
// tracks global progress and captures stdout/stderr until p.Wait finishes
2021-01-16 06:56:38 +00:00
r.progress.Init(r.inputCount, templateCount, totalRequests)
for _, t := range finalTemplates {
2020-10-17 00:10:47 +00:00
wgtemplates.Add()
2020-12-29 11:03:25 +00:00
go func(template *templates.Template) {
2020-12-29 13:18:13 +00:00
defer wgtemplates.Done()
if len(template.Workflows) > 0 {
results.CAS(false, r.processWorkflowWithList(template))
} else {
results.CAS(false, r.processTemplateWithList(template))
2020-06-26 12:37:55 +00:00
}
2020-08-02 16:33:55 +00:00
}(t)
}
wgtemplates.Wait()
2021-01-16 06:56:38 +00:00
r.progress.Stop()
}
2020-07-23 18:19:19 +00:00
if r.issuesClient != nil {
r.issuesClient.Close()
}
if !results.Load() {
if r.output != nil {
r.output.Close()
2020-09-10 11:02:01 +00:00
os.Remove(r.options.Output)
}
2021-01-11 14:49:16 +00:00
gologger.Info().Msgf("No results found. Better luck next time!")
}
}