0Pricing
Ethical Hacking Academy · Lesson

Memory and the Stack

How overflows work.

Memory and the Stack is a free Ethical Hacking Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ethical Hacking Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Process Memory Layout

When a program runs, the operating system gives it a memory layout with distinct regions: the text (code), data/BSS (globals), the heap (dynamic allocation), and the stack.

Buffer overflows most often abuse the stack, so understanding it is essential.

What Is the Stack?

The stack is a region of memory used for function calls. It stores local variables, function arguments, and return addresses.

It grows downward on x86: pushing data moves the stack pointer toward lower addresses. This direction matters when an overflow happens.

Stack Frames

Each function call creates a stack frame. A frame typically contains local variables, the saved base pointer (EBP), and the return address (saved EIP).

The return address tells the CPU where to continue once the function finishes. Controlling it is the heart of stack overflow exploitation.

Key Registers

On 32-bit x86, the important registers are:

  • ESP: stack pointer, top of the stack.
  • EBP: base pointer, anchor for the current frame.
  • EIP: instruction pointer, address of the next instruction.

If you control EIP, you control execution flow.

What Is a Buffer?

A buffer is a fixed-size block of memory, such as a local character array. The program expects data to fit within it.

If the program copies more data than the buffer can hold without checking the length, the excess spills into adjacent memory.

char buffer[64];
strcpy(buffer, user_input); // no bounds check

The Overflow

A buffer overflow occurs when input exceeds the buffer's size and overwrites neighboring data on the stack.

Because the saved EBP and return address sit just past local buffers, a large enough input can overwrite the return address, hijacking control flow.

Dangerous Functions

Overflows usually stem from unsafe C functions that do not check length:

  • strcpy, strcat, sprintf, gets.

Safer alternatives like strncpy or snprintf take a size limit, but legacy code is full of the dangerous ones.

gets(buffer); // never use: no length limit at all

What Overwriting EIP Gives You

When the vulnerable function returns, the CPU pops the (now attacker-controlled) return address into EIP and jumps there.

If you point EIP at your own injected machine code (shellcode), the CPU executes it with the privileges of the program.

Little-Endian Addressing

x86 is little-endian: multi-byte values are stored least-significant byte first. To overwrite the return address with 0x080414C3, you write the bytes in reverse order.

Getting endianness wrong is a common beginner mistake that makes exploits silently fail.

address 0x080414C3 -> bytes \xc3\x14\x04\x08

Modern Protections

Modern systems add defenses that complicate exploitation:

  • DEP/NX: marks the stack non-executable.
  • ASLR: randomizes memory addresses.
  • Stack canaries: detect overwrites before return.

This course focuses on the classic case (protections off) to teach fundamentals.

Why This Still Matters

Although modern defenses make classic stack overflows harder, the fundamentals remain essential. Embedded systems, IoT, and legacy software often lack these protections entirely.

Mastering the basic case is also the prerequisite for advanced techniques like ROP that bypass modern mitigations.

Quick Check

Recall which value the attacker ultimately wants to control.

Recap

You now understand the foundations:

  • The stack stores locals, saved EBP, and the return address, and grows downward.
  • ESP, EBP, EIP are the key x86 registers; controlling EIP controls execution.
  • Unsafe functions like strcpy and gets cause overflows.
  • x86 is little-endian; modern defenses are DEP, ASLR, and canaries.

Next you will fuzz a program to find the crash offset.

Frequently asked questions

Is the “Memory and the Stack” lesson free?

Yes — the full text of “Memory and the Stack” is free to read here on the web, and the Ethical Hacking Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ethical Hacking Academy course, upgrade to CoddyKit PRO.

What will I learn in “Memory and the Stack”?

How overflows work. You practise Ethical Hacking Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ethical Hacking Academy?

No prior experience is required. Ethical Hacking Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Memory and the Stack” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ethical Hacking Academy lesson?

Yes. Every Ethical Hacking Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Memory and the Stack
  2. Fuzzing for Crashes
  3. Controlling EIP
  4. Shellcode and Exploitation
← Back to Ethical Hacking Academy