본문으로 건너뛰기

Heap Exploitation: tcache 공격 기법 입문

TL;DR: glibc의 tcache를 이해하면 heap exploit의 절반은 풀린다. 구조와 공격 패턴을 정리했다.

tcache란?

glibc 2.26부터 도입된 Thread Local Cache. 각 스레드가 freed chunk를 최대 7개까지 bins에 캐싱해 빠른 재할당을 지원한다. 빠른 만큼 보안 검증이 약하다 — exploit 입장에선 선물.

핵심 구조

typedef struct tcache_entry {
struct tcache_entry *next; // fd 포인터
struct tcache_perthread_struct *key; // double-free 탐지 (2.29+)
} tcache_entry;

key 필드가 double-free 방어용이지만, heap 주소를 leak할 수 있다면 우회 가능.

대표 공격 패턴

1. tcache poisoning

freed chunk의 fd를 덮어 임의 주소를 tcache bin에 삽입.

free(A)          → tcache[0x20]: A → NULL
*A = &target → tcache[0x20]: A → target
malloc(0x18) → A 반환
malloc(0x18) → target 반환 ← 임의 쓰기!

2. tcache dup (double-free)

glibc 2.29 이전: key 없음 → 동일 chunk 두 번 free 가능 → 위와 동일 흐름.

2.29 이후: key XOR로 난독화(2.32+). heap leak 후 XOR 값 계산해 우회.

3. house of botcake

large bin + tcache를 조합, key 검증 우회하면서 double-free 달성. 실전 CTF에서 자주 등장.

실습 환경

# pwndbg + pwntools
pip install pwntools
gdb-multiarch ./vuln
pwndbg> heap # heap 시각화
pwndbg> bins # bins 상태

pwn.college의 heap exploitation 챕터가 가장 체계적.

방어 관점

  • glibc 버전 업: safe-linking, key 검증 강화
  • AddressSanitizer (ASAN): UAF·double-free 런타임 탐지
  • jemalloc / tcmalloc: 대안 allocator

heap 공격은 "allocator를 속이는 예술"이다. tcache를 이해하면 fastbin → unsorted bin → large bin 공격도 자연스럽게 이어진다.