-
Notifications
You must be signed in to change notification settings - Fork 2
/
obf.py
88 lines (72 loc) · 1.74 KB
/
obf.py
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
#!/usr/bin/env python3
import sys
import os
import re
if len(sys.argv) != 2:
print("\tUsage:%s src"%sys.argv[0])
exit(0)
try:
with open(sys.argv[1],"r") as f:
data = f.read()
except:
print("not found file")
exit(0)
def get_uuid(a):
return os.path.basename(a).replace(".","_")
def get_dst_file(a):
return open(a.replace(".s","_new.s"),"w+")
def get_obj_file(a):
return open(a.replace(".s","_obf.s"),"w+")
uuid = get_uuid(sys.argv[1])
dst_file = get_dst_file(sys.argv[1])
obf_file = get_obj_file(sys.argv[1])
label_m = re.compile("\$L[0-9a-zA-Z]*")
def replace_label(a):
b = a
for label in label_m.findall(a):
b = b.replace(label,"_%s_%s"%(uuid,label[1:]))
return b
asm = data.split("\n")
global_name = "__"+uuid
cnt = 0
for line in asm:
line = line.strip()
if line == "":
continue
line = replace_label(line)
if line.startswith("_") and line.endswith(":"):
label = line[:-1]
dst_file.write(".global %s\n"%label)
dst_file.write(line+"\n")
continue
if not line.startswith("j") and not line.startswith("b"):
dst_file.write(line+"\n")
continue
prefix = "%s__%d"%(global_name,cnt)
func_begin = prefix + "_begin"
func_jmp = prefix + "_jmp"
func_ret = prefix + "_ret"
new_code = """
.global %s
mtc0 $0,$9
%s:
b .
.global %s
%s:
"""%(func_begin,func_begin,func_ret,func_ret)
cnt += 1
dst_file.write(new_code)
obf_code = """
.section .data.obf
.word %s
.word %s
.section .text.obf
.global %s
%s:
%s
b %s
nop
"""%(func_begin,func_jmp,func_jmp,func_jmp,line,func_ret)
obf_file.write(obf_code)
dst_file.close()
obf_file.close()