how2heap-2.23-04-unsorted_bin_leak

本文介绍了如何通过unsortedbinleak漏洞,利用内存分配和释放操作来保留fastbin和topchunk之间的地址信息,进而计算出main_arena的地址以及libc库的偏移,揭示了内存管理中的安全风险。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<malloc.h>

int main()
{
        char* a = malloc(0x88);
        char* b = malloc(0x8);

        free(a);
        long* c = malloc(0x88);
        printf("%lx , %lx\n",c[0],c[1]);
        return 0;
}

unsorted bin leak原理:将chunk从unsorted bin申请回来时,chunk中保存的与unsorted bin相关的地址没有被清除,可以通过该地址获取libc的基地址

main_arean->bins初始化数据的含义

之前的文章过,画了个图介绍过,how2heap-2.23-03-fastbin_dup_consolidate,现在粘贴过来
在这里插入图片描述

将chunk释放到unsorted bin中

  • char* a = malloc(0x88); 申请一个大于fastbin范围的chunk a
  • char* b = malloc(0x8); 预防chunk a被释放后与top chunk合并
  • free(a); 将chunk a释放到 unsorted bin中

可以看到unsorted bin中保存了chunk a的地址,chunk a的fd,bk也保存了unsorted bin的地址(实际是top的地址)
在这里插入图片描述


从unsorted bin中申请回chunk

long* c = malloc(0x88);将chunk a重新申请回来,unsorted bin恢复为chunk a放进去之前的数据,而chunk a fd,bk中保存的unsorted bin的地址(实际是top的地址)并没有被清除
读取chunk c的c[0],c[1]就能获取unsorted bin的地址(实际是top的地址)
在这里插入图片描述

获取main_arean的地址

可以计算出top到main_arean的距离是8 * 11 = 88(在64位系统下:4字节的mutex + 4字节的flags + 8*10的fastbinY数组),从而得到main_aren的地址

获取main_arean在libc中的偏移

通过 __malloc_trim 函数得出

int
__malloc_trim (size_t s)
{
  int result = 0;

  if (__malloc_initialized < 0)
    ptmalloc_init ();

  mstate ar_ptr = &main_arena;	// <<<<<<<<<<<<<<<<<
  do
    {
      (void) mutex_lock (&ar_ptr->mutex);
      result |= mtrim (ar_ptr, s);
      (void) mutex_unlock (&ar_ptr->mutex);

      ar_ptr = ar_ptr->next;
    }
  while (ar_ptr != &main_arena);

  return result;
}

在这里插入图片描述


通过__malloc_hook得出
在这里插入图片描述

main_arena_offset = ELF("libc.so.6").symbols["__malloc_hook"] + 0x10

之后便能计算出libc的基地址

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值