UAF (Use After Free)

2022. 3. 12. 23:00·System Hacking
반응형

Heap영역은 동적으로 메모리가 할당되고 해제된다.

할당될 때는 malloc, calloc, realloc등이 사용된다. 해제할때는 free함수를 사용해서 할당한 부분을 해제해준다.

UAF(Use After Free)는 메모리 영역을 할당하고 해제한 후에 재사용할 경우 발생하는 취약점이다.

 

코드를 보면 좀더 쉽게 이해가 가능하다.

 

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

int main()
{
        int* heap1;
        int* heap2;
        int* heap3;

        heap1 = (int*)malloc(sizeof(int) * 40);
        heap2 = (int*)malloc(sizeof(int) * 40);

        printf("heap1 address : %p\n", heap1);
        printf("heap2 address : %p\n", heap2);

        *heap2 = 1234;
        printf("heap2 value : %d\n", *heap2);
        free(heap2);
        printf("heap2 is free \n");

        heap3 = (int*)malloc(sizeof(int) * 40);
        printf("heap3 address : %p\n", heap3);

        return 0;
}

컴파일

 

40바이트를 heap영역이 두 번 할당하고 heap2를 해제한뒤 heap3를 다시 할당한다.

그리고 heap3영역의 주소를 봤을때 heap2가 해제된 영역의 주소와 heap3할당된 주소가 똑같은 것을 볼 수 있다.

힙의 할당을 좀 더 효율적으로 하기 위해, 반환된 heap영역의 크기를 기억해놨다가 같은 크기의 할당 요청이 들어오면 이전 영역을 재사용 하는것이다.

또한 해제(free)를 한다고 해서 해당 영역이 초기화 되는것이 아니라 재사용했을때 값이 그대로 들어가있다.

위와 같은 그림으로 같은영역에 할당 - 해제 -할당 된 것을 볼 수 있다.

 

이렇게 해제된 공간을 재사용하면 원하지 않는 값을 참조할 수 있다.

이러한 취약점을 이용하여 어떤 heap영역에 할당 후 해제되는 것을 보면 새로운 heap을 할당하고

악의적인 코드를 집어넣고 기존 heap을 실행시켜 공격할 수있다.

반응형

'System Hacking' 카테고리의 다른 글

Return to Libc (RTL)  (0) 2022.02.18
GOT Overwrite  (0) 2022.01.22
PLT & GOT 파헤치기  (0) 2022.01.20
'System Hacking' 카테고리의 다른 글
  • Return to Libc (RTL)
  • GOT Overwrite
  • PLT & GOT 파헤치기
Penguin Dev
Penguin Dev
What does the Penguin say?
    글쓰기 관리
  • Penguin Dev
    Pengha!
    Penguin Dev
  • 전체
    오늘
    어제
    • 분류 전체보기 (151) N
      • Java & Spring (3) N
      • System Hacking (4)
      • Algorithm (8)
        • Sorting algorithm (3)
      • Python (6)
      • DB (1)
      • Web (2)
        • Web Hacking & Security (2)
      • Write-Up (108)
        • pwnable.kr (17)
        • HackCTF (16)
        • 해커스쿨 FTZ (21)
        • LOB(lord of bufferoverflow) (19)
        • LOS (lord of sql injection) (28)
        • XSS-game (6)
        • Webhacking.kr (1)
      • SUA (19)
        • 오픈소스 보안 (19)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    computeifpresent
    putval()
    selection sort
    코드트리
    lord of bufferoverflow
    ConcurrentHashMap
    concurrenthashmap vs hashmap
    computeifabsent
    시스템해킹
    코딩테스트
    nop sled
    enumerate #list comprehension
    computeifabsent()
    코드트리조별과제
    spring boot
    sqlinjection
    동시성처리
    computeifpresent()
    DB정리
    hashmap vs concurrenthashmap
    AtomicLong
    CountDownLatch
    LOB
    MAP
    thread-safe
    동시성
    tabat
    spring
    Bubble Sort
    Java
  • 최근 댓글

  • 반응형
  • hELLO· Designed By정상우.v4.10.3
Penguin Dev
UAF (Use After Free)
상단으로

티스토리툴바