Intro to Fuzzing with AFL++ and libFuzzer

Intro to Fuzzing with AFL++ and libFuzzer

Fuzzing is one of the most effective techniques for finding bugs that no human would think to test for. The idea is simple: throw massive amounts of malformed, unexpected, or random input at your program and watch what breaks. Google's Project Zero, Microsoft's Security Response Center, and countless open-source projects have credited fuzzing with finding thousands of critical vulnerabilities. This post walks you through getting started with two of the most widely used fuzzers: AFL++ and libFuzzer.

What Is Fuzzing?

A fuzzer is a program that automatically generates test inputs and feeds them to a target program, monitoring for crashes, hangs, or assertion failures. Unlike unit tests, which check specific known behaviors, fuzzers explore the vast space of possible inputs looking for anything that causes the program to misbehave.

There are two main approaches:

  • Mutation-based fuzzing: Start with valid seed inputs and mutate them (flip bits, insert bytes, truncate, splice together).
  • Generation-based fuzzing: Build inputs from scratch using knowledge of the format (a grammar or schema).

Both AFL++ and libFuzzer are primarily mutation-based with coverage guidance — they track which code paths each input exercises and prioritize mutations that reach new code.

AFL++: American Fuzzy Lop, Improved

AFL++ is a fork of the original AFL with significant improvements in performance, instrumentation modes, and mutation strategies. It works by compiling your target with instrumentation that reports code coverage back to the fuzzer.

Installation

# On Ubuntu/Debian
sudo apt-get install afl++

# From source
git clone https://github.com/AFLplusplus/AFLplusplus
cd AFLplusplus
make distrib
sudo make install

Compiling Your Target

AFL++ provides drop-in compiler wrappers. Replace gcc/g++/clang with afl-clang-fast:

# For a simple C program
afl-clang-fast -o target_fuzz target.c

# For a project using autotools
CC=afl-clang-fast CXX=afl-clang-fast++ ./configure
make

A Simple Fuzzing Target

Let's fuzz a basic parser function:

// parse.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void parse_input(const char *buf, size_t len) {
    if (len < 4) return;
    if (buf[0] == 'F' && buf[1] == 'U' && buf[2] == 'Z' && buf[3] == 'Z') {
        // Intentional bug: null pointer dereference
        char *p = NULL;
        *p = buf[4];
    }
}

int main(int argc, char **argv) {
    FILE *f = fopen(argv[1], "rb");
    if (!f) return 1;
    char buf[1024];
    size_t n = fread(buf, 1, sizeof(buf), f);
    fclose(f);
    parse_input(buf, n);
    return 0;
}

Compile and run:

afl-clang-fast -o parse_fuzz parse.c

# Create seed corpus
mkdir seeds
echo "hello" > seeds/seed1
echo "test" > seeds/seed2

# Start fuzzing
afl-fuzz -i seeds -o findings ./parse_fuzz @@

AFL++ will run until you stop it. Crashes land in findings/default/crashes/. Within minutes (often seconds), it should find the FUZZ trigger above.

Reading AFL++ Output

The AFL++ status screen shows:

  • exec speed: How many executions per second. Higher is better; aim for 1000+.
  • corpus count: Number of unique test cases in the queue.
  • saved crashes: Inputs that caused a crash — your bugs.
  • saved hangs: Inputs that caused a timeout — also worth investigating.

libFuzzer: In-Process Fuzzing

libFuzzer is part of LLVM and takes a different approach: the fuzzer runs inside the same process as your target. This means faster execution but also means a crash terminates the fuzzer. You write a harness function called LLVMFuzzerTestOneInput:

// fuzz_harness.c
#include <stdint.h>
#include <stddef.h>

// Your library code
extern void parse_input(const char *buf, size_t len);

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    parse_input((const char *)data, size);
    return 0;
}

Compile with address sanitizer and fuzzer instrumentation:

clang -g -O1 -fsanitize=fuzzer,address \
    fuzz_harness.c parse_logic.c \
    -o fuzz_harness

Run it:

# Create seed corpus directory
mkdir corpus

# Run the fuzzer
./fuzz_harness corpus/

# libFuzzer prints progress and stops on the first crash

libFuzzer outputs a minimal reproducer when it finds a crash:

==12345== ERROR: AddressSanitizer: ...
artifact_prefix='./'; Test unit written to ./crash-abc123def456

Sanitizers: Your Bug Detectors

Fuzzing without sanitizers only catches crashes and hangs. Sanitizers catch a much wider class of bugs:

  • AddressSanitizer (ASan): Buffer overflows, use-after-free, heap corruption
  • UndefinedBehaviorSanitizer (UBSan): Integer overflow, null pointer dereference, misaligned access
  • MemorySanitizer (MSan): Use of uninitialized memory

Stack them together:

clang -fsanitize=fuzzer,address,undefined \
    -fno-omit-frame-pointer -g \
    harness.c target.c -o fuzz_target

Building Good Seed Corpora

The seed corpus is your starting point. Better seeds = faster fuzzing. For a JSON parser, your seeds should be valid JSON files. For an image decoder, use a variety of valid images. Collect real-world examples from:

  • Unit test fixtures already in your repo
  • Public test suites for the format (e.g., the JSON test suite)
  • Minimized samples from past bug reports

AFL++ includes afl-cmin to minimize a large corpus to the smallest set that covers the same code paths:

afl-cmin -i raw_corpus -o minimized_corpus ./target @@

Continuous Fuzzing

Running a fuzzer for five minutes and calling it done is not fuzzing — it's theater. Real fuzzing needs to run continuously, accumulate corpus, and alert on new crashes. Tools for this:

  • OSS-Fuzz: Free continuous fuzzing for open-source projects. Google runs it 24/7.
  • ClusterFuzz: The infrastructure behind OSS-Fuzz, open-sourced.
  • libFuzzer + CI: Run libFuzzer for a fixed time budget (e.g., 60 seconds) in CI to catch regressions.

For functional and integration-level testing, platforms like HelpMeTest handle browser and API test automation so your team can focus on lower-level hardening like fuzzing. They complement each other: fuzzing catches memory corruption and parser bugs; integration tests verify business logic.

Minimizing Crashes

When AFL++ or libFuzzer finds a crash, the reproducer input is often large and noisy. Minimize it before filing a bug:

# AFL++ minimizer
afl-tmin -i findings/crashes/id:000000 -o minimized ./target @@

# libFuzzer minimizer
./fuzz_target -minimize_crash=1 -max_total_time=60 crash-abc123

A minimized input is easier to debug and turns directly into a regression test.

What to Fuzz

Not everything needs fuzzing, but these are high-value targets:

  • Parsers: Anything that reads a file format (JSON, XML, image, audio, binary protocol)
  • Network protocol handlers: Especially anything that processes untrusted input
  • Cryptographic code: Edge cases in padding, length handling, key parsing
  • Compression/decompression: Historically a rich source of bugs
  • Regular expressions: Catastrophic backtracking is a fuzzing-findable bug

Getting Started Today

  1. Pick one parser or input handler in your codebase.
  2. Write a 10-line libFuzzer harness.
  3. Compile with -fsanitize=fuzzer,address.
  4. Run for an hour.

You will almost certainly find something interesting. The first time a fuzzer finds a real bug in code you thought was solid, fuzzing becomes a permanent part of your workflow.

AFL++ and libFuzzer are mature, well-documented, and free. There's no reason not to start today.

Read more

Start now free