A grammar-based custom mutator for AFL++ to handle highly-structured inputs.
- Developer: Shengtuo Hu (h1994st)
- Mentors: Marc Heuse, Andrea Fioraldi
- Overview
- Quick Start
- Prerequisites
- Building the Grammar Mutator
- Seeds
- Fuzzing
- Configuration
- Contact and Contributions
The grammar mutator lets AFL++ fuzz targets that read highly-structured inputs, such as JSON or Ruby. It does not mutate a byte buffer. It parses each test case into a tree, changes the tree, and writes the tree back as bytes. It uses ideas from the F1 fuzzer for test case generation and from Nautilus for the mutations.
This repository includes:
- Grammars in a JSON format or in the ANTLR 4 format (
.g4), for example from grammars-v4 - Tree-based mutation: rules mutation, random mutation, random recursive mutation, splicing mutation
- Tree-based trimming: subtree trimming, recursive trimming
- An ANTLR 4 shim that parses test cases while AFL++ runs
- Tools that generate seeds (
grammar_generator) and parse existing seeds (grammar_parser, for.g4grammars) - Sample grammars and a script that converts Nautilus Python grammars
- Unit tests
For more details about tree-based mutation, trimming, and grammar-based fuzzing, refer to the Nautilus paper. A fuzzing writeup on Apache that uses the AFL++ Grammar Mutator is at https://securitylab.github.com/research/fuzzing-apache-1.
make antlr-jar # download the ANTLR 4.13.2 jar
make GRAMMAR_FILE=grammars/ruby.json # build the mutator for a grammar
./grammar_generator-ruby 100 1000 ./seeds ./trees
mkdir -p out/default && cp -r trees out/default
export AFL_CUSTOM_MUTATOR_LIBRARY=./libgrammarmutator-ruby.so
export AFL_CUSTOM_MUTATOR_ONLY=1
afl-fuzz -i seeds -o out -- /path/to/target @@The sections below explain each step.
Install these packages:
sudo apt install valgrind uuid-dev default-jre python3 python3-venvThe build needs the ANTLR 4.13.2 jar (Java 11 or later).
In the Grammar-Mutator folder, make antlr-jar downloads it.
You can also download it yourself:
wget https://www.antlr.org/download/antlr-4.13.2-complete.jar
sudo cp -f antlr-4.13.2-complete.jar /usr/local/libThe build looks for the jar in the Grammar-Mutator folder and in /usr/local/lib.
If the jar is in another place, add ANTLR_JAR_LOCATION=... to the make command.
A .g4 grammar also needs the Python tools in .venv.
Run make deps once to install them (this needs python3-venv and access to PyPI).
The grammar mutator uses the latest custom mutator APIs of AFL++.
Use the latest stable or dev branch of AFL++:
git clone https://github.com/AFLplusplus/AFLplusplus.git
cd AFLplusplus
make distrib
sudo make installSet the grammar with GRAMMAR_FILE.
The grammar can be one of two kinds:
- A JSON grammar file. There are several in the
grammarsfolder, such asjson.json,ruby.json, andhttp.json. - An ANTLR 4 grammar (
.g4files). You can use a grammar from grammars-v4 directly. Give all its.g4files, for example the lexer grammar and the parser grammar. Samples are ingrammars/g4.
# A JSON grammar
make GRAMMAR_FILE=grammars/ruby.json
# An ANTLR 4 grammar: one combined grammar ...
make deps
make GRAMMAR_FILE=grammars/g4/JSON.g4
# ... or a lexer grammar and a parser grammar. This grammar has action code,
# so it also needs its C++ base classes (G4_SUPPORT_DIR).
make GRAMMAR_FILE="grammars/g4/javascript/JavaScriptLexer.g4 grammars/g4/javascript/JavaScriptParser.g4" \
G4_SUPPORT_DIR=grammars/g4/javascript/CppRefer to customizing-grammars.md for both kinds of grammar.
Its ANTLR 4 section gives the limits of .g4 grammars.
Pull requests with new grammars are welcome! :-)
The build creates these files in the src folder, with symbolic links to them in the repository root:
libgrammarmutator-<name>.so: the custom mutator library for AFL++grammar_generator-<name>: makes seeds and their trees (see Generating Seeds)grammar_parser-<name>: parses existing seeds into trees, for a.g4grammar only (see Using Existing Seeds)
The name comes from the grammar file, so you can build the mutator for more than one grammar:
- For a JSON grammar, the build cuts the file name at the first underscore, dash, or dot. So
ruby.jsongivesruby. - For a
.g4grammar, the name is the last file name withoutLexerorParser. So the JavaScript example above givesjavascript. - To set your own name, add
GRAMMAR_FILENAME=yournameto the make command.
To fork the project, fix bugs, or contribute, refer to building-grammar-mutator.md for the full build instructions.
Before you fuzz the target, prepare the seeds. You can generate seeds from the grammar, or use existing seeds. In both cases, you can give the mutator the trees of the seeds, so that it does not parse them again.
grammar_generator-<name> makes seeds and their tree files from the grammar of the build.
You can set the number of seeds and the maximum tree size.
A larger tree usually gives a more complex seed.
The generator does not write empty seeds or the same seed twice.
If 100 tries in a row give no new seed, it stops early and writes fewer than <max_num> seeds.
# Usage:
# ./grammar_generator-<name> <max_num> <max_size> <seed_output_dir> <tree_output_dir> [<random seed>]
./grammar_generator-ruby 100 1000 ./seeds ./treesThen copy the trees.
You can give your own seeds to the fuzzer. They do not have to match the grammar. The mutator parses each seed when AFL++ loads it. What happens with a seed that the grammar does not cover depends on the kind of grammar:
- A JSON grammar: the mutator keeps the bad part as a terminal node in the tree, so the seed does not lose much information. For example,
grammars/ruby.jsonis a simplified Ruby grammar, so most real Ruby files have parse errors. - A
.g4grammar: a seed with a syntax error gets no tree, and the grammar mutator does not change it. The other AFL++ stages change it, unlessAFL_CUSTOM_MUTATOR_ONLYis set. WithAFL_CUSTOM_MUTATOR_ONLY=1, AFL++ does not fuzz this seed at all.
For example, to use the test cases of the mruby project as seeds, clone it with git clone https://github.com/mruby/mruby.git and give -i mruby/test/t to afl-fuzz.
With a .g4 grammar, you can parse the seeds before you fuzz.
This makes the start of afl-fuzz faster:
./grammar_parser-javascript ./seeds ./treesThen copy the trees.
The ANTLR 4 section of customizing-grammars.md gives the skip reasons, the parse time limit, and G4_SUPPORT_DIR.
Copy the trees folder, with this exact name, into the output folder of afl-fuzz (for example -o out with the default fuzzer name default):
mkdir -p out/default
cp -r trees out/defaultIf you use more than one fuzzer instance (-M/-S sync mode), do this for each instance.
For example, for the instances fuzzer1 to fuzzer8:
for i in 1 2 3 4 5 6 7 8; do
mkdir -p out/fuzzer$i
cp -r trees out/fuzzer$i/
doneRefer to sample-fuzzing-targets.md to build the sample fuzzing targets.
This example uses the Ruby grammar (grammars/ruby.json) and the mruby target:
export AFL_CUSTOM_MUTATOR_LIBRARY=./libgrammarmutator-ruby.so
export AFL_CUSTOM_MUTATOR_ONLY=1
afl-fuzz -i seeds -o out -- /path/to/target @@The fuzzer can be slow at the start, for two reasons:
- Large seeds take more time to load. A large
<max_size>forgrammar_generator-<name>gives large seeds. - The mutator must parse each seed that has no tree. To skip this step, copy the trees of the seeds (see Copying the Trees).
The mutator reads these environment variables:
| Variable | Effect |
|---|---|
RANDOM_MUTATION_STEPS |
The number of random mutations (default 1000) |
RANDOM_RECURSIVE_MUTATION_STEPS |
The number of random recursive mutations (default 1000) |
SPLICING_MUTATION_STEPS |
The number of splicing mutations (default 1000) |
RULES_MUTATION_STEPS |
The maximum number of rules mutations (default: no limit) |
TRIMMING_STEPS |
The maximum number of trimming steps (default: no limit) |
PARSE_TIME_BUDGET |
0 removes the time limit for the parse of a test case with a .g4 grammar. Any other value keeps the limit. |
By default, the rules mutation tries every other rule at every nonterminal node of the tree.
With RULES_MUTATION_STEPS, it tries at most that many of these node and rule pairs.
It samples them uniformly across the whole tree and tries them in tree order.
The trimming of a new queue entry tries every nonterminal node and every recursion of its tree, from the root down.
With TRIMMING_STEPS, it stops after that many tries and keeps what it has trimmed by then.
For example:
export RANDOM_MUTATION_STEPS=10000
export RANDOM_RECURSIVE_MUTATION_STEPS=10000
export SPLICING_MUTATION_STEPS=10000
export RULES_MUTATION_STEPS=10000
export TRIMMING_STEPS=10000
export AFL_CUSTOM_MUTATOR_LIBRARY=./libgrammarmutator-ruby.so
export AFL_CUSTOM_MUTATOR_ONLY=1
afl-fuzz -i seeds -o out -- /path/to/target @@We welcome questions and contributions! Feel free to open an issue or submit a pull request. Read CONTRIBUTING.md before you open a pull request.
stable is the only long-lived branch.
Send all pull requests to stable.
The dev branch is abandoned.
It stays in the repository for history, but it does not get new changes.
Release Please makes the releases from stable.
See CHANGELOG.md and the GitHub releases for the changes in each version.