-
Notifications
You must be signed in to change notification settings - Fork 15
/
remove_opaque.py
105 lines (75 loc) · 2.85 KB
/
remove_opaque.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
#!/usr/bin/python3
import z3
from miasm.analysis.binary import Container
from miasm.analysis.machine import Machine
from miasm.core.locationdb import LocationDB
from miasm.ir.symbexec import SymbolicExecutionEngine
from miasm.ir.translators.z3_ir import TranslatorZ3
def branch_cannot_be_taken(expression, jump_target):
# init solver
solver = z3.Solver()
# init translator miasm ir -> z3
translator = TranslatorZ3()
# add constraint
solver.add(translator.from_expr(expression) ==
translator.from_expr(jump_target))
# check for unsat
return solver.check() == z3.unsat
# hardcode file path and address
file_path = "samples/x-tunnel"
start_addr = 0x491aa0
# symbol table
loc_db = LocationDB()
# open the binary for analysis
container = Container.from_stream(open(file_path, 'rb'), loc_db)
# cpu abstraction
machine = Machine(container.arch)
# init disassemble engine
mdis = machine.dis_engine(container.bin_stream, loc_db=loc_db)
# initialize lifter to intermediate representation
lifter = machine.lifter_model_call(mdis.loc_db)
# disassemble the function at address
asm_cfg = mdis.dis_multiblock(start_addr)
# translate asm_cfg into ira_cfg
ira_cfg = lifter.new_ircfg_from_asmcfg(asm_cfg)
# set opaque predicate counter
opaque_counter = 0
# dictionary of byte patches
patches = {}
# walk over all basic blocks
for basic_block in asm_cfg.blocks:
# get address of first basic block instruction
address = basic_block.lines[0].offset
# init symbolic execution engine
sb = SymbolicExecutionEngine(lifter)
# symbolically execute basic block
e = sb.run_block_at(ira_cfg, address)
# skip if no conditional jump
if not e.is_cond():
continue
# cond ? src1 : src2
# check if opaque predicate -- jump
if branch_cannot_be_taken(e, e.src1):
print(f"opaque predicate at {hex(address)} (jump is never taken)")
opaque_counter += 1
# get the jump instruction
jump_instruction = basic_block.lines[-1]
# get file offset from virtual address
offset_of_jump_instruction = container.bin_stream.bin.virt2off(
jump_instruction.offset)
# walk over all instruction bytes and set corresponding file offsets to 0x90 (nop)
for index in range(offset_of_jump_instruction, offset_of_jump_instruction + len(jump_instruction.b)):
patches[index] = 0x90
# check if opaque predicate -- fall-through
elif branch_cannot_be_taken(e, e.src2):
print(f"opaque predicate at {hex(address)} (always jump)")
opaque_counter += 1
print(f"number of opaque predicates: {opaque_counter}")
print("patching")
# read raw bytes of file
raw_bytes = bytearray(open(file_path, 'rb').read())
# apply patches
for index, byte in patches.items():
raw_bytes[index] = byte
# save patched file
open("samples/patched", 'wb').write(raw_bytes)