/
obfuscation.go
388 lines (344 loc) · 10.5 KB
/
obfuscation.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Package pakkero will pack, compress and encrypt any type of executable.
Obfuscation library
*/
package pakkero
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io/ioutil"
"regexp"
"strings"
)
// Secrets are the group of strings that we want to obfuscate.
var Secrets = map[string][]string{}
// LauncherStub Stub of the Launcher.go, put here during compilation time.
var LauncherStub = "unset"
var extras = []string{
// ELF Headers
".gopclntab",
".go.buildinfo",
".noptrdata",
".noptrbss",
".data",
".rodata",
".text",
".itablink",
".shstrtab",
".data",
".dynamic",
".dynstr",
".dynsym",
".gnu.version_r",
".gopclntab",
".got.plt",
".init_array",
".interp",
".itablink",
".rela.dyn",
".rela.plt",
".tbss",
".plt",
".init",
// internal golang
"name", "runtime", "command", "cmd",
"ptr", "process", "unicode", "main",
"path", "get", "reflect", "context",
"debug", "fmt", "sync", "sort",
"size", "heap", "fatal", "call",
"fixed", "slice", "bit", "file",
"read", "write", "buffer", "encrypt",
"decrypt", "hash", "state",
"external", "internal", "float",
// anti debug traces
"env", "trace", "pid",
}
/*
StripUPXHeaders will ensure no trace of UPX headers are left
so that reversing will be more challenging and break
simple attempts like "upx -d" in case of compression.
*/
func StripUPXHeaders(infile string) bool {
// Bit sequence of UPX copyright and header infos
header := []string{
`\x49\x6e\x66\x6f\x3a\x20\x54\x68\x69\x73`,
`\x20\x66\x69\x6c\x65\x20\x69\x73\x20\x70`,
`\x61\x63\x6b\x65\x64\x20\x77\x69\x74\x68`,
`\x20\x74\x68\x65\x20\x55\x50\x58\x20\x65`,
`\x78\x65\x63\x75\x74\x61\x62\x6c\x65\x20`,
`\x70\x61\x63\x6b\x65\x72\x20\x68\x74\x74`,
`\x70\x3a\x2f\x2f\x75\x70\x78\x2e\x73\x66`,
`\x2e\x6e\x65\x74\x20\x24\x0a\x00\x24\x49`,
`\x64\x3a\x20\x55\x50\x58\x20\x33\x2e\x39`,
`\x36\x20\x43\x6f\x70\x79\x72\x69\x67\x68`,
`\x74\x20\x28\x43\x29\x20\x31\x39\x39\x36`,
`\x2d\x32\x30\x32\x30\x20\x74\x68\x65\x20`,
`\x55\x50\x58\x20\x54\x65\x61\x6d\x2e\x20`,
`\x41\x6c\x6c\x20\x52\x69\x67\x68\x74\x73`,
`\x20\x52\x65\x73\x65\x72\x76\x65\x64\x2e`,
`\x55\x50\x58\x21`,
}
result := true
for _, v := range header {
sedString := ""
// generate random byte sequence
replace := make([]byte, 1)
for len(sedString) < len(v) {
_, err := rand.Read(replace)
if err != nil {
return false
}
sedString += `\x` + hex.EncodeToString(replace)
}
// replace UPX sequence with random garbage
result = ExecCommand("sed", []string{"-i", `s/` + v + `/` + sedString + `/g`, infile})
if !result {
return result
}
}
return result
}
/*
StripFile will strip out all unneeded headers from and ELF
file in input.
*/
func StripFile(infile string, launcherFile string) bool {
// strip symbols and headers
if !ExecCommand("strip",
[]string{
"-sxX",
"--remove-section=.bss",
"--remove-section=.comment",
"--remove-section=.eh_frame",
"--remove-section=.eh_frame_hdr",
"--remove-section=.fini",
"--remove-section=.fini_array",
"--remove-section=.gnu.build.attributes",
"--remove-section=.gnu.hash",
"--remove-section=.gnu.version",
"--remove-section=.gosymtab",
"--remove-section=.got",
"--remove-section=.note.ABI-tag",
"--remove-section=.note.gnu.build-id",
"--remove-section=.shstrtab",
"--remove-section=.typelink",
infile,
}) {
return false
}
// ------------------------------------------------------------------------
// proceede with manual
// stripping of golang builtins and keyWords strings
removeStrings := []string{}
removeStrings = append(removeStrings, extras...)
// anonymize the launcherFile string to hide the original launcher file name
removeStrings = append(removeStrings, launcherFile)
// deduplicate
removeStrings = Unique(removeStrings)
// read file to string
byteContent, err := ioutil.ReadFile(infile)
if err != nil {
return false
}
input := string(byteContent)
for _, remove := range removeStrings {
// generate new random string to place instead
newName := GenerateNullString(len(remove))
input = strings.ReplaceAll(input, remove, newName)
input = strings.ReplaceAll(input, strings.Title(remove), newName)
}
// save.
err = ioutil.WriteFile(infile, []byte(input), 0600)
// ------------------------------------------------------------------------
return err == nil
}
/*
GenerateTyposquatName is a typosquat name generator
based on a length (128 default) this will create a random
uniqe string composed only of letters and zeroes that are lookalike.
*/
func GenerateTyposquatName(length int) string {
// We divide between an alphabet with number
// and one without, because function/variable names
// must not start with a number.
letterRunes := []rune("OÓÕÔÒÖŌŎŐƠΘΟ")
mixedRunes := []rune("0OÓÕÔÒÖŌŎŐƠΘΟ")
b := make([]rune, length)
// ensure we do not start with a number or we will break code.
b[0] = letterRunes[Random(0, int64(len(mixedRunes)-1))]
for i := range b {
if i != 0 {
b[i] = mixedRunes[Random(0, int64(len(mixedRunes)-1))]
}
}
return string(b)
}
/*
ObfuscateFuncVars will:
- extract all obfuscation-enabled func and var names:
- those start with "ob*" and will be listed
- for each matching string generate a typosquatted random string and
replace all string with that
*/
func ObfuscateFuncVars(input string) string {
// obfuscate functions and variables names
regex := regexp.MustCompile(`\bob[a-zA-Z0-9_]+`)
words := regex.FindAllString(input, -1)
words = ReverseStringArray(words)
words = Unique(words)
for _, w := range words {
// generate random name for each matching string
input = strings.ReplaceAll(input, w, GenerateTyposquatName(128))
}
return input
}
/*
GenerateStringFunc will hide a string creating a function that returns
that value as a string encoded with a series of length calculation of randomic
string arrays generated.
*/
func GenerateStringFunc(txt string, function string) string {
resString := "{"
for _, b := range []byte(txt) {
resString += "\"" + GenerateTyposquatName(int(b)) + "\"" + ","
}
// remove last comma
resString = strings.TrimSuffix(resString, ",")
// close array
resString += "}"
result := fmt.Sprintf("func " +
function +
"() string {\nobRegistered := []string" + resString +
"\nvar obResult []byte\nfor _,obValue := range obRegistered " +
"{\nobResult = append(obResult, byte(len([]rune(obValue))))\n}\nreturn string(obResult)\n}")
return result
}
/*
ObfuscateStrings will extract all plaintext strings denotet with
backticks and obfuscate them using byteshift wise operations.
*/
func ObfuscateStrings(input string) string {
// parse the launcher file to create the list of imports in it
imports := strings.Index(input, "import (")
endimports := strings.Index(input[imports:], ")")
// import section
importSection := input[:imports+endimports+1]
// the rest of the program
body := input[imports+endimports+1:]
// various types of string delimiter
tickTypes := []string{"`", `'`, `"`}
// for each ticktype, try to get all the strings and
// obfuscate them using functions
for _, v := range tickTypes {
regex := regexp.MustCompile(v + ".*?" + v)
words := regex.FindAllString(body, -1)
words = Unique(words)
for _, w := range words {
// string not void, accounting for quotes
if len(w) > 2 && !strings.Contains(w, `\`) {
// add string to the secrets! if not present
_, present := Secrets[w]
if !present {
secret := w[1 : len(w)-1]
Secrets[w] = []string{secret, GenerateTyposquatName(128)}
}
}
}
}
// create function call
funcString := ""
// replace all secrects with the respective obfuscated string
for k, w := range Secrets {
// in case we manually added some secrets that we want to leave
if !strings.Contains(w[1], "leave") {
funcString = funcString + GenerateStringFunc(w[0], w[1]) + "\n"
body = strings.ReplaceAll(body, k, w[1]+"()")
} else {
body = strings.ReplaceAll(body, k, w[0])
}
}
// reconstruct the program correctly and
// insert all the functions before the main
body = body + "\n" + funcString
// join back with the import section
return importSection + body
}
/*
GenerateRandomAntiDebug will Insert random order of anti-debug check
together with inline compilation to induce big number
of instructions in random order.
*/
func GenerateRandomAntiDebug(input string) string {
lines := strings.Split(input, "\n")
randomChecks := []string{
`obDependencyCheck()`,
`obEnvArgsDetect()`,
`obParentTracerDetect()`,
`obParentCmdLineDetect()`,
`obEnvDetect()`,
`obEnvParentDetect() `,
`obLdPreloadDetect()`,
`obParentDetect()`,
}
var obfile []string
// find OB_CHECK and put the checks there.
for _, v := range lines {
if strings.Contains(v, "//") {
if strings.Contains(v, "// OB_CHECK") {
threadString := ""
checkString := ""
// randomize order of check to replace
for j, v := range ShuffleSlice(randomChecks) {
threadString = threadString + "go " + v + ";"
checkString += v
if j != (len(randomChecks) - 1) {
checkString += `||`
}
}
// add action in case of failed check
obfile = append(obfile, threadString)
continue
} else {
// remove comment, this is more to scramble the line numbers
// and make error reporting less accurate
continue
}
}
obfile = append(obfile, v)
}
// back to single string
return strings.Join(obfile, "\n")
}
/*
ObfuscateLauncher the go code of the runner before compiling it.
Basic techniques are applied:
- GenerateRandomAntiDebug
- ObfuscateStrings
- ObfuscateFuncVars.
*/
func ObfuscateLauncher(infile string) error {
byteContent, err := ioutil.ReadFile(infile)
if err != nil {
return fmt.Errorf("failed to read input file: %w", err)
}
content := string(byteContent)
// ------------------------------------------------------------------------
// --- Start anti-debug checks
content = GenerateRandomAntiDebug(content)
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// --- Start string obfuscation
content = ObfuscateStrings(content)
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// --- Start function name obfuscation
content = ObfuscateFuncVars(content)
// ------------------------------------------------------------------------
// save.
err = ioutil.WriteFile(infile, []byte(content), 0600)
if err != nil {
return fmt.Errorf("failed to save output file: %w", err)
}
return nil
}