Practice library / Exploit development and reversing
Workbook · Medium · 5 hoursFree account
Binary Exploitation Foundations
Memory layout, protections (ASLR, NX, canaries), and exploitation primitives.
On this page 5 sections
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
checksecandpwndbgoutput 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 commandsInstall 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 AAAAHello, AAAAThe -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:
| Region | Holds | Permissions |
|---|---|---|
.text | The machine code of the program | read, execute |
.data and .bss | Global and static variables | read, write |
| Heap | Memory from malloc; grows toward higher addresses | read, write |
| Shared libraries | libc and others, mapped by the loader | read, execute (code), read, write (data) |
| Stack | Local variables, saved registers, return addresses; grows toward lower addresses | read, 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:
- The
callinstruction pushes the return address, the address of the next instruction in the caller, and jumps to the function. - The function starts with
push rbp, which saves the caller's frame pointer. - It runs
mov rbp, rsp, sorbpnow marks its own frame. - It runs
sub rsp, N, which reserves space for local variables. - At the end,
leaverestoresrspandrbp, andretpops the return address intorip.
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 addressesThe 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.
| Item | Register |
|---|---|
| Arguments 1 to 6 (integers and pointers) | rdi, rsi, rdx, rcx, r8, r9 |
| More arguments | On the stack |
| Return value | rax |
| The callee must preserve | rbx, rbp, r12 to r15 |
| System call number and arguments | rax; 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=greet0000000000401156 <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 retRead it line by line.
endbr64is a marker for a hardware protection called CET. It does nothing else for now.push rbpandmov rbp,rspbuild the frame.sub rsp,0x30reserves 48 bytes.mov [rbp-0x28],rdisaves the first argument, thenamepointer. The caller put it inrdi, as the convention says.lea rax,[rbp-0x20]computes the address ofbuf. It is 32 bytes belowrbp. The code then puts it inrdi(argument 1) and putsnameinrsi(argument 2). These are the arguments ofstrcpy(dest, src).leaveandretend 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,0x0main 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_offpwndbg> break *0x401179
pwndbg> run ABCDEFG
pwndbg> x/10gx $rsp
0x7fffffffdc30: 0x0000000000000000 0x00007fffffffe0a2
0x7fffffffdc40: 0x0047464544434241 0x00007ffff7fc1000
0x7fffffffdc50: 0x0000000000000000 0x0000000000000001
0x7fffffffdc60: 0x00007fffffffdc80 0x00000000004011c3
0x7fffffffdc70: 0x00007fffffffdd98 0x0000000200000000The addresses on your VM differ. The structure is the same. The x/10gx command shows 10 giant words (8 bytes) in hex.
- At
0x7fffffffdc40you find your text: bytes41 42 43 44 45 46 47 00, which isABCDEFGand the end-of-string byte. This isbuf, atrbp-0x20. - At
0x7fffffffdc60is the savedrbp, which points intomain's frame. - At
0x7fffffffdc68is0x4011c3, the return address inmain. This is the value thatretwill load intorip.
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
- The stack grows toward lower addresses, but a buffer fills toward higher addresses. Why does this matter for an overflow?
- A function is called as
f(a, b, c)in 64-bit C code on Linux. Which registers hold the three arguments? - In the frame above, what is at
rbp+0x8? What is atrbp+0x0? - The value
0x00000000004011c3is in memory. Write the bytes in the order thatx/8bxshows them. - A function has
char buf[32]and the compiler puts it atrbp-0x20. How many bytes can you write before you touch the savedrbp? How many before the return address?
Answers
- The buffer is below the control data (saved
rbpand return address). Writes that are too long move toward higher addresses, so they run over exactly that control data. ainrdi,binrsi,cinrdx.- At
rbp+0x8is the return address. Atrbp+0x0is the savedrbpof the caller. c3 11 40 00 00 00 00 00(little-endian: lowest byte first).- 32 bytes before the saved
rbp. 40 bytes before the return address (32 bytes of buffer plus 8 bytes of savedrbp).
Sign in to continue
3 more sections are for signed-in members. It is free: sign in with Google.