2020-12-25 02:24:55 +05:30
|
|
|
package dns
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-30 13:17:47 +03:00
|
|
|
"encoding/hex"
|
2022-04-01 14:29:02 -05:00
|
|
|
"fmt"
|
2020-12-25 02:24:55 +05:30
|
|
|
"net/url"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-29 19:43:46 +03:00
|
|
|
|
2020-12-25 02:24:55 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
2020-12-25 20:39:09 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-10-12 20:06:55 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/expressions"
|
2021-10-01 16:52:38 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/eventcreator"
|
2021-10-01 14:24:45 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
|
2021-11-03 19:53:45 +05:30
|
|
|
templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
|
2022-01-12 18:33:17 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
|
2021-11-25 17:09:20 +02:00
|
|
|
"github.com/projectdiscovery/retryabledns"
|
2020-12-25 02:24:55 +05:30
|
|
|
)
|
|
|
|
|
|
2020-12-25 20:39:09 +05:30
|
|
|
var _ protocols.Request = &Request{}
|
|
|
|
|
|
2021-11-03 19:53:45 +05:30
|
|
|
// Type returns the type of the protocol request
|
|
|
|
|
func (request *Request) Type() templateTypes.ProtocolType {
|
|
|
|
|
return templateTypes.DNSProtocol
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 02:24:55 +05:30
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) ExecuteWithResults(input string, metadata /*TODO review unused parameter*/, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
2020-12-25 02:24:55 +05:30
|
|
|
// Parse the URL and return domain if URL.
|
|
|
|
|
var domain string
|
2022-01-12 18:33:17 +05:30
|
|
|
if utils.IsURL(input) {
|
2020-12-25 02:24:55 +05:30
|
|
|
domain = extractDomain(input)
|
|
|
|
|
} else {
|
|
|
|
|
domain = input
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compile each request for the template based on the URL
|
2021-10-01 14:30:04 +03:00
|
|
|
compiledRequest, err := request.Make(domain)
|
2020-12-25 02:24:55 +05:30
|
|
|
if err != nil {
|
2021-11-05 03:01:41 +05:30
|
|
|
request.options.Output.Request(request.options.TemplatePath, domain, request.Type().String(), err)
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-01-01 19:36:21 +05:30
|
|
|
return errors.Wrap(err, "could not build request")
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
2021-11-18 14:52:11 +01: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 19:38:31 +05:30
|
|
|
requestString := compiledRequest.String()
|
2021-10-07 19:40:16 +05:30
|
|
|
if varErr := expressions.ContainsUnresolvedVariables(requestString); varErr != nil {
|
2021-10-11 13:58:20 +03:00
|
|
|
gologger.Warning().Msgf("[%s] Could not make dns request for %s: %v\n", request.options.TemplateID, domain, varErr)
|
2021-10-07 19:38:31 +05:30
|
|
|
return nil
|
|
|
|
|
}
|
2022-04-01 14:29:02 -05:00
|
|
|
if request.options.Options.Debug || request.options.Options.DebugRequests || request.options.Options.StoreResponse {
|
|
|
|
|
msg := fmt.Sprintf("[%s] Dumped DNS request for %s", request.options.TemplateID, domain)
|
|
|
|
|
if request.options.Options.Debug || request.options.Options.DebugRequests {
|
|
|
|
|
gologger.Info().Str("domain", domain).Msgf(msg)
|
|
|
|
|
gologger.Print().Msgf("%s", requestString)
|
|
|
|
|
}
|
|
|
|
|
if request.options.Options.StoreResponse {
|
|
|
|
|
request.options.Output.WriteStoreDebugData(domain, request.options.TemplateID, request.Type().String(), fmt.Sprintf("%s\n%s", msg, requestString))
|
|
|
|
|
}
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send the request to the target servers
|
2021-11-18 14:52:11 +01:00
|
|
|
response, err := dnsClient.Do(compiledRequest)
|
2020-12-25 02:24:55 +05:30
|
|
|
if err != nil {
|
2021-11-05 03:01:41 +05:30
|
|
|
request.options.Output.Request(request.options.TemplatePath, domain, request.Type().String(), err)
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-03-14 01:01:32 +05:30
|
|
|
}
|
2021-10-30 13:17:47 +03:00
|
|
|
if response == nil {
|
2021-01-01 19:36:21 +05:30
|
|
|
return errors.Wrap(err, "could not send dns request")
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementRequests()
|
2020-12-25 20:33:52 +05:30
|
|
|
|
2021-11-05 03:01:41 +05:30
|
|
|
request.options.Output.Request(request.options.TemplatePath, domain, request.Type().String(), err)
|
2021-10-30 13:17:47 +03:00
|
|
|
gologger.Verbose().Msgf("[%s] Sent DNS request to %s\n", request.options.TemplateID, domain)
|
2020-12-25 02:24:55 +05:30
|
|
|
|
2021-11-18 14:52:11 +01:00
|
|
|
// perform trace if necessary
|
2021-11-25 18:54:16 +02:00
|
|
|
var traceData *retryabledns.TraceData
|
2021-11-18 14:52:11 +01:00
|
|
|
if request.Trace {
|
2021-11-25 18:54:16 +02:00
|
|
|
traceData, err = request.dnsClient.Trace(domain, request.question, request.TraceMaxRecursion)
|
2021-11-18 14:52:11 +01:00
|
|
|
if err != nil {
|
|
|
|
|
request.options.Output.Request(request.options.TemplatePath, domain, "dns", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 18:54:16 +02:00
|
|
|
outputEvent := request.responseToDSLMap(compiledRequest, response, input, input, traceData)
|
2021-01-16 14:10:24 +05:30
|
|
|
for k, v := range previous {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
2020-12-25 02:24:55 +05:30
|
|
|
|
2021-10-12 20:06:55 +03:00
|
|
|
event := eventcreator.CreateEvent(request, outputEvent, request.options.Options.Debug || request.options.Options.DebugResponse)
|
2021-11-18 14:52:11 +01:00
|
|
|
// TODO: dynamic values are not supported yet
|
2021-10-01 14:24:45 +03:00
|
|
|
|
2022-04-01 14:29:02 -05:00
|
|
|
dumpResponse(event, request, response.String(), domain)
|
2021-11-18 14:52:11 +01:00
|
|
|
if request.Trace {
|
2021-11-25 18:54:16 +02:00
|
|
|
dumpTraceData(event, request.options, traceToString(traceData, true), domain)
|
2021-11-18 14:52:11 +01:00
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
|
|
|
|
|
callback(event)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:29:02 -05:00
|
|
|
func dumpResponse(event *output.InternalWrappedEvent, request *Request, response, domain string) {
|
|
|
|
|
cliOptions := request.options.Options
|
|
|
|
|
if cliOptions.Debug || cliOptions.DebugResponse || cliOptions.StoreResponse {
|
2021-10-30 13:17:47 +03:00
|
|
|
hexDump := false
|
2021-11-01 20:45:54 +02:00
|
|
|
if responsehighlighter.HasBinaryContent(response) {
|
2021-10-30 13:17:47 +03:00
|
|
|
hexDump = true
|
|
|
|
|
response = hex.Dump([]byte(response))
|
|
|
|
|
}
|
2021-11-01 20:45:54 +02:00
|
|
|
highlightedResponse := responsehighlighter.Highlight(event.OperatorsResult, response, cliOptions.NoColor, hexDump)
|
2022-04-01 14:29:02 -05:00
|
|
|
msg := fmt.Sprintf("[%s] Dumped DNS response for %s\n\n%s", request.options.TemplateID, domain, highlightedResponse)
|
|
|
|
|
if cliOptions.Debug || cliOptions.DebugResponse {
|
|
|
|
|
gologger.Debug().Msg(msg)
|
|
|
|
|
}
|
|
|
|
|
if cliOptions.StoreResponse {
|
|
|
|
|
request.options.Output.WriteStoreDebugData(domain, request.options.TemplateID, request.Type().String(), msg)
|
|
|
|
|
}
|
2021-10-30 13:17:47 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 18:54:16 +02:00
|
|
|
func dumpTraceData(event *output.InternalWrappedEvent, requestOptions *protocols.ExecuterOptions, traceData, domain string) {
|
2021-11-18 14:52:11 +01:00
|
|
|
cliOptions := requestOptions.Options
|
|
|
|
|
if cliOptions.Debug || cliOptions.DebugResponse {
|
|
|
|
|
hexDump := false
|
2021-11-25 18:54:16 +02:00
|
|
|
if responsehighlighter.HasBinaryContent(traceData) {
|
2021-11-18 14:52:11 +01:00
|
|
|
hexDump = true
|
2021-11-25 18:54:16 +02:00
|
|
|
traceData = hex.Dump([]byte(traceData))
|
2021-11-18 14:52:11 +01:00
|
|
|
}
|
2021-11-25 18:54:16 +02:00
|
|
|
highlightedResponse := responsehighlighter.Highlight(event.OperatorsResult, traceData, cliOptions.NoColor, hexDump)
|
2021-11-18 14:52:11 +01:00
|
|
|
gologger.Debug().Msgf("[%s] Dumped DNS Trace data for %s\n\n%s", requestOptions.TemplateID, domain, highlightedResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 02:24:55 +05:30
|
|
|
// 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()
|
|
|
|
|
}
|