nuclei/internal/runner/runner.go

195 lines
5.2 KiB
Go
Raw Normal View History

package runner
import (
"bufio"
2020-04-04 17:12:29 +05:30
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
2020-04-04 17:12:29 +05:30
"time"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/pkg/extractors"
"github.com/projectdiscovery/nuclei/pkg/matchers"
"github.com/projectdiscovery/nuclei/pkg/requests"
"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"
)
// 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
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 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
}
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-05 01:18:57 +05:30
// RunEnumeration sets up the input layer for giving input nuclei.
// 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)
}
// If the template path is a single template and not a glob, use that.
if !strings.Contains(r.options.Templates, "*") {
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 {
r.processTemplateRequest(template, request)
}
2020-04-04 17:36:20 +05:30
return
}
// 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
for _, match := range matches {
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 {
r.processTemplateRequest(template, request)
}
}
}
// 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{}) {
// 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)
}
r.processTemplateWithList(template, request, file)
file.Close()
return
}
// Handle stdin input
if r.options.Stdin {
r.processTemplateWithList(template, request, os.Stdin)
}
}
// 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) {
// 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)
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)
<-limiter
wg.Done()
}(text)
}
close(limiter)
wg.Wait()
}
// sendRequest sends a request to the target based on a template
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 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 22:45:02 +02:00
if writer != nil {
r.outputMutex.Lock()
writer.WriteString(output)
r.outputMutex.Unlock()
}
}
}
}
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()
}
}
}