Buffer Overflow

Buffer Overflow

One-liner: A memory corruption vulnerability where data written beyond a buffer's boundary overwrites adjacent memory, potentially allowing arbitrary code execution.

🎯 What Is It?

A buffer overflow occurs when a program writes more data to a buffer (fixed-size memory block) than it can hold, causing data to overflow into adjacent memory locations. This can overwrite critical data like return addresses, function pointers, or other variables, leading to crashes, privilege escalation, or remote code execution.

Core concept: Writing 10 bytes to an 8-byte buffer = 2 bytes overflow into adjacent memory.

πŸ€” Why It Matters

Impact

Historical Significance

πŸ”¬ How It Works

Memory Layout (Stack)

High Memory
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Command Args β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Environment  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    Stack     β”‚ ← Function calls, local variables
β”‚      ↓       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      ↑       β”‚
β”‚     Heap     β”‚ ← Dynamic allocation (malloc)
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     BSS      β”‚ ← Uninitialized data
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    Data      β”‚ ← Initialized data
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    Text      β”‚ ← Program code
Low Memory

Stack Frame Structure

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” ← Higher addresses
β”‚ Function args    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Return address   β”‚ ← Target for overwrite!
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Saved base ptr   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Local variables  β”‚
β”‚ (buffers here)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ← Lower addresses

Exploitation Flow

// Vulnerable code
void vulnerable_function(char *input) {
    char buffer[64];          // 64-byte buffer
    strcpy(buffer, input);    // No bounds checking!
}

int main(int argc, char **argv) {
    vulnerable_function(argv[1]);
    return 0;
}

Attack:

Input: 64 bytes (A's) + return address overwrite + shellcode
       └──Buffer fillβ”€β”€β”˜ └──Control flowβ”€β”€β”˜ └──Payloadβ”€β”€β”˜

πŸ“Š Types of Buffer Overflows

Type Location Exploitation Complexity
Stack-based Stack memory Overwrite return address Medium
Heap-based Heap memory Overwrite function pointers/vtables High
Integer Overflow Any Cause buffer size miscalculation Medium
Off-by-one Any Write 1 byte beyond buffer High
Format String Stack Use format specifiers to write memory High

πŸ”“ Exploitation Techniques

1. Basic Stack Overflow

# Generate payload
payload = b"A" * 64           # Fill buffer
payload += b"BBBB"            # Overwrite saved base pointer
payload += b"\x10\x20\x30\x40" # Overwrite return address (little-endian)
payload += shellcode          # Execute this code

# Send payload
./vulnerable_program "$(python -c 'print payload')"

2. Return-to-libc (Bypass NX)

# Instead of shellcode, call existing functions
payload = b"A" * 64
payload += p32(libc_system)   # Address of system()
payload += p32(0x41414141)    # Fake return address
payload += p32(libc_binsh)    # Address of "/bin/sh" string

3. ROP (Return-Oriented Programming)

# Chain together existing code snippets ("gadgets")
payload = b"A" * 64
payload += p32(gadget1)  # pop eax; ret
payload += p32(value)
payload += p32(gadget2)  # pop ebx; ret
payload += p32(value2)
payload += p32(gadget3)  # int 0x80 (syscall)

πŸ›‘οΈ Detection & Prevention

How to Detect (Blue Team)

Static Analysis

# Find vulnerable functions
grep -r "strcpy\|strcat\|sprintf\|gets" source_code/

# Use static analyzers
flawfinder source_code.c
cppcheck --enable=all source_code.c

Dynamic Analysis

# Fuzz testing with AFL
afl-fuzz -i input_dir -o output_dir ./target_binary @@

# Memory sanitizers
gcc -fsanitize=address program.c -o program
./program

Runtime Detection

How to Prevent / Mitigate

Secure Coding Practices

// ❌ Vulnerable
strcpy(buffer, user_input);

// βœ… Secure
strncpy(buffer, user_input, sizeof(buffer) - 1);
buffer[sizeof(buffer) - 1] = '\0';

// βœ… Better
snprintf(buffer, sizeof(buffer), "%s", user_input);

Modern Protections

Protection Description Bypass Difficulty
DEP/NX (Data Execution Prevention) Marks stack as non-executable Medium (ROP)
ASLR (Address Space Layout Randomization) Randomizes memory addresses Medium (info leak)
Stack Canaries Detection value before return address Medium (leak/overwrite)
RELRO (Relocation Read-Only) Makes GOT read-only High
CFI (Control Flow Integrity) Validates control flow transfers High
SafeSEH Validates exception handlers Medium

Compiler Flags

# Enable all protections
gcc -fstack-protector-all \    # Stack canaries
    -D_FORTIFY_SOURCE=2 \      # Runtime checks
    -Wl,-z,relro,-z,now \      # Full RELRO
    -fPIE -pie \               # PIE (ASLR for executable)
    program.c -o program

πŸ” Famous Vulnerabilities

CVE Target Year Impact
CVE-2017-0144 Windows SMB (EternalBlue) 2017 WannaCry, NotPetya ransomware
CVE-2019-18634 sudo 2019 Local privilege escalation
CVE-2014-0160 OpenSSL (Heartbleed) 2014 Memory leak (related concept)
CVE-2001-0500 IIS (Code Red) 2001 Remote code execution

🎀 Interview Angles

Common Questions

STAR Story

Situation: Penetration test revealed a legacy internal application that crashed when submitting long usernames.
Task: Determine if the crash was exploitable and assess risk to the organization.
Action: Used gdb with pattern generation to identify the exact offset to the return address (272 bytes). Verified I could control EIP. Checked protectionsβ€”no ASLR, no NX. Developed proof-of-concept exploit that spawned a shell with elevated privileges. Demonstrated to stakeholders by popping calc instead of malicious payload.
Result: Application was immediately taken offline. Vendor patched the vulnerability within 2 weeks. Recommended input validation, bounds checking, and compiler protections for all internal development going forward.

βœ… Best Practices

❌ Common Misconceptions

πŸ“š References