ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 0x03.How2heap - first_fit
    0x.Heap Tutorial 2018. 3. 8. 13:43

    0x03.How2heap - first_fit



    자 이제 Heap Exploit에 대한 공부를 시작하도록 한다.

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

    int main()
    {
    fprintf(stderr, "This file doesn't demonstrate an attack, but shows the nature of glibc's allocator.\n");
    fprintf(stderr, "glibc uses a first-fit algorithm to select a free chunk.\n");
    fprintf(stderr, "If a chunk is free and large enough, malloc will select this chunk.\n");
    fprintf(stderr, "This can be exploited in a use-after-free situation.\n");

    fprintf(stderr, "Allocating 2 buffers. They can be large, don't have to be fastbin.\n");
    char* a = malloc(512);
    char* b = malloc(256);
    char* c;

    fprintf(stderr, "1st malloc(512): %p\n", a);
    fprintf(stderr, "2nd malloc(256): %p\n", b);
    fprintf(stderr, "we could continue mallocing here...\n");
    fprintf(stderr, "now let's put a string at a that we can read later \"this is A!\"\n");
    strcpy(a, "this is A!");
    fprintf(stderr, "first allocation %p points to %s\n", a, a);

    fprintf(stderr, "Freeing the first one...\n");
    free(a);

    fprintf(stderr, "We don't need to free anything again. As long as we allocate less than 512, it will end up at %p\n", a);

    fprintf(stderr, "So, let's allocate 500 bytes\n");
    c = malloc(500);
    fprintf(stderr, "3rd malloc(500): %p\n", c);
    fprintf(stderr, "And put a different string here, \"this is C!\"\n");
    strcpy(c, "this is C!");
    fprintf(stderr, "3rd allocation %p points to %s\n", c, c);
    fprintf(stderr, "first allocation %p points to %s\n", a, a);
    fprintf(stderr, "If we reuse the first allocation, it now holds the data from the third allocation.");
    }

    위의 코드를 리눅스에서 컴파일해서 실행시켜보았더니,

    This file doesn't demonstrate an attack, but shows the nature of glibc's allocator.
    glibc uses a first-fit algorithm to select a free chunk.
    If a chunk is free and large enough, malloc will select this chunk.
    This can be exploited in a use-after-free situation.
    Allocating 2 buffers. They can be large, don't have to be fastbin.
    1st malloc(512): 0x24b2010
    2nd malloc(256): 0x24b2220
    we could continue mallocing here...
    now let's put a string at a that we can read later "this is A!"
    first allocation 0x24b2010 points to this is A!
    Freeing the first one...
    We don't need to free anything again. As long as we allocate less than 512, it will end up at 0x24b2010
    So, let's allocate 500 bytes
    3rd malloc(500): 0x24b2010
    And put a different string here, "this is C!"
    3rd allocation 0x24b2010 points to this is C!
    first allocation 0x24b2010 points to this is C!
    If we reuse the first allocation, it now holds the data from the third allocation.

    위와 같은 결과가 나왔다. 이제 하나씩 살펴보도록 하자.

    char* a = malloc(512);
    char* b = malloc(256);

    malloc()을 통해 a를 512, b를 256바이트 만큼 할당을 하였고,

    1st malloc(512): 0x24b2010
    2nd malloc(256): 0x24b2220

    0x24b2010, 0x24b2220이라는 주소값으로 해당 영역을 할당한 것을 확인할 수 있다.

    strcpy(a, "this is A!");

    strcpy로 512바이트의 동적 할당된 힙 영역에 "this is A!"라는 문자열을 넣었고

    first allocation 0x24b2010 points to this is A!

    0x24b2010이 값으로 "this is A!"를 가지고 있음을 알 수 있다.

    free(a);

    그런데 이 공간 a를 해제한 다음 unsorted bin 상태가 되면 어떤 일이 벌어질까? 512만큼의 공간이 해제 되었기에 그 보다 작은 공간인 500 바이트만큼을 malloc을 진행해보면,

    c = malloc(500);

    이 값은 바로,

    3rd malloc(500): 0x24b2010

    해제한 a영역을 재할당받는다.

    strcpy(c, "this is C!");

    데이터 값으로 "this is C!"를 넣고 확인해보니,

    3rd allocation 0x24b2010 points to this is C!

    역시나 이상없이 잘 들어간다. 그런데 만약에 free를 진행한 a의 값을 참조하면 어떤 결과가 일어날까?

    fprintf(stderr, "first allocation %p points to %s\n", a, a);

    위 구문의 결과로,

    first allocation 0x24b2010 points to this is C!

    "this is C!"를 가리키고 있는 것을 확인할 수 있는데, 이를 통해 말도 해제하고 그 영역을 재할당 받으면 재할당받은 데이터값을 기존의 포인터로 확인할 수 있다는 사실을 알 수 있다.


    이는 Use-After-Free 버그로써, 익스플로잇에 활용될 수 있다.

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

    0x05.How2heap - fastbin_dup_into_stack  (0) 2018.03.12
    0x04.How2heap - fastbin_dup  (0) 2018.03.08
    0x02.Arena and Chunk  (0) 2018.03.07
    0x01.Glibc Bins  (0) 2018.03.06
    0x00. glibc - ptmalloc2  (0) 2018.03.05

    댓글

Designed by Tistory.