



~ Heap Pwn 方法論 ~
講到研究 heap pwn,和其他 vulnerability 研究題目不同的是,不是由漏洞和方法開始;很重要的是要先明白內部格式。有幾個知道是 pre-requisite,是需要預先知道的:
- Chunk 格式
- Assign 和 Free 的過程,和對 Chunk 的影響
- 不同的 Bin 的情況
會包括這篇在內,連續用幾篇的篇幅先解釋這些。然後才可以進入 vulnerability 和相關方法的討論。
值得留意的是 Heap Pwn 方法的用處只屬 Researcher 或 CTF 比賽用途。不建議作其他用途。
~ malloc_chunk 格式 ~
struct malloc_chunk { // below size assumed 64-bit systems
INTERNAL_SIZE_T prev_size; // 8 bytes, size of previous chunk (if free)
INTERNAL_SIZE_T size; // 8 bytes, size in bytes, including overhead
struct malloc_chunk* fd; // 8 bytes, double linked-list. used only if freed
struct malloc_chunk* bk; // 8 bytes, same
struct malloc_chunk* fd_nextsize; // 8 bytes, double linked-list. used only if freed, and only large blocks.
struct malloc_chunk* bk_nextsize; // 8 bytes, same
} // 48 bytes in total


講解下幾個成員變數:
- prev_size:如果上一個 chunk 是 freed,那麼是它的 size。否則就會是上一個 chunk 的一部份,會重疊了。
- INTERNAL_SIZE_T = typedef unsigned long size_t = 8 bytes on 64-bit machines
- size:現在的 chunk 的大小。
- 必然是 2*SIZE_SZ 的整數倍。在 64-bit 環境下,SIZE_SZ=8;32-bit 環境下 SIZE_SZ=4。
- 而且最後 3 個 bit 是狀態標籤:
- NON_MAIN_ARENA:標記 chunk 是來自 main arena。
- IS_MMAPPED:標記 chunk 是來自 mmap。
- PREV_INUSE:標記上一個 chunk 是 assigned (1) 或 freed (0)。
- 因為就算是 32-bit 底下,2*SIZE_SZ = 8。所以 size 最小都會是 0x1000。即是,最後 3 個 bit 必然不用。
- fd, bk
- 若是 freed:這會是在 bin 鏈結串列中,指向上一個和下一個 free chunk。
- 若是 assigned:這會是使用者使用的空間,即是資料空間的一部份。
- fd_nextsize, bk_nextize
- 若是 freed:僅用於 large bin;指向前一個和後一個和當前 chunk 不同大小的 chunk。
- 若是 assigned:這會是使用者使用的空間,即是資料空間的一部份。
~ Reference ~
Glibc Malloc Principle
https://www.openeuler.org/en/blog/wangshuo/Glibc%20Malloc%20Principle/Glibc_Malloc_Principle
Tut09: Exploiting Heap Allocators
https://tc.gts3.org/cs6265/2019/tut/tut09-02-advheap.html
The Heap: How to exploit a Heap Overflow – bin 0x15
https://www.youtube.com/watch?v=TfJrU95q1J4
Heap exploitation, glibc internals and nifty tricks.
https://blog.quarkslab.com/heap-exploitation-glibc-internals-and-nifty-tricks.html
《資安 pwn 不正常技術研究所》系列: