2020-12-21 10:21:43 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-12-24 15:17:41 +00:00
|
|
|
|
2021-07-19 18:04:08 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2021-10-01 13:52:38 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
|
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"
|
2021-10-06 18:53:03 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-10-05 10:56:02 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
|
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
|
2021-10-01 11:30:04 +00:00
|
|
|
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
|
2021-10-29 16:10:17 +00:00
|
|
|
item, ok := request.getMatchPart(matcher.Part, data)
|
2022-01-13 07:52:43 +00:00
|
|
|
if !ok && matcher.Type.MatcherType != matchers.DSLMatcher {
|
2021-09-29 16:43:46 +00:00
|
|
|
return false, []string{}
|
2020-12-24 20:54:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 15:17:41 +00:00
|
|
|
switch matcher.GetType() {
|
|
|
|
case matchers.StatusMatcher:
|
2021-10-05 10:56:02 +00:00
|
|
|
statusCode, ok := getStatusCode(data)
|
2020-12-24 15:17:41 +00:00
|
|
|
if !ok {
|
2021-09-29 16:43:46 +00:00
|
|
|
return false, []string{}
|
2020-12-24 15:17:41 +00:00
|
|
|
}
|
2021-10-08 17:18:00 +00:00
|
|
|
return matcher.Result(matcher.MatchStatusCode(statusCode)), []string{responsehighlighter.CreateStatusCodeSnippet(data["response"].(string), statusCode)}
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.SizeMatcher:
|
2021-09-29 16:43:46 +00:00
|
|
|
return matcher.Result(matcher.MatchSize(len(item))), []string{}
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.WordsMatcher:
|
2021-11-24 16:49:42 +00:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(item, data))
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.RegexMatcher:
|
2021-09-29 16:43:46 +00:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(item))
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.BinaryMatcher:
|
2021-09-29 16:43:46 +00:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(item))
|
2020-12-24 15:17:41 +00:00
|
|
|
case matchers.DSLMatcher:
|
2021-09-29 16:43:46 +00:00
|
|
|
return matcher.Result(matcher.MatchDSL(data)), []string{}
|
2020-12-24 15:17:41 +00:00
|
|
|
}
|
2021-09-29 16:43:46 +00:00
|
|
|
return false, []string{}
|
2020-12-24 15:17:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 10:56:02 +00:00
|
|
|
func getStatusCode(data map[string]interface{}) (int, bool) {
|
|
|
|
statusCodeValue, ok := data["status_code"]
|
|
|
|
if !ok {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
statusCode, ok := statusCodeValue.(int)
|
|
|
|
if !ok {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return statusCode, true
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:31:46 +00:00
|
|
|
// Extract performs extracting operation for an extractor on model and returns true or false.
|
2021-10-01 11:30:04 +00:00
|
|
|
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
|
2021-10-29 16:10:17 +00:00
|
|
|
item, ok := request.getMatchPart(extractor.Part, data)
|
2022-01-13 07:52:43 +00:00
|
|
|
if !ok && extractor.Type.ExtractorType != extractors.KValExtractor {
|
2021-01-01 14:06:21 +00:00
|
|
|
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)
|
2021-08-02 16:13:50 +00:00
|
|
|
case extractors.XPathExtractor:
|
|
|
|
return extractor.ExtractHTML(item)
|
2021-07-31 20:49:23 +00:00
|
|
|
case extractors.JSONExtractor:
|
2021-08-01 12:42:04 +00:00
|
|
|
return extractor.ExtractJSON(item)
|
2021-01-01 14:06:21 +00:00
|
|
|
}
|
|
|
|
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.
|
2021-10-29 16:10:17 +00:00
|
|
|
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
|
2021-11-11 12:00:25 +00:00
|
|
|
if part == "" {
|
|
|
|
part = "body"
|
|
|
|
}
|
2021-01-01 14:06:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-07 14:31:46 +00:00
|
|
|
// responseToDSLMap converts an HTTP response to a map for use in DSL matching
|
2021-10-12 17:06:55 +00:00
|
|
|
func (request *Request) responseToDSLMap(resp *http.Response, host, matched, rawReq, rawResp, body, headers string, duration time.Duration, extra map[string]interface{}) output.InternalEvent {
|
|
|
|
data := make(output.InternalEvent, 12+len(extra)+len(resp.Header)+len(resp.Cookies()))
|
2020-12-21 10:21:43 +00:00
|
|
|
for k, v := range extra {
|
|
|
|
data[k] = v
|
|
|
|
}
|
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, " ")
|
|
|
|
}
|
2021-08-19 13:31:26 +00:00
|
|
|
data["host"] = host
|
2021-11-22 12:23:25 +00:00
|
|
|
data["type"] = request.Type().String()
|
2021-08-19 13:31:26 +00:00
|
|
|
data["matched"] = matched
|
|
|
|
data["request"] = rawReq
|
|
|
|
data["response"] = rawResp
|
|
|
|
data["status_code"] = resp.StatusCode
|
|
|
|
data["body"] = body
|
|
|
|
data["content_length"] = resp.ContentLength
|
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-10-01 11:30:04 +00:00
|
|
|
data["template-id"] = request.options.TemplateID
|
|
|
|
data["template-info"] = request.options.TemplateInfo
|
|
|
|
data["template-path"] = request.options.TemplatePath
|
2021-12-22 16:12:21 +00:00
|
|
|
|
|
|
|
if request.StopAtFirstMatch || request.options.StopAtFirstMatch {
|
|
|
|
data["stop-at-first-match"] = true
|
|
|
|
}
|
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
|
2021-10-01 11:30:04 +00:00
|
|
|
func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
|
2021-10-06 18:53:03 +00:00
|
|
|
return protocols.MakeDefaultResultEvent(request, wrapped)
|
2021-01-11 15:41:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 13:52:38 +00:00
|
|
|
func (request *Request) GetCompiledOperators() []*operators.Operators {
|
|
|
|
return []*operators.Operators{request.CompiledOperators}
|
|
|
|
}
|
|
|
|
|
2021-10-06 18:53:03 +00:00
|
|
|
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
|
2021-01-11 15:41:35 +00:00
|
|
|
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-07-12 14:20:01 +00:00
|
|
|
Info: wrapped.InternalEvent["template-info"].(model.Info),
|
2021-11-22 12:23:25 +00:00
|
|
|
Type: types.ToString(wrapped.InternalEvent["type"]),
|
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-11-22 12:23:25 +00:00
|
|
|
MatcherStatus: true,
|
2021-02-04 12:59:28 +00:00
|
|
|
IP: types.ToString(wrapped.InternalEvent["ip"]),
|
2021-09-09 13:23:55 +00:00
|
|
|
Request: types.ToString(wrapped.InternalEvent["request"]),
|
|
|
|
Response: types.ToString(wrapped.InternalEvent["response"]),
|
2021-10-15 08:25:50 +00:00
|
|
|
CURLCommand: types.ToString(wrapped.InternalEvent["curl-command"]),
|
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
|
|
|
}
|