Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Include Page
00.Notice
00.Notice

List

Table of Contents
outlinetrue
excludeList

UAF(Use-After-Free)

  • UAF는 이전에 비워진 메모리를 사용하여 유효한 데이터의 손상에서 임의의 코드 실행에 이르기까지 여러 가지 부정적인 결과가 발생할 수 있습니다.
  • 예를 들어 다음 코드와 같이 프로그램이 2개의 메모리를 할당하고, 첫번째 메모리를 해제한 후 비슷한 크기의 메모리 할당을 요청하면 이전에 해제된 메모리가 할당됩니다.
    • 그리고 a에는 처음 할당받은 메모리의 포인터가 가지고 있으며, c도 같은 메모리의 포인터를 가지게됩니다. 
    • 즉, a가 가지고 있는 포인터를 이용하여 데이터 변경 및 출력이 가능합니다.
    • 이는 프로세스에서 정의되지 않은 동작을 유발합니다.

...

Code Block
titleUAF-2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char* a = malloc(160);
    strcpy(a, "Secret message");
    free(a);
    printf("%s\n",a);
}

Example

Example1

  • 다음 코드는 크기가 160byte, 256byte인 메모리의 할당을 malloc()에 요청합니다.
    • 반환된 pointer는 a, b에 저장됩니다. 
    • 첫번째 메모리가 해제되고, 144byte 메모리를 할당을 요청하고 반환된 pointer는 변수 c에 저장됩니다.
    • 해당 메모리에 문자열을 복사되고, a가 가리키는 메모리의 데이터를 출력합니다.

...

Code Block
titleOutput the data stored in the pointer of 'a'
gdb-peda$ c
Continuing.

Breakpoint 4, 0x0000000000400616 in main ()
gdb-peda$ i r rax
rax            0x602010	0x602010
gdb-peda$ x/s 0x602010
0x602010:	"Secret message"
gdb-peda$ c
Continuing.

Breakpoint 5, 0x000000000040061d in main ()
gdb-peda$ i r rdi
rdi            0x602010	0x602010
gdb-peda$ ni
Secret message

gdb-peda$ 

Example2

  • 이 예제에서는 UAF-2.c를 사용합니다.
    • 0x4005c8에서 할당된 pointer를 확인하고, 0x4005f5에서는 해제된 메모리의 데이터를 확인합니다. 
    • 0x400601에서는 데이터 출력을 확인합니다.

...

Code Block
titleOutput the data stored in the freed memory.
gdb-peda$ c
Continuing.

Breakpoint 3, 0x0000000000400601 in main ()
gdb-peda$ i r rdi
rdi            0x602010	0x602010
gdb-peda$ x/i $rip
=> 0x400601 <main+75>:	call   0x400480 <puts@plt>
gdb-peda$ ni
Secret message
gdb-peda$

Related information

...