2020-04-04 15:59:05 +05:30
|
|
|
package runner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
2020-06-26 10:23:54 +02:00
|
|
|
"context"
|
2020-04-04 15:59:05 +05:30
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
2020-07-29 00:43:05 +02:00
|
|
|
"regexp"
|
2020-04-04 15:59:05 +05:30
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
|
2020-08-24 00:16:18 +05:30
|
|
|
"github.com/logrusorgru/aurora"
|
2020-04-04 15:59:05 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
2020-07-23 20:19:19 +02:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/internal/progress"
|
2020-07-24 18:12:16 +02:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/atomicboolean"
|
2020-07-01 16:17:24 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/workflows"
|
2020-04-04 15:59:05 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Runner is a client for running the enumeration process.
|
|
|
|
|
type Runner struct {
|
2020-07-25 23:02:24 +02:00
|
|
|
input string
|
2020-07-23 20:19:19 +02:00
|
|
|
inputCount int64
|
|
|
|
|
|
2020-04-04 18:21:05 +05:30
|
|
|
// output is the output file to write if any
|
|
|
|
|
output *os.File
|
|
|
|
|
outputMutex *sync.Mutex
|
2020-04-26 06:48:10 +05:30
|
|
|
|
2020-06-25 03:53:37 +05:30
|
|
|
tempFile string
|
|
|
|
|
templatesConfig *nucleiConfig
|
2020-04-04 18:21:05 +05:30
|
|
|
// options contains configuration options for runner
|
2020-04-04 15:59:05 +05:30
|
|
|
options *Options
|
2020-07-24 18:12:16 +02:00
|
|
|
limiter chan struct{}
|
2020-07-23 20:19:19 +02:00
|
|
|
|
|
|
|
|
// progress tracking
|
2020-07-31 23:07:33 +02:00
|
|
|
progress progress.IProgress
|
2020-07-29 00:43:05 +02:00
|
|
|
|
|
|
|
|
// output coloring
|
|
|
|
|
colorizer aurora.Aurora
|
|
|
|
|
decolorizer *regexp.Regexp
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates a new client for running enumeration process.
|
|
|
|
|
func New(options *Options) (*Runner, error) {
|
|
|
|
|
runner := &Runner{
|
2020-04-04 18:21:05 +05:30
|
|
|
outputMutex: &sync.Mutex{},
|
|
|
|
|
options: options,
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|
2020-04-04 17:12:29 +05:30
|
|
|
|
2020-06-25 03:53:37 +05:30
|
|
|
if err := runner.updateTemplates(); err != nil {
|
2020-07-06 12:30:02 +05:30
|
|
|
gologger.Warningf("Could not update templates: %s\n", err)
|
2020-06-25 03:53:37 +05:30
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-08-29 23:02:45 +02:00
|
|
|
if options.TemplateList {
|
|
|
|
|
runner.listAvailableTemplates()
|
2020-08-28 11:17:37 +02:00
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-14 00:04:19 +02:00
|
|
|
if (len(options.Templates) == 0 || (options.Targets == "" && !options.Stdin && options.Target == "")) && options.UpdateTemplates {
|
2020-06-25 03:53:37 +05:30
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
2020-08-24 00:16:18 +05:30
|
|
|
// Read nucleiignore file if given a templateconfig
|
|
|
|
|
if runner.templatesConfig != nil {
|
|
|
|
|
runner.readNucleiIgnoreFile()
|
|
|
|
|
}
|
2020-06-25 03:53:37 +05:30
|
|
|
|
2020-07-29 00:43:05 +02:00
|
|
|
// output coloring
|
|
|
|
|
useColor := !options.NoColor
|
|
|
|
|
runner.colorizer = aurora.NewAurora(useColor)
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-29 00:43:05 +02:00
|
|
|
if useColor {
|
|
|
|
|
// compile a decolorization regex to cleanup file output messages
|
2020-08-25 23:24:31 +02:00
|
|
|
runner.decolorizer = regexp.MustCompile(`\x1B\[[0-9;]*[a-zA-Z]`)
|
2020-07-29 00:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 06:48:10 +05:30
|
|
|
// If we have stdin, write it to a new file
|
|
|
|
|
if options.Stdin {
|
|
|
|
|
tempInput, err := ioutil.TempFile("", "stdin-input-*")
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-26 06:48:10 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-26 07:00:28 +05:30
|
|
|
if _, err := io.Copy(tempInput, os.Stdin); err != nil {
|
2020-04-26 06:48:10 +05:30
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-26 06:48:10 +05:30
|
|
|
runner.tempFile = tempInput.Name()
|
|
|
|
|
tempInput.Close()
|
|
|
|
|
}
|
2020-06-25 21:40:20 +05:30
|
|
|
// If we have single target, write it to a new file
|
|
|
|
|
if options.Target != "" {
|
|
|
|
|
tempInput, err := ioutil.TempFile("", "stdin-input-*")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-26 21:17:42 +02:00
|
|
|
fmt.Fprintf(tempInput, "%s\n", options.Target)
|
2020-06-25 21:40:20 +05:30
|
|
|
runner.tempFile = tempInput.Name()
|
|
|
|
|
tempInput.Close()
|
|
|
|
|
}
|
2020-04-26 06:48:10 +05:30
|
|
|
|
2020-07-23 20:19:19 +02:00
|
|
|
// Setup input, handle a list of hosts as argument
|
|
|
|
|
var err error
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-25 23:02:24 +02:00
|
|
|
var input *os.File
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-23 20:19:19 +02:00
|
|
|
if options.Targets != "" {
|
2020-07-25 23:02:24 +02:00
|
|
|
input, err = os.Open(options.Targets)
|
2020-07-23 20:19:19 +02:00
|
|
|
} else if options.Stdin || options.Target != "" {
|
2020-07-25 23:02:24 +02:00
|
|
|
input, err = os.Open(runner.tempFile)
|
2020-07-23 20:19:19 +02:00
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-23 20:19:19 +02:00
|
|
|
if err != nil {
|
|
|
|
|
gologger.Fatalf("Could not open targets file '%s': %s\n", options.Targets, err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-26 16:43:53 +02:00
|
|
|
// Sanitize input and pre-compute total number of targets
|
2020-07-25 23:02:24 +02:00
|
|
|
var usedInput = make(map[string]bool)
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-25 23:02:24 +02:00
|
|
|
dupeCount := 0
|
|
|
|
|
sb := strings.Builder{}
|
|
|
|
|
scanner := bufio.NewScanner(input)
|
2020-07-23 20:19:19 +02:00
|
|
|
runner.inputCount = 0
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-23 20:19:19 +02:00
|
|
|
for scanner.Scan() {
|
2020-07-25 23:02:24 +02:00
|
|
|
url := scanner.Text()
|
2020-07-26 16:43:53 +02:00
|
|
|
// skip empty lines
|
2020-08-25 23:24:31 +02:00
|
|
|
if url == "" {
|
2020-07-26 16:43:53 +02:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// deduplication
|
2020-07-25 23:02:24 +02:00
|
|
|
if _, ok := usedInput[url]; !ok {
|
|
|
|
|
usedInput[url] = true
|
|
|
|
|
runner.inputCount++
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-25 23:02:24 +02:00
|
|
|
sb.WriteString(url)
|
|
|
|
|
sb.WriteString("\n")
|
|
|
|
|
} else {
|
|
|
|
|
dupeCount++
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-26 15:35:26 +02:00
|
|
|
input.Close()
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-25 23:02:24 +02:00
|
|
|
runner.input = sb.String()
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-25 23:02:24 +02:00
|
|
|
if dupeCount > 0 {
|
|
|
|
|
gologger.Labelf("Supplied input was automatically deduplicated (%d removed).", dupeCount)
|
2020-07-23 20:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-04 18:21:05 +05:30
|
|
|
// Create the output file if asked
|
|
|
|
|
if options.Output != "" {
|
|
|
|
|
output, err := os.Create(options.Output)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Fatalf("Could not create output file '%s': %s\n", options.Output, err)
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-04 18:21:05 +05:30
|
|
|
runner.output = output
|
|
|
|
|
}
|
2020-07-23 20:19:19 +02:00
|
|
|
|
2020-07-31 23:07:33 +02:00
|
|
|
// Creates the progress tracking object
|
2020-07-31 23:37:45 +02:00
|
|
|
runner.progress = progress.NewProgress(runner.options.NoColor, !options.Silent && options.EnableProgressBar)
|
2020-07-23 20:19:19 +02:00
|
|
|
|
2020-07-24 18:12:16 +02:00
|
|
|
runner.limiter = make(chan struct{}, options.Threads)
|
|
|
|
|
|
2020-04-04 15:59:05 +05:30
|
|
|
return runner, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close releases all the resources and cleans up
|
2020-04-04 18:21:05 +05:30
|
|
|
func (r *Runner) Close() {
|
|
|
|
|
r.output.Close()
|
2020-04-26 06:48:10 +05:30
|
|
|
os.Remove(r.tempFile)
|
2020-04-04 18:21:05 +05:30
|
|
|
}
|
2020-04-04 15:59:05 +05:30
|
|
|
|
2020-08-02 13:49:16 +02:00
|
|
|
// RunEnumeration sets up the input layer for giving input nuclei.
|
|
|
|
|
// binary and runs the actual enumeration
|
|
|
|
|
func (r *Runner) RunEnumeration() {
|
2020-08-02 15:48:10 +02:00
|
|
|
// resolves input templates definitions and any optional exclusion
|
|
|
|
|
includedTemplates := r.getTemplatesFor(r.options.Templates)
|
|
|
|
|
excludedTemplates := r.getTemplatesFor(r.options.ExcludedTemplates)
|
|
|
|
|
// defaults to all templates
|
|
|
|
|
allTemplates := includedTemplates
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-08-02 15:48:10 +02: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-25 23:24:31 +02:00
|
|
|
|
2020-08-02 15:48:10 +02:00
|
|
|
for _, incl := range includedTemplates {
|
|
|
|
|
if _, found := excludedMap[incl]; !found {
|
|
|
|
|
allTemplates = append(allTemplates, incl)
|
|
|
|
|
} else {
|
|
|
|
|
gologger.Warningf("Excluding '%s'", incl)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-02 13:49:16 +02:00
|
|
|
|
2020-08-02 18:33:55 +02:00
|
|
|
// pre-parse all the templates, apply filters
|
|
|
|
|
availableTemplates, workflowCount := r.getParsedTemplatesFor(allTemplates, r.options.Severity)
|
|
|
|
|
templateCount := len(availableTemplates)
|
|
|
|
|
hasWorkflows := workflowCount > 0
|
|
|
|
|
|
2020-05-02 12:10:52 -05:00
|
|
|
// 0 matches means no templates were found in directory
|
2020-08-02 18:33:55 +02:00
|
|
|
if templateCount == 0 {
|
2020-07-14 00:04:19 +02:00
|
|
|
gologger.Fatalf("Error, no templates were found.\n")
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|
2020-06-27 20:50:43 +05:30
|
|
|
|
2020-08-02 18:33:55 +02:00
|
|
|
gologger.Infof("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 20:19:19 +02:00
|
|
|
|
2020-07-25 23:22:09 +02:00
|
|
|
// precompute total request count
|
2020-07-25 14:56:20 +02:00
|
|
|
var totalRequests int64 = 0
|
|
|
|
|
|
2020-08-02 18:33:55 +02:00
|
|
|
for _, t := range availableTemplates {
|
2020-08-25 23:24:31 +02:00
|
|
|
switch av := t.(type) {
|
2020-07-24 18:38:27 +02:00
|
|
|
case *templates.Template:
|
2020-08-25 23:24:31 +02:00
|
|
|
totalRequests += (av.GetHTTPRequestCount() + av.GetDNSRequestCount()) * r.inputCount
|
2020-07-27 00:00:06 +02:00
|
|
|
case *workflows.Workflow:
|
|
|
|
|
// workflows will dynamically adjust the totals while running, as
|
|
|
|
|
// it can't be know in advance which requests will be called
|
2020-08-26 20:05:31 +02:00
|
|
|
} // nolint:wsl // comment
|
2020-07-25 14:56:20 +02:00
|
|
|
}
|
2020-07-23 20:19:19 +02:00
|
|
|
|
2020-07-24 18:12:16 +02:00
|
|
|
var (
|
|
|
|
|
wgtemplates sync.WaitGroup
|
|
|
|
|
results atomicboolean.AtomBool
|
|
|
|
|
)
|
|
|
|
|
|
2020-07-27 18:47:23 +02:00
|
|
|
if r.inputCount == 0 {
|
|
|
|
|
gologger.Errorf("Could not find any valid input URLs.")
|
2020-07-27 20:38:48 +02:00
|
|
|
} else if totalRequests > 0 || hasWorkflows {
|
2020-08-25 23:24:31 +02:00
|
|
|
ctx := context.Background()
|
2020-08-01 21:44:14 +02:00
|
|
|
// tracks global progress and captures stdout/stderr until p.Wait finishes
|
2020-08-02 18:33:55 +02:00
|
|
|
p := r.progress
|
2020-07-31 23:07:33 +02:00
|
|
|
p.InitProgressbar(r.inputCount, templateCount, totalRequests)
|
2020-07-27 18:47:23 +02:00
|
|
|
|
2020-08-02 18:33:55 +02:00
|
|
|
for _, t := range availableTemplates {
|
2020-07-27 18:47:23 +02:00
|
|
|
wgtemplates.Add(1)
|
2020-08-02 18:33:55 +02:00
|
|
|
go func(template interface{}) {
|
2020-07-27 18:47:23 +02:00
|
|
|
defer wgtemplates.Done()
|
2020-08-25 23:24:31 +02:00
|
|
|
switch tt := template.(type) {
|
2020-07-27 18:47:23 +02:00
|
|
|
case *templates.Template:
|
2020-08-25 23:24:31 +02:00
|
|
|
for _, request := range tt.RequestsDNS {
|
|
|
|
|
results.Or(r.processTemplateWithList(ctx, p, tt, request))
|
2020-07-27 18:47:23 +02:00
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
for _, request := range tt.BulkRequestsHTTP {
|
|
|
|
|
results.Or(r.processTemplateWithList(ctx, p, tt, request))
|
2020-07-27 18:47:23 +02:00
|
|
|
}
|
|
|
|
|
case *workflows.Workflow:
|
2020-08-02 18:33:55 +02:00
|
|
|
workflow := template.(*workflows.Workflow)
|
2020-08-29 15:26:11 +02:00
|
|
|
r.processWorkflowWithList(p, workflow)
|
2020-06-26 14:37:55 +02:00
|
|
|
}
|
2020-08-02 18:33:55 +02:00
|
|
|
}(t)
|
2020-07-27 18:47:23 +02:00
|
|
|
}
|
2020-07-24 18:12:16 +02:00
|
|
|
|
2020-07-27 18:47:23 +02:00
|
|
|
wgtemplates.Wait()
|
2020-07-31 23:07:33 +02:00
|
|
|
p.Wait()
|
2020-07-27 18:47:23 +02:00
|
|
|
}
|
2020-07-23 20:19:19 +02:00
|
|
|
|
2020-07-24 18:12:16 +02:00
|
|
|
if !results.Get() {
|
2020-06-22 19:57:32 +05:30
|
|
|
if r.output != nil {
|
|
|
|
|
outputFile := r.output.Name()
|
|
|
|
|
r.output.Close()
|
|
|
|
|
os.Remove(outputFile)
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-18 20:32:00 +02:00
|
|
|
gologger.Infof("No results found. Happy hacking!")
|
2020-06-22 19:57:32 +05:30
|
|
|
}
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|