ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 0x04.How2heap - fastbin_dup
    0x.Heap Tutorial 2018. 3. 8. 14:18

    0x04.How2heap - fastbin_dup



    이번에는 fastbin의 취약점이다.

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    fprintf(stderr, "This file demonstrates a simple double-free attack with fastbins.\n");

    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);

    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));
    }

    이번에도 먼저 컴파일을 해서 실행시키고 차례대로 분석하도록 하겠다.

    This file demonstrates a simple double-free attack with fastbins.
    Allocating 3 buffers.
    1st malloc(8): 0x7a4010
    2nd malloc(8): 0x7a4030
    3rd malloc(8): 0x7a4050
    Freeing the first one...
    If we free 0x7a4010 again, things will crash because 0x7a4010 is at the top of the free list.
    So, instead, we'll free 0x7a4030.
    Now, we can free 0x7a4010 again, since it's not the head of the free list.
    Now the free list has [ 0x7a4010, 0x7a4030, 0x7a4010 ]. If we malloc 3 times, we'll get 0x7a4010 twice!
    1st malloc(8): 0x7a4010
    2nd malloc(8): 0x7a4030
    3rd malloc(8): 0x7a4010

    처음부터 시작을 해보면

    int *a = malloc(8);
    int *b = malloc(8);
    int *c = malloc(8);

    a, b, c 영역을 8바이트의 크기로 할당을 해준다. 주소값을 출력해보면,

    1st malloc(8): 0x7a4010
    2nd malloc(8): 0x7a4030
    3rd malloc(8): 0x7a4050

    각각 0x7a4010,30,50을 할당받았고, 서로 인접해있다는 사실을 알 수 있다. double free 버그를 발생시키기 위해 가장 먼저 동적 할당된 영역 a를 free해준다.


    free(a);

    해당 주소값을 보면

    If we free 0x7a4010 again, things will crash because 0x7a4010 is at the top of the free list.

    0x7a4010이고, 이는 fastbin의 가장 최상위 free list에 위치할 것이다. 참고로 fastbin 같은 경우 할당을 요청하는 메모리 영역과 같은 크기의 free청크가 list에 존재할 경우 그 영역을 재할당한다. 즉, 만약 여기서 malloc(8)을 통해 할당을 진행하면 free가 진행된 a영역인 0x7a4010에 재할당 되게 된다. 어쨌거나 계속 따라가도록 하겠다.

    free(b);

    이번에는 2번째 동적할당 영역인 b를 free하였고, 결과를 출력해보니


    So, instead, we'll free 0x7a4030.

    0x7a4030영역이 free된다. 이를 통해 fastbin의 freelist의 최상위에는 동적 할당 영역인 b가 위치하게 될 것이다. 여기서 fastbin의 문제가 발생한다. 다시 a 영역에 대한 free가 가능하기 때문이다.


    free(a);

    a영역을 다시한번 해제하였더니.

    Now, we can free 0x7a4010 again, since it's not the head of the free list.

    정상적으로 free가 진행이 되었다!!

    Now the free list has [ 0x7a4010, 0x7a4030, 0x7a4010 ]. If we malloc 3 times, we'll get 0x7a4010 twice!

    a라는 동적할당된 영역이 2번이나 해제가 된 것이다.


    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));

    a, b, c 영역의 주소값을 할당한 뒤 출력해보았더니,

    1st malloc(8): 0x7a4010
    2nd malloc(8): 0x7a4030
    3rd malloc(8): 0x7a4010

    a, b, a 순서로 할당이 된다. fastbin의 경우에 *fd를 사용하여 free 청크에 대한 관리를 진행하는데, 같은 영역이 재할당되는 경우가 발생한다. 이를 통해서 의도적으로 어떤 한 영역을 double-free 버그를 활용하여 해제를 진행하면 그 위치에 정확하게 값을 쓸 수 있다. 즉 이미 할당되어 데이터 값이 들어있는 영역으로 의도적으로 접근하여 정보 수정이 가능하다.


    최근의 CTF 문제에서 이 버그를 활용하여 double-free 버그를 활용하여 의도적으로 어떤 동적 메모리 영역에 접근을 하여 *fd값을 바꿔 익스를 진행하는 문제들이 출제되었다. fastbin_dup_into_stack을 통해 알 수 있지만, 힙 영역을 가리키는 *fd 값을 스택 또는 다른 영역으로 변경하여 자신이 의도하는 주소로 청크를 할당하여 익스를 진행하는 방법으로도 발전이 가능하다.

    '0x.Heap Tutorial' 카테고리의 다른 글

    0x06.Malloc hook  (0) 2018.03.19
    0x05.How2heap - fastbin_dup_into_stack  (0) 2018.03.12
    0x03.How2heap - first_fit  (0) 2018.03.08
    0x02.Arena and Chunk  (0) 2018.03.07
    0x01.Glibc Bins  (0) 2018.03.06

    댓글

Designed by Tistory.