From fc66195b28d09efe1eb5d28c94e2cbfd78e14c26 Mon Sep 17 00:00:00 2001 From: forgedhallpass <13679401+forgedhallpass@users.noreply.github.com> Date: Fri, 8 Oct 2021 20:18:00 +0300 Subject: [PATCH] [feature] Add coloring to debug information #999 * Code review changes --- .../response_highlighter.go | 18 ++++++++---------- v2/pkg/protocols/http/operators.go | 2 +- v2/pkg/protocols/offlinehttp/operators.go | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/v2/pkg/protocols/common/helpers/responsehighlighter/response_highlighter.go b/v2/pkg/protocols/common/helpers/responsehighlighter/response_highlighter.go index 6d185dbdb..1cb914e97 100644 --- a/v2/pkg/protocols/common/helpers/responsehighlighter/response_highlighter.go +++ b/v2/pkg/protocols/common/helpers/responsehighlighter/response_highlighter.go @@ -1,7 +1,7 @@ package responsehighlighter import ( - "fmt" + "strconv" "strings" "github.com/logrusorgru/aurora" @@ -9,10 +9,11 @@ import ( "github.com/projectdiscovery/nuclei/v2/pkg/operators" ) +var colorizer = aurora.NewAurora(true) + func Highlight(operatorResult *operators.Result, response string, noColor bool) string { result := response if operatorResult != nil && !noColor { - colorizer := aurora.NewAurora(true) for _, matches := range operatorResult.Matches { if len(matches) > 0 { for _, currentMatch := range matches { @@ -25,13 +26,10 @@ func Highlight(operatorResult *operators.Result, response string, noColor bool) return result } -func CreateHTTPStatusMatcherSnippets(statusCode int) []string { - httpVersions := []string{"0.9", "1.0", "1.1", "2", "2.0", "3", "3.0"} - var matcherValues = make([]string, 0, len(httpVersions)) - - for _, httpVersion := range httpVersions { - matcherValues = append(matcherValues, fmt.Sprintf("HTTP/%s %d", httpVersion, statusCode)) +func CreateStatusCodeSnippet(response string, statusCode int) string { + if strings.HasPrefix(response, "HTTP/") { + strStatusCode := strconv.Itoa(statusCode) + return response[:strings.Index(response, strStatusCode)+len(strStatusCode)] } - - return matcherValues + return "" } diff --git a/v2/pkg/protocols/http/operators.go b/v2/pkg/protocols/http/operators.go index a53081945..6a8bcfad1 100644 --- a/v2/pkg/protocols/http/operators.go +++ b/v2/pkg/protocols/http/operators.go @@ -28,7 +28,7 @@ func (request *Request) Match(data map[string]interface{}, matcher *matchers.Mat if !ok { return false, []string{} } - return matcher.Result(matcher.MatchStatusCode(statusCode)), responsehighlighter.CreateHTTPStatusMatcherSnippets(statusCode) + return matcher.Result(matcher.MatchStatusCode(statusCode)), []string{responsehighlighter.CreateStatusCodeSnippet(data["response"].(string), statusCode)} case matchers.SizeMatcher: return matcher.Result(matcher.MatchSize(len(item))), []string{} case matchers.WordsMatcher: diff --git a/v2/pkg/protocols/offlinehttp/operators.go b/v2/pkg/protocols/offlinehttp/operators.go index 7789b7e4b..730aa357d 100644 --- a/v2/pkg/protocols/offlinehttp/operators.go +++ b/v2/pkg/protocols/offlinehttp/operators.go @@ -28,7 +28,7 @@ func (request *Request) Match(data map[string]interface{}, matcher *matchers.Mat if !ok { return false, []string{} } - return matcher.Result(matcher.MatchStatusCode(statusCode)), responsehighlighter.CreateHTTPStatusMatcherSnippets(statusCode) + return matcher.Result(matcher.MatchStatusCode(statusCode)), []string{responsehighlighter.CreateStatusCodeSnippet(data["response"].(string), statusCode)} case matchers.SizeMatcher: return matcher.Result(matcher.MatchSize(len(item))), []string{} case matchers.WordsMatcher: