2021-10-01 14:24:45 +03:00
|
|
|
package responsehighlighter
|
|
|
|
|
|
|
|
|
|
import (
|
2021-11-19 12:15:43 +02:00
|
|
|
"sort"
|
2021-10-08 20:18:00 +03:00
|
|
|
"strconv"
|
2021-10-01 14:24:45 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/logrusorgru/aurora"
|
|
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
|
2021-10-01 14:24:45 +03:00
|
|
|
)
|
|
|
|
|
|
2021-10-30 13:17:47 +03:00
|
|
|
var colorFunction = aurora.Green
|
2021-10-08 20:18:00 +03:00
|
|
|
|
2021-10-30 13:17:47 +03:00
|
|
|
func Highlight(operatorResult *operators.Result, response string, noColor, hexDump bool) string {
|
2021-10-01 14:24:45 +03:00
|
|
|
result := response
|
|
|
|
|
if operatorResult != nil && !noColor {
|
2021-11-19 12:15:43 +02:00
|
|
|
for _, currentMatch := range getSortedMatches(operatorResult) {
|
|
|
|
|
if hexDump {
|
|
|
|
|
highlightedHexDump, err := toHighLightedHexDump(result, currentMatch)
|
|
|
|
|
if err == nil {
|
|
|
|
|
result = highlightedHexDump.String()
|
2021-10-01 14:24:45 +03:00
|
|
|
}
|
2021-11-19 12:15:43 +02:00
|
|
|
} else {
|
|
|
|
|
result = highlightASCII(currentMatch, result)
|
2021-10-01 14:24:45 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
2021-10-05 13:56:02 +03:00
|
|
|
|
2021-11-19 12:15:43 +02:00
|
|
|
func highlightASCII(currentMatch string, result string) string {
|
|
|
|
|
var coloredMatchBuilder strings.Builder
|
|
|
|
|
for _, char := range currentMatch {
|
|
|
|
|
coloredMatchBuilder.WriteString(addColor(string(char)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.ReplaceAll(result, currentMatch, coloredMatchBuilder.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getSortedMatches(operatorResult *operators.Result) []string {
|
|
|
|
|
sortedMatches := make([]string, 0, len(operatorResult.Matches))
|
|
|
|
|
for _, matches := range operatorResult.Matches {
|
|
|
|
|
sortedMatches = append(sortedMatches, matches...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.Slice(sortedMatches, func(i, j int) bool {
|
|
|
|
|
return len(sortedMatches[i]) > len(sortedMatches[j])
|
|
|
|
|
})
|
|
|
|
|
return sortedMatches
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 20:18:00 +03:00
|
|
|
func CreateStatusCodeSnippet(response string, statusCode int) string {
|
|
|
|
|
if strings.HasPrefix(response, "HTTP/") {
|
|
|
|
|
strStatusCode := strconv.Itoa(statusCode)
|
|
|
|
|
return response[:strings.Index(response, strStatusCode)+len(strStatusCode)]
|
2021-10-05 13:56:02 +03:00
|
|
|
}
|
2021-10-08 20:18:00 +03:00
|
|
|
return ""
|
2021-10-05 13:56:02 +03:00
|
|
|
}
|
2021-10-30 13:17:47 +03:00
|
|
|
|
|
|
|
|
func addColor(value string) string {
|
|
|
|
|
return colorFunction(value).String()
|
|
|
|
|
}
|