2020-12-25 02:24:55 +05:30
|
|
|
package dns
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"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-07 19:38:31 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/expressions"
|
2020-12-25 02:24:55 +05:30
|
|
|
)
|
|
|
|
|
|
2020-12-25 20:39:09 +05:30
|
|
|
var _ protocols.Request = &Request{}
|
|
|
|
|
|
2020-12-25 02:24:55 +05:30
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2021-08-23 16:21:18 +03:00
|
|
|
func (r *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
|
|
|
|
|
if isURL(input) {
|
|
|
|
|
domain = extractDomain(input)
|
|
|
|
|
} else {
|
|
|
|
|
domain = input
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compile each request for the template based on the URL
|
|
|
|
|
compiledRequest, err := r.Make(domain)
|
|
|
|
|
if err != nil {
|
|
|
|
|
r.options.Output.Request(r.options.TemplateID, domain, "dns", err)
|
2021-03-02 02:22:15 +01:00
|
|
|
r.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-10-07 19:38:31 +05:30
|
|
|
requestString := compiledRequest.String()
|
|
|
|
|
if varErr := expressions.ContainsUnresolvedVariables(string(requestString)); varErr != nil {
|
|
|
|
|
gologger.Warning().Msgf("[%s] Could not make dns request for %s: %v\n", r.options.TemplateID, domain, varErr)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-01-12 17:18:08 +05:30
|
|
|
if r.options.Options.Debug || r.options.Options.DebugRequests {
|
2020-12-25 02:24:55 +05:30
|
|
|
gologger.Info().Str("domain", domain).Msgf("[%s] Dumped DNS request for %s", r.options.TemplateID, domain)
|
2021-10-07 19:38:31 +05:30
|
|
|
gologger.Print().Msgf("%s", requestString)
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send the request to the target servers
|
|
|
|
|
resp, err := r.dnsClient.Do(compiledRequest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
r.options.Output.Request(r.options.TemplateID, domain, "dns", err)
|
2021-03-02 02:22:15 +01:00
|
|
|
r.options.Progress.IncrementFailedRequestsBy(1)
|
2021-03-14 01:01:32 +05:30
|
|
|
}
|
|
|
|
|
if resp == 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
|
|
|
}
|
2020-12-25 20:33:52 +05:30
|
|
|
r.options.Progress.IncrementRequests()
|
|
|
|
|
|
2020-12-25 02:24:55 +05:30
|
|
|
r.options.Output.Request(r.options.TemplateID, domain, "dns", err)
|
|
|
|
|
gologger.Verbose().Msgf("[%s] Sent DNS request to %s", r.options.TemplateID, domain)
|
|
|
|
|
|
2021-01-12 17:18:08 +05:30
|
|
|
if r.options.Options.Debug || r.options.Options.DebugResponse {
|
2020-12-25 02:24:55 +05:30
|
|
|
gologger.Debug().Msgf("[%s] Dumped DNS response for %s", r.options.TemplateID, domain)
|
2021-01-17 00:51:43 +05:30
|
|
|
gologger.Print().Msgf("%s", resp.String())
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
2021-01-16 14:10:24 +05:30
|
|
|
outputEvent := r.responseToDSLMap(compiledRequest, resp, input, input)
|
|
|
|
|
for k, v := range previous {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
2020-12-25 02:24:55 +05:30
|
|
|
|
2021-01-16 14:10:24 +05:30
|
|
|
event := &output.InternalWrappedEvent{InternalEvent: outputEvent}
|
2020-12-30 13:26:55 +05:30
|
|
|
if r.CompiledOperators != nil {
|
2021-01-16 14:10:24 +05:30
|
|
|
result, ok := r.CompiledOperators.Execute(outputEvent, r.Match, r.Extract)
|
2021-01-13 12:18:56 +05:30
|
|
|
if ok && result != nil {
|
|
|
|
|
event.OperatorsResult = result
|
|
|
|
|
event.Results = r.MakeResultEvent(event)
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
callback(event)
|
|
|
|
|
return nil
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// isURL tests a string to determine if it is a well-structured url or not.
|
|
|
|
|
func isURL(toTest string) bool {
|
2021-08-31 12:55:52 +03:00
|
|
|
if _, err := url.ParseRequestURI(toTest); err != nil {
|
2020-12-25 02:24:55 +05:30
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
u, err := url.Parse(toTest)
|
|
|
|
|
if err != nil || u.Scheme == "" || u.Host == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
}
|