What Exploitation Means in a Test
"Exploitation" sounds like the climax of a penetration test. In a professional engagement it is a narrow, controlled step with one purpose: to demonstrate impact that can't be demonstrated any other way, at a risk the client has accepted. Understanding what an exploit is (a bug turned into a capability), what it can go wrong with, and when it adds anything over safe validation is part of the tester's judgement. This lesson explains the concepts, shows the anatomy of a memory-corruption bug in a toy model with its detection and fix, and ends with the rules that keep exploitation in a test controlled.
The track's safety line applies with full force here: this course contains no exploit code for real software. Memory-safety bugs and their mitigations are taught in depth, with sanitizers and a stack visualisation, in Memory Safety & Binary Exploitation Concepts; reading binaries in Reverse Engineering & Binary Analysis.
From bug to capability
| Term | Meaning | Example |
|---|---|---|
| Vulnerability | a flaw that lets an input do something the designer didn't intend | a length not checked before a copy |
| Primitive | the basic capability the flaw gives an attacker | "write N bytes past the end of a buffer", "read any file the server can read", "make the server send a request" |
| Exploit | a method that turns primitives into a goal | change a control value, run a command, read a secret |
| Payload | what is done once control is gained | in a test: show the account name, read a harmless marker file |
| Reliability | how often the exploit works without crashing the target | memory-corruption exploits may crash services when they fail |
Most vulnerabilities found in tests are logic and configuration flaws whose primitive is the impact: an IDOR reads another user's data, a missing authentication check gives the admin page. Nothing has to be "developed". Memory-corruption bugs are different: the primitive (an out-of-bounds write) is far from the goal, the path depends on the binary, the platform and its mitigations, and a failed attempt crashes the service. That difference is why testers rarely exploit memory bugs on production systems.
A toy model of memory corruption
The model below is a pure Python simulation, not a real program: a record of 16 bytes holds an 8-byte name field followed by a role byte and padding, laid out next to each other the way C structures lay out fields in memory. A set_name function copies the input without checking its length, which is the bug.
ROLE_USER, ROLE_ADMIN = 0x01, 0x7f
NAME, ROLE = slice(0, 8), 8 # layout: name[8] | role[1] | padding[7]
def new_record(name):
mem = bytearray(16)
mem[ROLE] = ROLE_USER
set_name_buggy(mem, name)
return mem
def set_name_buggy(mem, name): # copies len(name) bytes: no bounds check
for i, b in enumerate(name):
if i < len(mem):
mem[i] = b
def show(label, mem):
role = {ROLE_USER: "user", ROLE_ADMIN: "ADMIN"}.get(mem[ROLE], f"0x{mem[ROLE]:02x}")
print(f"{label:<22} {mem[:12].hex(' ')} role={role}")
show("normal name", new_record(b"alice"))
show("8-byte name", new_record(b"bob-lab1"))
show("9 bytes: role changed", new_record(b"bob-lab1" + bytes([ROLE_ADMIN])))
show("12 bytes: garbage", new_record(b"AAAAAAAAAAAA"))normal name 61 6c 69 63 65 00 00 00 01 00 00 00 role=user
8-byte name 62 6f 62 2d 6c 61 62 31 01 00 00 00 role=user
9 bytes: role changed 62 6f 62 2d 6c 61 62 31 7f 00 00 00 role=ADMIN
12 bytes: garbage 41 41 41 41 41 41 41 41 41 41 41 41 role=0x41
The first two records are fine. Nine bytes overwrite the role byte: the primitive "write past the name" became the impact "become admin" because a security-relevant value happened to sit next to the buffer. Twelve bytes of junk corrupt the role to a value the program never expected: in a real program, the kind of corruption that crashes it. That is the reliability problem in miniature.
How it is detected
CANARY = b"\xa5\x5a"
def new_record_checked(name):
mem = bytearray(16)
mem[8] = 0x01
mem[10:12] = CANARY # a guard value after the field: a toy "canary"
for i, b in enumerate(name): # still the buggy copy
if i < len(mem):
mem[i] = b
if bytes(mem[10:12]) != CANARY:
return "DETECTED: guard bytes overwritten, record rejected"
return f"ok, role byte 0x{mem[8]:02x}"
for n in [b"alice", b"bob-lab1" + b"\x7f", b"AAAAAAAAAAAA"]:
print(f"{len(n):>2} bytes -> {new_record_checked(n)}") 5 bytes -> ok, role byte 0x01
9 bytes -> ok, role byte 0x7f
12 bytes -> DETECTED: guard bytes overwritten, record rejected
The guard catches the long overwrite but not the 9-byte one, which stops before the guard. This is the real behaviour of stack canaries in miniature: they detect linear overflows that reach them and miss corruption of data that sits before them. Real detection layers are AddressSanitizer in testing (which flags the first out-of-bounds byte), crash monitoring in production, and EDR rules for unusual child processes of services.
How it is fixed
def set_name_fixed(mem, name):
if len(name) > 8:
raise ValueError(f"name too long ({len(name)} > 8)")
mem[0:8] = name.ljust(8, b"\0")
mem = bytearray(16); mem[8] = 0x01
for n in [b"alice", b"bob-lab1\x7f"]:
try:
set_name_fixed(mem, n)
print(f"{n!r}: stored, role byte 0x{mem[8]:02x}")
except ValueError as e:
print(f"{n!r}: refused, {e}")b'alice': stored, role byte 0x01
b'bob-lab1\x7f': refused, name too long (9 > 8)
The fix is a bounds check at the copy, which is what safe string functions, length-carrying types and memory-safe languages provide by construction. Mitigations (canaries, ASLR, non-executable memory) make exploitation harder; only the bounds check removes the bug.
When a tester exploits
In a penetration test, exploitation is justified when:
- impact can't be shown by reading configuration, observing a response or demonstrating the primitive itself;
- the client has agreed to it for this finding (the RoE's validation section, and a written approval for anything risky);
- the method is reliable and its side effects are understood and reversible;
- the action is minimal: show the account the code runs as, read a marker file the client placed, then stop.
Typical cases: a web flaw whose primitive needs one step to show impact (a server-side request forgery shown by reaching a test endpoint the client set up); a misconfiguration whose impact is the point (an unauthenticated admin console, demonstrated by loading it, not by using it). Typical non-cases: memory-corruption exploits against production services, anything that would change data, anything that leaves software running on the target.
Pitfalls
- "Pop a shell" as the success criterion. The client pays for understanding the risk, not for a screenshot of a prompt.
- Running public exploit code unread. Exploit code from the Internet may be broken, destructive or itself malicious; it is not used in professional tests without review, and not at all in this course.
- Leaving things behind. Every exploit's artefacts (files, accounts, processes) are listed and removed: see Evidence Collection and Cleanup.
- Confusing a primitive with impact in the report. "Out-of-bounds write in the parser" and "an attacker can change any user's role" are different claims; prove the one you report.
Practice
- In the toy layout, move the role byte before the name (
role | name[8] | padding). Does the 9-byte name still change the role? What does that say about why memory layout matters for exploitability? - Explain in two sentences to a client why you didn't exploit a confirmed buffer overflow in their production VPN appliance.
Answers
- No: the overflow now runs into the padding after the name, so the role is unchanged (it would corrupt whatever follows). Exploitability depends on what sits next to the buffer, which is why the same bug can be harmless in one build and critical in another, and why mitigations reorder and guard memory.
- "Exploiting memory corruption on a production appliance risks crashing it and cutting off remote access for everyone, and a failed attempt can leave it unstable. The vulnerable version is confirmed by the vendor's advisory and the installed firmware version, which is sufficient evidence to fix it with high priority."
Test yourself
What is a 'primitive' in exploitation?
- An old exploit
- The basic capability a flaw gives, such as writing past a buffer or reading any file
- A payload
- A type of malware
In the toy model, why does the guard value not detect the 9-byte name?
- The guard is broken
- The overwrite stops at the role byte, before reaching the guard
- Python ignores it
- Nine bytes is not an overflow
Which fix removes the toy bug rather than making it harder to exploit?
- A canary
- Address randomisation
- A bounds check that refuses names longer than the field
- Logging
When is exploitation justified in a penetration test?
- Always, it is the main goal
- When impact can't be shown otherwise, the client agreed, the method is reliable and the action is minimal
- Whenever a public exploit exists
- Only at night