# Grammar-Based Fuzzing > Structure-aware fuzzing in which a grammar, protobuf schema, template or protocol model constrains generation and mutation so inputs survive early parsing and validation and reach deep logic — the answer to byte-level mutators stalling on languages, nested binary containers and stateful protocols. ## How it works - **Generative vs mutational.** Generative fuzzers build each sample from scratch from the grammar; mutational grammar fuzzers mutate parse trees so outputs stay grammatical, and with coverage feedback keep samples that hit new edges ([[raw/projectzero.google/on-the-effectiveness-of-mutational-grammar-fuzzing-8582|P0: mutational grammar fuzzing]], [[raw/fuzzingbook.org/the-fuzzing-book-generating-software-tests-4292|The Fuzzing Book]]). - **Why byte mutation fails.** MP4 is an n-ary atom tree where every atom carries a 4-byte size covering its descendants; a naive insert/delete invalidates every ancestor's size, the demuxer rejects the file early, and the probability a length-changing mutation is seen by deep parsing code drops exponentially with atom depth ([[raw/atredis.com/atredis-crafting-targeted-mp4-fuzzers-14956|Atredis: targeted MP4 fuzzers]]). The same plateau hits text formats under AFL's bit-flip/arith/splice mutators ([[raw/github.blog/ai-powered-fuzzing-with-the-github-security-lab-taskflow-agent-12103|GHSL Fuzzing Taskflow]]). - **Spectrum of structure.** dictionaries → `FuzzedDataProvider`-shaped harnesses → libFuzzer custom mutators → protobuf (libprotobuf-mutator, "already close to a grammar fuzzer") → full grammars (Nautilus, Jackalope, Fandango, syzlang) ([[raw/appsec.guide/writing-harnesses-testing-handbook-3921|Testing Handbook: Writing harnesses]]). AFL++'s tutorial index covers the libprotobuf and Superion routes ([[raw/aflplus.plus/afl-tutorials-3926|AFL++ Tutorials]]). ## Techniques ### Partially structure-aware custom mutators Atredis's `qtdemux` fuzzers mutate one target atom inside a valid template, then walk the ancestor chain and fix sizes; a libFuzzer custom mutator repairs size/entry-count fields only **50% of the time** so malformed-length paths stay reachable ([[raw/atredis.com/atredis-crafting-targeted-mp4-fuzzers-14956|Atredis]]). GHSL's Fuzzing Taskflow ships per-format dictionaries and `LLVMFuzzerCustomMutator` C files (JSON token splicing, balanced-bracket duplication; XML, regex, PNG, TLV) and auto-adds guard magic constants to the dictionary ([[raw/github.blog/ai-powered-fuzzing-with-the-github-security-lab-taskflow-agent-12103|GHSL Fuzzing Taskflow]]). ### Protobuf on binaries Airbus fuzzed a closed-source X509 parser (`parse_cert_buf` overflow) with AFL++-QEMU persistent mode plus libprotobuf via `AFL_CUSTOM_MUTATOR_LIBRARY` / `AFL_CUSTOM_MUTATOR_ONLY` and `AFL_QEMU_PERSISTENT_ADDR`/`_RET`/`_HOOK`, after `afl-cmin`/`afl-tmin` corpus prep ([[raw/airbus-seclab.github.io/advanced-binary-fuzzing-using-afl-qemu-and-libprotobuf-4882|Airbus SecLab]]). ### Grammar engines - **Nautilus in Go** via gosentry: `go test -fuzz` with grammar-based inputs over existing `testing.F` harnesses ([[raw/blog.trailofbits.com/go-fuzzing-was-missing-half-the-toolkit-we-forked-the-toolchain-to-fix-8584|gosentry]]). LibAFL 0.16.1 improved grammar fuzzing in Frida mode ([[raw/github.com/libafl-0-16-1-release-14414|LibAFL 0.16.1]]). - **Jackalope** (P0) for XSLT and JIT engines; **Fuzzilli** is the generative JS engine fuzzer behind continuous V8 fuzzing ([[raw/projectzero.google/fuzzers-vs-ai-comparing-fuzzilli-and-bigsleep-on-javascript-engines-12406|P0: Fuzzilli vs BigSleep]]). ### Protocols and state - **Spec → grammar by hand:** Doyensec rewrote MS-SMB2 wire formats (e.g. `smb2_ioctl_req`) into syzkaller descriptions, parsed responses for TreeID/SessionID to make state transitions valid, and patched NTLMv2/credit limits and `max connections = 128` out of the way — yielding a long list of ksmbd CVEs ([[raw/blog.doyensec.com/ksmbd-fuzzing-improvements-and-vulnerability-discovery-4290|Doyensec ksmbd]]; detail in [[Kernel Fuzzing]]). - **Conversations as grammar:** Fandango 1.1 specs describe both messages and inter-message constraints; it can play client or server, read replies and continue from the reached state (FTP, DNS examples) — "for a protocol, the test case is the interaction" ([[raw/pepezamudio.com/when-the-input-is-a-conversation-fandango-1-1-stateful-protocol-fuzzin-14700|Fandango 1.1]]). - **Block-based:** boofuzz `Session`/`s_initialize`/`s_string` definitions with multi-stage states and checksums (CRC32, IPv4) ([[raw/nyxfault.github.io/mastering-boofuzz-from-basics-to-advanced-3905|Mastering Boofuzz]]); Scapy's `fuzz()` randomizes every non-computed field, e.g. `send(IP(dst="t")/fuzz(UDP()/NTP(version=4)),loop=1)` ([[raw/scapy.readthedocs.io/usage-scapy-2-4-5-documentation-5438|Scapy usage]]). - **Prefix replay:** InterceptFuzz sits between AFL++ and a single-process target, records benign traffic into a prefix tree, replays a prefix to a state ID, then injects fuzzed packets — 7 new crash signatures in OpenThread, beating Concat and adapted AFLNet ([[raw/arxiv.org/interceptfuzz-interception-based-stateful-fuzzing-of-single-process-ta-14564|InterceptFuzz]]). The protocol-fuzzing survey frames syntax acquisition and state as the core problems ([[raw/arxiv.org/a-survey-of-network-protocol-fuzzing-model-techniques-and-directions-4885|Protocol fuzzing survey]]). ### LLM-derived grammars and generators - **ChatAFL** extracts message grammars from RFC text with an LLM, then mutates/predicts message sequences; beats AFLNet and NSFuzz on state and code coverage in ProFuzzBench ([[raw/ndss-symposium.org/large-language-model-guided-protocol-fuzzing-ndss-4878|ChatAFL]]). - **G2Fuzz** (USENIX Sec '25) has the LLM write Python *input generators* for non-textual formats (TIFF, MP4) — "holistic search" — and lets AFL++ do "local search", so the LLM is called rarely; beats AFL++, Fuzztruction and FormatFuzzer on UNIFUZZ/FuzzBench, 3 CVEs ([[raw/arxiv.org/g2fuzz-grammar-aware-fuzzing-with-llms-3915|G2Fuzz]]). GenEvo does the same with constraint solving: 25 new vulns ([[raw/conf.researchr.org/hybrid-fuzzing-in-input-generation-logic-ase-2026-12676|GenEvo]]). - **Typos** (typed template fuzzing) turns seeds into typed templates via an LLM, no per-language parser; 21 input languages, 135 bugs (128 new) in compilers/runtimes ([[raw/arxiv.org/typed-template-fuzzing-typos-13292|Typos]]). See [[LLM-Assisted Fuzzing]]. ## Pitfalls (Project Zero) 1. **More coverage ≠ more bugs.** The libxslt bug needs `generate-id(document(''))` — dataflow between two functions. Two corpus samples each using one function have the same union coverage as the bug, so the combining sample is never saved; chaining three+ calls gets exponentially harder. Dataflow coverage would help; no practical implementation exists. 2. **Greedy saving makes corpora near-identical.** Mutated children carry most of their parent. 3. **Fix:** periodically restart each worker with an **empty corpus** for T seconds, then merge with the server corpus for T more. On old libxslt, T=3600 and T=600 both beat long-lived sessions in unique crashes and time-to-crash; long sessions varied 5 vs 2 crashes by start conditions ([[raw/projectzero.google/on-the-effectiveness-of-mutational-grammar-fuzzing-8582|P0]]). ## Notable cases - GStreamer `qtdemux`: CVE-2025-47183 (heap OOB read), -47219, -47806/-47807/-47808 (subtitle NULL deref / stack overflow), plus CVE-2026-46469/-46470 (div-by-zero) and a PlayReady-metadata info leak CVE-2026-46471 — all after years of format-agnostic fuzzing ([[raw/atredis.com/atredis-crafting-targeted-mp4-fuzzers-14956|Atredis]]); a further OOB read CVE-2026-125041 came from targeted fuzzing ([[raw/github.com/gstreamer-qtdemux-oob-read-found-via-targeted-fuzzing-cve-2026-125041-15242|GStreamer CVE-2026-125041]]). - The Fuzzing Book's grammar techniques are credited with 2,600+ bugs in Firefox/Chrome/Edge JS interpreters ([[raw/fuzzingbook.org/the-fuzzing-book-generating-software-tests-4292|The Fuzzing Book]]). ## Related - [[Fuzzing]] - [[Coverage-Guided Fuzzing]] - [[Fuzzing Harness Design]] - [[LLM-Assisted Fuzzing]] - [[Kernel Fuzzing]] - [[API Fuzzing]] ## Sources - [[raw/projectzero.google/on-the-effectiveness-of-mutational-grammar-fuzzing-8582|On the Effectiveness of Mutational Grammar Fuzzing]] - [[raw/fuzzingbook.org/the-fuzzing-book-generating-software-tests-4292|The Fuzzing Book: Generating Software Tests]] - [[raw/atredis.com/atredis-crafting-targeted-mp4-fuzzers-14956|Atredis: Crafting targeted MP4 fuzzers]] - [[raw/github.com/gstreamer-qtdemux-oob-read-found-via-targeted-fuzzing-cve-2026-125041-15242|GStreamer qtdemux OOB read found via targeted fuzzing (CVE-2026-125041)]] - [[raw/github.blog/ai-powered-fuzzing-with-the-github-security-lab-taskflow-agent-12103|AI-powered fuzzing with the GitHub Security Lab Taskflow Agent]] - [[raw/appsec.guide/writing-harnesses-testing-handbook-3921|Writing Harnesses - Testing Handbook]] - [[raw/airbus-seclab.github.io/advanced-binary-fuzzing-using-afl-qemu-and-libprotobuf-4882|Advanced binary fuzzing using AFL++-QEMU and libprotobuf]] - [[raw/blog.trailofbits.com/go-fuzzing-was-missing-half-the-toolkit-we-forked-the-toolchain-to-fix-8584|Go fuzzing was missing half the toolkit. We forked the toolchain to fix it.]] - [[raw/github.com/libafl-0-16-1-release-14414|LibAFL 0.16.1 release]] - [[raw/projectzero.google/fuzzers-vs-ai-comparing-fuzzilli-and-bigsleep-on-javascript-engines-12406|Fuzzers vs. AI: Comparing Fuzzilli and BigSleep on JavaScript Engines]] - [[raw/blog.doyensec.com/ksmbd-fuzzing-improvements-and-vulnerability-discovery-4290|ksmbd: Fuzzing Improvements and Vulnerability Discovery]] - [[raw/pepezamudio.com/when-the-input-is-a-conversation-fandango-1-1-stateful-protocol-fuzzin-14700|When the input is a conversation: Fandango 1.1 stateful protocol fuzzing]] - [[raw/nyxfault.github.io/mastering-boofuzz-from-basics-to-advanced-3905|Mastering Boofuzz: From Basics to Advanced]] - [[raw/scapy.readthedocs.io/usage-scapy-2-4-5-documentation-5438|Usage Scapy 2.4.5. documentation]] - [[raw/arxiv.org/interceptfuzz-interception-based-stateful-fuzzing-of-single-process-ta-14564|InterceptFuzz: interception-based stateful fuzzing of single-process targets]] - [[raw/arxiv.org/interceptfuzz-interception-based-stateful-fuzzing-13293|InterceptFuzz: interception-based stateful fuzzing]] - [[raw/arxiv.org/a-survey-of-network-protocol-fuzzing-model-techniques-and-directions-4885|A Survey of Network Protocol Fuzzing: Model, Techniques and Directions]] - [[raw/ndss-symposium.org/large-language-model-guided-protocol-fuzzing-ndss-4878|Large Language Model guided Protocol Fuzzing (NDSS)]] - [[raw/arxiv.org/g2fuzz-grammar-aware-fuzzing-with-llms-3915|G2Fuzz: Grammar-Aware Fuzzing with LLMs]] - [[raw/conf.researchr.org/hybrid-fuzzing-in-input-generation-logic-ase-2026-12676|Hybrid Fuzzing in Input Generation Logic (ASE 2026)]] - [[raw/arxiv.org/typed-template-fuzzing-typos-13292|Typed Template Fuzzing (Typos)]] - [[raw/aflplus.plus/afl-tutorials-3926|AFL++ Tutorials]]