╱╱╭╮╱╱╱╱╱╱╭━━━╮╱╱╱╭╮╱╭╮╱╱╱╱╱╱ ╱╱┃┃╱╱╱╱╱╱┃╭━╮┃╱╱╱┃┃╱┃┃╱╱╱╱╱╱ ╱╱┃┣━━┳━━╮┃┃╱┃┣━╮╱┃╰━╯┣━━┳━╮╱ ╭╮┃┃╭╮┃┃━┫┃╰━╯┃╭╮╮┃╭━╮┃╭╮┃╭╮╮ ┃╰╯┃╭╮┃┃━┫┃╭━╮┃┃┃┃┃┃╱┃┃╭╮┃┃┃┃ ╰━━┻╯╰┻━━╯╰╯╱╰┻╯╰╯╰╯╱╰┻╯╰┻╯╰╯

CS/정보 보안

[정보 보안] Buffer Overflow (7)

재안안 2024. 6. 22. 19:34


[6] Buffer Overflow
prevention techniques는 알려져 있으나 코드가 너무 커짐
legacy code나 careless programming으로 인해 뚫림

BufferOverflow basics
more data to be stored thant capacity (fixed sized buffer)
- stack
- heeap
- global data

인접하고 있는 메모리를 overwrite
- corruption of program data
- unexpected transfer of control
- memory access violation
- execution of code chosen by attacker

사실 구현하기 존나 빡센 공격 방식이다
결국 메모리를 덮어써야하는데 덮어쓰는 메모리가 타겟인지를 알아야한다
- 버퍼가 어디에 있는지
- 얼마나 덮어써야 하는지

range of buffer overflow attacks
- stack overflow variants
- heap overflow
- global data overflow
- format string overflow (특수)
- integer overflow (특수)

Stack Buffer Overflow
ESP : 하나의 스택프레임의 끝 지점 주소가 저장된다. 
EBP : 하나의 스택프레임의 시작주소가 저장된다. 현재 스택프레임이 소멸되면 이전 스택프레임을 가르키게 된다.

Function Calls and Stack Frames
Stack frames
- calling function : 종료시 돌아갈 주소와 매개변수가 필요
- called function : local 변수를 저장할 공간이 필요
구조파악후 local 변수 또는 매개변수와 return address가 얼마나 인접하는지만 알면 덮어쓸 수 있다

Stack Overflow 유발하는 함수
- gets
- sprintf
- strcat
- strcpy
- vsprintf

Stack Overflow Variant
이상한 곳을 덮어쓸 수 있다

Buffer Overflow Defenses
코드를 짤 때 주의해서 짜야한다

compile-time : harden new programs
strong typing : compiler enforces range checks and permissible operations 
on variables

safe coding : check for sufficient space in any buffer

laguage extension, safe libraries

stack protection : stackgaurd (add function entry and exit code to check stack for signs of corruption)

run-time : handle attacks on existing programs
non-executable address space : virtual memory support (make some regions of memory non-executable)

address space randomization : maniupulate location of key data structures

guard pages (stack 주변에 gaurd page)

Heap Overflow
RET 변조는 불가하고 동적 메모리 할당 연결을 덮어씀으로써 프로그램 함수 포인터를 조작

Defenses : non-executable or random heap

Global Data Overflow
overwrite function pointer later called

Defenses: non-executable or random global data region, move function pointers, guard pages

Format String Overflow
application 레이어에서 커멘드로 인식될 때

Integer Overflow
??

Software Security
원인의 대부분은 코드를 못짜서
input validation이 부족하거나
진짜 생각못한 에러거나
버그 발생한다면 이슈는 총 몇개인지가 아니라 얼마나 자주 발생하는지

Defensive Programming
to all aspects of program execution, environment, data processed
assume nothing, check all potential errors
validate all assumptions

Hanlding Program Input (all input as danger)
identify all data sources
validate on size and type of values before use

- binary : encoding이 application에 따라 다를 수 있다
- text : encoded in ASCII

Injection Attacks
주로 scripting languages에서 발생

Cross Site Scripting Attacks
Attacks where input from one user is later output to another user