nuclei/v2/pkg/executor/executor_dns.go

153 lines
4.1 KiB
Go
Raw Normal View History

2020-04-26 05:50:33 +05:30
package executor
import (
"bufio"
2020-06-22 19:30:01 +05:30
"fmt"
"os"
"sync"
"sync/atomic"
"github.com/pkg/errors"
2020-06-22 19:30:01 +05:30
"github.com/projectdiscovery/gologger"
2020-07-01 16:17:24 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/matchers"
"github.com/projectdiscovery/nuclei/v2/pkg/requests"
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
2020-04-26 05:50:33 +05:30
retryabledns "github.com/projectdiscovery/retryabledns"
)
// DNSExecutor is a client for performing a DNS request
// for a template.
type DNSExecutor struct {
2020-06-22 19:30:01 +05:30
debug bool
2020-06-27 20:19:43 +05:30
jsonOutput bool
results uint32
dnsClient *retryabledns.Client
template *templates.Template
dnsRequest *requests.DNSRequest
writer *bufio.Writer
outputMutex *sync.Mutex
2020-04-26 05:50:33 +05:30
}
// DefaultResolvers contains the list of resolvers known to be trusted.
var DefaultResolvers = []string{
"1.1.1.1:53", // Cloudflare
"1.0.0.1:53", // Cloudflare
"8.8.8.8:53", // Google
"8.8.4.4:53", // Google
}
// DNSOptions contains configuration options for the DNS executor.
type DNSOptions struct {
2020-06-22 19:30:01 +05:30
Debug bool
2020-06-27 20:19:43 +05:30
JSON bool
Template *templates.Template
DNSRequest *requests.DNSRequest
Writer *bufio.Writer
}
2020-04-26 05:50:33 +05:30
// NewDNSExecutor creates a new DNS executor from a template
// and a DNS request query.
func NewDNSExecutor(options *DNSOptions) *DNSExecutor {
dnsClient := retryabledns.New(DefaultResolvers, options.DNSRequest.Retries)
2020-04-26 05:50:33 +05:30
executer := &DNSExecutor{
2020-06-22 19:30:01 +05:30
debug: options.Debug,
2020-06-27 20:19:43 +05:30
jsonOutput: options.JSON,
results: 0,
2020-04-26 23:11:49 +05:30
dnsClient: dnsClient,
template: options.Template,
dnsRequest: options.DNSRequest,
writer: options.Writer,
outputMutex: &sync.Mutex{},
2020-04-26 05:50:33 +05:30
}
return executer
}
// GotResults returns true if there were any results for the executor
func (e *DNSExecutor) GotResults() bool {
if atomic.LoadUint32(&e.results) == 0 {
return false
}
return true
}
2020-04-26 05:50:33 +05:30
// ExecuteDNS executes the DNS request on a URL
2020-07-10 09:04:38 +02:00
func (e *DNSExecutor) ExecuteDNS(URL string) (result Result) {
2020-04-26 05:50:33 +05:30
// Parse the URL and return domain if URL.
var domain string
if isURL(URL) {
domain = extractDomain(URL)
} else {
domain = URL
}
// Compile each request for the template based on the URL
compiledRequest, err := e.dnsRequest.MakeDNSRequest(domain)
2020-04-26 05:50:33 +05:30
if err != nil {
2020-07-10 09:04:38 +02:00
result.Error = errors.Wrap(err, "could not make dns request")
return
2020-04-26 05:50:33 +05:30
}
2020-06-22 19:30:01 +05:30
if e.debug {
gologger.Infof("Dumped DNS request for %s (%s)\n\n", URL, e.template.ID)
fmt.Fprintf(os.Stderr, "%s\n", compiledRequest.String())
}
2020-04-26 05:50:33 +05:30
// Send the request to the target servers
resp, err := e.dnsClient.Do(compiledRequest)
if err != nil {
2020-07-10 09:04:38 +02:00
result.Error = errors.Wrap(err, "could not send dns request")
return
2020-04-26 05:50:33 +05:30
}
2020-06-22 19:30:01 +05:30
gologger.Verbosef("Sent DNS request to %s\n", "dns-request", URL)
if e.debug {
gologger.Infof("Dumped DNS response for %s (%s)\n\n", URL, e.template.ID)
fmt.Fprintf(os.Stderr, "%s\n", resp.String())
}
matcherCondition := e.dnsRequest.GetMatchersCondition()
2020-04-26 05:50:33 +05:30
for _, matcher := range e.dnsRequest.Matchers {
// Check if the matcher matched
if !matcher.MatchDNS(resp) {
// If the condition is AND we haven't matched, return.
if matcherCondition == matchers.ANDCondition {
2020-07-10 09:04:38 +02:00
return
}
} else {
// If the matcher has matched, and its an OR
// write the first output then move to next matcher.
if matcherCondition == matchers.ORCondition && len(e.dnsRequest.Extractors) == 0 {
e.writeOutputDNS(domain, matcher, nil)
atomic.CompareAndSwapUint32(&e.results, 0, 1)
}
2020-04-26 05:50:33 +05:30
}
}
// All matchers have successfully completed so now start with the
// next task which is extraction of input from matchers.
2020-04-26 05:50:33 +05:30
var extractorResults []string
for _, extractor := range e.dnsRequest.Extractors {
2020-04-27 23:34:08 +05:30
for match := range extractor.ExtractDNS(resp.String()) {
extractorResults = append(extractorResults, match)
}
2020-04-26 05:50:33 +05:30
}
// Write a final string of output if matcher type is
// AND or if we have extractors for the mechanism too.
if len(e.dnsRequest.Extractors) > 0 || matcherCondition == matchers.ANDCondition {
e.writeOutputDNS(domain, nil, extractorResults)
atomic.CompareAndSwapUint32(&e.results, 0, 1)
}
2020-07-10 09:04:38 +02:00
return
}
// Close closes the dns executor for a template.
func (e *DNSExecutor) Close() {
e.outputMutex.Lock()
e.writer.Flush()
e.outputMutex.Unlock()
2020-04-26 05:50:33 +05:30
}