Fuzzing

Fuzzing an MQTT broker with AFL++ persistent mode and a state-aware harness

Naive packet fuzzing never got past CONNECT. Building a harness that drives a real session, keeps state between messages, and runs fast enough in persistent mode to find three crashes in a week.

1 min read

The AFL++ status screen during a long fuzzing run, showing three saved crashes
On this page

Summary

Feeding random bytes to an MQTT broker mostly exercises the first parser and nothing else, because almost every interesting handler sits behind a valid CONNECT. A harness that performs the handshake once and then fuzzes the following packets in persistent mode reached the subscription and retained-message code paths and found three memory-safety bugs.

Background

MQTT is a small publish-subscribe protocol that runs under a large amount of IoT infrastructure. Brokers are long-lived, stateful and exposed, which makes them a good fuzzing target and a worthwhile one.

Method

I split the broker’s connection handling from its socket code, replaced the network layer with an in-memory buffer, and wrapped a session setup in an AFL++ persistent loop. A small dictionary of packet types and flag bytes kept mutations structured without forcing them to be valid.

Findings

Throughput went from a few hundred to roughly 20,000 executions per second. Coverage climbed steadily for two days and then plateaued, at which point a custom mutator that rewrote the remaining-length field found two more crashes.

Takeaways

Harness quality matters more than raw speed. Spend the first day on state and reachability, and only then on performance.