-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathunity_loader.py
239 lines (189 loc) · 6.91 KB
/
unity_loader.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
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
import os, sys
import re
import random
import idaapi
import idc
import idautils
INFO = idaapi.get_inf_structure()
if INFO.is_64bit():
BITS = 64
elif INFO.is_32bit():
BITS = 32
else:
BITS = 16
ENUM_FILE_TYPE =\
[
"f_EXE_old", #// MS DOS EXE File
"f_COM_old", #// MS DOS COM File
"f_BIN", #// Binary File
"f_DRV", #// MS DOS Driver
"f_WIN", #// New Executable (NE)
"f_HEX", #// Intel Hex Object File
"f_MEX", #// MOS Technology Hex Object File
"f_LX", #// Linear Executable (LX)
"f_LE", #// Linear Executable (LE)
"f_NLM", #// Netware Loadable Module (NLM)
"f_COFF", #// Common Object File Format (COFF)
"f_PE", #// Portable Executable (PE)
"f_OMF", #// Object Module Format
"f_SREC", #// R-records
"f_ZIP", #// ZIP file (this file is never loaded to IDA database)
"f_OMFLIB", #// Library of OMF Modules
"f_AR", #// ar library
"f_LOADER", #// file is loaded using LOADER DLL
"f_ELF", #// Executable and Linkable Format (ELF)
"f_W32RUN", #// Watcom DOS32 Extender (W32RUN)
"f_AOUT", #// Linux a.out (AOUT)
"f_PRC", #// PalmPilot program file
"f_EXE", #// MS DOS EXE File
"f_COM", #// MS DOS COM File
"f_AIXAR", #// AIX ar library
"f_MACHO", #// Max OS X
];
FILE_TYPE = ENUM_FILE_TYPE[INFO.filetype]
def IncreaseAddr(addr):
if BITS == 64:
return addr+8
elif BITS == 32:
return addr+4
def DecreaseAddr(addr):
if BITS == 64:
return addr-8
elif BITS == 32:
return addr-4
def GetVarFromAddr(addr):
if BITS == 64:
return idc.Qword(addr)
elif BITS == 32:
return idc.Dword(addr)
def GetMethodFromAddr(addr):
return GetVarFromAddr(addr) & 0xFFFFFFFE
def IsCode(addr):
return idc.isCode(idc.GetFlags(addr))
def IsData(addr):
return idc.isData(idc.GetFlags(addr))
def IsSubFollowing(addr):
i = 0
while i < 20:
pAddr = GetMethodFromAddr(addr)
if not IsCode(pAddr):
return False;
addr = idc.NextHead(addr)
i = i + 1
return True
def IsDataFollowing(addr):
i = 0
while i < 20:
pAddr = GetVarFromAddr(addr)
if not IsData(addr):
return False;
addr = idc.NextHead(addr)
i = i + 1
return True
def LocateMethodPointers():
seg = idc.FirstSeg()
initArrayAddr = 0
while seg != idc.BADADDR:
seg = idc.NextSeg(seg)
segName = idc.SegName(seg)
if segName == ".data.rel.ro":
data_rel_ro = idc.SegStart(seg)
break
addr = data_rel_ro
referedVars = []
while idc.SegName(addr) == ".data.rel.ro":
for r in idautils.XrefsTo(addr,0):
#print "is refered: 0x%x" % addr
referedVars.append(addr)
break
addr += 4
candidateMethodPointers = []
for var in referedVars:
if IsSubFollowing(var):
candidateMethodPointers.append(var)
for candidate in candidateMethodPointers:
for referedVar in referedVars:
if referedVar == candidate:
nextVar = referedVars[referedVars.index(referedVar)+1]
print "candidate: 0x%x, candidate end: 0x%x, method numbers: %d" % (candidate, nextVar, (nextVar-candidate)/4)
break
def LocateStringLiterals():
seg = idc.FirstSeg()
initArrayAddr = 0
while seg != idc.BADADDR:
seg = idc.NextSeg(seg)
segName = idc.SegName(seg)
if segName == ".data.rel.ro":
data_rel_ro = idc.SegStart(seg)
break
addr = data_rel_ro
referedVars = []
while idc.SegName(addr) == ".data.rel.ro":
for r in idautils.XrefsTo(addr,0):
referedVars.append(addr)
break
addr += 4
candidateMetadaUsages = []
for idx, var in enumerate(referedVars):
if idx < (len(referedVars)-1) and (referedVars[idx+1]-referedVars[idx]) >= 1024:
if idc.Dword(var) == 0x0:
continue
if IsDataFollowing(var) and idc.SegName(idc.Dword(var) ) == '.bss':
candidateMetadaUsages.append(var)
for candidate in candidateMetadaUsages:
for referedVar in referedVars:
if referedVar == candidate:
nextVar = referedVars[referedVars.index(referedVar)+1]
print "candidate: 0x%x, candidate end: 0x%x, data numbers: %d" % (candidate, nextVar, (nextVar-candidate)/4)
break
def LoadMethods(ea = None):
if ea is None:
ea = ScreenEA();
path = os.getcwd()
os.system(path+'/unity_decoder.exe')
file = open('./method_name.txt')
str_count = file.readline()
i = 0;
for line in file:
line = line.strip(' ').replace('\r', '').replace('\n', '')
new_line = re.sub(r'[^a-zA-Z0-9_$]', '_', line)
i = 0;
addr = GetMethodFromAddr(ea)
ret = idc.MakeNameEx(addr, str(new_line), SN_NOWARN)
while ret == 0 and i < 5: # failed
new_line_rand = new_line + '_' + str(random.randint(0, 99999))
ret = idc.MakeNameEx(addr, str(new_line_rand), SN_NOWARN)
idc.MakeComm(ea, str(line))
i = i + 1
ea = IncreaseAddr(ea)
file.close()
def LoadStringLiterals(ea = None):
if ea is None:
ea = ScreenEA();
path = os.getcwd()
os.system(path+'/unity_decoder.exe')
file = open('./string_literal.txt')
total_count = file.readline()
str_count = file.readline()
skip_count = int(total_count) - int(str_count)
ea += int(skip_count) * 0x4
for line in file:
line = line.strip(' ').replace('\r', '').replace('\n', '')
new_line = re.sub(r'[^a-zA-Z0-9_]', '_', line)
new_line = 'StringLiteral_' + new_line
i = 0;
addr = GetVarFromAddr(ea)
ret = idc.MakeNameEx(addr, str(new_line), SN_NOWARN)
#ret = idc.MakeNameEx(addr, "", SN_NOWARN)
while ret == 0 and i < 5: # failed
new_line_rand = new_line + '_' + str(random.randint(0, 99999))
ret = idc.MakeNameEx(addr, str(new_line_rand), SN_NOWARN)
i = i + 1
idc.MakeComm(ea, str(line))
#idc.MakeComm(ea, "")
ea = IncreaseAddr(ea)
file.close()
print "Type LocateMethodPointers() to print suggested candidate for method pointers"
print "Click the location where you believe the method pointers start, type LoadMethods()"
print "Type LocateStringLiterals() to print suggested candidate for string literals"
print "Click the location where you believe the string literals start, type LoadStringLiterals()"