23
[ Overflow ] Security and Programming SSM_14 th 김김김

[ Overflow ]

  • Upload
    tayte

  • View
    63

  • Download
    1

Embed Size (px)

DESCRIPTION

[ Overflow ]. Security and Programming. SSM_14 th 김광철. Agenda. What is the Overflow? Stack Overflow Heap Overflow Integer Overflow Writing Secure code. What is the Overflow. Overflow 사전적 의미 : 넘치다 , 범람 컴퓨터 분야에서의 의미 제한된 크기를 지닌 저장공간에 그 크기 보다 큰 데이터가 입력되는 것을 의미 - PowerPoint PPT Presentation

Citation preview

Page 1: [  Overflow ]

[ Overflow ]

Security and Programming

SSM_14th 김광철

Page 2: [  Overflow ]

Agenda

What is the Overflow? Stack Overflow Heap Overflow Integer Overflow Writing Secure code

Page 3: [  Overflow ]

What is the Overflow

Overflow 사전적 의미 : 넘치다 , 범람 컴퓨터 분야에서의 의미

제한된 크기를 지닌 저장공간에 그 크기 보다 큰 데이터가 입력되는 것을 의미 결과적으로 프로그램의 오동작의 원인 변수의 크기는 인간의 판단으로 충분하다고 생각하는 크기Underflow 는 반대되는 의미로서 포맷스트링 버그라고도 함 .

Page 4: [  Overflow ]

What is the Overflow

종류 Stack Overflow Heap Overflow Frame pointer Overflow Integer Overflow

Page 5: [  Overflow ]

What is the Overflow

Overflow 의 위험성 Overflow 는 프로그램의 오동작을 유발한다 . Overflow 를 통해 자신이 원하는 코드를 실행시키거나 , 또는 관리 권한을 획득

임의의 사용자에게 관리 권한이 노출됨 Overflow 는 탐지가 어렵다 .( 현재까지 탐지하는 방법은 거의 없음 ) Exploit, 바이러스 및 웜 등의 침입방법으로서 사용됨

ex) Blaster worm : RPC 의 버퍼 오버플로우 이용 – MS 보안 게시판 MS03-026 참조

Page 6: [  Overflow ]

Stack Overflow

Backgrounds Process 의 메모리 구조 Stack

LIFO(Last In First Out) PUSH/POP procedure, function call SP, FP (Stack Pointer, Frame Pointer Local variables, return address, previous stack frame, and etc

text

data

stack

lower memory address

higher memory address

Page 7: [  Overflow ]

Stack Overflow

Stack overflow 의 근본적 원인 C 언어는 bound check 를 잘 하지 않는다 .

ex) gets(), strcpy(), strcat(), getc(), etc 최근에 오면서 리눅스에서도 알 수 있듯이 Open source 를 지향하고 있다 .

Stack Overflow Simple Examplevoid function(char *str) { char buffer[8];

strcpy(buffer,str);} str

retsfp

buffer

Page 8: [  Overflow ]

Heap Overflow

Heap Program 실행시 동적으로 할당되는 메모리 영역 – 실행시 크기가 결정되는 가변적 데이터 저장 용도로 사용 C - malloc() 에 의해 할당되는 영역 할당되고 해제 된 영역은 Garbage Collector 와 같은 것에 의해 정리됨 . Stack 과 저장공간의 특성이 다를 뿐 Overflow 원리는 비슷

heap3heap2

none

heap1

Page 9: [  Overflow ]

Frame pointer Overflow

Buffer Overflow 의 한종류 1byte overflow 에 의한 FP(frame pointer) 변경에 의해 프로그램의 실행 흐름이 변경될 수 있다 .

Page 10: [  Overflow ]

Integer Overflow

정수란 ? 분수형으로 나타내지 않을 수 있는 실제적인 수 정수형 또한 다른 변수들과 마찬가지로 메모리 상의 한 부분이다 . 양수 음수를 표현하기 위한 방법이 필요

* 첫비트를 부호 비트로 사용 일반적으로 32bit(4bytes) 의 크기를 갖는다 .

………………………………………………………..

첫 비트 int 에서는 부호비트 , unsigned int 에서는 부호비트가 아니다 .

Page 11: [  Overflow ]

Integer Overflow

정수형의 종류 int, unsigned int, short, usigned short….

Integer Overflow 의 위험성 발생한 후에 알아차릴 수 없다 . 발견하기 어려워서 잘짜여진 에러 검사 코드도 피해갈 수 있다 . Integer overflow 의 경우 프로그램 수행결과가 올바른지 알 수 없다 . – 올바르다고 가정하고 진행된다 .

Page 12: [  Overflow ]

Integer Overflow

Widthness overflows 변수가 저장하려는 값보다 작을 때 발생

Arithmetic overflows 변수에 저장하려는 연산의 결과값이 변수의 크기보다 클 때 발생

Page 13: [  Overflow ]

Integer Overflow

Widthness overflows/* ex1.c - loss of precision */ #include <stdio.h>

int main(void){ int l; short s; char c;

l = 0xdeadbeef; s = l; c = l;

printf("l = 0x%x (%d bits)\n", l, sizeof(l) * 8); printf("s = 0x%x (%d bits)\n", s, sizeof(s) * 8); printf("c = 0x%x (%d bits)\n", c, sizeof(c) * 8);

return 0; } /* EOF */

Page 14: [  Overflow ]

l = 0xdeadbeef (32 bits) s = 0xffffbeef (16 bits) c = 0xffffffef (8 bits)

Page 15: [  Overflow ]

Integer Overflow

Arithmetic overflows/* ex2.c - an integer overflow */ #include <stdio.h>

int main(void){ unsigned int num = 0xffffffff;

printf("num is %d bits long\n", sizeof(num) * 8); printf("num = 0x%x\n", num); printf("num + 1 = 0x%x\n", num + 1);

return 0; } /* EOF */

num is 32 bits long num = 0xffffffff num + 1 = 0x0

Page 16: [  Overflow ]

Integer Overflow

Arithmetic overflow 를 이용한 exploit에 사용

int myfunction(int *array, int len){ int *myarray, i;

myarray = malloc(len * sizeof(int)); /* [1] */ if(myarray == NULL){ return -1; }

for(i = 0; i < len; i++){ /* [2] */ myarray[i] = array[i]; }

return myarray; }

Page 17: [  Overflow ]

Integer Overflow

Signedness bugsunsigned ~> signed,signed~> unsignedSigned 형 정수가 비교문에서 사용될때Signed 형 정수가 연산에서 사용될때Unsigned 형 정수가 signed 형 정수를 비교할때

int copy_something(char *buf, int len){ char kbuf[800];

if(len > sizeof(kbuf)){ /* [1] */ return -1; }

return memcpy(kbuf, buf, len); /* [2] */ }

Page 18: [  Overflow ]

Integer Overflow

signed 형 버그는 정수형 오버플로우에 의해 일어난다 . 프로그램이 음수를 고려하지 않았다면 부호 버그를 사용하여 signed 형 버그를 유발 할 수 있다 . Integer overflow 는 의도적인 버퍼 오버 플로우를 발생시킬 수 있다 .

Page 19: [  Overflow ]

Writing Secure Code

Overflow 를 어떻게 방지할 것인가 ? 잘 코딩하라 !!!

외부에서 입력되는 데이터를 신용하지 마라 . C 를 사용할 경우 bound check 를 하지 않는 함수의 사용을 자제한다 .

ex) strcpy, strcat, getc…. Etc. 안전한 문자열의 전송은 길이를 같이 전송하는 것이다 . compiler 의 옵션 사용

Page 20: [  Overflow ]

Writing Secure code

Overflow 를 완벽히 막는다는 것은 현재로선 거의 불가능하다 . Visual C++.Net 의 /GS option

스택에 선언되는 로컬 변수와 ebp 포인터 , 리턴 주소와 해당 함수의 오류 처리 루틴을 서로 검증하는 새로운 컴파일러 옵션

gcc 의 StackGuard 와 유사그러나 모든 overflow 를 검출하지는 못한다 . - heap overflow

Page 21: [  Overflow ]

Writing Secure Code

C 언어를 사용할 경우 bound check 를 하지 않은 strcpy, gets, strcat 같은 함수를 사용하지 않거나 사용해야 할 경우 길이 체크를 반드시 한다 . 변수의 타입 선언시 용도의 범위 (scope)에 맞게 선언하고 scope 검사도 같이 병행해야 한다 .

Page 22: [  Overflow ]

Conclusion

안전하지 못한 함수 호출 금지코드 리뷰철저한 테스트소스 코드 스캐닝 툴

가장 좋은 것은 안전하고 견실한 잘 짜여진 프로그램 코드이다 .

Page 23: [  Overflow ]

Appendix

References phrack-55-8 phrack-60-10 phrack-55-15 phrack-49-14 Corezine-1-1 Writing Secure code 2nd [ 정보문화사 ]