通过 glibc2.25 学习 house_of_spirit

前言:再进步一点。

在 how2heap 中 house_of_spirit 就是伪造 chunk 去 free,也就是说,今天的重点在与:如何绕过 free 中的而全部检查

0X00 例子

#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[10] __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[1], &fake_chunks[9]);

    fprintf(stderr, "This chunk.size of this region has to be 16 more than the region (to accomodate 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[1] = 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[9] because 0x40 / sizeof(unsigned long long) = 8
    fake_chunks[9] = 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[1]);
    fprintf(stderr, "... note that the memory address of the *region* associated with this chunk must be 16-byte aligned.\n");
    a = &fake_chunks[2];

    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[1], &fake_chunks[2]);
    fprintf(stderr, "malloc(0x30): %p\n", malloc(0x30));
}

0X01 动手调试与原理讲解

我们一起来调试吧!

进入 _int_free 中:

单步调试,看我们的第一个检查:

首先 __build_expect,遇到 if(__builtin_expect(A, 0)) 的时候 直接把它看成 if(A)

所以我们的第一个要满足的条件就是

p < -size && misaligned_chunk(p)

由于 size 是无符号数字,所以 -size 是一个非常大的数字,所以这个只要是正常的指针都应该能绕过。

#define misaligned_chunk(p) \
  ((uintptr_t)(MALLOC_ALIGNMENT == 2 * SIZE_SZ ? (p) : chunk2mem (p)) \
   & MALLOC_ALIGN_MASK)

这是一个宏,哎,很头疼,宏套着宏,关键是我不知道怎么用 gdb 看这些值。好吧,做个弊,我们来搜索一下,书上说:misaligned_chunk(p) 用于校验地址是否是按 2SIZE_SZ 对齐*

SIZE_SZ 是 8 所以 2 * 8 = 16 也就是说,p 结尾必须是 0 了。

我们用 gdb 看一下:

确实如此。在 how2heap 中体现在:

__attribute__ ((aligned (16))

第一个检查就这么绕过了。好我们继续,遇到了第二个检查:

这个就是用来检查 size 了。

在 _int_free 里面:

#define chunksize(p) (chunksize_nomask (p) & ~(SIZE_BITS))
size = chunksize (p);

所以 size 就是那个 chunk 的 size,在 how2heap 中体现在:

fake_chunks[1] = 0x40; // this is the size

这个 size 有两个条件:

size > MINSIZE size 的大小是 16(64 位)或 8 (32 位)的倍数

好,这个条件我们也绕过了,我们继续:

这里就是检查下一个 chunk 的大小了,条件很宽松很容易绕过。我们继续:

接下来的很多检查都是与 double free 相关由于我们是自己构造的 chunk 也就不存在 double free 了。

0X02 总结一下

对于自己伪造的小 chunk,要让它合法 free 还是很简单的,没有什么太多的检查。

  • 地址对齐
  • 自己的 size 大小合理
  • 下一个 chunk 的大小合理就 ok 了

完结撒花!

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

相关阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,369评论 0赞 38
  • 参考文章: 关于heap overflow的一些笔记 by ETenal [CTF]Heap vuln -- u...
    BJChangAn阅读 2,956评论 2赞 5
  • 现在是2019.3.22,晚上10.13,记录今天。 今日清晨 早上到了综合办,才知道了许多不为人知的隐秘,一片平...
    饭饭_bms阅读 257评论 0赞 1
  • 我喜欢过你,讨厌过你,忽视过你,最后发现能相依为命的依旧是你。 在我七八岁的时候,爸爸妈妈问我,给你添个弟弟或者妹...
    小狍子不爱吃肉呀阅读 462评论 3赞 4
  • 樱花雨撒下,撒在涓涓细流之上,落下的声音裹挟着流水的潺潺流向远方。 他独自一人,盘着腿,临溪而坐,在那樱花树下。与...
    三寸流火阅读 465评论 5赞 17

友情链接更多精彩内容