-
fastbin_dup - how2heapHeap exploitation/how2heap 2020. 2. 17. 20:22
Abstract
double-free bug를 이용해 fastbin에 동일한 청클를 두번 등록해 할당받을 수 있는 기법이다.
Exploit Flow
1. 동일한 크기의 fast chunk를 3개 할당
2. (1), (2) 순서로 free
3. (1)을 한번 더 free → double-free
4. 동일한 크기의 fast chunk를 3개 할당
→ 첫번째, 세번째 할당받은 주소가 같다.
Code
#include <stdio.h> #include <stdlib.h> int main() { fprintf(stderr, "This file demonstrates a simple double-free attack with fastbins.\n"); // 3개의 힙을 할당 fprintf(stderr, "Allocating 3 buffers.\n"); int *a = malloc(8); int *b = malloc(8); int *c = malloc(8); fprintf(stderr, "1st malloc(8): %p\n", a); fprintf(stderr, "2nd malloc(8): %p\n", b); fprintf(stderr, "3rd malloc(8): %p\n", c); // 첫번째 힙을 해제 fprintf(stderr, "Freeing the first one...\n"); free(a); // 첫번째 힙이 빈의 가장 상위에 있기 때문에 다시 해제하면 크래시가 난다. fprintf(stderr, "If we free %p again, things will crash because %p is at the top of the free list.\n", a, a); // free(a); // 그래서 먼저 다른 힙을 해제한다. fprintf(stderr, "So, instead, we'll free %p.\n", b); free(b); // 이렇게 되면 첫번째 힙을 다시 해제해도 크래시가 나지 않는다. fprintf(stderr, "Now, we can free %p again, since it's not the head of the free list.\n", a); free(a); // 이제 다시 세번 malloc을 하면 (1), (2), (1) 순서로 할당을 해줄 것이다. fprintf(stderr, "Now the free list has [ %p, %p, %p ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a); fprintf(stderr, "1st malloc(8): %p\n", malloc(8)); fprintf(stderr, "2nd malloc(8): %p\n", malloc(8)); fprintf(stderr, "3rd malloc(8): %p\n", malloc(8)); }
malloc을 세번 했을 때의 힙 구조이다. 여기서 (1), (2) 순으로 free를 하면 아래와 같이 빈에 들어간다.
fastbinsY[0]에 (2) → (1) 순서로 들어가 있다. 여기서 다시 (1)을 free하게 되면 (1) → (2) → (1) → (2) ... 이 될 것이다.
의도대로 (1) → (2) → (1) → (2) ... 가 되었다. 이제 같은 크기의 힙을 요청하면 fastbinsY[0]에서 꺼내 할당해줄 것이므로 (1), (2), (1) ... 순서로 재할당이 될 것이다.
세번의 할당이 끝난 후 빈의 상황이다. 세번 할당을 해줬지만 서로가 서로를 가리키고 있으므로 계속해서 반복될 것이다.
'Heap exploitation > how2heap' 카테고리의 다른 글
House of Orange - how2heap (0) 2020.02.26 fastbin_dup_consolidate - how2heap (0) 2020.02.17 fastbin_dup_into_stack - how2heap (0) 2020.02.17