Fuzzing for Vulnerabilities
Defensive Exploitation & Red Team Techniques · Defense & Forensics track
8 min read
In this module
Finding Bugs Faster Than Humans
Reading code line by line is effective but slow. Even expert auditors miss bugs, especially in large codebases. Fuzzing automates the process: feed a program random or semi-random inputs and watch whether it crashes, hangs, or does something unexpected.
The logic is blunt: if a program crashes on some input, that crash might be a security vulnerability. Generate millions of inputs, run the program on each, and the fuzzer surfaces bugs that manual review misses — bugs hiding in code paths that rarely execute, complex error-handling logic, or unusual input combinations that no human would naturally try.
Fuzzing has been extraordinarily successful. Google's OSS-Fuzz project has found over 10,000 vulnerabilities in open-source software. Major software projects including Chrome, Firefox, the Linux kernel, and OpenSSL all use continuous fuzzing as a core part of their security strategy.
Dumb Fuzzing vs. Smart Fuzzing
The simplest fuzzer generates completely random bytes and feeds them to the program. This is dumb fuzzing (or black-box fuzzing). It works surprisingly well for simple input formats: a random byte stream might happen to start with the right magic bytes, pass a basic header check, and then hit a bug in the payload processing code.
But dumb fuzzing wastes most of its effort on inputs that are rejected immediately. If the program expects a PNG file, random bytes will fail the PNG magic number check and never reach the interesting parsing code.
Smart fuzzing (or white-box / grey-box fuzzing) uses information about the program to generate better inputs. The most successful approach is coverage-guided fuzzing, where the fuzzer monitors which code paths are executed and uses this feedback to guide input generation.
Coverage-guided fuzzing instruments the target program to record which code paths (basic blocks, branches, or edges) are executed for each input. The fuzzer maintains a corpus of inputs and mutates them. When a mutation triggers a new code path, it is saved and used as a basis for further mutations. This allows the fuzzer to systematically explore the program rather than relying on pure randomness.
AFL: American Fuzzy Lop
AFL (and its successor AFL++) is one of the most influential fuzzers in security research. Created by Michal Zalewski, AFL introduced many of the techniques that modern fuzzers use.
AFL works as follows:
-
Instrumentation: AFL instruments the target program at compile time (using a compiler wrapper) to insert code at every branch point that records which edges (transitions between basic blocks) are taken. This data is written to a shared memory bitmap.
-
Seed corpus: The user provides initial input files (seed corpus) that are valid inputs for the program. For a PNG parser, the seeds might be small, valid PNG files.
-
Mutation: AFL takes inputs from the corpus and applies mutations: flipping bits, inserting bytes, deleting bytes, splicing parts of different inputs together, and using interesting values (0, 1, -1, MAX_INT, etc.).
-
Execution: AFL runs the target program with the mutated input, typically using
fork()to avoid process startup overhead on each run. -
Coverage tracking: After execution, AFL checks the bitmap. If the mutated input triggered a new edge (or hit an existing edge a different number of times), the input is considered "interesting" and added to the corpus.
-
Loop: The process repeats, with AFL continuously mutating corpus entries and saving inputs that discover new coverage.
Seed corpus → Mutate → Execute → Check coverage → New path? ↑ │ └──── Yes: save to corpus ◄────────────┘ No: discardAFL achieves thousands to millions of executions per second (depending on the target), and the coverage guidance ensures that each execution has a good chance of exploring new behaviour.
libFuzzer: In-Process Fuzzing
libFuzzer is an in-process, coverage-guided fuzzer that is part of the LLVM project. Unlike AFL, which runs the target as a separate process, libFuzzer links directly with the target code and calls a harness function:
/* Fuzzing harness for a PNG decoder */int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { decode_png(data, size); return 0;}The fuzzer calls this function millions of times with different data buffers. Because there is no fork() or exec() overhead, libFuzzer can achieve very high throughput - often 10,000+ executions per second even for complex targets.
libFuzzer is particularly effective for fuzzing library APIs because the harness function can call any exported function with arbitrary arguments. It's one of the engines Google's OSS-Fuzz runs, alongside AFL++, Honggfuzz and Centipede, and the same LLVMFuzzerTestOneInput harness works with all of them.
Sanitizers: Making Bugs Visible
A common challenge in fuzzing is that not all bugs cause crashes. A buffer overflow that writes one byte past the buffer might not crash because the adjacent memory is valid. A use-after-free that reads freed memory might get lucky and read a valid value. These bugs are vulnerabilities but are invisible to a fuzzer that only detects crashes.
Sanitizers solve this by instrumenting the program to detect memory errors at runtime:
- AddressSanitizer (ASan): Detects out-of-bounds reads/writes, use-after-free, double-free, and stack buffer overflows. It works by placing "red zones" around every memory allocation and checking every memory access against them.
- MemorySanitizer (MSan): Detects reads of uninitialised memory.
- UndefinedBehaviourSanitizer (UBSan): Detects undefined behaviour including signed integer overflow, null pointer dereferences, and type mismatches.
- ThreadSanitizer (TSan): Detects data races in multi-threaded programs.
When fuzzing with sanitizers enabled, bugs that would silently corrupt memory instead trigger an immediate, detailed error report. The combination of coverage-guided fuzzing and sanitizers is the most effective automated bug-finding approach available today.
Without ASan: Buffer overflow writes past the buffer → maybe crashes, maybe silently corrupts → fuzzer might not detect it
With ASan: Buffer overflow writes past the buffer → ASan detects the red zone violation → immediate crash with detailed report (stack trace, allocation info, overflow size)Here's the whole loop on a toy target: three nested byte checks guarding an off-by-one heap write. Press Run until crash with ASan off and watch the corpus climb past each check while nothing crashes, then tick ASan and see the saved input blow up straight away. Keep an eye on the blind fuzzer, which hasn't got past the second check after the same number of runs.
Coverage-guided fuzzing vs blind fuzzing
Both fuzzers attack the same function. The coverage-guided one mutates inputs from its corpus and keeps any input that reaches a new edge. The blind one throws fresh random bytes every time.
- 1void parse(const uint8_t *data, size_t size) {
- 2 if (size < 3) return;(covered)
- 3 char *buf = malloc(size);(covered)
- 4 if (data[0] == 'F')(covered)
- 5 if (data[1] == 'U')
- 6 if (data[2] == 'Z')
- 7 buf[size] = 0; /* off by one */
- 8 free(buf);(covered)
- 9}
Seed 7. Runs are repeatable: the same seed gives the same inputs.
- Executions
- 0
- Corpus
- 1 inputs
- Edges
- 4 of 11
- Checks passed
- 0 of 3
- Crashes
- 0
- Executions
- 0
- Corpus
- none
- Edges
- 0 of 11
- Checks passed
- 0 of 3
- Crashes
- 0
Expected executions to pass all three checks: about 22,400,000 (a 1 in 256³ chance per input of length 3 or more).
- #0"AAAA"seed, new edges 2→3, 3→4, 4→8, 8→ret
Structure-Aware Fuzzing
For programs that expect structured input (XML, JSON, protocol buffers, programming languages), random byte mutation is inefficient because most mutations produce syntactically invalid input that is rejected by the parser immediately.
Structure-aware fuzzers understand the input format and generate mutations that maintain syntactic validity:
- Grammar-based fuzzing: The fuzzer uses a grammar (BNF or similar) to generate inputs that are always syntactically valid. Mutations operate at the grammar level (replacing one production with another) rather than at the byte level.
- Protobuf-based harnesses: For programs that accept protocol buffers, the harness takes a protobuf message and converts it to the target's input format. libFuzzer mutates the protobuf bytes, but every mutation produces a valid protobuf that maps to a valid input.
- Custom mutators: AFL++ supports custom mutator plugins that understand the input format and generate intelligent mutations.
Structure-aware fuzzing dramatically improves effectiveness for structured inputs because every execution tests deep parsing logic rather than the superficial format-checking code.
Fuzzing at Scale
Major software organisations run fuzzing at massive scale:
- OSS-Fuzz: Google's continuous fuzzing service for open-source projects. It runs fuzzers 24/7 on cloud infrastructure, automatically files bug reports, and verifies fixes. By August 2023 it had helped find and fix over 10,000 vulnerabilities and 36,000 bugs across 1,000 projects.
- syzbot: A continuous fuzzer for the Linux kernel, using the syzkaller system call fuzzer. It generates sequences of system calls with random arguments and monitors for kernel crashes, hangs, and memory errors.
- ClusterFuzz: the distributed fuzzing and crash-reporting system behind OSS-Fuzz, also used for Chrome. It's open source, so you can run your own instance (or the lighter ClusterFuzzLite in CI).
The economics are hard to argue with: write the harness once, run the fuzzer indefinitely, catch new bugs as code evolves. Fuzzing-found bugs come with a reproducer (the exact input that triggered it) and enough context — stack trace, sanitizer report — for a developer to diagnose and fix quickly.
Fuzzing has reshuffled vulnerability research. Manual analysis still finds the subtle logic bugs; fuzzing vacuums up the shallow ones first, leaving humans to focus where they actually add value. For any organisation building C or C++ software, continuous fuzzing is baseline hygiene.