本项目主要为How2heap提供了一份中文翻译,尽可能的做到尊重原文,便于读者理解。
如果有帮到各位师傅,还请动动手点个Star吧!如果发现本文有任何谬误或对本文有任何建议,欢迎提交commit。
正在施工中。。。
这个仓库用于学习各种堆利用技术。我们使用Ubuntu的Libc版本作为黄金标准。每种技术都经过验证的,可以在相应的Ubuntu版本上正常工作。您可以运行“apt source libc6”来下载您在基于Debian的操作系统上使用的Libc的源代码。您还可以单击:arrow_forward:使用gdb在浏览器中调试技术。
我们在黑客会议期间想出了这个想法,并实现了以下技术:
文件 | 技术概括 | Glibc-版本 | 补丁(patch) | 相关的CTF挑战 | |
---|---|---|---|---|---|
first_fit.c | 展示glibc malloc使用first-fit算法的方式。 | ||||
calc_tcache_idx.c | 展示glibc的tcache索引计算方式。 | ||||
fastbin_dup.c | 通过利用 fastbin 的 freelist 来诱骗 malloc 返回已分配的堆指针。 | 最新 | |||
fastbin_dup_into_stack.c | 通过利用 fastbin 的 freelist 来诱骗 malloc 返回几乎任意的指针。 | 最新 | 9447-search-engine, 0ctf 2017-babyheap | ||
fastbin_dup_consolidate.c | 通过将指针放在 fast bin freelist 和 unsorted bin freelist 中来诱骗 malloc 返回已分配的堆指针。 | 最新 | Hitcon 2016 SleepyHolder | ||
unsafe_unlink.c | 利用损坏的块进行自由攻击,能够进行任意地址写入。 | 最新 | HITCON CTF 2014-stkof, Insomni'hack 2017-Wheel of Robots | ||
house_of_spirit.c | free 一个伪造的 fastbin chunk 使 malloc 返回一个几乎任意的指针。 | 最新 | hack.lu CTF 2014-OREO | ||
poison_null_byte.c | 利用单个空字节溢出。 | 最新 | PlaidCTF 2015-plaiddb, BalsnCTF 2019-PlainNote | ||
house_of_lore.c | 通过利用 small bin freelist 来诱骗 malloc 返回几乎任意的指针。 | 最新 | |||
overlapping_chunks.c | 利用 unsorted bin 中释放的区块大小的覆盖,以使新的分配与现有区块重叠 | < 2.29 | 补丁 | hack.lu CTF 2015-bookstore, Nuit du Hack 2016-night-deamonic-heap | |
overlapping_chunks_2.c | 覆盖正在使用的区块大小,使新的分配与现有区块重叠 | < 2.29 | 补丁 | ||
mmap_overlapping_chunks.c | 利用正在使用的 mmap 区块,使新的分配与当前 mmap 区块重叠 | 最新 | |||
house_of_force.c | 利用 top chunk (Wilderness)header 来让 malloc 返回几乎任意的指针 | < 2.29 | 补丁 | Boston Key Party 2016-cookbook, BCTF 2016-bcloud | |
unsorted_bin_into_stack.c | 覆盖 unsorted bin freelist 上的已被释放的堆块来返回几乎任意的指针。 | < 2.29 | 补丁 | ||
unsorted_bin_attack.c | 覆盖 unsorted bin freelist 上的已被释放的堆块将一个较大的值写入任意地址 | < 2.29 | 补丁 | 0ctf 2016-zerostorage | |
large_bin_attack.c | 覆盖 large bin freelist 上的已被释放的堆块将一个较大的值写入任意地址 | 最新 | 0ctf 2018-heapstorm2 | ||
house_of_einherjar.c | 利用单个空字节溢出诱骗 malloc 返回受控指针 | 最新 | Seccon 2016-tinypad | ||
house_of_orange.c | 利用 top chunk(Wilderness)来进行任意代码执行 | < 2.26 | 补丁 | Hitcon 2016 houseoforange | |
house_of_roman.c | 一种无泄漏技术,以通过伪造的fastbins,unsorted bin和相对覆盖来实现远程代码执行。 | < 2.29 | 补丁 | ||
tcache_poisoning.c | 通过利用 tcache freelist 来诱骗 malloc 返回完全任意的指针。(glibc版本至少要在2.32以上并且同时需要对堆泄漏进行利用) | > 2.25 | 补丁 | ||
tcache_house_of_spirit.c | 释放一个伪造的块,让 malloc 返回一个几乎任意的指针。 | > 2.25 | |||
house_of_botcake.c | 绕过tcache的双重释放检查机制。Make tcache_dup great again! |
> 2.25 | |||
tcache_stashing_unlink_attack.c | 覆盖 small bin freelist 上的已被释放的块来诱骗 malloc 返回任意指针,并在 calloc 的帮助下将一个较大的值写入任意地址。 | > 2.25 | Hitcon 2019 one punch man | ||
fastbin_reverse_into_tcache.c | 覆盖 fastbin 中已被释放的块将一个较大的值写入任意地址。 | > 2.25 | |||
house_of_mind_fastbin.c | 利用arena处理的单字节覆盖将一个较大的值(堆指针)写入任意地址 | 最新 | |||
house_of_storm.c | 利用对 large bin 和unsorted bin 的 use-after-free 漏洞以从 malloc 返回任意的块 | < 2.29 | |||
house_of_gods.c | 一种在 8 次分配以内劫持线程arena的技术 | < 2.27 | |||
decrypt_safe_linking.c | 解密链表中的poisoned value来恢复实际指针 | >= 2.32 | |||
tcache_dup.c(已废弃) | 利用 tcache freelist 诱骗 malloc 返回已分配的堆指针。 | 2.26 - 2.28 | 补丁 |
获取仓库
$ git clone https://github.com/ffreeez/how2heap_zh
$ cd ./how2heap_zh
$ git submodule update --init --recursive
在根目录下直接make即可
$ cd ./how2heap_zh
$ make
使用glibc_run.sh来执行可执行文件,可以更方便的替换glibc
在使用这个脚本之前,需要安装patchelf
,并且要在glibc-all-in-one
项目中更新可下载的列表
$ sudo apt install patchelf
$ cd ./how2heap_zh/glibc-all-in-one
$ chmod +x ./*
$ ./update_list
执行update_list
时,可能会提示-bash: ./update_list: /usr/bin/python: bad interpreter: No such file or directory
,在文本编辑器中把update_list
文件中的第一行#!/usr/bin/python
改为#!/usr/bin/python3
即可
[用法] ./glibc_run.sh <glibc版本号> <可执行文件> [-h] [-i686] [-u] [-r] [-gdb | -r2 | -p]
-i686 -使用32位的libc
-u 在glibc-all-in-one中更新libc列表
-r 在glibc-all-in-one中下载libc
-gdb -在gdb中执行目标文件
-r2 -在radare2中执行目标文件
-p -只在可执行文件中修改interpreter和rpath来指向对应的glibc并且不执行
eg: ./glibc_run.sh 2.23 ./first_fit
Gnu Libc 正在不断发展,上面的几种技术已经允许在 malloc/free 逻辑中引入一致性检查。
因此,这些检查会定期破坏某些技术,并需要进行调整以绕过它们(如果可能)。
我们通过为每个需要调整的 Glibc 版本保留相同技术的多个版本来解决此问题。
项目结构为glibc_<version>/技术名称.c
这里有一些广为流传的堆利用工具,具体内容暂不做翻译(懒懒。
jemalloc exploitation framework: https://github.com/CENSUS/shadow
Examine the glibc heap in gdb: https://github.com/cloudburst/libheap
Examine the glibc heap in IDA Pro: https://github.com/danigargu/heap-viewer
A Python based heap playground with good visualization for educational purposes: https://github.com/matrix1001/heapinspect
Debugger that lets you set "checkpoints" as well as view and edit the heap using a hexeditor: https://github.com/haxkor/forkever
The malloc_playground.c
file given is the source for a program that prompts the user for commands to allocate and free memory interactively.
Examine the glibc heap in gdb: https://github.com/scwuaptx/Pwngdb
Helps you visualize heap operations by replacing addresses with symbols: https://github.com/Arinerron/heaptrace
Search for applicable heap exploitation techniques based on primitive requirements: https://kissprogramming.com/heap/heap-search
Some good heap exploitation resources, roughly in order of their publication, are:
- glibc in-depth tutorial (https://heap-exploitation.dhavalkapil.com/) - book and exploit samples
- ptmalloc fanzine, a set of resources and examples related to meta-data attacks on ptmalloc (http://tukan.farm/2016/07/26/ptmalloc-fanzine/)
- A malloc diagram, from libheap (https://raw.githubusercontent.com/cloudburst/libheap/master/heap.png)
- Glibc Adventures: The Forgotten Chunk (https://go.contextis.com/rs/140-OCV-459/images/Glibc_Adventures-The_Forgotten_Chunks.pdf) - advanced heap exploitation
- Pseudomonarchia jemallocum (http://www.phrack.org/issues/68/10.html)
- The House Of Lore: Reloaded (http://phrack.org/issues/67/8.html)
- Malloc Des-Maleficarum (http://phrack.org/issues/66/10.html) - some malloc exploitation techniques
- Yet another free() exploitation technique (http://phrack.org/issues/66/6.html)
- Understanding the heap by breaking it (https://www.blackhat.com/presentations/bh-usa-07/Ferguson/Whitepaper/bh-usa-07-ferguson-WP.pdf) - explains heap implementation and a couple exploits
- The use of set_head to defeat the wilderness (http://phrack.org/issues/64/9.html)
- The Malloc Maleficarum (http://seclists.org/bugtraq/2005/Oct/118)
- OS X heap exploitation techniques (http://phrack.org/issues/63/5.html)
- Exploiting The Wilderness (http://seclists.org/vuln-dev/2004/Feb/25)
- Advanced Doug lea's malloc exploits (http://phrack.org/issues/61/6.html)
- GDB Enhanced Features (GEF) Heap Exploration Tools (https://gef.readthedocs.io/en/master/commands/heap/)
- Painless intro to the Linux userland heap (https://sensepost.com/blog/2017/painless-intro-to-the-linux-userland-heap/)
- Heap exploitation techniques that work on glibc-2.31 (https://github.com/StarCross-Tech/heap_exploit_2.31)
- Overview of GLIBC heap exploitation techniques (https://0x434b.dev/overview-of-glibc-heap-exploitation-techniques/)
There are a couple of "hardening" measures embedded in glibc, like export MALLOC_CHECK_=1
(enables some checks), export MALLOC_PERTURB_=1
(data is overwritten), export MALLOC_MMAP_THRESHOLD_=1
(always use mmap()), ...
More info: mcheck(), mallopt().
There's also some tracing support as mtrace(), malloc_stats(), malloc_info(), memusage, and in other functions in this family.