how2heap-2.23-04-unsorted_bin_leak

c 复制代码
#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 函数得出

c 复制代码
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得出

python 复制代码
main_arena_offset = ELF("libc.so.6").symbols["__malloc_hook"] + 0x10

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

相关推荐
goodcat6667 个月前
问题慢慢解决-通过android emulator调试android kernel-内核条件断点遇到的问题和临时解决方案
android·linux pwn
goodcat6667 个月前
midnightsun-2018-flitbip:任意地址写
linux pwn
goodcat6668 个月前
完美调试android-goldfish(linux kernel) aarch64的方法
android·linux·运维·linux pwn
goodcat6668 个月前
Memory Deduplication Attacks
linux pwn
goodcat6661 年前
00-linux pwn环境搭建
linux pwn