-
Notifications
You must be signed in to change notification settings - Fork 4
/
stackstrings.py
275 lines (223 loc) · 8.26 KB
/
stackstrings.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
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
# Deobfuscate stackstrings used by Godzilla Loader
# @category: Godzilla Loader
import string
def set_comments(stackstrings):
for addr, value in stackstrings.items():
if len(value) <= 4:
continue
listing = currentProgram.getListing()
codeUnit = listing.getCodeUnitAt(toAddr(addr))
codeUnit.setComment(codeUnit.EOL_COMMENT, value)
print('%08x: %s' % (addr, value))
def is_ascii(value):
if value == 0 or chr(value) in string.printable:
return True
return False
def is_ascii_dword(value):
for i in range(4):
if not is_ascii((value >> (i * 8)) & 0xff):
return False
return True
def get_ascii_from_dword(value):
return ''.join([chr((value >> (i * 8)) & 0xff) for i in range(4)]).rstrip('\x00')
def decode_ss2(stackstrings):
result = {}
prev_offset = None
for offset, ss in sorted(stackstrings.items()):
if prev_offset == None:
value = ss[0]
start_addr = ss[1]
elif ss[0] == '\x00':
if len(value) > 4:
result[start_addr] = value
prev_offset = None
continue
elif prev_offset + 2 == offset:
value += ss[0]
prev_offset = offset
return result
def handle_ss2_clear(reg_state, inst):
reg = inst.getOpObjects(0)[0].getName()[1:]
reg_state[reg] = '\x00'
return reg_state
def handle_ss2_push(inst):
value = chr(inst.getOpObjects(0)[0].getUnsignedValue())
return value
def handle_ss2_pop(reg_state, inst, tos_value):
dst_reg = inst.getOpObjects(0)[0].getName()[1:]
reg_state[dst_reg] = tos_value
return reg_state
def handle_ss2_copy(reg_state, inst):
dst_reg = inst.getOpObjects(0)[0].getName()[1:]
src_reg = inst.getOpObjects(1)[0].getName()[1:]
src_value = reg_state.get(src_reg, '\x00')
reg_state[dst_reg] = src_value
return reg_state
def handle_ss2_store(reg_state, inst):
dst = inst.getOpObjects(0)
if isinstance(dst[0], ghidra.program.model.lang.Register):
if len(dst) == 2:
dst_offset = dst[1].getSignedValue()
else:
dst_offset = 0
else:
dst_offset = dst[0].getSignedValue()
src_reg = inst.getOpObjects(1)[0].getName()
src_value = reg_state.get(src_reg, '\x00')
return dst_offset, src_value
def deobfuscate_stackstrings2(func):
stackstrings = {}
tos_value = '\x00'
reg_state = {}
func_addr = func.getEntryPoint()
inst = getInstructionAt(func_addr)
while inst and getFunctionContaining(inst.getAddress()) == func:
# print('[%08x] %s' % (inst.getAddress().getOffset(), inst.toString()))
if is_ss2_clear(inst):
reg_state = handle_ss2_clear(reg_state, inst)
elif is_ss2_push(inst):
tos_value = handle_ss2_push(inst)
elif is_ss2_pop(inst):
reg_state = handle_ss2_pop(reg_state, inst, tos_value)
elif is_ss2_store(inst):
offset, value = handle_ss2_store(reg_state, inst)
inst_addr = inst.getAddress().getOffset()
stackstrings[offset] = (value, inst_addr)
elif is_ss2_copy(inst):
reg_state = handle_ss2_copy(reg_state, inst)
inst = inst.getNext()
result = decode_ss2(stackstrings)
return result
def is_ss2_clear(inst):
mnemonic = inst.getMnemonicString()
if not mnemonic.startswith('XOR'):
return False
dst = inst.getOpObjects(0)
if len(dst) != 1 or not isinstance(dst[0], ghidra.program.model.lang.Register) or \
dst[0].minimumByteSize != 4:
return False
src = inst.getOpObjects(1)
if len(src) != 1 or not isinstance(src[0], ghidra.program.model.lang.Register) or \
src[0].minimumByteSize != 4:
return False
src_reg = src[0].getName()
dst_reg = dst[0].getName()
if src_reg != dst_reg:
return False
return True
def is_ss2_push(inst):
mnemonic = inst.getMnemonicString()
if not mnemonic.startswith('PUSH') or inst.getLength() != 2:
return False
op = inst.getOpObjects(0)[0]
if not isinstance(op, ghidra.program.model.scalar.Scalar):
return False
value = op.getUnsignedValue() & 0xff
if not chr(value) in string.printable:
return False
return True
def is_ss2_pop(inst):
mnemonic = inst.getMnemonicString()
if not mnemonic.startswith('POP'):
return False
op = inst.getOpObjects(0)[0]
if not isinstance(op, ghidra.program.model.lang.Register):
return False
return True
def is_ss2_copy(inst):
mnemonic = inst.getMnemonicString()
if not mnemonic.startswith('MOV'):
return False
dst = inst.getOpObjects(0)
if len(dst) != 1 or not isinstance(dst[0], ghidra.program.model.lang.Register):
return False
src = inst.getOpObjects(1)
if len(src) != 1 or not isinstance(src[0], ghidra.program.model.lang.Register) or \
dst[0].getName() == 'ESP':
return False
return True
def is_ss2_store(inst):
mnemonic = inst.getMnemonicString()
if not mnemonic.startswith('MOV'):
return False
src = inst.getOpObjects(1)
if len(src) != 1 or not isinstance(src[0], ghidra.program.model.lang.Register) or \
src[0].minimumByteSize != 2:
return False
dst = inst.getOpObjects(0)
if len(dst) == 1 and isinstance(dst[0], ghidra.program.model.lang.Register) and \
dst[0].getName() == 'EBP':
return True
if len(dst) == 2 and isinstance(dst[0], ghidra.program.model.lang.Register) and \
dst[0].getName() == 'EBP' and isinstance(dst[1], ghidra.program.model.scalar.Scalar):
return True
if len(dst) == 2 and isinstance(dst[1], ghidra.program.model.lang.Register) and \
dst[1].getName() == 'EBP' and isinstance(dst[0], ghidra.program.model.scalar.Scalar):
return True
return False
def decode_ss1(stackstrings):
result = {}
prev_offset = None
for offset, ss in sorted(stackstrings.items()):
if prev_offset == None:
if len(ss[0]) == 4:
value = ss[0]
start_addr = ss[1]
prev_offset = offset
elif prev_offset + 4 == offset:
if len(ss[0]) == 4:
value += ss[0]
prev_offset = offset
else:
value += ss[0]
result[start_addr] = value
prev_offset = None
else:
result[start_addr] = value
prev_offset = offset
value = ss[0]
start_addr = ss[1]
return result
def is_ss1_store(inst):
mnemonic = inst.getMnemonicString()
if not mnemonic.startswith('MOV'):
return False
dst = inst.getOpObjects(0)
if len(dst) != 2 or not isinstance(dst[0], ghidra.program.model.lang.Register) or \
dst[0].getName() != 'EBP' or not isinstance(dst[1], ghidra.program.model.scalar.Scalar):
return False
offset = dst[1].getSignedValue()
src = inst.getOpObjects(1)
if len(src) != 1 or not isinstance(src[0], ghidra.program.model.scalar.Scalar):
return False
value = src[0].getUnsignedValue()
if not is_ascii_dword(value):
return False
return True
def handle_ss1_store(inst):
offset = inst.getOpObjects(0)[1].getSignedValue()
value = get_ascii_from_dword(inst.getOpObjects(1)[0].getUnsignedValue())
return offset, value
def deobfuscate_stackstrings1(func):
stackstrings = {}
func_addr = func.getEntryPoint()
inst = getInstructionAt(func_addr)
while inst and getFunctionContaining(inst.getAddress()) == func:
# print('[%08x] %s' % (inst.getAddress().getOffset(), inst.toString()))
if is_ss1_store(inst):
offset, value = handle_ss1_store(inst)
inst_addr = inst.getAddress().getOffset()
stackstrings[offset] = (value, inst_addr)
# print('[%08x:%08x] %s' % (inst_addr, offset, value))
inst = inst.getNext()
result = decode_ss1(stackstrings)
return result
def deobfuscate_godzilla_loader():
func = getFirstFunction()
while func:
stackstrings1 = deobfuscate_stackstrings1(func)
set_comments(stackstrings1)
stackstrings2 = deobfuscate_stackstrings2(func)
set_comments(stackstrings2)
func = getFunctionAfter(func)
deobfuscate_godzilla_loader()