nuclei/pkg/executer/output_dns.go

104 lines
2.4 KiB
Go
Raw Normal View History

2020-07-16 10:57:28 +02:00
package executer
2020-04-26 05:50:33 +05:30
import (
"strings"
"github.com/miekg/dns"
2020-06-27 20:19:43 +05:30
jsoniter "github.com/json-iterator/go"
"github.com/projectdiscovery/gologger"
2020-07-01 16:17:24 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/matchers"
2020-04-26 05:50:33 +05:30
)
// writeOutputDNS writes dns output to streams
// nolint:interfacer // dns.Msg is out of current scope
func (e *DNSExecuter) writeOutputDNS(domain string, req, resp *dns.Msg, matcher *matchers.Matcher, extractorResults []string) {
2020-06-27 20:19:43 +05:30
if e.jsonOutput {
output := jsonOutput{
2020-07-16 10:57:28 +02:00
Template: e.template.ID,
Type: "dns",
Matched: domain,
Name: e.template.Info.Name,
2020-07-16 10:57:28 +02:00
Severity: e.template.Info.Severity,
Author: e.template.Info.Author,
Description: e.template.Info.Description,
2020-06-27 20:19:43 +05:30
}
2020-06-27 20:19:43 +05:30
if matcher != nil && len(matcher.Name) > 0 {
output.MatcherName = matcher.Name
}
2020-06-27 20:19:43 +05:30
if len(extractorResults) > 0 {
output.ExtractedResults = extractorResults
}
2020-08-04 15:16:41 +02:00
if e.jsonRequest {
output.Request = req.String()
output.Response = resp.String()
}
2020-06-27 20:19:43 +05:30
data, err := jsoniter.Marshal(output)
if err != nil {
gologger.Warningf("Could not marshal json output: %s\n", err)
}
gologger.Silentf("%s", string(data))
if e.writer != nil {
2020-09-10 16:32:01 +05:30
if err := e.writer.Write(data); err != nil {
gologger.Errorf("Could not write output data: %s\n", err)
return
}
2020-06-27 20:19:43 +05:30
}
return
}
2020-04-26 05:50:33 +05:30
builder := &strings.Builder{}
colorizer := e.colorizer
2020-04-26 05:50:33 +05:30
builder.WriteRune('[')
builder.WriteString(colorizer.BrightGreen(e.template.ID).String())
if matcher != nil && len(matcher.Name) > 0 {
builder.WriteString(":")
builder.WriteString(colorizer.BrightGreen(matcher.Name).Bold().String())
}
builder.WriteString("] [")
builder.WriteString(colorizer.BrightBlue("dns").String())
builder.WriteString("] ")
2020-04-26 05:50:33 +05:30
builder.WriteString(domain)
// If any extractors, write the results
if len(extractorResults) > 0 {
builder.WriteString(" [")
2020-04-26 05:50:33 +05:30
for i, result := range extractorResults {
builder.WriteString(colorizer.BrightCyan(result).String())
2020-04-26 05:50:33 +05:30
if i != len(extractorResults)-1 {
builder.WriteRune(',')
}
}
2020-04-26 05:50:33 +05:30
builder.WriteString("]")
}
2020-04-26 05:50:33 +05:30
builder.WriteRune('\n')
// Write output to screen as well as any output file
message := builder.String()
gologger.Silentf("%s", message)
if e.writer != nil {
if e.coloredOutput {
message = e.decolorizer.ReplaceAllString(message, "")
}
2020-09-10 16:32:01 +05:30
if err := e.writer.WriteString(message); err != nil {
gologger.Errorf("Could not write output data: %s\n", err)
return
}
}
2020-04-26 05:50:33 +05:30
}