2020-12-21 10:21:43 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-12-24 15:17:41 +00:00
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators/extractors"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers"
|
2020-12-28 20:00:07 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
2020-12-24 20:54:55 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
2020-12-21 10:21:43 +00:00
|
|
|
)
|
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
// Match matches a generic data response again a given matcher
|
|
|
|
func (r *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) bool {
|
2021-01-01 14:06:21 +00:00
|
|
|
item, ok := getMatchPart(matcher.Part, data)
|
|
|
|
if !ok {
|
|
|
|
return false
|
2020-12-24 20:54:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
switch matcher.GetType() {
|
|
|
|
case matchers.StatusMatcher:
|
|
|
|
statusCode, ok := data["status_code"]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2021-03-13 17:50:04 +00:00
|
|
|
status, ok := statusCode.(int)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return matcher.Result(matcher.MatchStatusCode(status))
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.SizeMatcher:
|
2021-01-01 14:06:21 +00:00
|
|
|
return matcher.Result(matcher.MatchSize(len(item)))
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.WordsMatcher:
|
2021-01-01 14:06:21 +00:00
|
|
|
return matcher.Result(matcher.MatchWords(item))
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.RegexMatcher:
|
2021-01-01 14:06:21 +00:00
|
|
|
return matcher.Result(matcher.MatchRegex(item))
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.BinaryMatcher:
|
2021-01-01 14:06:21 +00:00
|
|
|
return matcher.Result(matcher.MatchBinary(item))
|
2020-12-24 15:17:41 +00:00
|
|
|
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{} {
|
2021-01-01 14:06:21 +00:00
|
|
|
item, ok := getMatchPart(extractor.Part, data)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2020-12-24 15:17:41 +00:00
|
|
|
}
|
2021-01-01 14:06:21 +00:00
|
|
|
switch extractor.GetType() {
|
|
|
|
case extractors.RegexExtractor:
|
|
|
|
return extractor.ExtractRegex(item)
|
|
|
|
case extractors.KValExtractor:
|
|
|
|
return extractor.ExtractKval(data)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-24 20:54:55 +00:00
|
|
|
|
2021-01-01 14:06:21 +00:00
|
|
|
// getMatchPart returns the match part honoring "all" matchers + others.
|
|
|
|
func getMatchPart(part string, data output.InternalEvent) (string, bool) {
|
|
|
|
if part == "header" {
|
|
|
|
part = "all_headers"
|
|
|
|
}
|
2021-01-01 11:37:24 +00:00
|
|
|
var itemStr string
|
2021-01-01 14:06:21 +00:00
|
|
|
|
|
|
|
if part == "all" {
|
2021-01-01 11:37:24 +00:00
|
|
|
builder := &strings.Builder{}
|
2021-02-04 12:59:28 +00:00
|
|
|
builder.WriteString(types.ToString(data["body"]))
|
|
|
|
builder.WriteString(types.ToString(data["all_headers"]))
|
2021-01-01 11:37:24 +00:00
|
|
|
itemStr = builder.String()
|
|
|
|
} else {
|
2021-01-01 14:06:21 +00:00
|
|
|
item, ok := data[part]
|
2021-01-01 11:37:24 +00:00
|
|
|
if !ok {
|
2021-01-01 14:06:21 +00:00
|
|
|
return "", false
|
2021-01-01 11:37:24 +00:00
|
|
|
}
|
|
|
|
itemStr = types.ToString(item)
|
2020-12-24 20:54:55 +00:00
|
|
|
}
|
2021-01-01 14:06:21 +00:00
|
|
|
return itemStr, true
|
2020-12-24 15:17:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 10:21:43 +00:00
|
|
|
// responseToDSLMap converts a HTTP response to a map for use in DSL matching
|
2020-12-29 06:12:46 +00:00
|
|
|
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 10:21:43 +00:00
|
|
|
for k, v := range extra {
|
|
|
|
data[k] = v
|
|
|
|
}
|
|
|
|
|
2020-12-29 06:12:46 +00:00
|
|
|
data["host"] = host
|
|
|
|
data["matched"] = matched
|
2021-02-02 06:40:47 +00:00
|
|
|
data["request"] = rawReq
|
|
|
|
data["response"] = rawResp
|
2020-12-21 10:21:43 +00:00
|
|
|
data["content_length"] = resp.ContentLength
|
|
|
|
data["status_code"] = resp.StatusCode
|
2020-12-23 20:12:04 +00:00
|
|
|
data["body"] = body
|
2020-12-24 06:43:18 +00:00
|
|
|
for _, cookie := range resp.Cookies() {
|
2021-01-12 07:50:46 +00:00
|
|
|
data[strings.ToLower(cookie.Name)] = cookie.Value
|
2020-12-24 06:43:18 +00:00
|
|
|
}
|
2020-12-21 10:21:43 +00:00
|
|
|
for k, v := range resp.Header {
|
2021-02-26 07:43:11 +00:00
|
|
|
k = strings.ToLower(strings.ReplaceAll(strings.TrimSpace(k), "-", "_"))
|
2020-12-21 10:21:43 +00:00
|
|
|
data[k] = strings.Join(v, " ")
|
|
|
|
}
|
2020-12-24 06:43:18 +00:00
|
|
|
data["all_headers"] = headers
|
2020-12-21 10:21:43 +00:00
|
|
|
data["duration"] = duration.Seconds()
|
2021-01-13 06:48:56 +00:00
|
|
|
data["template-id"] = r.options.TemplateID
|
|
|
|
data["template-info"] = r.options.TemplateInfo
|
2021-06-05 12:31:08 +00:00
|
|
|
data["template-path"] = r.options.TemplatePath
|
2020-12-21 10:21:43 +00:00
|
|
|
return data
|
|
|
|
}
|
2020-12-28 20:00:07 +00:00
|
|
|
|
2021-01-13 06:48:56 +00:00
|
|
|
// MakeResultEvent creates a result event from internal wrapped event
|
|
|
|
func (r *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
|
2020-12-29 06:12:46 +00:00
|
|
|
results := make([]*output.ResultEvent, 0, len(wrapped.OperatorsResult.Matches)+1)
|
2020-12-28 20:00:07 +00:00
|
|
|
|
2021-01-11 15:41:35 +00:00
|
|
|
// 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 := r.makeResultEventItem(wrapped)
|
|
|
|
data.MatcherName = k
|
|
|
|
results = append(results, data)
|
|
|
|
}
|
|
|
|
} else if len(wrapped.OperatorsResult.Extracts) > 0 {
|
|
|
|
for k, v := range wrapped.OperatorsResult.Extracts {
|
|
|
|
data := r.makeResultEventItem(wrapped)
|
|
|
|
data.ExtractedResults = v
|
|
|
|
data.ExtractorName = k
|
|
|
|
results = append(results, data)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data := r.makeResultEventItem(wrapped)
|
|
|
|
results = append(results, data)
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) makeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
|
|
|
|
data := &output.ResultEvent{
|
2021-02-04 12:59:28 +00:00
|
|
|
TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),
|
2021-06-05 12:31:08 +00:00
|
|
|
TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),
|
2021-02-03 19:39:29 +00:00
|
|
|
Info: wrapped.InternalEvent["template-info"].(map[string]interface{}),
|
2020-12-28 20:00:07 +00:00
|
|
|
Type: "http",
|
2021-02-04 12:59:28 +00:00
|
|
|
Host: types.ToString(wrapped.InternalEvent["host"]),
|
|
|
|
Matched: types.ToString(wrapped.InternalEvent["matched"]),
|
2020-12-28 20:00:07 +00:00
|
|
|
Metadata: wrapped.OperatorsResult.PayloadValues,
|
|
|
|
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
|
2021-02-02 06:40:47 +00:00
|
|
|
Timestamp: time.Now(),
|
2021-02-04 12:59:28 +00:00
|
|
|
IP: types.ToString(wrapped.InternalEvent["ip"]),
|
2020-12-28 20:00:07 +00:00
|
|
|
}
|
|
|
|
if r.options.Options.JSONRequests {
|
2021-02-04 12:59:28 +00:00
|
|
|
data.Request = types.ToString(wrapped.InternalEvent["request"])
|
2021-02-07 20:37:19 +00:00
|
|
|
data.Response = types.ToString(wrapped.InternalEvent["response"])
|
2020-12-28 20:00:07 +00:00
|
|
|
}
|
2021-01-11 15:41:35 +00:00
|
|
|
return data
|
2020-12-28 20:00:07 +00:00
|
|
|
}
|