Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Exploit development and reversing

Lab · Hard · 1.5 hoursFree account

Buffer Overflow Basics

Classic stack overflow: control EIP/RIP, find bad chars, and deliver shellcode.

  • buffer-overflow
  • stack
  • eip
  • shellcode
  • gdb
  • oscp
On this page 8 sections
  1. 1How the Stack Works
  2. 2What is a Buffer Overflow?
  3. Locked: Lab Setup
  4. Locked: Fuzzing (Find the Crash)
  5. Locked: Find the Exact Offset
  6. Locked: Controlling EIP
  7. Locked: Generate Shellcode
  8. Locked: Full Exploit Script

Before you start

A stack buffer overflow writes past the end of a fixed-size buffer and overwrites the saved return address on the stack. An attacker who controls that address can run arbitrary code, and this flaw is behind the Morris worm, SQL Slammer, and many memory-corruption CVEs since. The lab crashes a vulnerable C program, finds the exact offset to EIP, and redirects execution into shellcode.

Lab Objectives

  • Understand how the stack and EIP register control program flow
  • Crash a vulnerable program by overflowing a buffer
  • Use pattern generation to find the exact offset to EIP
  • Overwrite EIP to redirect execution
  • Generate shellcode and get a reverse shell

Buffer overflows are one of the oldest vulnerabilities in computing, but they still appear on the OSCP exam and in real-world CTFs. Understanding them builds a deep knowledge of how programs actually work in memory.

Step 1: How the Stack Works

# The stack is a region of memory used for:
# - Local variables
# - Function parameters
# - Return addresses (where to go after function ends)

High addresses
┌──────────────┐
│  Parameters  │
├──────────────┤
│  Return Addr │ ← EIP (Instruction Pointer)
├──────────────┤  ← EBP (Base Pointer)
│  Saved EBP   │
├──────────────┤
│  Local vars  │ ← buffer lives here
│  buffer[64]  │
└──────────────┘
Low addresses (stack grows DOWN)

# Key registers:
EIP → Points to next instruction to execute. Control this = control execution.
ESP → Stack pointer (top of stack)
EBP → Base pointer (bottom of current stack frame)

Step 2: What is a Buffer Overflow?

# Vulnerable C code:
void vulnerable_function(char *input) {
    char buffer[64];      // only 64 bytes allocated
    strcpy(buffer, input); // no length check! copies everything
}
// If input > 64 bytes → overwrites saved EBP → overwrites return address (EIP)

# What happens with a 100-byte input:
buffer[0-63]  → fills the buffer (normal)
bytes[64-67]  → overwrites saved EBP
bytes[68-71]  → OVERWRITES EIP (return address!)

When function returns → jumps to whatever address is in EIP
→ If we control EIP, we control where code execution goes

Sign in to continue

6 more sections are for signed-in members. It is free: sign in with Google.