2021-02-06 00:36:43 +05:30
|
|
|
package offlinehttp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
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"
|
2021-02-06 00:36:43 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators/extractors"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers"
|
|
|
|
|
"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"
|
2021-02-06 00:36:43 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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-02-06 00:36:43 +05:30
|
|
|
item, ok := getMatchPart(matcher.Part, data)
|
|
|
|
|
if !ok {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2021-02-06 00:36:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch matcher.GetType() {
|
|
|
|
|
case matchers.StatusMatcher:
|
2021-10-05 13:56:02 +03:00
|
|
|
statusCode, ok := getStatusCode(data)
|
2021-02-06 00:36:43 +05:30
|
|
|
if !ok {
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2021-02-06 00:36:43 +05:30
|
|
|
}
|
2021-10-05 13:56:02 +03:00
|
|
|
return matcher.Result(matcher.MatchStatusCode(statusCode)), responsehighlighter.CreateHTTPStatusMatcherSnippets(statusCode)
|
2021-02-06 00:36:43 +05:30
|
|
|
case matchers.SizeMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchSize(len(item))), []string{}
|
2021-02-06 00:36:43 +05:30
|
|
|
case matchers.WordsMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(item, nil))
|
2021-02-06 00:36:43 +05:30
|
|
|
case matchers.RegexMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(item))
|
2021-02-06 00:36:43 +05:30
|
|
|
case matchers.BinaryMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(item))
|
2021-02-06 00:36:43 +05:30
|
|
|
case matchers.DSLMatcher:
|
2021-09-29 19:43:46 +03:00
|
|
|
return matcher.Result(matcher.MatchDSL(data)), []string{}
|
2021-02-06 00:36:43 +05:30
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
return false, []string{}
|
2021-02-06 00:36:43 +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-02-06 00:36:43 +05:30
|
|
|
item, ok := getMatchPart(extractor.Part, data)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
switch extractor.GetType() {
|
|
|
|
|
case extractors.RegexExtractor:
|
|
|
|
|
return extractor.ExtractRegex(item)
|
|
|
|
|
case extractors.KValExtractor:
|
|
|
|
|
return extractor.ExtractKval(data)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getMatchPart returns the match part honoring "all" matchers + others.
|
|
|
|
|
func getMatchPart(part string, data output.InternalEvent) (string, bool) {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 17:31:46 +03:00
|
|
|
// responseToDSLMap converts an HTTP response to a map for use in DSL matching
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) responseToDSLMap(resp *http.Response, host, matched, rawReq, rawResp, body, headers string, duration time.Duration, extra map[string]interface{}) map[string]interface{} {
|
2021-02-06 00:36:43 +05:30
|
|
|
data := make(map[string]interface{}, len(extra)+8+len(resp.Header)+len(resp.Cookies()))
|
|
|
|
|
for k, v := range extra {
|
|
|
|
|
data[k] = v
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 12:14:46 +05:30
|
|
|
data["path"] = host
|
2021-02-06 00:36:43 +05:30
|
|
|
data["matched"] = matched
|
|
|
|
|
data["request"] = rawReq
|
|
|
|
|
data["response"] = rawResp
|
|
|
|
|
data["content_length"] = resp.ContentLength
|
|
|
|
|
data["status_code"] = resp.StatusCode
|
|
|
|
|
data["body"] = body
|
|
|
|
|
for _, cookie := range resp.Cookies() {
|
|
|
|
|
data[strings.ToLower(cookie.Name)] = cookie.Value
|
|
|
|
|
}
|
|
|
|
|
for k, v := range resp.Header {
|
|
|
|
|
k = strings.ToLower(strings.TrimSpace(k))
|
|
|
|
|
data[k] = strings.Join(v, " ")
|
|
|
|
|
}
|
|
|
|
|
data["all_headers"] = headers
|
|
|
|
|
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
|
2021-02-06 00:36:43 +05:30
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-02-06 00:36:43 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-01 16:52:38 +03:00
|
|
|
func (request *Request) GetCompiledOperators() []*operators.Operators {
|
|
|
|
|
return request.compiledOperators
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 21:53:03 +03:00
|
|
|
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
|
2021-02-06 00:36:43 +05:30
|
|
|
data := &output.ResultEvent{
|
|
|
|
|
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-02-06 00:36:43 +05:30
|
|
|
Type: "http",
|
2021-03-05 19:25:09 +05:30
|
|
|
Path: types.ToString(wrapped.InternalEvent["path"]),
|
2021-02-06 00:36:43 +05:30
|
|
|
Matched: types.ToString(wrapped.InternalEvent["matched"]),
|
|
|
|
|
Metadata: wrapped.OperatorsResult.PayloadValues,
|
|
|
|
|
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
|
|
|
|
|
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["raw"]),
|
2021-02-06 00:36:43 +05:30
|
|
|
}
|
|
|
|
|
return data
|
|
|
|
|
}
|