2020-12-21 15:51:43 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2020-12-24 20:47:41 +05:30
|
|
|
|
2021-07-19 21:04:08 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2021-10-01 16:52:38 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
|
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"
|
2021-10-06 21:53:03 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-10-05 13:56:02 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
|
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
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
|
2021-10-29 19:10:17 +03:00
|
|
|
item, ok := request.getMatchPart(matcher.Part, data)
|
2021-01-01 19:36:21 +05:30
|
|
|
if !ok {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
|
|
|
|
|
2020-12-24 20:47:41 +05:30
|
|
|
switch matcher.GetType() {
|
|
|
|
|
case matchers.StatusMatcher:
|
2021-10-05 13:56:02 +03:00
|
|
|
statusCode, ok := getStatusCode(data)
|
2020-12-24 20:47:41 +05:30
|
|
|
if !ok {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-12-24 20:47:41 +05:30
|
|
|
}
|
2021-10-08 20:18:00 +03:00
|
|
|
return matcher.Result(matcher.MatchStatusCode(statusCode)), []string{responsehighlighter.CreateStatusCodeSnippet(data["response"].(string), statusCode)}
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.SizeMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchSize(len(item))), []string{}
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.WordsMatcher:
|
2021-11-24 22:19:42 +05:30
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(item, data))
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.RegexMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(item))
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.BinaryMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(item))
|
2020-12-24 20:47:41 +05:30
|
|
|
case matchers.DSLMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchDSL(data)), []string{}
|
2020-12-24 20:47:41 +05:30
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2020-12-24 20:47:41 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-05 13:56:02 +03: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 17:31:46 +03:00
|
|
|
// Extract performs extracting operation for an extractor on model and returns true or false.
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
|
2021-10-29 19:10:17 +03:00
|
|
|
item, ok := request.getMatchPart(extractor.Part, data)
|
2021-01-01 19:36:21 +05:30
|
|
|
if !ok {
|
|
|
|
|
return nil
|
2020-12-24 20:47:41 +05:30
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
switch extractor.GetType() {
|
|
|
|
|
case extractors.RegexExtractor:
|
|
|
|
|
return extractor.ExtractRegex(item)
|
|
|
|
|
case extractors.KValExtractor:
|
|
|
|
|
return extractor.ExtractKval(data)
|
2021-08-02 21:43:50 +05:30
|
|
|
case extractors.XPathExtractor:
|
|
|
|
|
return extractor.ExtractHTML(item)
|
2021-07-31 22:49:23 +02:00
|
|
|
case extractors.JSONExtractor:
|
2021-08-01 14:42:04 +02:00
|
|
|
return extractor.ExtractJSON(item)
|
2021-01-01 19:36:21 +05:30
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-12-25 02:24:55 +05:30
|
|
|
|
2021-01-01 19:36:21 +05:30
|
|
|
// getMatchPart returns the match part honoring "all" matchers + others.
|
2021-10-29 19:10:17 +03:00
|
|
|
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
|
2021-11-11 17:30:25 +05:30
|
|
|
if part == "" {
|
|
|
|
|
part = "body"
|
|
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
if part == "header" {
|
|
|
|
|
part = "all_headers"
|
|
|
|
|
}
|
2021-01-01 17:07:24 +05:30
|
|
|
var itemStr string
|
2021-01-01 19:36:21 +05:30
|
|
|
|
|
|
|
|
if part == "all" {
|
2021-01-01 17:07:24 +05:30
|
|
|
builder := &strings.Builder{}
|
2021-02-04 18:29:28 +05:30
|
|
|
builder.WriteString(types.ToString(data["body"]))
|
|
|
|
|
builder.WriteString(types.ToString(data["all_headers"]))
|
2021-01-01 17:07:24 +05:30
|
|
|
itemStr = builder.String()
|
|
|
|
|
} else {
|
2021-01-01 19:36:21 +05:30
|
|
|
item, ok := data[part]
|
2021-01-01 17:07:24 +05:30
|
|
|
if !ok {
|
2021-01-01 19:36:21 +05:30
|
|
|
return "", false
|
2021-01-01 17:07:24 +05:30
|
|
|
}
|
|
|
|
|
itemStr = types.ToString(item)
|
2020-12-25 02:24:55 +05:30
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
return itemStr, true
|
2020-12-24 20:47:41 +05:30
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:31:46 +03:00
|
|
|
// responseToDSLMap converts an HTTP response to a map for use in DSL matching
|
2021-10-12 20:06:55 +03: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 15:51:43 +05:30
|
|
|
for k, v := range extra {
|
|
|
|
|
data[k] = v
|
|
|
|
|
}
|
2020-12-24 12:13:18 +05:30
|
|
|
for _, cookie := range resp.Cookies() {
|
2021-01-12 13:20:46 +05:30
|
|
|
data[strings.ToLower(cookie.Name)] = cookie.Value
|
2020-12-24 12:13:18 +05:30
|
|
|
}
|
2020-12-21 15:51:43 +05:30
|
|
|
for k, v := range resp.Header {
|
2021-02-26 13:13:11 +05:30
|
|
|
k = strings.ToLower(strings.ReplaceAll(strings.TrimSpace(k), "-", "_"))
|
2020-12-21 15:51:43 +05:30
|
|
|
data[k] = strings.Join(v, " ")
|
|
|
|
|
}
|
2021-08-19 15:31:26 +02:00
|
|
|
data["host"] = host
|
2021-11-22 17:53:25 +05:30
|
|
|
data["type"] = request.Type().String()
|
2021-08-19 15:31:26 +02: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 12:13:18 +05:30
|
|
|
data["all_headers"] = headers
|
2020-12-21 15:51:43 +05:30
|
|
|
data["duration"] = duration.Seconds()
|
2021-10-01 14:30:04 +03:00
|
|
|
data["template-id"] = request.options.TemplateID
|
|
|
|
|
data["template-info"] = request.options.TemplateInfo
|
|
|
|
|
data["template-path"] = request.options.TemplatePath
|
2020-12-21 15:51:43 +05:30
|
|
|
return data
|
|
|
|
|
}
|
2020-12-29 01:30:07 +05:30
|
|
|
|
2021-01-13 12:18:56 +05:30
|
|
|
// MakeResultEvent creates a result event from internal wrapped event
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
|
2021-10-06 21:53:03 +03:00
|
|
|
return protocols.MakeDefaultResultEvent(request, wrapped)
|
2021-01-11 21:11:35 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-01 16:52:38 +03:00
|
|
|
func (request *Request) GetCompiledOperators() []*operators.Operators {
|
|
|
|
|
return []*operators.Operators{request.CompiledOperators}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 21:53:03 +03:00
|
|
|
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
|
2021-01-11 21:11:35 +05:30
|
|
|
data := &output.ResultEvent{
|
2021-02-04 18:29:28 +05:30
|
|
|
TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),
|
2021-06-05 18:01:08 +05:30
|
|
|
TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),
|
2021-07-12 17:20:01 +03:00
|
|
|
Info: wrapped.InternalEvent["template-info"].(model.Info),
|
2021-11-22 17:53:25 +05:30
|
|
|
Type: types.ToString(wrapped.InternalEvent["type"]),
|
2021-02-04 18:29:28 +05:30
|
|
|
Host: types.ToString(wrapped.InternalEvent["host"]),
|
|
|
|
|
Matched: types.ToString(wrapped.InternalEvent["matched"]),
|
2020-12-29 01:30:07 +05:30
|
|
|
Metadata: wrapped.OperatorsResult.PayloadValues,
|
|
|
|
|
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
|
2021-02-02 12:10:47 +05:30
|
|
|
Timestamp: time.Now(),
|
2021-11-22 17:53:25 +05:30
|
|
|
MatcherStatus: true,
|
2021-02-04 18:29:28 +05:30
|
|
|
IP: types.ToString(wrapped.InternalEvent["ip"]),
|
2021-09-09 18:53:55 +05:30
|
|
|
Request: types.ToString(wrapped.InternalEvent["request"]),
|
|
|
|
|
Response: types.ToString(wrapped.InternalEvent["response"]),
|
2021-10-15 13:55:50 +05:30
|
|
|
CURLCommand: types.ToString(wrapped.InternalEvent["curl-command"]),
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|
2021-01-11 21:11:35 +05:30
|
|
|
return data
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|