1 / 23

[ Overflow ]

[ 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 사전적 의미 : 넘치다 , 범람 컴퓨터 분야에서의 의미 제한된 크기를 지닌 저장공간에 그 크기 보다 큰 데이터가 입력되는 것을 의미

tayte
Download Presentation

[ Overflow ]

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. [ Overflow ] Security and Programming SSM_14th김광철

  2. Agenda • What is the Overflow? • Stack Overflow • Heap Overflow • Integer Overflow • Writing Secure code

  3. What is the Overflow • Overflow • 사전적 의미 : 넘치다, 범람 • 컴퓨터 분야에서의 의미 • 제한된 크기를 지닌 저장공간에 그 크기 보다 큰 데이터가 입력되는 것을 의미 • 결과적으로 프로그램의 오동작의 원인 • 변수의 크기는 인간의 판단으로 충분하다고 생각하는 크기 • Underflow는 반대되는 의미로서 포맷스트링 버그라고도 함.

  4. What is the Overflow • 종류 • Stack Overflow • Heap Overflow • Frame pointer Overflow • Integer Overflow

  5. What is the Overflow • Overflow의 위험성 • Overflow는 프로그램의 오동작을 유발한다. • Overflow를 통해 자신이 원하는 코드를 실행시키거나, 또는 관리 권한을 획득 임의의 사용자에게 관리 권한이 노출됨 • Overflow는 탐지가 어렵다.(현재까지 탐지하는 방법은 거의 없음) • Exploit, 바이러스 및 웜 등의 침입방법으로서 사용됨 ex) Blaster worm : RPC의 버퍼 오버플로우 이용 –MS 보안 게시판 MS03-026 참조

  6. text data stack Stack Overflow lower memory address • 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 higher memory address

  7. buffer sfp ret str Stack Overflow • Stack overflow의 근본적 원인 • C 언어는 bound check를 잘 하지 않는다. ex) gets(), strcpy(), strcat(), getc(), etc • 최근에 오면서 리눅스에서도 알 수 있듯이 Open source를 지향하고 있다. • Stack Overflow Simple Example void function(char *str) { char buffer[8]; strcpy(buffer,str); }

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

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

  10. ………………………………………………………..……………………………………………………….. Integer Overflow • 정수란 ? • 분수형으로 나타내지 않을 수 있는 실제적인 수 • 정수형 또한 다른 변수들과 마찬가지로 메모리 상의 한 부분이다. • 양수 음수를 표현하기 위한 방법이 필요 * 첫비트를 부호 비트로 사용 • 일반적으로 32bit(4bytes)의 크기를 갖는다. 첫 비트 int에서는 부호비트, unsigned int에서는 부호비트가 아니다.

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

  12. Integer Overflow • Widthness overflows • 변수가 저장하려는 값보다 작을 때 발생 • Arithmetic overflows • 변수에 저장하려는 연산의 결과값이 변수의 크기보다 클 때 발생

  13. 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 */

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

  15. 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

  16. 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; }

  17. Integer Overflow • Signedness bugs • unsigned ~> signed,signed~> unsigned • Signed 형 정수가 비교문에서 사용될때 • 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] */ }

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

  19. Writing Secure Code • Overflow를 어떻게 방지할 것인가? • 잘 코딩하라!!! • 외부에서 입력되는 데이터를 신용하지 마라. • C를 사용할 경우 bound check를 하지 않는 함수의 사용을 자제한다. ex) strcpy, strcat, getc…. Etc. • 안전한 문자열의 전송은 길이를 같이 전송하는 것이다. • compiler의 옵션 사용

  20. Writing Secure code • Overflow를 완벽히 막는다는 것은 현재로선 거의 불가능하다. • Visual C++.Net 의 /GS option • 스택에 선언되는 로컬 변수와 ebp 포인터, 리턴 주소와 해당 함수의 오류 처리 루틴을 서로 검증하는 새로운 컴파일러 옵션 gcc의 StackGuard 와 유사 • 그러나 모든 overflow를 검출하지는 못한다. - heap overflow

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

  22. Conclusion • 안전하지 못한 함수 호출 금지 • 코드 리뷰 • 철저한 테스트 • 소스 코드 스캐닝 툴 • 가장 좋은 것은 안전하고 견실한 잘 짜여진 프로그램 코드이다.

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

More Related