>

UAF에 대한 고찰

Research 2014. 3. 9. 12:22

#include<stdio.h>

#include<stdlib.h>

int main()

{

char *buf;

char *buf2;

buf = malloc(20);

buf2 = malloc(20);

printf("%p %p\n",buf,buf2);

}


만약 프로그램에서 이런식으로 할당을 두 번 해주면 각각 할당된 주소가 다름

-------------------------------------

output


0x843e008 0x843e020


------------------------------------

#include<stdio.h>

#include<stdlib.h>

int main()

{

char *buf;

char *buf2;

buf = malloc(20);

printf("%p",buf);

free(buf);

buf2 = malloc(20);

printf(" %p\n",buf2);


}

----------------------------------------

하지만 이런식이라면 free 되기 전 buf와 buf2는 동일한 주소를 가질 것임.


output


0x827c008 0x827c008


같은 사이즈를 힙 할당할 때 이러한 현상이 일어남 -> 이것은 힙 매니지먼트 시스템에 의해서 그럼.


-----------------------------------------

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct poo_struct

{

char *message;

void (*constructor)(void);

};

void constructor()

{

printf("this is stupid constructor\n");

}

int main(int argc,int **argv)

{

char input[100] = {0,};

struct poo_struct *s1;

char *tmp;

s1 = malloc(12);

s1->constructor=constructor;

fgets(input,100,stdin);

s1->message = malloc(strlen(input));

strcpy(s1->message,input);

memset(input,0,100);

s1->constructor();

printf("message : %s\n",s1->message);


free(s1);

fgets(input,100,stdin);

tmp = malloc(strlen(input));

strcpy(s1->message,input);

s1->constructor();

}

--------------------------------------------------

만약 이런 정신나간 프로그램이 있다고 가정해보자

s1을 free 해주지만 안의 데이터는 사라지지 않고 그대로 쓰임이 가능하다. -> 그래서 use after free

내가 입력해준 만큼 malloc 해주니 두번째 fgets에서 12만큼 입력을 하면 constructor 부분을 덮을 수 있다.

-------------------------------------------------------

[0x62626262] --- SIGSEGV (Segmentation fault) ---

[0xffffffff] +++ killed by SIGSEGV +++

win32virus@ubuntu-for-b10s:~/uaf$ 

------------------------------------------------------

ltrace로 확인해보면 바뀌는 것을 알 수 있음


요약하자면


data = malloc(size);

free(data);

이런 상태에서 data 변수를 어디서 참조하면 유심히 봐야됨

'Research' 카테고리의 다른 글

쉘코드 수신 쉘코드  (0) 2015.09.12
arm exploitation using ROP  (0) 2015.02.26
arm exploitation using shellcode  (0) 2015.02.21
blind sql injection script template  (1) 2015.02.20
Posted by Mungsul
,