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

Workbook · Medium · 5 hoursFree account

Binary Exploitation Foundations

Memory layout, protections (ASLR, NX, canaries), and exploitation primitives.

  • memory-corruption
  • aslr
  • nx
  • stack-canaries
  • exploit-primitives
On this page 5 sections
  1. ·About This Workbook
  2. ·Chapter 1 — Memory & Process Layout
  3. Locked: Chapter 2 — Corruption Classes
  4. Locked: Chapter 3 — Mitigations
  5. Locked: Putting It Together

Before you start

Memory-corruption bugs sit under many of the worst vulnerabilities in software history. One missing length check lets a program read or write memory it does not own, and that small flaw can leak secrets or hand control of the process to an attacker. This workbook covers the foundation every exploit author and every defender needs: process memory layout, how a function call works on the CPU, the bug classes that produce corruption, and the protections modern systems add on top. You learn to read checksec and pwndbg output, to recognise the bug in source code, and to judge what NX, ASLR, canaries, PIE and RELRO each stop. Every example runs on a small C program you build on your own lab VM.

What You'll Learn

  • Draw the layout of a process in memory and name each segment
  • Read a stack frame and find the saved return address
  • Name the six argument registers of the System V AMD64 calling convention
  • Explain how a stack overflow corrupts control data, and how to fix the bug
  • Describe NX, ASLR, stack canaries, PIE and RELRO, and what each one stops
  • Read checksec and pwndbg output on a practice binary
  • Explain why an exploit that works on one run can fail on the next

About This Workbook

A memory-corruption bug lets a program read or write memory that it does not own. Many of the worst vulnerabilities in history are of this kind. The cause is small: one copy without a length check. The effect can be a crash, a data leak or a change of the program's control flow.

This workbook teaches the foundation: how a program sits in memory, how a function call works on the CPU, how the bug classes arise, and which protections the system adds. It is a defender's foundation. You learn to read the evidence in a debugger, to recognise the bug in source code, to fix it, and to judge what a protection does and does not give you.

The workbook shows no exploit for a real product. All examples use one small C program that you write, compile and break on your own lab VM. Each chapter ends with questions and answers. When you finish, practise on the hands-on lab Buffer Overflow Basics. You need the register and instruction basics from Assembly Language first.

Authorised targets only. Run every command in this workbook on a lab VM that you control, against the practice binary that you compile yourself. For more practice, use public CTF targets such as picoCTF. Never run a test against a system that you do not own or have written permission to test.

Lab setup

You need a Kali or Ubuntu VM, 64-bit, with these tools. The commands below install them on Debian-based systems.

sudo apt update
sudo apt install -y gcc gdb binutils python3-pip
python3 -m pip install --user pwntools      # gives the checksec and cyclic commands

Install pwndbg, a plug-in that makes gdb show registers, code and stack in one view. Follow the install steps in the project's own README, then start gdb and check that the prompt reads pwndbg>.

Create a folder and the practice program vuln.c. The bug is on purpose. The program copies its first argument into a 32-byte buffer with no length check.

#include <stdio.h>
#include <string.h>

void greet(const char *name) {
    char buf[32];
    strcpy(buf, name);              /* the bug: no length check */
    printf("Hello, %s\n", buf);
}

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

Compile two builds. The first turns the protections off, so that you can see the bare mechanics. The second uses the defaults of a modern compiler.

gcc -m64 -g -O0 -fno-stack-protector -no-pie -o vuln_off vuln.c
gcc -g -O0 -o vuln_on vuln.c
./vuln_off AAAA
Hello, AAAA

The -g flag adds debug information. The -O0 flag stops the compiler from rewriting the code, so the assembly matches the source. Your compiler version can give slightly different addresses and offsets. The method does not change.

Chapter 1 — Memory & Process Layout

Objectives

  • Name the segments of a process and say what each holds
  • Explain how the stack grows and what a stack frame contains
  • Apply the System V AMD64 calling convention to a call
  • Locate the saved return address in a frame

Explanation

Process layout. When the kernel runs a program, it gives the process a virtual address space. The process sees its own private range of addresses. The kernel maps them to real memory. The main regions, from low addresses to high addresses, are:

RegionHoldsPermissions
.textThe machine code of the programread, execute
.data and .bssGlobal and static variablesread, write
HeapMemory from malloc; grows toward higher addressesread, write
Shared librarieslibc and others, mapped by the loaderread, execute (code), read, write (data)
StackLocal variables, saved registers, return addresses; grows toward lower addressesread, write

The stack is a last-in, first-out area. A call to a function pushes data on it. A return pops data off it. The register rsp (stack pointer) points to the top. The register rbp (base pointer) points to a fixed place inside the current function's frame, so that the code can address locals as rbp-0x20.

Pointers. A pointer is a variable that holds an address. On a 64-bit system, a pointer is 8 bytes. The CPU stores numbers in little-endian order: the lowest byte comes first in memory. The value 0x00000000004011c3 appears in memory as the bytes c3 11 40 00 00 00 00 00. Remember this when you read a hex dump.

The stack frame. Each call to a function creates a stack frame. The standard sequence is:

  1. The call instruction pushes the return address, the address of the next instruction in the caller, and jumps to the function.
  2. The function starts with push rbp, which saves the caller's frame pointer.
  3. It runs mov rbp, rsp, so rbp now marks its own frame.
  4. It runs sub rsp, N, which reserves space for local variables.
  5. At the end, leave restores rsp and rbp, and ret pops the return address into rip.

The frame looks like this. High addresses are at the top.

 higher addresses
 +----------------------+
 | return address       |  rbp+0x8   <- ret jumps here after leave
 +----------------------+
 | saved rbp            |  rbp+0x0
 +----------------------+
 | local: buf[32]       |  rbp-0x20 ... rbp-0x01
 +----------------------+  <- rsp (after sub rsp)
 lower addresses

The buffer sits below the return address. Data written into a buffer goes toward higher addresses. A write that is too long therefore runs over the saved rbp and then the return address. This one fact is the basis of the classic stack overflow.

The calling convention. A calling convention is the agreement between caller and callee about where arguments and results go. Linux on x86-64 uses the System V AMD64 convention.

ItemRegister
Arguments 1 to 6 (integers and pointers)rdi, rsi, rdx, rcx, r8, r9
More argumentsOn the stack
Return valuerax
The callee must preserverbx, rbp, r12 to r15
System call number and argumentsrax; then rdi, rsi, rdx, r10, r8, r9

At the moment of a call, rsp must be a multiple of 16. This is the stack alignment rule. Some library functions crash if you break it, which is a common cause of confusion in debugging.

On 32-bit x86, the convention is different: the caller pushes all arguments on the stack. Chapter 3 of Assembly Language explains how to recognise 32-bit code.

Worked example: read the frame of greet

Disassemble the function in the build that has no protections.

objdump -d -M intel vuln_off --disassemble=greet
0000000000401156 <greet>:
  401156:	f3 0f 1e fa          	endbr64
  40115a:	55                   	push   rbp
  40115b:	48 89 e5             	mov    rbp,rsp
  40115e:	48 83 ec 30          	sub    rsp,0x30
  401162:	48 89 7d d8          	mov    QWORD PTR [rbp-0x28],rdi
  401166:	48 8b 55 d8          	mov    rdx,QWORD PTR [rbp-0x28]
  40116a:	48 8d 45 e0          	lea    rax,[rbp-0x20]
  40116e:	48 89 d6             	mov    rsi,rdx
  401171:	48 89 c7             	mov    rdi,rax
  401174:	e8 d7 fe ff ff       	call   401050 <strcpy@plt>
  ...
  401195:	c9                   	leave
  401196:	c3                   	ret

Read it line by line.

  • endbr64 is a marker for a hardware protection called CET. It does nothing else for now.
  • push rbp and mov rbp,rsp build the frame. sub rsp,0x30 reserves 48 bytes.
  • mov [rbp-0x28],rdi saves the first argument, the name pointer. The caller put it in rdi, as the convention says.
  • lea rax,[rbp-0x20] computes the address of buf. It is 32 bytes below rbp. The code then puts it in rdi (argument 1) and puts name in rsi (argument 2). These are the arguments of strcpy(dest, src).
  • leave and ret end the function.

Now look at the caller, main, to see the matching call.

objdump -d -M intel vuln_off --disassemble=main | grep -B4 -A2 "call.*greet"
  4011b8:	48 8b 00             	mov    rax,QWORD PTR [rax]
  4011bb:	48 89 c7             	mov    rdi,rax
  4011be:	e8 93 ff ff ff       	call   401156 <greet>
  4011c3:	b8 00 00 00 00       	mov    eax,0x0

main loads argv[1] into rdi and calls greet. The call pushes the address of the next instruction, 0x4011c3. That is the return address of this frame.

Now confirm it in the debugger. Start gdb, stop after the copy, and dump the stack. Use a 7-character argument.

gdb -q ./vuln_off
pwndbg> break *0x401179
pwndbg> run ABCDEFG
pwndbg> x/10gx $rsp
0x7fffffffdc30:	0x0000000000000000	0x00007fffffffe0a2
0x7fffffffdc40:	0x0047464544434241	0x00007ffff7fc1000
0x7fffffffdc50:	0x0000000000000000	0x0000000000000001
0x7fffffffdc60:	0x00007fffffffdc80	0x00000000004011c3
0x7fffffffdc70:	0x00007fffffffdd98	0x0000000200000000

The addresses on your VM differ. The structure is the same. The x/10gx command shows 10 giant words (8 bytes) in hex.

  • At 0x7fffffffdc40 you find your text: bytes 41 42 43 44 45 46 47 00, which is ABCDEFG and the end-of-string byte. This is buf, at rbp-0x20.
  • At 0x7fffffffdc60 is the saved rbp, which points into main's frame.
  • At 0x7fffffffdc68 is 0x4011c3, the return address in main. This is the value that ret will load into rip.

The distance from the start of buf (...dc40) to the return address (...dc68) is 0x28, which is 40 bytes. Keep this number. Chapter 2 finds it again with a different method.

In pwndbg, telescope $rsp 10 and stack 10 show the same memory with arrows to the data that each value points to. Use x/10gx first to learn the raw form.

Practice questions

  1. The stack grows toward lower addresses, but a buffer fills toward higher addresses. Why does this matter for an overflow?
  2. A function is called as f(a, b, c) in 64-bit C code on Linux. Which registers hold the three arguments?
  3. In the frame above, what is at rbp+0x8? What is at rbp+0x0?
  4. The value 0x00000000004011c3 is in memory. Write the bytes in the order that x/8bx shows them.
  5. A function has char buf[32] and the compiler puts it at rbp-0x20. How many bytes can you write before you touch the saved rbp? How many before the return address?
Answers
  1. The buffer is below the control data (saved rbp and return address). Writes that are too long move toward higher addresses, so they run over exactly that control data.
  2. a in rdi, b in rsi, c in rdx.
  3. At rbp+0x8 is the return address. At rbp+0x0 is the saved rbp of the caller.
  4. c3 11 40 00 00 00 00 00 (little-endian: lowest byte first).
  5. 32 bytes before the saved rbp. 40 bytes before the return address (32 bytes of buffer plus 8 bytes of saved rbp).

Sign in to continue

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

Related