Course contentsShow
← Security
Lesson 2108 of 3,05242. Penetration Testing and Red TeamingFree lesson

Memory Corruption Exploits: Buffer Overflows

Exploiting stack-based buffer overflows to overwrite return addresses and redirect execution flow.

Memory Corruption Exploits: Buffer Overflows

You'll learn how attackers exploit stack-based buffer overflows to hijack program execution by overwriting critical memory addresses.

The Core Idea

A buffer overflow occurs when a program writes more data into a fixed-size memory buffer than it can hold. On the stack, local variables sit alongside critical control data like the return address—the pointer telling the CPU where to jump after a function completes. By overflowing a buffer, an attacker can overwrite this return address with their own chosen value, redirecting execution to malicious code.

Think of it like a hotel guest registry: each guest (function) writes their name in a designated space, with the checkout instructions (return address) written just below. If a guest writes their name too long, they scribble over the checkout instructions, potentially directing staff to the wrong room—or to a room the attacker controls.

How It Works

  1. Vulnerable code accepts user input without bounds checking (e.g., strcpy, gets)
  2. Stack layout: buffer → saved frame pointer → return address
  3. Overflow: malicious input floods the buffer, overwriting the return address
  4. Return hijack: when the function returns, CPU jumps to attacker-controlled address
  5. Payload execution: attacker's shellcode or gadget chain runs

The attacker calculates the exact offset needed to reach the return address, then crafts input containing padding + new address + (optionally) executable payload.

Historical Importance

This technique defined early exploitation—the 1988 Morris Worm used it, and it dominated vulnerabilities through the 2000s. Modern defenses (ASLR, DEP, stack canaries) make basic exploitation harder, but buffer overflows remain foundational to understanding memory corruption and building advanced exploits like ROP chains.

Key Takeaway: Stack-based buffer overflows exploit poor input validation to overwrite return addresses, hijacking control flow—the classic technique that launched modern exploitation research and defensive technologies.