-
Notifications
You must be signed in to change notification settings - Fork 43
/
auto.go
306 lines (288 loc) · 7.33 KB
/
auto.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package codec
import (
"bytes"
"encoding/binary"
"encoding/json"
"github.com/yaklang/yaklang/common/log"
"html"
"net/url"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"github.com/asaskevich/govalidator"
)
type AutoDecodeResult struct {
Type string
TypeVerbose string
Origin string
Result string
}
var jsonUnicodeRegexp = regexp.MustCompile(`(?i)\\u[\dabcdef]{4}`)
var base64Regexp = regexp.MustCompile(`(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})`)
var urlRegexp = regexp.MustCompile(`%[\da-fA-F]{2}`)
var htmlEntityRegexp = regexp.MustCompile(`^((&[a-zA-Z]+;)|(&#[a-fA-F0-9]+;))+$`)
var hexRegexp = regexp.MustCompile(`^\b([0-9A-Fa-f]{2}\s?)+[0-9A-Fa-f]{2}\b$`)
var base32Regexp = regexp.MustCompile(`^([A-Z2-7]{8})*([A-Z2-7]{8}|[A-Z2-7]{2}([A-Z2-7]{6})*|[A-Z2-7]{4}([A-Z2-7]{4})*|[A-Z2-7]{5}([A-Z2-7]{3})*|[A-Z2-7]{7})(=){0,6}$`)
var encodeMap = map[string]func(string) string{
"UrlDecode": func(s string) string {
return EncodeUrlCode(s)
},
"Html Entity Decode": html.EscapeString,
"Hex Decode": func(s string) string {
return EncodeToHex(s)
},
"Json Unicode Decode": func(s string) string {
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(s)
if err != nil {
log.Errorf("json encode error: %v", err)
return ""
}
return buf.String()
},
"Base64 Decode": func(s string) string {
return EncodeBase64(s)
},
"Base32 Decode": func(s string) string {
return EncodeBase32(s)
},
"Base64Url Decode": func(s string) string {
return EncodeBase64Url(s)
},
"Base64Utf8 Decode": func(s string) string {
byt, err := Utf8ToGB18030([]byte(s))
if err != nil {
return ""
}
return string(byt)
},
"jwt": func(s string) string {
blocks := strings.Split(s, ".")
encodeBlocks := []string{}
for _, i := range blocks {
encodeBlocks = append(encodeBlocks, EncodeBase64(i))
}
return strings.Join(encodeBlocks, ".")
},
}
func EncodeByType(t string, i interface{}) string {
if f, ok := encodeMap[t]; ok {
return f(AnyToString(i))
}
return ""
}
func AutoDecode(i interface{}) []*AutoDecodeResult {
rawBytes := interfaceToBytes(i)
rawStr := string(rawBytes)
var results []*AutoDecodeResult
var origin = rawStr
for i := 0; i < 100; i++ {
// urlencode
if r := urlRegexp.MatchString(rawStr); r {
rawStr, _ = url.QueryUnescape(rawStr)
if rawStr != "" {
results = append(results, &AutoDecodeResult{
Type: "UrlDecode",
TypeVerbose: "URL解码",
Origin: origin,
Result: rawStr,
})
origin = rawStr
continue
}
}
// html entity encode
if r := htmlEntityRegexp.MatchString(rawStr); r {
rawStr = html.UnescapeString(rawStr)
if rawStr != "" {
results = append(results, &AutoDecodeResult{
Type: "Html Entity Decode",
TypeVerbose: "Html Entity 解码",
Origin: origin,
Result: rawStr,
})
origin = rawStr
continue
}
}
// hex
if hexRegexp.MatchString(rawStr) {
rawStr = hexRegexp.ReplaceAllStringFunc(rawStr, func(s string) string {
result, err := DecodeHex(strings.ReplaceAll(s, " ", ""))
if err != nil {
return s
}
for _, ch := range []rune(string(result)) {
if !strconv.IsPrint(ch) {
return s
}
}
return EscapeInvalidUTF8Byte(result)
})
if rawStr != "" && rawStr != origin {
results = append(results, &AutoDecodeResult{
Type: "Hex Decode",
TypeVerbose: "Hex 解码",
Origin: origin,
Result: rawStr,
})
origin = rawStr
continue
}
}
// json-unicode
if r := jsonUnicodeRegexp.MatchString(rawStr); r {
rawStr = jsonUnicodeRegexp.ReplaceAllStringFunc(rawStr, func(s string) string {
number, err := DecodeHex(strings.TrimLeft(s, "\\u"))
if err != nil {
return s
}
return string(rune(binary.BigEndian.Uint16(number)))
})
if rawStr != "" {
results = append(results, &AutoDecodeResult{
Type: "Json Unicode Decode",
TypeVerbose: "Json Unicode 解码",
Origin: origin,
Result: rawStr,
})
origin = rawStr
continue
}
}
// base64
if govalidator.IsBase64(rawStr) {
rawStr = base64Regexp.ReplaceAllStringFunc(rawStr, func(s string) string {
result, err := DecodeBase64(s)
if err != nil {
return s
}
for _, ch := range []rune(string(result)) {
if !strconv.IsPrint(ch) {
return s
}
}
return EscapeInvalidUTF8Byte(result)
})
if rawStr != "" && rawStr != origin {
results = append(results, &AutoDecodeResult{
Type: "Base64 Decode",
TypeVerbose: "Base64 解码",
Origin: origin,
Result: rawStr,
})
origin = rawStr
continue
}
}
// base32
if base32Regexp.MatchString(rawStr) {
rawStr = base32Regexp.ReplaceAllStringFunc(rawStr, func(s string) string {
result, err := DecodeBase32(s)
if err != nil {
return s
}
for _, ch := range []rune(string(result)) {
if !strconv.IsPrint(ch) {
return s
}
}
return EscapeInvalidUTF8Byte(result)
})
if rawStr != "" && rawStr != origin {
results = append(results, &AutoDecodeResult{
Type: "Base32 Decode",
TypeVerbose: "Base32 解码",
Origin: origin,
Result: rawStr,
})
origin = rawStr
continue
}
}
// base64 with urlencode
decodedByBas64, err := DecodeBase64Url(rawStr)
if len(decodedByBas64) > 0 && err == nil {
if utf8.Valid(decodedByBas64) {
results = append(results, &AutoDecodeResult{
Type: "Base64Url Decode",
TypeVerbose: "Base64 解码",
Origin: origin,
Result: string(decodedByBas64),
})
origin = rawStr
rawStr = string(decodedByBas64)
continue
}
decoded, err := GB18030ToUtf8(decodedByBas64)
if err == nil && len(decoded) > 0 {
results = append(results, &AutoDecodeResult{
Type: "Base64Utf8 Decode",
TypeVerbose: "Base64 解码(UTF8-Invalid)",
Origin: origin,
Result: EscapeInvalidUTF8Byte(decodedByBas64),
})
origin = rawStr
results = append(results, &AutoDecodeResult{
Type: "GB(K/18030) Decode",
TypeVerbose: "GB(K/18030) 解码",
Origin: origin,
Result: EscapeInvalidUTF8Byte(decoded),
})
origin = rawStr
rawStr = string(decoded)
continue
}
}
// jwt
if strings.Count(rawStr, ".") > 1 {
var blocks = strings.Split(rawStr, ".")
var buf bytes.Buffer
var failed = false
for index, i := range blocks {
base64Decoded, _ := DecodeBase64(i)
if len(base64Decoded) <= 0 {
failed = true
break
}
if govalidator.IsPrintableASCII(string(base64Decoded)) {
buf.WriteString(EscapeInvalidUTF8Byte(base64Decoded))
} else {
buf.WriteString(i)
}
if index != len(blocks)-1 {
buf.WriteByte('.')
}
}
if failed {
continue
}
rawStr = buf.String()
if rawStr != "" && rawStr != origin {
results = append(results, &AutoDecodeResult{
Type: "jwt",
TypeVerbose: "JWT",
Origin: origin,
Result: rawStr,
})
origin = rawStr
continue
}
}
if rawStr == origin {
break
}
}
if len(results) <= 0 {
return []*AutoDecodeResult{
{
Type: "No",
TypeVerbose: "无编码",
Origin: rawStr,
Result: rawStr,
},
}
}
return results
}