堆利用教学
本仓库用于学习各种堆利用技术。
我们以 Ubuntu 的 Libc 发行版作为黄金标准。每种技术都已在对应的 Ubuntu 发行版上验证有效。
你可以在基于 Debian 的操作系统上运行 apt source libc6 下载你正在使用的 Libc 源码。你也可以点击 ▶️ 使用 gdb 在浏览器中调试该技术。
我们是在一次黑客聚会上想到这个主意的,并已实现了以下技术:
GnuLibc 在不断开发中,上述多种技术已导致 malloc/free 逻辑中引入一致性检查。
因此,这些检查经常会使某些技术失效,并需要调整以绕过它们(如果可能的话)。
我们通过为每个需要调整的 Glibc 版本保留同一技术的多个版本来解决这个问题。
目录结构为 glibc_<version>/technique.c。
有好例子吗?
把它添加到这里!
尝试将整个技术内联到单个 .c 文件中——这样学起来容易得多。
快速入门
快速设置
- 确保你已安装以下软件包/工具:
patchelf zstd wget(当然还需要 build-essential 或类似的编译器工具包、make 等)。
- 此外,
/usr/bin/python 必须是你 python 二进制文件的路径或指向该路径(例如 /usr/bin/python3)。```shell
git clone https://github.com/shellphish/how2heap
cd how2heap
make clean base
./malloc_playground
注意,这会将二进制文件与你的系统 libc 链接。如果你想使用其他 libc 版本,请参阅 `Complete Setup`。
## 完整设置
如果你尝试对在宿主机上编译的二进制文件进行 `LD_PRELOAD` libc,将会遇到符号版本问题(参见[这个](https://github.com/shellphish/how2heap/issues/169))。
我们有两种方法可以绕过它。
### 方法 1:链接到较旧的 libc
这个方法告诉链接器将目标二进制文件与目标 libc 链接。```shell
git clone https://github.com/shellphish/how2heap
cd how2heap
H2H_USE_SYSTEM_LIBC=N make v2.23
这将把所有二进制文件与相应的 libc 版本链接起来。更好的是,它自带调试符号。现在,你可以在宿主机上使用任意 libc 版本进行实验。
在此示例中,它将编译所有 glibc-2.23 二进制文件,并将它们与 libc-2.23 链接。你可以更改该数字,以尝试其他 libc 版本。
方法 2:使用 docker
该方法使用基于 Docker 的方式,在旧版 ubuntu 容器内编译二进制文件,从而使其能在目标 libc 版本下运行。```shell
git clone https://github.com/shellphish/how2heap
cd how2heap
the next command will prepare the target binary so it runs with
the expected libc version
make base
./glibc_run.sh 2.30 ./malloc_playground -d -p
now you can play with the binary with glibc-2.30
and even debug it with the correct symbols
readelf -d -W malloc_playground | grep RUNPATH # or use checksec
readelf -l -W malloc_playground | grep interpreter
gdb -q -ex "start" ./malloc_playground
# Heap Exploitation Tools
这里有一些流传的堆利用工具。
## Malloc Playground
所给出的 `malloc_playground.c` 文件是一个程序的源代码,该程序会提示用户输入命令,以交互方式分配和释放内存。
## Pwngdb
在 gdb 中检查 glibc 堆:https://github.com/scwuaptx/Pwngdb
## pwndbg
一个以利用为中心的 gdb 插件,提供查看/篡改 glibc 堆的能力:https://github.com/pwndbg/pwndbg
## gef
另一个优秀的 gdb 插件,提供检查 glibc 堆的能力:https://github.com/hugsy/gef
## heap-viewer
在 IDA Pro 中检查 glibc 堆:https://github.com/danigargu/heap-viewer
## heaptrace
通过用符号替换地址来帮助你可视化堆操作:https://github.com/Arinerron/heaptrace
# 其他资源
一些不错的堆利用资源,大致按发布时间的倒序排列如下:
## 有用的堆利用教程
- GLIBC 堆利用技术概述 (https://0x434b.dev/overview-of-glibc-heap-exploitation-techniques/) <!-- 2022 -->
- glibc 深入教程 (https://heap-exploitation.dhavalkapil.com/) - 书籍和漏洞利用示例 <!-- 2022 -->
- 适用于 glibc-2.31 的堆利用技术 (https://github.com/StarCross-Tech/heap_exploit_2.31) <!-- 2020 -->
- Linux 用户态堆的无痛入门 (https://sensepost.com/blog/2017/painless-intro-to-the-linux-userland-heap/) <!-- 2017 -->
- ptmalloc 爱好者杂志,一组与 ptmalloc 元数据攻击相关的资源和示例 (http://tukan.farm/2016/07/26/ptmalloc-fanzine/) <!-- 2016 -->
- Glibc 冒险:被遗忘的块 (https://github.com/bash-c/slides/blob/master/pwn_heap/Glibc%20Adventures:%20The%20forgotten%20chunks.pdf) - 高级堆利用 <!-- 2015 -->
## 历史堆利用(历史)
- Pseudomonarchia jemallocum (http://www.phrack.org/issues/68/10.html) <!-- 2012 -->
- The House Of Lore: Reloaded (http://phrack.org/issues/67/8.html) <!-- 2010 -->
- Malloc Des-Maleficarum (http://phrack.org/issues/66/10.html) - 一些 malloc 利用技术 <!-- 2009 -->
- 又一种 free() 利用技术 (http://phrack.org/issues/66/6.html) <!-- 2009 -->
- 使用 set_head 对抗 wilderness (http://phrack.org/issues/64/9.html) <!-- 2007 -->
- 通过破坏堆来理解堆 (https://www.blackhat.com/presentations/bh-usa-07/Ferguson/Whitepaper/bh-usa-07-ferguson-WP.pdf) - 解释堆的实现和一些漏洞利用 <!-- 2007 -->
- OS X 堆利用技术 (http://phrack.org/issues/63/5.html) <!-- 2005 -->
- The Malloc Maleficarum (http://seclists.org/bugtraq/2005/Oct/118) <!-- 2005 -->
- 利用 Wilderness (http://seclists.org/vuln-dev/2004/Feb/25) <!-- 2004 -->
- 高级 Doug lea malloc 漏洞利用技术 (http://phrack.org/issues/61/6.html) <!-- 2003 -->
# 加固
glibc 中内置了一些“加固”措施,比如 `export MALLOC_CHECK_=1`(启用一些检查)、`export MALLOC_PERTURB_=1`(数据会被覆盖)、`export MALLOC_MMAP_THRESHOLD_=1`(始终使用 mmap())……
更多信息:[mcheck()](http://www.gnu.org/software/libc/manual/html_node/Heap-Consistency-Checking.html)、[mallopt()](http://www.gnu.org/software/libc/manual/html_node/Malloc-Tunable-Parameters.html)。
还有一些跟踪支持,如 [mtrace()](http://manpages.ubuntu.com/mtrace)、[malloc_stats()](http://manpages.ubuntu.com/malloc_stats)、[malloc_info()](http://manpages.ubuntu.com/malloc_info)、[memusage](http://manpages.ubuntu.com/memusage),以及该系列中的其他函数。