2020-04-04 15:59:05 +05:30
|
|
|
package runner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
2020-04-04 17:12:29 +05:30
|
|
|
"crypto/tls"
|
2020-04-04 15:59:05 +05:30
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
2020-04-04 17:12:29 +05:30
|
|
|
"time"
|
2020-04-04 15:59:05 +05:30
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
2020-04-06 00:44:45 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/pkg/extractors"
|
2020-04-04 15:59:05 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/pkg/matchers"
|
2020-04-23 03:56:41 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/pkg/requests"
|
2020-04-04 15:59:05 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/pkg/templates"
|
2020-04-22 22:45:02 +02:00
|
|
|
retryabledns "github.com/projectdiscovery/retryabledns"
|
2020-04-04 17:12:29 +05:30
|
|
|
retryablehttp "github.com/projectdiscovery/retryablehttp-go"
|
2020-04-04 15:59:05 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Runner is a client for running the enumeration process.
|
|
|
|
|
type Runner struct {
|
2020-04-04 18:21:05 +05:30
|
|
|
// output is the output file to write if any
|
|
|
|
|
output *os.File
|
|
|
|
|
outputMutex *sync.Mutex
|
|
|
|
|
// options contains configuration options for runner
|
2020-04-04 15:59:05 +05:30
|
|
|
options *Options
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-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)
|
|
|
|
|
}
|
|
|
|
|
runner.output = output
|
|
|
|
|
}
|
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-04 15:59:05 +05:30
|
|
|
|
2020-04-05 01:18:57 +05:30
|
|
|
// RunEnumeration sets up the input layer for giving input nuclei.
|
2020-04-04 15:59:05 +05:30
|
|
|
// binary and runs the actual enumeration
|
|
|
|
|
func (r *Runner) RunEnumeration() {
|
2020-04-04 17:24:31 +05:30
|
|
|
if !strings.HasSuffix(r.options.Templates, ".yaml") {
|
|
|
|
|
gologger.Fatalf("Could not run recognize template extension: %s\n", r.options.Templates)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-04 15:59:05 +05:30
|
|
|
// If the template path is a single template and not a glob, use that.
|
|
|
|
|
if !strings.Contains(r.options.Templates, "*") {
|
2020-04-23 03:56:41 +05:30
|
|
|
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 22:45:02 +02:00
|
|
|
|
|
|
|
|
// process http requests
|
|
|
|
|
for _, request := range template.RequestsHTTP {
|
|
|
|
|
r.processTemplateRequest(template, request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// process dns requests
|
|
|
|
|
for _, request := range template.RequestsDNS {
|
2020-04-23 03:56:41 +05:30
|
|
|
r.processTemplateRequest(template, request)
|
|
|
|
|
}
|
2020-04-04 17:36:20 +05:30
|
|
|
return
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 19:06:30 +05:30
|
|
|
|
2020-04-04 15:59:05 +05:30
|
|
|
for _, match := range matches {
|
2020-04-23 03:56:41 +05:30
|
|
|
template, err := templates.ParseTemplate(match)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Errorf("Could not parse template file '%s': %s\n", match, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-04-22 22:45:02 +02:00
|
|
|
for _, request := range template.RequestsHTTP {
|
|
|
|
|
r.processTemplateRequest(template, request)
|
|
|
|
|
}
|
|
|
|
|
for _, request := range template.RequestsDNS {
|
2020-04-23 03:56:41 +05:30
|
|
|
r.processTemplateRequest(template, request)
|
|
|
|
|
}
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// processTemplate processes a template and runs the enumeration on all the targets
|
2020-04-22 22:45:02 +02:00
|
|
|
func (r *Runner) processTemplateRequest(template *templates.Template, request interface{}) {
|
2020-04-04 15:59:05 +05:30
|
|
|
// Handle a list of hosts as argument
|
|
|
|
|
if r.options.Targets != "" {
|
|
|
|
|
file, err := os.Open(r.options.Targets)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Fatalf("Could not open targets file '%s': %s\n", r.options.Targets, err)
|
|
|
|
|
}
|
2020-04-23 03:56:41 +05:30
|
|
|
r.processTemplateWithList(template, request, file)
|
2020-04-04 15:59:05 +05:30
|
|
|
file.Close()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle stdin input
|
|
|
|
|
if r.options.Stdin {
|
2020-04-23 03:56:41 +05:30
|
|
|
r.processTemplateWithList(template, request, os.Stdin)
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// processDomain processes the list with a template
|
2020-04-22 22:45:02 +02:00
|
|
|
func (r *Runner) processTemplateWithList(template *templates.Template, request interface{}, reader io.Reader) {
|
2020-04-04 15:59:05 +05:30
|
|
|
// 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)
|
|
|
|
|
|
|
|
|
|
limiter := make(chan struct{}, r.options.Threads)
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
|
2020-04-04 18:21:05 +05:30
|
|
|
var writer *bufio.Writer
|
|
|
|
|
if r.output != nil {
|
|
|
|
|
writer = bufio.NewWriter(r.output)
|
|
|
|
|
defer writer.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 22:45:02 +02:00
|
|
|
httpclient := r.makeHTTPClientByRequest(request)
|
|
|
|
|
dnsclient := r.makeDNSClientByRequest(request)
|
2020-04-23 03:56:41 +05:30
|
|
|
|
2020-04-04 15:59:05 +05:30
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
text := scanner.Text()
|
|
|
|
|
if text == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
limiter <- struct{}{}
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
|
|
go func(URL string) {
|
2020-04-22 22:45:02 +02:00
|
|
|
r.sendRequest(template, request, URL, writer, httpclient, dnsclient)
|
2020-04-04 15:59:05 +05:30
|
|
|
<-limiter
|
|
|
|
|
wg.Done()
|
|
|
|
|
}(text)
|
|
|
|
|
}
|
|
|
|
|
close(limiter)
|
|
|
|
|
wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sendRequest sends a request to the target based on a template
|
2020-04-26 06:33:59 +05:30
|
|
|
func (r *Runner) sendRequest(template *templates.Template, request interface{}, URL string, httpclient *retryablehttp.Client, dnsclient *retryabledns.Client) {
|
2020-04-24 15:42:11 +02:00
|
|
|
switch request.(type) {
|
|
|
|
|
case *requests.HTTPRequest:
|
2020-04-26 05:50:33 +05:30
|
|
|
|
2020-04-22 14:32:13 +02:00
|
|
|
|
2020-04-22 22:45:02 +02:00
|
|
|
// All the matchers matched, print the output on the screen
|
|
|
|
|
output := buildOutputHTTP(template, req, extractorResults, matcher)
|
|
|
|
|
gologger.Silentf("%s", output)
|
2020-04-22 14:32:13 +02:00
|
|
|
|
2020-04-22 22:45:02 +02:00
|
|
|
if writer != nil {
|
|
|
|
|
r.outputMutex.Lock()
|
|
|
|
|
writer.WriteString(output)
|
|
|
|
|
r.outputMutex.Unlock()
|
|
|
|
|
}
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-24 15:42:11 +02:00
|
|
|
case *requests.DNSRequest:
|
2020-04-22 22:45:02 +02:00
|
|
|
|
|
|
|
|
// All the matchers matched, print the output on the screen
|
2020-04-24 16:15:56 +02:00
|
|
|
output := buildOutputDNS(template, domain, extractorResults)
|
2020-04-22 22:45:02 +02:00
|
|
|
gologger.Silentf("%s", output)
|
|
|
|
|
|
|
|
|
|
if writer != nil {
|
|
|
|
|
r.outputMutex.Lock()
|
|
|
|
|
writer.WriteString(output)
|
|
|
|
|
r.outputMutex.Unlock()
|
|
|
|
|
}
|
2020-04-04 15:59:05 +05:30
|
|
|
}
|
|
|
|
|
}
|