2020-12-24 20:54:55 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2021-10-30 10:17:47 +00:00
|
|
|
"encoding/hex"
|
2020-12-24 20:54:55 +00:00
|
|
|
"net/url"
|
|
|
|
|
2022-04-01 20:44:00 +00:00
|
|
|
"github.com/miekg/dns"
|
2020-12-24 20:54:55 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-09-29 16:43:46 +00:00
|
|
|
|
2020-12-24 20:54:55 +00:00
|
|
|
"github.com/projectdiscovery/gologger"
|
2022-04-01 20:44:00 +00:00
|
|
|
"github.com/projectdiscovery/iputil"
|
2020-12-24 20:54:55 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
2020-12-25 15:09:09 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-10-12 17:06:55 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/expressions"
|
2022-04-01 20:44:00 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/generators"
|
2021-10-01 13:52:38 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/eventcreator"
|
2021-10-01 11:24:45 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
|
2021-11-03 14:23:45 +00:00
|
|
|
templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
|
2022-01-12 13:03:17 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
|
2021-11-25 15:09:20 +00:00
|
|
|
"github.com/projectdiscovery/retryabledns"
|
2020-12-24 20:54:55 +00:00
|
|
|
)
|
|
|
|
|
2020-12-25 15:09:09 +00:00
|
|
|
var _ protocols.Request = &Request{}
|
|
|
|
|
2021-11-03 14:23:45 +00:00
|
|
|
// Type returns the type of the protocol request
|
|
|
|
func (request *Request) Type() templateTypes.ProtocolType {
|
|
|
|
return templateTypes.DNSProtocol
|
|
|
|
}
|
|
|
|
|
2020-12-24 20:54:55 +00:00
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2021-10-01 11:30:04 +00:00
|
|
|
func (request *Request) ExecuteWithResults(input string, metadata /*TODO review unused parameter*/, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
2020-12-24 20:54:55 +00:00
|
|
|
// Parse the URL and return domain if URL.
|
|
|
|
var domain string
|
2022-01-12 13:03:17 +00:00
|
|
|
if utils.IsURL(input) {
|
2020-12-24 20:54:55 +00:00
|
|
|
domain = extractDomain(input)
|
|
|
|
} else {
|
|
|
|
domain = input
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:44:00 +00:00
|
|
|
var err error
|
|
|
|
domain, err = request.parseDNSInput(domain)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not build request")
|
|
|
|
}
|
|
|
|
vars := GenerateDNSVariables(domain)
|
|
|
|
variablesMap := request.options.Variables.Evaluate(vars)
|
|
|
|
vars = generators.MergeMaps(variablesMap, vars)
|
|
|
|
|
2020-12-24 20:54:55 +00:00
|
|
|
// Compile each request for the template based on the URL
|
2022-04-01 20:44:00 +00:00
|
|
|
compiledRequest, err := request.Make(domain, vars)
|
2020-12-24 20:54:55 +00:00
|
|
|
if err != nil {
|
2021-11-04 21:31:41 +00:00
|
|
|
request.options.Output.Request(request.options.TemplatePath, domain, request.Type().String(), err)
|
2021-10-01 11:30:04 +00:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-01-01 14:06:21 +00:00
|
|
|
return errors.Wrap(err, "could not build request")
|
2020-12-24 20:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:52:11 +00:00
|
|
|
dnsClient := request.dnsClient
|
|
|
|
if varErr := expressions.ContainsUnresolvedVariables(request.Resolvers...); varErr != nil {
|
|
|
|
if dnsClient, varErr = request.getDnsClient(request.options, metadata); varErr != nil {
|
|
|
|
gologger.Warning().Msgf("[%s] Could not make dns request for %s: %v\n", request.options.TemplateID, domain, varErr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 14:08:31 +00:00
|
|
|
requestString := compiledRequest.String()
|
2021-10-07 14:10:16 +00:00
|
|
|
if varErr := expressions.ContainsUnresolvedVariables(requestString); varErr != nil {
|
2021-10-11 10:58:20 +00:00
|
|
|
gologger.Warning().Msgf("[%s] Could not make dns request for %s: %v\n", request.options.TemplateID, domain, varErr)
|
2021-10-07 14:08:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-01 11:30:04 +00:00
|
|
|
if request.options.Options.Debug || request.options.Options.DebugRequests {
|
|
|
|
gologger.Info().Str("domain", domain).Msgf("[%s] Dumped DNS request for %s", request.options.TemplateID, domain)
|
2021-10-07 14:08:31 +00:00
|
|
|
gologger.Print().Msgf("%s", requestString)
|
2020-12-24 20:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the request to the target servers
|
2021-11-18 13:52:11 +00:00
|
|
|
response, err := dnsClient.Do(compiledRequest)
|
2020-12-24 20:54:55 +00:00
|
|
|
if err != nil {
|
2021-11-04 21:31:41 +00:00
|
|
|
request.options.Output.Request(request.options.TemplatePath, domain, request.Type().String(), err)
|
2021-10-01 11:30:04 +00:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-03-13 19:31:32 +00:00
|
|
|
}
|
2021-10-30 10:17:47 +00:00
|
|
|
if response == nil {
|
2021-01-01 14:06:21 +00:00
|
|
|
return errors.Wrap(err, "could not send dns request")
|
2020-12-24 20:54:55 +00:00
|
|
|
}
|
2021-10-01 11:30:04 +00:00
|
|
|
request.options.Progress.IncrementRequests()
|
2020-12-25 15:03:52 +00:00
|
|
|
|
2021-11-04 21:31:41 +00:00
|
|
|
request.options.Output.Request(request.options.TemplatePath, domain, request.Type().String(), err)
|
2021-10-30 10:17:47 +00:00
|
|
|
gologger.Verbose().Msgf("[%s] Sent DNS request to %s\n", request.options.TemplateID, domain)
|
2020-12-24 20:54:55 +00:00
|
|
|
|
2021-11-18 13:52:11 +00:00
|
|
|
// perform trace if necessary
|
2021-11-25 16:54:16 +00:00
|
|
|
var traceData *retryabledns.TraceData
|
2021-11-18 13:52:11 +00:00
|
|
|
if request.Trace {
|
2021-11-25 16:54:16 +00:00
|
|
|
traceData, err = request.dnsClient.Trace(domain, request.question, request.TraceMaxRecursion)
|
2021-11-18 13:52:11 +00:00
|
|
|
if err != nil {
|
|
|
|
request.options.Output.Request(request.options.TemplatePath, domain, "dns", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 16:54:16 +00:00
|
|
|
outputEvent := request.responseToDSLMap(compiledRequest, response, input, input, traceData)
|
2021-01-16 08:40:24 +00:00
|
|
|
for k, v := range previous {
|
|
|
|
outputEvent[k] = v
|
|
|
|
}
|
2022-04-01 20:44:00 +00:00
|
|
|
for k, v := range vars {
|
|
|
|
outputEvent[k] = v
|
|
|
|
}
|
2021-10-12 17:06:55 +00:00
|
|
|
event := eventcreator.CreateEvent(request, outputEvent, request.options.Options.Debug || request.options.Options.DebugResponse)
|
2021-11-18 13:52:11 +00:00
|
|
|
// TODO: dynamic values are not supported yet
|
2021-10-01 11:24:45 +00:00
|
|
|
|
2021-11-01 18:45:54 +00:00
|
|
|
dumpResponse(event, request.options, response.String(), domain)
|
2021-11-18 13:52:11 +00:00
|
|
|
if request.Trace {
|
2021-11-25 16:54:16 +00:00
|
|
|
dumpTraceData(event, request.options, traceToString(traceData, true), domain)
|
2021-11-18 13:52:11 +00:00
|
|
|
}
|
2021-09-29 16:43:46 +00:00
|
|
|
|
|
|
|
callback(event)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:44:00 +00:00
|
|
|
func (request *Request) parseDNSInput(host string) (string, error) {
|
|
|
|
isIP := iputil.IsIP(host)
|
|
|
|
switch {
|
|
|
|
case request.question == dns.TypePTR && isIP:
|
|
|
|
var err error
|
|
|
|
host, err = dns.ReverseAddr(host)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if isIP {
|
|
|
|
return "", errors.New("cannot use IP address as DNS input")
|
|
|
|
}
|
|
|
|
host = dns.Fqdn(host)
|
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
2021-11-18 13:52:11 +00:00
|
|
|
func dumpResponse(event *output.InternalWrappedEvent, requestOptions *protocols.ExecuterOptions, response, domain string) {
|
2021-11-01 18:45:54 +00:00
|
|
|
cliOptions := requestOptions.Options
|
|
|
|
if cliOptions.Debug || cliOptions.DebugResponse {
|
2021-10-30 10:17:47 +00:00
|
|
|
hexDump := false
|
2021-11-01 18:45:54 +00:00
|
|
|
if responsehighlighter.HasBinaryContent(response) {
|
2021-10-30 10:17:47 +00:00
|
|
|
hexDump = true
|
|
|
|
response = hex.Dump([]byte(response))
|
|
|
|
}
|
2021-11-01 18:45:54 +00:00
|
|
|
highlightedResponse := responsehighlighter.Highlight(event.OperatorsResult, response, cliOptions.NoColor, hexDump)
|
|
|
|
gologger.Debug().Msgf("[%s] Dumped DNS response for %s\n\n%s", requestOptions.TemplateID, domain, highlightedResponse)
|
2021-10-30 10:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 16:54:16 +00:00
|
|
|
func dumpTraceData(event *output.InternalWrappedEvent, requestOptions *protocols.ExecuterOptions, traceData, domain string) {
|
2021-11-18 13:52:11 +00:00
|
|
|
cliOptions := requestOptions.Options
|
|
|
|
if cliOptions.Debug || cliOptions.DebugResponse {
|
|
|
|
hexDump := false
|
2021-11-25 16:54:16 +00:00
|
|
|
if responsehighlighter.HasBinaryContent(traceData) {
|
2021-11-18 13:52:11 +00:00
|
|
|
hexDump = true
|
2021-11-25 16:54:16 +00:00
|
|
|
traceData = hex.Dump([]byte(traceData))
|
2021-11-18 13:52:11 +00:00
|
|
|
}
|
2021-11-25 16:54:16 +00:00
|
|
|
highlightedResponse := responsehighlighter.Highlight(event.OperatorsResult, traceData, cliOptions.NoColor, hexDump)
|
2021-11-18 13:52:11 +00:00
|
|
|
gologger.Debug().Msgf("[%s] Dumped DNS Trace data for %s\n\n%s", requestOptions.TemplateID, domain, highlightedResponse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 20:54:55 +00:00
|
|
|
// extractDomain extracts the domain name of a URL
|
|
|
|
func extractDomain(theURL string) string {
|
|
|
|
u, err := url.Parse(theURL)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return u.Hostname()
|
|
|
|
}
|