-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex.go
More file actions
48 lines (44 loc) · 1.04 KB
/
hex.go
File metadata and controls
48 lines (44 loc) · 1.04 KB
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
package main
import (
"encoding/hex"
"log"
"strings"
)
// de-$HEX[] lines
func checkForHex(line string) string {
if strings.HasSuffix(line, "\r") {
line = strings.TrimSuffix(line, "\r")
}
if !strings.HasPrefix(line, "$HEX[") {
return line
}
// check for trailing bracket ]
if !strings.HasSuffix(line, "]") {
line += "]"
}
// extract text inside brackets
start := strings.Index(line, "[")
end := strings.LastIndex(line, "]")
hexContent := line[start+1 : end]
// try to decode
lineDecode, err := hex.DecodeString(hexContent)
if err != nil {
// strip invalid chars (here we go again trying defoobar crappy wordlists)
cleaned := strings.Map(func(r rune) rune {
if strings.ContainsRune("0123456789abcdefABCDEF", r) {
return r
}
return -1
}, hexContent)
// pad to even length if needed (2x foobar award)
if len(cleaned)%2 != 0 {
cleaned = "0" + cleaned
}
lineDecode, err = hex.DecodeString(cleaned)
if err != nil {
log.Printf("hex decode failed: %v", err)
return line
}
}
return string(lineDecode)
}