[Dreamhack] off_by_one_001

김성진·2022년 7월 17일
0

Dreamhack_System

목록 보기
9/44

📒 Description

문제를 풀어보자.


📒 C code

#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_str(char *ptr, int size)
{
    int len;
    len = read(0, ptr, size);
    printf("%d", len);
    ptr[len] = '\0';
}

void get_shell()
{
    system("/bin/sh");
}

int main()
{
    char name[20];
    int age = 1;

    initialize();

    printf("Name: ");
    read_str(name, 20);

    printf("Are you baby?");

    if (age == 0)
    {
        get_shell();
    }
    else
    {
        printf("Ok, chance: \n");
        read(0, name, 20);
    }

    return 0;
}

read_str 함수를 보면 read(0, ptr, 20)을 하며 ptr[len] = 0으로 맞추게 된다.
만약 우리가 20바이트를 입려갛게 되면 ptr[20] = 0이 되고, 그 값은 age를 0으로 바꾼다.
따라서 get_shell을 실행하게 된다.


📒 Exploit

profile
Today I Learned

0개의 댓글