-
Notifications
You must be signed in to change notification settings - Fork 4
/
hacklu_solve.py
177 lines (155 loc) · 6.04 KB
/
hacklu_solve.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
from pwn import *
context.arch = 'amd64'
######## CONFIGURATION ##########
TIMEOUT = 60
MAX_STEP = 100
_IO_vtable_check = 0x89f70
libc_path = "./bins/libc.so.6"
libc_symbol_path = "./bins/389d485a9793dbe873f0ea2c93e02efaa9aa3d.debug"
output_dir = "hacklu_outputs"
#################################
e = ELF(libc_path)
raw_bytes = e.section('__libc_IO_vtables')
os.makedirs(output_dir, exist_ok=True)
raw_ptrs = [u64(raw_bytes[i:i+8]) for i in range(0, len(raw_bytes), 8)]
def get_symbols():
symbols = [(x, y, e.functions[x].size) for x, y in e.symbols.items() if x in e.functions]
output = subprocess.getoutput(f"readelf -W -s {libc_symbol_path}")
for line in output.splitlines():
elems = line.split()
if len(elems) != 8:
continue
_, addr_str, size_str, _, _, _, _, sym_name = elems
try:
addr = int(addr_str, 16)
except Exception:
continue
size = int(size_str)
if addr == 0:
continue
if '@' in sym_name:
sym_name = sym_name.split('@')[0]
# print(sym_name, hex(addr))
symbols.append((sym_name, addr, size))
return symbols
symbols = get_symbols()
# get all the pointers in the vtable section
ptrs = []
for i in range(0, len(raw_bytes), 8):
ptr = u64(raw_bytes[i:i+8])
if ptr == 0:
continue
ptrs.append(ptr)
# get names of all the functions that need to be examined
func_names = []
for ptr in ptrs:
for name, addr, _ in symbols:
if addr == ptr:
func_names.append(name)
break
else:
print(ptr)
print(func_names)
raise
func_names = set(func_names)
#for func_name in func_names:
# for name, addr, size in symbols:
# if func_name != name:
# continue
# disassembly = e.disasm(addr, size)
# print(name, hex(addr))
# print(disassembly)
# print("="*0x10)
import sys
import angr
import claripy
import archinfo
import logging
from angr.concretization_strategies import SimConcretizationStrategy
#sys.setrecursionlimit(0x100000)
logging.getLogger("angr.storage.memory_mixins.default_filler_mixin").setLevel("ERROR")
logging.getLogger("angr.engines.successors").setLevel("ERROR")
target_symbols = {(x, y, z) for x, y, z in symbols if x in func_names}
class FSOPSimConcretizationStrategy(SimConcretizationStrategy):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._cnt = 0
def _concretize(self, memory, addr, **kwargs):
addrs = memory.state.solver.eval_upto(addr, 3)
#import IPython; IPython.embed()
#prin(addr, addrs)
if len(addrs) == 3:
self._cnt += 1
return [0, 0x1000000000000 * self._cnt]
else:
return addrs
#print(addr, kwargs)
#print([0, 0x1000000000000 * self._cnt])
#print(memory, addr)
#import IPython; IPython.embed()
proj = angr.Project(libc_path, main_opts={"base_addr": 0})
file_struct = claripy.Concat(*[claripy.BVS("content_%#x" % x, 8*8, explicit_name=True) for x in range(0, 0xe0, 8)])
#wide_data = claripy.BVS("wide_data", 8*8, explicit_name=True)
for name, addr, size in target_symbols:
#if name != "_IO_file_doallocate":
# continue
print(name)
state = proj.factory.blank_state(addr=addr)
reg_list = [x for x in state.arch.default_symbolic_registers if x not in ['rip', 'rsp']]
for reg in reg_list:
setattr(state.regs, reg, claripy.BVS(f"orig_{reg}", 8*8, explicit_name=True))
state.stack_push(0x41414141)
state.options.add(angr.sim_options.SYMBOLIC_WRITE_ADDRESSES)
# import IPython; IPython.embed()
state.regs.rdi = 0x5000000
state.memory.store(state.regs.rdi, file_struct)
state.memory.store(state.regs.rdi+0xa0, claripy.BVV(0x4000000, 8*8), endness=archinfo.Endness.LE)
state.memory.store(state.regs.rdi+0xe0, claripy.BVV(0, 8*0x100))
state.memory.store(0x4000000, claripy.BVV(0, 8*0xe8))
# assuming we are calling __xsputn (offset 0x38, hacklu's setup)
# then the vtable must be the offset of the function - 0x38
vtable_candidates = [0x215a00+8*i-0x38 for i in range(len(raw_ptrs)) if raw_ptrs[i] == addr]
actives = []
for vtable in vtable_candidates:
a = state.copy()
a.memory.store(0x5000000+0xd8, claripy.BVV(vtable, 8*8), endness=archinfo.Endness.LE)
actives.append(a)
#state.memory.read_strategies = [FSOPSimConcretizationStrategy()]
#state.memory.write_strategies = [FSOPSimConcretizationStrategy()]
#import IPython; IPython.embed()
# state.options.add(angr.sim_options.SYMBOLIC_WRITE_ADDRESSES)
simgr = proj.factory.simgr(actives, save_unconstrained=True)
#veri = angr.exploration_techniques.Veritesting()
#simgr.use_technique(veri)
# do exploration
simgr.stashes['avoided'] = []
simgr.stashes['bad'] = []
simgr.stashes['unconstrained'] = []
simgr.stashes['bad_addr'] = []
step = 0
try:
while simgr.active:
start = time.time()
simgr.step()
elapsed_time = time.time() - start
simgr.move("active", "deadended", filter_func=lambda s: s.addr == 0x41414141)
simgr.move("active", "avoided", filter_func=lambda s: s.addr == _IO_vtable_check)
simgr.move("unconstrained", "bad", filter_func=lambda s: s.regs.pc.depth > 1)
simgr.move("active", "bad_addr", filter_func=lambda s: proj.loader.find_segment_containing(s.addr) is None or s.addr < 0x1000)
print(f"\ntime: {elapsed_time}")
print(simgr)
step += 1
if elapsed_time > TIMEOUT:
break
if step > MAX_STEP:
break
#if simgr.unconstrained:
# print("FOUND!!!!!!!!!!!!!!!" + "\n!!!!!!!!!!!!!!!"*3)
# import IPython; IPython.embed()
except Exception:
pass
#import IPython; IPython.embed()
# save results if there are any
if simgr.unconstrained:
with open(f"{output_dir}/{name}.pickle", 'wb') as f:
pickle.dump(simgr.unconstrained, f)