2020-07-16 10:57:28 +02:00
|
|
|
package executer
|
2020-04-26 05:50:33 +05:30
|
|
|
|
|
|
|
|
import (
|
2020-07-15 13:38:45 +02:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httputil"
|
2020-04-26 05:50:33 +05:30
|
|
|
"strings"
|
|
|
|
|
|
2020-06-27 20:19:43 +05:30
|
|
|
jsoniter "github.com/json-iterator/go"
|
2020-04-26 06:33:59 +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"
|
2020-04-26 05:50:33 +05:30
|
|
|
)
|
|
|
|
|
|
2020-04-26 06:33:59 +05:30
|
|
|
// writeOutputHTTP writes http output to streams
|
2020-08-25 23:24:31 +02:00
|
|
|
func (e *HTTPExecuter) writeOutputHTTP(req *requests.HTTPRequest, resp *http.Response, body string, matcher *matchers.Matcher, extractorResults []string) {
|
2020-06-27 20:19:43 +05:30
|
|
|
URL := req.Request.URL.String()
|
|
|
|
|
|
|
|
|
|
if e.jsonOutput {
|
|
|
|
|
output := jsonOutput{
|
2020-07-16 10:57:28 +02:00
|
|
|
Template: e.template.ID,
|
|
|
|
|
Type: "http",
|
|
|
|
|
Matched: URL,
|
2020-08-27 22:13:42 +02:00
|
|
|
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-08-25 23:24:31 +02:00
|
|
|
|
2020-06-27 20:19:43 +05:30
|
|
|
if matcher != nil && len(matcher.Name) > 0 {
|
|
|
|
|
output.MatcherName = matcher.Name
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-06-27 20:19:43 +05:30
|
|
|
if len(extractorResults) > 0 {
|
|
|
|
|
output.ExtractedResults = extractorResults
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-15 13:38:45 +02:00
|
|
|
if e.jsonRequest {
|
|
|
|
|
dumpedRequest, err := httputil.DumpRequest(req.Request.Request, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Warningf("could not dump request: %s\n", err)
|
|
|
|
|
} else {
|
|
|
|
|
output.Request = string(dumpedRequest)
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-15 13:38:45 +02:00
|
|
|
dumpedResponse, err := httputil.DumpResponse(resp, false)
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-15 13:38:45 +02:00
|
|
|
if err != nil {
|
|
|
|
|
gologger.Warningf("could not dump response: %s\n", err)
|
|
|
|
|
} else {
|
|
|
|
|
output.Response = string(dumpedResponse) + body
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-06-27 20:19:43 +05:30
|
|
|
data, err := jsoniter.Marshal(output)
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-06-27 20:19:43 +05:30
|
|
|
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 {
|
2020-08-25 23:24:31 +02:00
|
|
|
gologger.Errorf("Could not write output data: %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-06-27 20:19:43 +05:30
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-06-27 20:19:43 +05:30
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 05:50:33 +05:30
|
|
|
builder := &strings.Builder{}
|
2020-07-29 00:43:05 +02:00
|
|
|
colorizer := e.colorizer
|
2020-04-26 05:50:33 +05:30
|
|
|
|
|
|
|
|
builder.WriteRune('[')
|
2020-07-29 00:43:05 +02:00
|
|
|
builder.WriteString(colorizer.BrightGreen(e.template.ID).String())
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-26 06:33:59 +05:30
|
|
|
if matcher != nil && len(matcher.Name) > 0 {
|
2020-04-26 05:50:33 +05:30
|
|
|
builder.WriteString(":")
|
2020-07-29 00:43:05 +02:00
|
|
|
builder.WriteString(colorizer.BrightGreen(matcher.Name).Bold().String())
|
2020-04-26 05:50:33 +05:30
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-07-29 00:43:05 +02:00
|
|
|
builder.WriteString("] [")
|
|
|
|
|
builder.WriteString(colorizer.BrightBlue("http").String())
|
|
|
|
|
builder.WriteString("] ")
|
2020-04-26 05:50:33 +05:30
|
|
|
|
|
|
|
|
// Escape the URL by replacing all % with %%
|
2020-08-25 23:24:31 +02:00
|
|
|
escapedURL := strings.ReplaceAll(URL, "%", "%%")
|
2020-04-26 05:50:33 +05:30
|
|
|
builder.WriteString(escapedURL)
|
|
|
|
|
|
|
|
|
|
// If any extractors, write the results
|
|
|
|
|
if len(extractorResults) > 0 {
|
|
|
|
|
builder.WriteString(" [")
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-26 05:50:33 +05:30
|
|
|
for i, result := range extractorResults {
|
2020-07-29 00:43:05 +02:00
|
|
|
builder.WriteString(colorizer.BrightCyan(result).String())
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-26 05:50:33 +05:30
|
|
|
if i != len(extractorResults)-1 {
|
|
|
|
|
builder.WriteRune(',')
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-04-26 05:50:33 +05:30
|
|
|
builder.WriteString("]")
|
|
|
|
|
}
|
2020-05-14 18:09:36 +02:00
|
|
|
|
|
|
|
|
// write meta if any
|
|
|
|
|
if len(req.Meta) > 0 {
|
|
|
|
|
builder.WriteString(" [")
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-05-14 18:09:36 +02:00
|
|
|
var metas []string
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-05-14 18:09:36 +02:00
|
|
|
for name, value := range req.Meta {
|
2020-08-25 23:24:31 +02:00
|
|
|
metas = append(metas, colorizer.BrightYellow(name).Bold().String()+"="+colorizer.BrightYellow(value.(string)).String())
|
2020-05-14 18:09:36 +02:00
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-05-14 18:09:36 +02:00
|
|
|
builder.WriteString(strings.Join(metas, ","))
|
|
|
|
|
builder.WriteString("]")
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 05:50:33 +05:30
|
|
|
builder.WriteRune('\n')
|
|
|
|
|
|
2020-04-26 06:33:59 +05:30
|
|
|
// Write output to screen as well as any output file
|
|
|
|
|
message := builder.String()
|
|
|
|
|
gologger.Silentf("%s", message)
|
|
|
|
|
|
|
|
|
|
if e.writer != nil {
|
2020-07-29 00:43:05 +02:00
|
|
|
if e.coloredOutput {
|
|
|
|
|
message = e.decolorizer.ReplaceAllString(message, "")
|
|
|
|
|
}
|
2020-08-25 23:24:31 +02:00
|
|
|
|
2020-09-10 16:32:01 +05:30
|
|
|
if err := e.writer.WriteString(message); err != nil {
|
2020-08-25 23:24:31 +02:00
|
|
|
gologger.Errorf("Could not write output data: %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-04-26 06:33:59 +05:30
|
|
|
}
|
2020-04-26 05:50:33 +05:30
|
|
|
}
|