nuclei/v2/pkg/protocols/http/operators.go

159 lines
5.5 KiB
Go
Raw Normal View History

package http
import (
"net/http"
"strings"
"time"
2020-12-24 15:17:41 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/model"
"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"
"github.com/projectdiscovery/nuclei/v2/pkg/output"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
)
2020-12-24 15:17:41 +00:00
// Match matches a generic data response again a given matcher
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
item, ok := request.getMatchPart(matcher.Part, data)
if !ok {
return false, []string{}
}
2020-12-24 15:17:41 +00:00
switch matcher.GetType() {
case matchers.StatusMatcher:
statusCode, ok := getStatusCode(data)
2020-12-24 15:17:41 +00:00
if !ok {
return false, []string{}
2020-12-24 15:17:41 +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:
return matcher.Result(matcher.MatchSize(len(item))), []string{}
2020-12-24 15:17:41 +00:00
case matchers.WordsMatcher:
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(item, data))
2020-12-24 15:17:41 +00:00
case matchers.RegexMatcher:
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(item))
2020-12-24 15:17:41 +00:00
case matchers.BinaryMatcher:
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(item))
2020-12-24 15:17:41 +00:00
case matchers.DSLMatcher:
return matcher.Result(matcher.MatchDSL(data)), []string{}
2020-12-24 15:17:41 +00:00
}
return false, []string{}
2020-12-24 15:17:41 +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.
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
item, ok := request.getMatchPart(extractor.Part, data)
if !ok {
return nil
2020-12-24 15:17:41 +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)
}
return nil
}
// getMatchPart returns the match part honoring "all" matchers + others.
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
2021-11-11 12:00:25 +00:00
if part == "" {
part = "body"
}
if part == "header" {
part = "all_headers"
}
var itemStr string
if part == "all" {
builder := &strings.Builder{}
builder.WriteString(types.ToString(data["body"]))
builder.WriteString(types.ToString(data["all_headers"]))
itemStr = builder.String()
} else {
item, ok := data[part]
if !ok {
return "", false
}
itemStr = types.ToString(item)
}
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
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()))
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
}
for k, v := range resp.Header {
2021-02-26 07:43:11 +00:00
k = strings.ToLower(strings.ReplaceAll(strings.TrimSpace(k), "-", "_"))
data[k] = strings.Join(v, " ")
}
data["host"] = host
data["type"] = request.Type().String()
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
data["duration"] = duration.Seconds()
data["template-id"] = request.options.TemplateID
data["template-info"] = request.options.TemplateInfo
data["template-path"] = request.options.TemplatePath
return data
}
// MakeResultEvent creates a result event from internal wrapped event
func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
return protocols.MakeDefaultResultEvent(request, wrapped)
2021-01-11 15:41:35 +00:00
}
func (request *Request) GetCompiledOperators() []*operators.Operators {
return []*operators.Operators{request.CompiledOperators}
}
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
2021-01-11 15:41:35 +00:00
data := &output.ResultEvent{
TemplateID: types.ToString(wrapped.InternalEvent["template-id"]),
2021-06-05 12:31:08 +00:00
TemplatePath: types.ToString(wrapped.InternalEvent["template-path"]),
Info: wrapped.InternalEvent["template-info"].(model.Info),
Type: types.ToString(wrapped.InternalEvent["type"]),
Host: types.ToString(wrapped.InternalEvent["host"]),
Matched: types.ToString(wrapped.InternalEvent["matched"]),
Metadata: wrapped.OperatorsResult.PayloadValues,
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
Timestamp: time.Now(),
MatcherStatus: true,
IP: types.ToString(wrapped.InternalEvent["ip"]),
Request: types.ToString(wrapped.InternalEvent["request"]),
Response: types.ToString(wrapped.InternalEvent["response"]),
CURLCommand: types.ToString(wrapped.InternalEvent["curl-command"]),
}
2021-01-11 15:41:35 +00:00
return data
}