2020-04-04 10:29:05 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
2020-04-26 01:18:10 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/pkg/executor"
|
2020-04-22 22:26:41 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/pkg/requests"
|
2020-04-04 10:29:05 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/pkg/templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Runner is a client for running the enumeration process.
|
|
|
|
type Runner struct {
|
2020-04-04 12:51:05 +00:00
|
|
|
// output is the output file to write if any
|
|
|
|
output *os.File
|
|
|
|
outputMutex *sync.Mutex
|
2020-04-26 01:18:10 +00:00
|
|
|
|
|
|
|
tempFile string
|
2020-04-04 12:51:05 +00:00
|
|
|
// options contains configuration options for runner
|
2020-04-04 10:29:05 +00:00
|
|
|
options *Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new client for running enumeration process.
|
|
|
|
func New(options *Options) (*Runner, error) {
|
|
|
|
runner := &Runner{
|
2020-04-04 12:51:05 +00:00
|
|
|
outputMutex: &sync.Mutex{},
|
|
|
|
options: options,
|
2020-04-04 10:29:05 +00:00
|
|
|
}
|
2020-04-04 11:42:29 +00:00
|
|
|
|
2020-04-26 01:18:10 +00:00
|
|
|
// If we have stdin, write it to a new file
|
|
|
|
if options.Stdin {
|
|
|
|
tempInput, err := ioutil.TempFile("", "stdin-input-*")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-26 01:30:28 +00:00
|
|
|
if _, err := io.Copy(tempInput, os.Stdin); err != nil {
|
2020-04-26 01:18:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
runner.tempFile = tempInput.Name()
|
|
|
|
tempInput.Close()
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:51:05 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
runner.output = output
|
|
|
|
}
|
2020-04-04 10:29:05 +00:00
|
|
|
return runner, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close releases all the resources and cleans up
|
2020-04-04 12:51:05 +00:00
|
|
|
func (r *Runner) Close() {
|
|
|
|
r.output.Close()
|
2020-04-26 01:18:10 +00:00
|
|
|
os.Remove(r.tempFile)
|
2020-04-04 12:51:05 +00:00
|
|
|
}
|
2020-04-04 10:29:05 +00:00
|
|
|
|
2020-04-04 19:48:57 +00:00
|
|
|
// RunEnumeration sets up the input layer for giving input nuclei.
|
2020-04-04 10:29:05 +00:00
|
|
|
// binary and runs the actual enumeration
|
|
|
|
func (r *Runner) RunEnumeration() {
|
2020-04-04 11:54:31 +00:00
|
|
|
if !strings.HasSuffix(r.options.Templates, ".yaml") {
|
|
|
|
gologger.Fatalf("Could not run recognize template extension: %s\n", r.options.Templates)
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:29:05 +00:00
|
|
|
// If the template path is a single template and not a glob, use that.
|
|
|
|
if !strings.Contains(r.options.Templates, "*") {
|
2020-04-22 22:26:41 +00:00
|
|
|
template, err := templates.ParseTemplate(r.options.Templates)
|
|
|
|
if err != nil {
|
|
|
|
gologger.Errorf("Could not parse template file '%s': %s\n", r.options.Templates, err)
|
|
|
|
return
|
|
|
|
}
|
2020-04-22 20:45:02 +00:00
|
|
|
|
|
|
|
// process http requests
|
|
|
|
for _, request := range template.RequestsHTTP {
|
|
|
|
r.processTemplateRequest(template, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
// process dns requests
|
|
|
|
for _, request := range template.RequestsDNS {
|
2020-04-22 22:26:41 +00:00
|
|
|
r.processTemplateRequest(template, request)
|
|
|
|
}
|
2020-04-04 12:06:20 +00:00
|
|
|
return
|
2020-04-04 10:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the glob, evaluate it and run all the template file checks
|
|
|
|
matches, err := filepath.Glob(r.options.Templates)
|
|
|
|
if err != nil {
|
|
|
|
gologger.Fatalf("Could not evaluate template path '%s': %s\n", r.options.Templates, err)
|
|
|
|
}
|
2020-04-04 13:36:30 +00:00
|
|
|
|
2020-04-04 10:29:05 +00:00
|
|
|
for _, match := range matches {
|
2020-04-22 22:26:41 +00:00
|
|
|
template, err := templates.ParseTemplate(match)
|
|
|
|
if err != nil {
|
|
|
|
gologger.Errorf("Could not parse template file '%s': %s\n", match, err)
|
|
|
|
return
|
|
|
|
}
|
2020-04-26 01:18:10 +00:00
|
|
|
for _, request := range template.RequestsDNS {
|
2020-04-22 20:45:02 +00:00
|
|
|
r.processTemplateRequest(template, request)
|
|
|
|
}
|
2020-04-26 01:18:10 +00:00
|
|
|
for _, request := range template.RequestsHTTP {
|
2020-04-22 22:26:41 +00:00
|
|
|
r.processTemplateRequest(template, request)
|
|
|
|
}
|
2020-04-04 10:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processTemplate processes a template and runs the enumeration on all the targets
|
2020-04-22 20:45:02 +00:00
|
|
|
func (r *Runner) processTemplateRequest(template *templates.Template, request interface{}) {
|
2020-04-26 01:18:10 +00:00
|
|
|
var file *os.File
|
|
|
|
var err error
|
|
|
|
|
2020-04-04 10:29:05 +00:00
|
|
|
// Handle a list of hosts as argument
|
|
|
|
if r.options.Targets != "" {
|
2020-04-26 01:18:10 +00:00
|
|
|
file, err = os.Open(r.options.Targets)
|
|
|
|
} else if r.options.Stdin {
|
|
|
|
file, err = os.Open(r.tempFile)
|
2020-04-04 10:29:05 +00:00
|
|
|
}
|
2020-04-26 01:18:10 +00:00
|
|
|
if err != nil {
|
|
|
|
gologger.Fatalf("Could not open targets file '%s': %s\n", r.options.Targets, err)
|
2020-04-04 10:29:05 +00:00
|
|
|
}
|
2020-04-26 01:18:10 +00:00
|
|
|
r.processTemplateWithList(template, request, file)
|
|
|
|
file.Close()
|
2020-04-04 10:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// processDomain processes the list with a template
|
2020-04-22 20:45:02 +00:00
|
|
|
func (r *Runner) processTemplateWithList(template *templates.Template, request interface{}, reader io.Reader) {
|
2020-04-04 10:29:05 +00:00
|
|
|
// Display the message for the template
|
|
|
|
message := fmt.Sprintf("[%s] Loaded template %s (@%s)", template.ID, template.Info.Name, template.Info.Author)
|
|
|
|
if template.Info.Severity != "" {
|
|
|
|
message += " [" + template.Info.Severity + "]"
|
|
|
|
}
|
|
|
|
gologger.Infof("%s\n", message)
|
|
|
|
|
2020-04-04 12:51:05 +00:00
|
|
|
var writer *bufio.Writer
|
|
|
|
if r.output != nil {
|
|
|
|
writer = bufio.NewWriter(r.output)
|
|
|
|
defer writer.Flush()
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:18:10 +00:00
|
|
|
var httpExecutor *executor.HTTPExecutor
|
|
|
|
var dnsExecutor *executor.DNSExecutor
|
2020-04-27 18:19:53 +00:00
|
|
|
var err error
|
2020-04-26 01:18:10 +00:00
|
|
|
|
|
|
|
// Create an executor based on the request type.
|
|
|
|
switch value := request.(type) {
|
|
|
|
case *requests.DNSRequest:
|
|
|
|
dnsExecutor = executor.NewDNSExecutor(&executor.DNSOptions{
|
|
|
|
Template: template,
|
|
|
|
DNSRequest: value,
|
|
|
|
Writer: writer,
|
|
|
|
})
|
|
|
|
case *requests.HTTPRequest:
|
2020-04-27 18:19:53 +00:00
|
|
|
httpExecutor, err = executor.NewHTTPExecutor(&executor.HTTPOptions{
|
2020-04-28 11:24:12 +00:00
|
|
|
Template: template,
|
|
|
|
HTTPRequest: value,
|
|
|
|
Writer: writer,
|
|
|
|
Timeout: r.options.Timeout,
|
|
|
|
Retries: r.options.Retries,
|
|
|
|
ProxyURL: r.options.ProxyURL,
|
|
|
|
ProxySocksURL: r.options.ProxySocksURL,
|
2020-04-26 01:18:10 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-27 18:19:53 +00:00
|
|
|
if err != nil {
|
|
|
|
gologger.Warningf("Could not create http client: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
limiter := make(chan struct{}, r.options.Threads)
|
|
|
|
wg := &sync.WaitGroup{}
|
2020-04-22 22:26:41 +00:00
|
|
|
|
2020-04-04 10:29:05 +00:00
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
for scanner.Scan() {
|
|
|
|
text := scanner.Text()
|
|
|
|
if text == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
limiter <- struct{}{}
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func(URL string) {
|
2020-04-26 01:18:10 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if httpExecutor != nil {
|
|
|
|
err = httpExecutor.ExecuteHTTP(text)
|
|
|
|
}
|
|
|
|
if dnsExecutor != nil {
|
|
|
|
err = dnsExecutor.ExecuteDNS(text)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
gologger.Warningf("Could not execute step: %s\n", err)
|
|
|
|
}
|
2020-04-04 10:29:05 +00:00
|
|
|
<-limiter
|
|
|
|
wg.Done()
|
|
|
|
}(text)
|
|
|
|
}
|
|
|
|
close(limiter)
|
|
|
|
wg.Wait()
|
|
|
|
}
|