-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvirtualizer.py
432 lines (363 loc) · 12.7 KB
/
virtualizer.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#-*-coding:utf-8-*-
from capstone import *
import subprocess
import struct
import sys
import pdb
# INFORMATIONS
#
register_table = {
"rax": '\x00',
"rbx": '\x01',
"rcx": '\x02',
"rdx": '\x03',
"rsi": '\x04',
"rdi": '\x05',
"rbp": '\x06',
"rsp": '\x07',
"r8" : '\x08',
"r9" : '\x09',
"r10": '\x0a',
"r11": '\x0b',
"r12": '\x0c',
"r13": '\x0d',
"r14": '\x0e',
"r15": '\x0f',
"rip": '\x10',
"rflags": '\x11',
"tmp": '\x12'
}
opcode_table = {
"zero" : '\xd0',
"leaq" : '\xd1',
"movb" : '\xd2',
"movw" : '\xd3',
"movl" : '\xd4',
"movq" : '\xd5',
"movabsq" : '\xd6',
"addb" : '\xd7',
"addw" : '\xd8',
"addl" : '\xd9',
"addq" : '\xda',
"subb" : '\xdb',
"subw" : '\xdc',
"subl" : '\xdd',
"subq" : '\xde',
"syscall" : '\xdf',
"cmpb" : '\xe0',
"cmpw" : '\xe1',
"cmpl" : '\xe2',
"cmpq" : '\xe3',
"jmp" : '\xe4',
"je" : '\xe5',
"jne" : '\xe6',
"xorb" : '\xe7',
"xorw" : '\xe8',
"xorl" : '\xe9',
"xorq" : '\xea'
}
class Code:
def __init__(self, offset, opcode, operands):
self.offset = offset
self.opcode = opcode
self.operands = operands
self.vcode = None
def virtualization(self):
#
# Instruction Format
#
# AA BB CC DD
#
# AA: opcode
# BC: operand info (a b c c a' b' c' c')
# a : register bit
# b : reference bit
# cc : size (00: 1, 01: 2, 10: 4, 11: 8)
# DD: opernad 1 (Register: 1 byte, Immediate: size)
# EE: operand 2 (Register: 1 byte, Immediate: size)
# opcode
#
self.vcode = opcode_table[self.opcode]
if len(self.operands) == 0:
return self.vcode
# operand info
#
v_info = 0b00000000
# operand
#
v_operand = ""
if self.opcode.startswith('j'):
v_operand += '\x00'
else:
for i in range(len(self.operands)):
operand = self.operands[i]
this_info = 0b0000
is_reg = False
is_ref = False
# REF
if operand.startswith('(') and operand.endswith(')'):
is_ref = True
operand = operand.strip("()")
# IMM
if operand.startswith('$'):
if "-0x" in operand:
op_str = operand[4:]
elif "0x" in operand:
op_str = operand[3:]
else:
op_str = operand[1:]
operand = int(operand.strip('$'), 16)
if len(op_str) % 2 == 1:
op_str = '0' + op_str
size = len(op_str) / 2
if size == 1:
op_size = 0b00
if operand > 0x7f:
suffix = 'B'
else:
suffix = 'b'
elif size == 2:
op_size = 0b01
if operand > 0x7fff:
suffix = 'H'
else:
suffix = 'h'
elif size == 3 or size == 4:
op_size = 0b10
if operand > 0x7fffffff:
suffix = 'I'
else:
suffix = 'i'
elif 4 < size and size <= 8:
op_size = 0b11
if operand > 0x7fffffffffffffff:
suffix = 'Q'
else:
suffix = 'q'
else:
print"op_size[{}] error: IMM".format(i)
exit(-1)
try:
v_operand += struct.pack('<' + suffix, operand)
except struct.error:
if suffix == 'B':
op_size = 0b01
suffix = 'H'
elif suffix == 'b':
op_size = 0b01
suffix = 'h'
elif suffix == 'H':
op_size = 0b10
suffix = 'I'
elif suffix == 'h':
op_size = 0b10
suffix = 'i'
elif suffix == 'I':
op_size = 0b11
suffix = 'Q'
elif suffix == 'i':
op_size = 0b11
suffix = 'q'
v_operand += struct.pack('<' + suffix, operand)
# REG
elif operand.startswith('%'):
is_reg = True
operand = operand.strip('%')
reg_name = operand[1] # rax -> 'a', r8 -> '8'
if reg_name == 'm':
# tmp
#
op_size = 0b11
elif reg_name.isdigit():
# r8, r9, r10, r11, r12, r13, r14 r15
#
if operand.endswith('b'):
op_size = 0b00
operand = operand[:-1]
elif operand.endswith('w'):
op_size = 0b01
operand = operand[:-1]
elif operand.endswith('d'):
op_size = 0b10
operand = operand[:-1]
else:
op_size = 0b11
else:
# rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp
#
if operand.endswith('l'):
op_size = 0b00
operand = 'r' + operand[:-1]
if len(operand) == 2:
operand += 'x'
else:
if len(operand) == 2:
op_size = 0b01
operand = 'r' + operand
else:
if operand.startswith('r'):
op_size = 0b11
elif operand.startswith('e'):
op_size = 0b10
operand = 'r' + operand[1:]
else:
print "op_size[{}] error: REG".format(i)
exit(-1)
v_operand += register_table[operand] # append 1 byte
this_info |= op_size
if is_reg:
this_info |= 0b1000
if is_ref:
this_info |= 0b0100
if i==0:
this_info <<= 4
v_info |= this_info
self.vcode += chr(v_info) + v_operand
def print_vcode(self):
print '\t',
for byte in self.vcode:
print "{0:0>2}".format(hex(ord(byte))[2:]),
print ''
def print_assembly(self):
print "{0:<8}{1:<8}".format(hex(self.offset).strip('L'), self.opcode),
for operand in self.operands:
print operand,
print ""
def atnt_to_vsyntax(address, opcode, operands):
vcode = []
# this vm doesn't support indexed addressing
#
indexed = False
for i in range(len(operands)):
if '(' in operands[i]:
if '+' in operands[i] or '-' in operands[i]:
indexed = True
break
if indexed:
offset, reg = operands[i].split('(')
reg = reg.strip(')')
operands[i] = "(%tmp)"
vcode.append(Code(address, "movq", (reg, "%tmp"))) # mov REG, %tmp
vcode.append(Code(0, "addq", ('$' + offset, "%tmp"))) # add OFFSET, %tmp
vcode.append(Code(0, opcode, operands)) # OPCODE OPERAND1, (%tmp)
else:
vcode.append(Code(address, opcode, operands))
for i in range(len(operands)):
if '$' not in operands[i] and "0x" in operands[i]:
operands[i] = '$' + operands[i]
return vcode
def divide_operand(operand_str):
operands = []
if operand_str != '':
if ',' in operand_str:
for operand in operand_str.split(','):
operands.append(operand.strip(' '))
else:
operands.append(operand_str)
return operands
def disassemble(filename, func_start, func_end):
func_size = func_end - func_start + 1
with open(filename, "rb") as f:
f.seek(func_start)
binary = f.read(func_size)
codes = []
md = Cs(CS_ARCH_X86, CS_MODE_64)
md.syntax = CS_OPT_SYNTAX_ATT
for inst in md.disasm(binary, func_start):
address = inst.address
opcode = inst.mnemonic
operands = divide_operand(inst.op_str)
codes += atnt_to_vsyntax(address, opcode, operands)
return codes
def main(filename, func_start, func_end):
codes = disassemble(filename, func_start, func_end)
for code in codes:
code.print_assembly()
code.virtualization()
code.print_vcode()
# relocation branch offset
#
for i in range(len(codes)):
if codes[i].opcode.startswith('j'):
v_offset = 0
origin_address = int(codes[i].operands[0].strip('$'), 16)
if origin_address > codes[i].offset:
direction = +1
else:
direction = -1
j = i
if direction == -1:
v_offset += len(codes[j].vcode)
while True:
j += direction
if codes[j].offset == origin_address:
if direction == -1:
v_offset += len(codes[j].vcode)
break
else:
v_offset += len(codes[j].vcode)
v_offset *= direction
codes[i].operands[0] = v_offset
if direction == -1:
v_offset_str = hex(v_offset)[3:]
else:
v_offset_str = hex(v_offset)[2:]
if len(v_offset_str) % 2 == 1:
v_offset_str = '0' + v_offset_str
v_offset_size = len(v_offset_str)/2
if v_offset_size == 1:
op_size = 0b000000
suffix = 'b'
elif v_offset_size == 2:
op_size = 0b010000
suffix = 'h'
elif v_offset_size == 4:
op_size = 0b100000
suffix = 'l'
elif v_offset_size == 8:
op_size = 0b110000
suffix = 'q'
codes[i].vcode = codes[i].vcode[0] + chr(op_size) + struct.pack('<' + suffix, v_offset)
# DEBUG LOG
#
for code in codes:
code.print_vcode()
# result
#
vcode = ""
for code in codes:
vcode += code.vcode
vcode += '\x00'
for i in range(len(vcode)):
print "\'\\x{0:0>2}\',".format(hex(ord(vcode[i]))[2:]),
def get_func_address(filename, funcname):
cmd = "objdump -d {}".format(filename)
out = subprocess.check_output(cmd, shell=True)
if "{}:\n".format(funcname) in out:
out = out.split("{}:\n".format(funcname))[-1]
elif "<{}>:\n".format(funcname) in out:
out = out.split("<{}>:\n".format(funcname))[-1]
out = out.split('\n')
start_addr = int(out[0].split(':')[0], 16)
for line in out:
if "ret" in line:
end_addr = int(line.split(':')[0], 16)
break
start_addr -= 0x400000
end_addr -= 0x400000
return start_addr, end_addr
if __name__ == "__main__":
# usage: virtualizer.py [filename] [funcname]
#
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = "./crackme/a.out"
funcname = "func" # virtualization target function's symbol name
func_start, func_end = get_func_address(filename, funcname)
# skip function's prologue and epilogue
#
func_start += 4
func_end -= 2 # [WARN] this may not be accurate
print "func_start: {}\n func_end: {}".format(hex(func_start), hex(func_end))
main(filename, func_start, func_end)