forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_highlighter.go
64 lines (52 loc) · 1.62 KB
/
response_highlighter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package responsehighlighter
import (
"sort"
"strconv"
"strings"
"github.com/logrusorgru/aurora"
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
)
var colorFunction = aurora.Green
func Highlight(operatorResult *operators.Result, response string, noColor, hexDump bool) string {
result := response
if operatorResult != nil && !noColor {
for _, currentMatch := range getSortedMatches(operatorResult) {
if hexDump {
highlightedHexDump, err := toHighLightedHexDump(result, currentMatch)
if err == nil {
result = highlightedHexDump.String()
}
} else {
result = highlightASCII(currentMatch, result)
}
}
}
return result
}
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
}
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 ""
}
func addColor(value string) string {
return colorFunction(value).String()
}