2020-12-21 15:51:43 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httputil"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2020-12-24 20:47:41 +05:30
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators/extractors"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers"
|
2020-12-29 01:30:07 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
2020-12-25 02:24:55 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
2020-12-21 15:51:43 +05:30
|
|
|
)
|
|
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
// Match matches a generic data response again a given matcher
|
|
|
|
|
func (r *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) bool {
|
2020-12-29 11:42:46 +05:30
|
|
|
partString := matcher.Part
|
2020-12-24 20:47:41 +05:30
|
|
|
switch partString {
|
|
|
|
|
case "header":
|
|
|
|
|
partString = "all_headers"
|
|
|
|
|
}
|
2020-12-25 02:24:55 +05:30
|
|
|
|
2021-01-01 17:07:24 +05:30
|
|
|
var itemStr string
|
|
|
|
|
if partString == "all" {
|
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
builder.WriteString(data["body"].(string))
|
|
|
|
|
builder.WriteString("\n\n")
|
|
|
|
|
builder.WriteString(data["all_headers"].(string))
|
|
|
|
|
itemStr = builder.String()
|
|
|
|
|
} else {
|
|
|
|
|
item, ok := data[partString]
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
itemStr = types.ToString(item)
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
switch matcher.GetType() {
|
|
|
|
|
case matchers.StatusMatcher:
|
|
|
|
|
statusCode, ok := data["status_code"]
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return matcher.Result(matcher.MatchStatusCode(statusCode.(int)))
|
|
|
|
|
case matchers.SizeMatcher:
|
2020-12-25 02:24:55 +05:30
|
|
|
return matcher.Result(matcher.MatchSize(len(itemStr)))
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.WordsMatcher:
|
2020-12-25 02:24:55 +05:30
|
|
|
return matcher.Result(matcher.MatchWords(itemStr))
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.RegexMatcher:
|
2020-12-25 02:24:55 +05:30
|
|
|
return matcher.Result(matcher.MatchRegex(itemStr))
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.BinaryMatcher:
|
2020-12-25 02:24:55 +05:30
|
|
|
return matcher.Result(matcher.MatchBinary(itemStr))
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.DSLMatcher:
|
|
|
|
|
return matcher.Result(matcher.MatchDSL(data))
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract performs extracting operation for a extractor on model and returns true or false.
|
|
|
|
|
func (r *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
|
2020-12-29 11:42:46 +05:30
|
|
|
partString := extractor.Part
|
2020-12-24 20:47:41 +05:30
|
|
|
switch partString {
|
|
|
|
|
case "header":
|
|
|
|
|
partString = "all_headers"
|
|
|
|
|
}
|
2020-12-25 02:24:55 +05:30
|
|
|
|
2021-01-01 17:07:24 +05:30
|
|
|
var itemStr string
|
|
|
|
|
if partString == "all" {
|
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
builder.WriteString(data["body"].(string))
|
|
|
|
|
builder.WriteString("\n\n")
|
|
|
|
|
builder.WriteString(data["all_headers"].(string))
|
|
|
|
|
itemStr = builder.String()
|
|
|
|
|
} else {
|
|
|
|
|
item, ok := data[partString]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
itemStr = types.ToString(item)
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
2020-12-24 20:47:41 +05:30
|
|
|
switch extractor.GetType() {
|
|
|
|
|
case extractors.RegexExtractor:
|
2020-12-25 02:24:55 +05:30
|
|
|
return extractor.ExtractRegex(itemStr)
|
2020-12-24 20:47:41 +05:30
|
|
|
case extractors.KValExtractor:
|
|
|
|
|
return extractor.ExtractKval(data)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 15:51:43 +05:30
|
|
|
// responseToDSLMap converts a HTTP response to a map for use in DSL matching
|
2020-12-29 11:42:46 +05:30
|
|
|
func (r *Request) responseToDSLMap(resp *http.Response, host, matched, rawReq, rawResp, body, headers string, duration time.Duration, extra map[string]interface{}) map[string]interface{} {
|
|
|
|
|
data := make(map[string]interface{}, len(extra)+8+len(resp.Header)+len(resp.Cookies()))
|
2020-12-21 15:51:43 +05:30
|
|
|
for k, v := range extra {
|
|
|
|
|
data[k] = v
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 11:42:46 +05:30
|
|
|
data["host"] = host
|
|
|
|
|
data["matched"] = matched
|
2020-12-29 01:30:07 +05:30
|
|
|
if r.options.Options.JSONRequests {
|
|
|
|
|
data["request"] = rawReq
|
|
|
|
|
data["response"] = rawResp
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 15:51:43 +05:30
|
|
|
data["content_length"] = resp.ContentLength
|
|
|
|
|
data["status_code"] = resp.StatusCode
|
|
|
|
|
|
2020-12-24 01:42:04 +05:30
|
|
|
data["body"] = body
|
2020-12-24 12:13:18 +05:30
|
|
|
for _, cookie := range resp.Cookies() {
|
|
|
|
|
data[cookie.Name] = cookie.Value
|
|
|
|
|
}
|
2020-12-21 15:51:43 +05:30
|
|
|
for k, v := range resp.Header {
|
|
|
|
|
k = strings.ToLower(strings.TrimSpace(strings.ReplaceAll(k, "-", "_")))
|
|
|
|
|
data[k] = strings.Join(v, " ")
|
|
|
|
|
}
|
2020-12-24 12:13:18 +05:30
|
|
|
data["all_headers"] = headers
|
2020-12-21 15:51:43 +05:30
|
|
|
|
|
|
|
|
if r, err := httputil.DumpResponse(resp, true); err == nil {
|
2020-12-24 12:13:18 +05:30
|
|
|
rawString := string(r)
|
|
|
|
|
data["raw"] = rawString
|
2020-12-21 15:51:43 +05:30
|
|
|
}
|
|
|
|
|
data["duration"] = duration.Seconds()
|
|
|
|
|
return data
|
|
|
|
|
}
|
2020-12-29 01:30:07 +05:30
|
|
|
|
|
|
|
|
// makeResultEvent creates a result event from internal wrapped event
|
|
|
|
|
func (r *Request) makeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
|
2020-12-29 11:42:46 +05:30
|
|
|
results := make([]*output.ResultEvent, 0, len(wrapped.OperatorsResult.Matches)+1)
|
2020-12-29 01:30:07 +05:30
|
|
|
|
|
|
|
|
data := output.ResultEvent{
|
|
|
|
|
TemplateID: r.options.TemplateID,
|
|
|
|
|
Info: r.options.TemplateInfo,
|
|
|
|
|
Type: "http",
|
|
|
|
|
Host: wrapped.InternalEvent["host"].(string),
|
|
|
|
|
Matched: wrapped.InternalEvent["matched"].(string),
|
|
|
|
|
Metadata: wrapped.OperatorsResult.PayloadValues,
|
|
|
|
|
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
|
|
|
|
|
}
|
|
|
|
|
if r.options.Options.JSONRequests {
|
|
|
|
|
data.Request = wrapped.InternalEvent["request"].(string)
|
|
|
|
|
data.Response = wrapped.InternalEvent["raw"].(string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have multiple matchers with names, write each of them separately.
|
|
|
|
|
if len(wrapped.OperatorsResult.Matches) > 0 {
|
|
|
|
|
for k := range wrapped.OperatorsResult.Matches {
|
|
|
|
|
data.MatcherName = k
|
|
|
|
|
results = append(results, &data)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
results = append(results, &data)
|
|
|
|
|
}
|
|
|
|
|
return results
|
|
|
|
|
}
|