xref: /aosp_15_r20/external/AFLplusplus/docs/fuzzing_in_depth.md (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1*08b48e0bSAndroid Build Coastguard Worker# Fuzzing with AFL++
2*08b48e0bSAndroid Build Coastguard Worker
3*08b48e0bSAndroid Build Coastguard WorkerThe following describes how to fuzz with a target if source code is available.
4*08b48e0bSAndroid Build Coastguard WorkerIf you have a binary-only target, go to
5*08b48e0bSAndroid Build Coastguard Worker[fuzzing_binary-only_targets.md](fuzzing_binary-only_targets.md).
6*08b48e0bSAndroid Build Coastguard Worker
7*08b48e0bSAndroid Build Coastguard WorkerFuzzing source code is a three-step process:
8*08b48e0bSAndroid Build Coastguard Worker
9*08b48e0bSAndroid Build Coastguard Worker1. Compile the target with a special compiler that prepares the target to be
10*08b48e0bSAndroid Build Coastguard Worker   fuzzed efficiently. This step is called "instrumenting a target".
11*08b48e0bSAndroid Build Coastguard Worker2. Prepare the fuzzing by selecting and optimizing the input corpus for the
12*08b48e0bSAndroid Build Coastguard Worker   target.
13*08b48e0bSAndroid Build Coastguard Worker3. Perform the fuzzing of the target by randomly mutating input and assessing if
14*08b48e0bSAndroid Build Coastguard Worker   that input was processed on a new path in the target binary.
15*08b48e0bSAndroid Build Coastguard Worker
16*08b48e0bSAndroid Build Coastguard Worker## 0. Common sense risks
17*08b48e0bSAndroid Build Coastguard Worker
18*08b48e0bSAndroid Build Coastguard WorkerPlease keep in mind that, similarly to many other computationally-intensive
19*08b48e0bSAndroid Build Coastguard Workertasks, fuzzing may put a strain on your hardware and on the OS. In particular:
20*08b48e0bSAndroid Build Coastguard Worker
21*08b48e0bSAndroid Build Coastguard Worker- Your CPU will run hot and will need adequate cooling. In most cases, if
22*08b48e0bSAndroid Build Coastguard Worker  cooling is insufficient or stops working properly, CPU speeds will be
23*08b48e0bSAndroid Build Coastguard Worker  automatically throttled. That said, especially when fuzzing on less suitable
24*08b48e0bSAndroid Build Coastguard Worker  hardware (laptops, smartphones, etc.), it's not entirely impossible for
25*08b48e0bSAndroid Build Coastguard Worker  something to blow up.
26*08b48e0bSAndroid Build Coastguard Worker
27*08b48e0bSAndroid Build Coastguard Worker- Targeted programs may end up erratically grabbing gigabytes of memory or
28*08b48e0bSAndroid Build Coastguard Worker  filling up disk space with junk files. AFL++ tries to enforce basic memory
29*08b48e0bSAndroid Build Coastguard Worker  limits, but can't prevent each and every possible mishap. The bottom line is
30*08b48e0bSAndroid Build Coastguard Worker  that you shouldn't be fuzzing on systems where the prospect of data loss is
31*08b48e0bSAndroid Build Coastguard Worker  not an acceptable risk.
32*08b48e0bSAndroid Build Coastguard Worker
33*08b48e0bSAndroid Build Coastguard Worker- Fuzzing involves billions of reads and writes to the filesystem. On modern
34*08b48e0bSAndroid Build Coastguard Worker  systems, this will be usually heavily cached, resulting in fairly modest
35*08b48e0bSAndroid Build Coastguard Worker  "physical" I/O - but there are many factors that may alter this equation. It
36*08b48e0bSAndroid Build Coastguard Worker  is your responsibility to monitor for potential trouble; with very heavy I/O,
37*08b48e0bSAndroid Build Coastguard Worker  the lifespan of many HDDs and SSDs may be reduced.
38*08b48e0bSAndroid Build Coastguard Worker
39*08b48e0bSAndroid Build Coastguard Worker  A good way to monitor disk I/O on Linux is the `iostat` command:
40*08b48e0bSAndroid Build Coastguard Worker
41*08b48e0bSAndroid Build Coastguard Worker  ```shell
42*08b48e0bSAndroid Build Coastguard Worker  $ iostat -d 3 -x -k [...optional disk ID...]
43*08b48e0bSAndroid Build Coastguard Worker  ```
44*08b48e0bSAndroid Build Coastguard Worker
45*08b48e0bSAndroid Build Coastguard Worker  Using the `AFL_TMPDIR` environment variable and a RAM-disk, you can have the
46*08b48e0bSAndroid Build Coastguard Worker  heavy writing done in RAM to prevent the aforementioned wear and tear. For
47*08b48e0bSAndroid Build Coastguard Worker  example, the following line will run a Docker container with all this preset:
48*08b48e0bSAndroid Build Coastguard Worker
49*08b48e0bSAndroid Build Coastguard Worker  ```shell
50*08b48e0bSAndroid Build Coastguard Worker  # docker run -ti --mount type=tmpfs,destination=/ramdisk -e AFL_TMPDIR=/ramdisk aflplusplus/aflplusplus
51*08b48e0bSAndroid Build Coastguard Worker  ```
52*08b48e0bSAndroid Build Coastguard Worker
53*08b48e0bSAndroid Build Coastguard Worker## 1. Instrumenting the target
54*08b48e0bSAndroid Build Coastguard Worker
55*08b48e0bSAndroid Build Coastguard Worker### a) Selecting the best AFL++ compiler for instrumenting the target
56*08b48e0bSAndroid Build Coastguard Worker
57*08b48e0bSAndroid Build Coastguard WorkerAFL++ comes with a central compiler `afl-cc` that incorporates various different
58*08b48e0bSAndroid Build Coastguard Workerkinds of compiler targets and instrumentation options. The following
59*08b48e0bSAndroid Build Coastguard Workerevaluation flow will help you to select the best possible.
60*08b48e0bSAndroid Build Coastguard Worker
61*08b48e0bSAndroid Build Coastguard WorkerIt is highly recommended to have the newest llvm version possible installed,
62*08b48e0bSAndroid Build Coastguard Workeranything below 9 is not recommended.
63*08b48e0bSAndroid Build Coastguard Worker
64*08b48e0bSAndroid Build Coastguard Worker```
65*08b48e0bSAndroid Build Coastguard Worker+--------------------------------+
66*08b48e0bSAndroid Build Coastguard Worker| clang/clang++ 11+ is available | --> use LTO mode (afl-clang-lto/afl-clang-lto++)
67*08b48e0bSAndroid Build Coastguard Worker+--------------------------------+     see [instrumentation/README.lto.md](instrumentation/README.lto.md)
68*08b48e0bSAndroid Build Coastguard Worker    |
69*08b48e0bSAndroid Build Coastguard Worker    | if not, or if the target fails with LTO afl-clang-lto/++
70*08b48e0bSAndroid Build Coastguard Worker    |
71*08b48e0bSAndroid Build Coastguard Worker    v
72*08b48e0bSAndroid Build Coastguard Worker+---------------------------------+
73*08b48e0bSAndroid Build Coastguard Worker| clang/clang++ 3.8+ is available | --> use LLVM mode (afl-clang-fast/afl-clang-fast++)
74*08b48e0bSAndroid Build Coastguard Worker+---------------------------------+     see [instrumentation/README.llvm.md](instrumentation/README.llvm.md)
75*08b48e0bSAndroid Build Coastguard Worker    |
76*08b48e0bSAndroid Build Coastguard Worker    | if not, or if the target fails with LLVM afl-clang-fast/++
77*08b48e0bSAndroid Build Coastguard Worker    |
78*08b48e0bSAndroid Build Coastguard Worker    v
79*08b48e0bSAndroid Build Coastguard Worker +--------------------------------+
80*08b48e0bSAndroid Build Coastguard Worker | gcc 5+ is available            | -> use GCC_PLUGIN mode (afl-gcc-fast/afl-g++-fast)
81*08b48e0bSAndroid Build Coastguard Worker +--------------------------------+    see [instrumentation/README.gcc_plugin.md](instrumentation/README.gcc_plugin.md) and
82*08b48e0bSAndroid Build Coastguard Worker                                       [instrumentation/README.instrument_list.md](instrumentation/README.instrument_list.md)
83*08b48e0bSAndroid Build Coastguard Worker    |
84*08b48e0bSAndroid Build Coastguard Worker    | if not, or if you do not have a gcc with plugin support
85*08b48e0bSAndroid Build Coastguard Worker    |
86*08b48e0bSAndroid Build Coastguard Worker    v
87*08b48e0bSAndroid Build Coastguard Worker   use GCC mode (afl-gcc/afl-g++) (or afl-clang/afl-clang++ for clang)
88*08b48e0bSAndroid Build Coastguard Worker```
89*08b48e0bSAndroid Build Coastguard Worker
90*08b48e0bSAndroid Build Coastguard WorkerClickable README links for the chosen compiler:
91*08b48e0bSAndroid Build Coastguard Worker
92*08b48e0bSAndroid Build Coastguard Worker* [LTO mode - afl-clang-lto](../instrumentation/README.lto.md)
93*08b48e0bSAndroid Build Coastguard Worker* [LLVM mode - afl-clang-fast](../instrumentation/README.llvm.md)
94*08b48e0bSAndroid Build Coastguard Worker* [GCC_PLUGIN mode - afl-gcc-fast](../instrumentation/README.gcc_plugin.md)
95*08b48e0bSAndroid Build Coastguard Worker* GCC/CLANG modes (afl-gcc/afl-clang) have no README as they have no own
96*08b48e0bSAndroid Build Coastguard Worker  features
97*08b48e0bSAndroid Build Coastguard Worker
98*08b48e0bSAndroid Build Coastguard WorkerYou can select the mode for the afl-cc compiler by one of the following methods:
99*08b48e0bSAndroid Build Coastguard Worker
100*08b48e0bSAndroid Build Coastguard Worker* Using a symlink to afl-cc: afl-gcc, afl-g++, afl-clang, afl-clang++,
101*08b48e0bSAndroid Build Coastguard Worker   afl-clang-fast, afl-clang-fast++, afl-clang-lto, afl-clang-lto++,
102*08b48e0bSAndroid Build Coastguard Worker   afl-gcc-fast, afl-g++-fast (recommended!).
103*08b48e0bSAndroid Build Coastguard Worker* Using the environment variable `AFL_CC_COMPILER` with `MODE`.
104*08b48e0bSAndroid Build Coastguard Worker* Passing --afl-`MODE` command line options to the compiler via
105*08b48e0bSAndroid Build Coastguard Worker   `CFLAGS`/`CXXFLAGS`/`CPPFLAGS`.
106*08b48e0bSAndroid Build Coastguard Worker
107*08b48e0bSAndroid Build Coastguard Worker`MODE` can be one of the following:
108*08b48e0bSAndroid Build Coastguard Worker
109*08b48e0bSAndroid Build Coastguard Worker* LTO (afl-clang-lto*)
110*08b48e0bSAndroid Build Coastguard Worker* LLVM (afl-clang-fast*)
111*08b48e0bSAndroid Build Coastguard Worker* GCC_PLUGIN (afl-g*-fast) or GCC (afl-gcc/afl-g++)
112*08b48e0bSAndroid Build Coastguard Worker* CLANG(afl-clang/afl-clang++)
113*08b48e0bSAndroid Build Coastguard Worker
114*08b48e0bSAndroid Build Coastguard WorkerBecause no AFL++ specific command-line options are accepted (beside the
115*08b48e0bSAndroid Build Coastguard Worker--afl-MODE command), the compile-time tools make fairly broad use of environment
116*08b48e0bSAndroid Build Coastguard Workervariables, which can be listed with `afl-cc -hh` or looked up in
117*08b48e0bSAndroid Build Coastguard Worker[env_variables.md](env_variables.md).
118*08b48e0bSAndroid Build Coastguard Worker
119*08b48e0bSAndroid Build Coastguard Worker### b) Selecting instrumentation options
120*08b48e0bSAndroid Build Coastguard Worker
121*08b48e0bSAndroid Build Coastguard WorkerIf you instrument with LTO mode (afl-clang-fast/afl-clang-lto), the following
122*08b48e0bSAndroid Build Coastguard Workeroptions are available:
123*08b48e0bSAndroid Build Coastguard Worker
124*08b48e0bSAndroid Build Coastguard Worker* Splitting integer, string, float, and switch comparisons so AFL++ can easier
125*08b48e0bSAndroid Build Coastguard Worker  solve these. This is an important option if you do not have a very good and
126*08b48e0bSAndroid Build Coastguard Worker  large input corpus. This technique is called laf-intel or COMPCOV. To use
127*08b48e0bSAndroid Build Coastguard Worker  this, set the following environment variable before compiling the target:
128*08b48e0bSAndroid Build Coastguard Worker  `export AFL_LLVM_LAF_ALL=1`. You can read more about this in
129*08b48e0bSAndroid Build Coastguard Worker  [instrumentation/README.laf-intel.md](../instrumentation/README.laf-intel.md).
130*08b48e0bSAndroid Build Coastguard Worker* A different technique (and usually a better one than laf-intel) is to
131*08b48e0bSAndroid Build Coastguard Worker  instrument the target so that any compare values in the target are sent to
132*08b48e0bSAndroid Build Coastguard Worker  AFL++ which then tries to put these values into the fuzzing data at different
133*08b48e0bSAndroid Build Coastguard Worker  locations. This technique is very fast and good - if the target does not
134*08b48e0bSAndroid Build Coastguard Worker  transform input data before comparison. Therefore, this technique is called
135*08b48e0bSAndroid Build Coastguard Worker  `input to state` or `redqueen`. If you want to use this technique, then you
136*08b48e0bSAndroid Build Coastguard Worker  have to compile the target twice, once specifically with/for this mode by
137*08b48e0bSAndroid Build Coastguard Worker  setting `AFL_LLVM_CMPLOG=1`, and pass this binary to afl-fuzz via the `-c`
138*08b48e0bSAndroid Build Coastguard Worker  parameter. Note that you can compile also just a cmplog binary and use that
139*08b48e0bSAndroid Build Coastguard Worker  for both, however, there will be a performance penalty. You can read more
140*08b48e0bSAndroid Build Coastguard Worker  about this in
141*08b48e0bSAndroid Build Coastguard Worker  [instrumentation/README.cmplog.md](../instrumentation/README.cmplog.md).
142*08b48e0bSAndroid Build Coastguard Worker
143*08b48e0bSAndroid Build Coastguard WorkerIf you use LTO, LLVM, or GCC_PLUGIN mode
144*08b48e0bSAndroid Build Coastguard Worker(afl-clang-fast/afl-clang-lto/afl-gcc-fast), you have the option to selectively
145*08b48e0bSAndroid Build Coastguard Workerinstrument _parts_ of the target that you are interested in. For afl-clang-fast,
146*08b48e0bSAndroid Build Coastguard Workeryou have to use an llvm version newer than 10.0.0 or a mode other than
147*08b48e0bSAndroid Build Coastguard WorkerDEFAULT/PCGUARD.
148*08b48e0bSAndroid Build Coastguard Worker
149*08b48e0bSAndroid Build Coastguard WorkerThis step can be done either by explicitly including parts to be instrumented or
150*08b48e0bSAndroid Build Coastguard Workerby explicitly excluding parts from instrumentation.
151*08b48e0bSAndroid Build Coastguard Worker
152*08b48e0bSAndroid Build Coastguard Worker* To instrument _only specified parts_, create a file (e.g., `allowlist.txt`)
153*08b48e0bSAndroid Build Coastguard Worker  with all the filenames and/or functions of the source code that should be
154*08b48e0bSAndroid Build Coastguard Worker  instrumented and then:
155*08b48e0bSAndroid Build Coastguard Worker
156*08b48e0bSAndroid Build Coastguard Worker  1. Just put one filename or function (prefixing with `fun: `) per line (no
157*08b48e0bSAndroid Build Coastguard Worker     directory information necessary for filenames) in the file `allowlist.txt`.
158*08b48e0bSAndroid Build Coastguard Worker
159*08b48e0bSAndroid Build Coastguard Worker     Example:
160*08b48e0bSAndroid Build Coastguard Worker
161*08b48e0bSAndroid Build Coastguard Worker     ```
162*08b48e0bSAndroid Build Coastguard Worker     foo.cpp        # will match foo/foo.cpp, bar/foo.cpp, barfoo.cpp etc.
163*08b48e0bSAndroid Build Coastguard Worker     fun: foo_func  # will match the function foo_func
164*08b48e0bSAndroid Build Coastguard Worker     ```
165*08b48e0bSAndroid Build Coastguard Worker
166*08b48e0bSAndroid Build Coastguard Worker  2. Set `export AFL_LLVM_ALLOWLIST=allowlist.txt` to enable selective positive
167*08b48e0bSAndroid Build Coastguard Worker     instrumentation.
168*08b48e0bSAndroid Build Coastguard Worker
169*08b48e0bSAndroid Build Coastguard Worker* Similarly to _exclude_ specified parts from instrumentation, create a file
170*08b48e0bSAndroid Build Coastguard Worker  (e.g., `denylist.txt`) with all the filenames of the source code that should
171*08b48e0bSAndroid Build Coastguard Worker  be skipped during instrumentation and then:
172*08b48e0bSAndroid Build Coastguard Worker
173*08b48e0bSAndroid Build Coastguard Worker  1. Same as above. Just put one filename or function per line in the file
174*08b48e0bSAndroid Build Coastguard Worker     `denylist.txt`.
175*08b48e0bSAndroid Build Coastguard Worker
176*08b48e0bSAndroid Build Coastguard Worker  2. Set `export AFL_LLVM_DENYLIST=denylist.txt` to enable selective negative
177*08b48e0bSAndroid Build Coastguard Worker     instrumentation.
178*08b48e0bSAndroid Build Coastguard Worker
179*08b48e0bSAndroid Build Coastguard Worker**NOTE:** During optimization functions might be
180*08b48e0bSAndroid Build Coastguard Workerinlined and then would not match the list! See
181*08b48e0bSAndroid Build Coastguard Worker[instrumentation/README.instrument_list.md](../instrumentation/README.instrument_list.md).
182*08b48e0bSAndroid Build Coastguard Worker
183*08b48e0bSAndroid Build Coastguard WorkerThere are many more options and modes available, however, these are most of the
184*08b48e0bSAndroid Build Coastguard Workertime less effective. See:
185*08b48e0bSAndroid Build Coastguard Worker
186*08b48e0bSAndroid Build Coastguard Worker* [instrumentation/README.llvm.md#6) AFL++ Context Sensitive Branch Coverage](../instrumentation/README.llvm.md#6-afl-context-sensitive-branch-coverage)
187*08b48e0bSAndroid Build Coastguard Worker* [instrumentation/README.llvm.md#7) AFL++ N-Gram Branch Coverage](../instrumentation/README.llvm.md#7-afl-n-gram-branch-coverage)
188*08b48e0bSAndroid Build Coastguard Worker
189*08b48e0bSAndroid Build Coastguard WorkerAFL++ performs "never zero" counting in its bitmap. You can read more about this
190*08b48e0bSAndroid Build Coastguard Workerhere:
191*08b48e0bSAndroid Build Coastguard Worker* [instrumentation/README.llvm.md#8-neverzero-counters](../instrumentation/README.llvm.md#8-neverzero-counters)
192*08b48e0bSAndroid Build Coastguard Worker
193*08b48e0bSAndroid Build Coastguard Worker### c) Selecting sanitizers
194*08b48e0bSAndroid Build Coastguard Worker
195*08b48e0bSAndroid Build Coastguard WorkerIt is possible to use sanitizers when instrumenting targets for fuzzing, which
196*08b48e0bSAndroid Build Coastguard Workerallows you to find bugs that would not necessarily result in a crash.
197*08b48e0bSAndroid Build Coastguard Worker
198*08b48e0bSAndroid Build Coastguard WorkerNote that sanitizers have a huge impact on CPU (= less executions per second)
199*08b48e0bSAndroid Build Coastguard Workerand RAM usage. Also, you should only run one afl-fuzz instance per sanitizer
200*08b48e0bSAndroid Build Coastguard Workertype. This is enough because e.g. a use-after-free bug will be picked up by ASAN
201*08b48e0bSAndroid Build Coastguard Worker(address sanitizer) anyway after syncing test cases from other fuzzing
202*08b48e0bSAndroid Build Coastguard Workerinstances, so running more than one address sanitized target would be a waste.
203*08b48e0bSAndroid Build Coastguard Worker
204*08b48e0bSAndroid Build Coastguard WorkerThe following sanitizers have built-in support in AFL++:
205*08b48e0bSAndroid Build Coastguard Worker
206*08b48e0bSAndroid Build Coastguard Worker* ASAN = Address SANitizer, finds memory corruption vulnerabilities like
207*08b48e0bSAndroid Build Coastguard Worker  use-after-free, NULL pointer dereference, buffer overruns, etc. Enabled with
208*08b48e0bSAndroid Build Coastguard Worker  `export AFL_USE_ASAN=1` before compiling.
209*08b48e0bSAndroid Build Coastguard Worker* MSAN = Memory SANitizer, finds read accesses to uninitialized memory, e.g., a
210*08b48e0bSAndroid Build Coastguard Worker  local variable that is defined and read before it is even set. Enabled with
211*08b48e0bSAndroid Build Coastguard Worker  `export AFL_USE_MSAN=1` before compiling.
212*08b48e0bSAndroid Build Coastguard Worker* UBSAN = Undefined Behavior SANitizer, finds instances where - by the C and C++
213*08b48e0bSAndroid Build Coastguard Worker  standards - undefined behavior happens, e.g., adding two signed integers where
214*08b48e0bSAndroid Build Coastguard Worker  the result is larger than what a signed integer can hold. Enabled with `export
215*08b48e0bSAndroid Build Coastguard Worker  AFL_USE_UBSAN=1` before compiling.
216*08b48e0bSAndroid Build Coastguard Worker* CFISAN = Control Flow Integrity SANitizer, finds instances where the control
217*08b48e0bSAndroid Build Coastguard Worker  flow is found to be illegal. Originally this was rather to prevent return
218*08b48e0bSAndroid Build Coastguard Worker  oriented programming (ROP) exploit chains from functioning. In fuzzing, this
219*08b48e0bSAndroid Build Coastguard Worker  is mostly reduced to detecting type confusion vulnerabilities - which is,
220*08b48e0bSAndroid Build Coastguard Worker  however, one of the most important and dangerous C++ memory corruption
221*08b48e0bSAndroid Build Coastguard Worker  classes! Enabled with `export AFL_USE_CFISAN=1` before compiling.
222*08b48e0bSAndroid Build Coastguard Worker* TSAN = Thread SANitizer, finds thread race conditions. Enabled with `export
223*08b48e0bSAndroid Build Coastguard Worker  AFL_USE_TSAN=1` before compiling.
224*08b48e0bSAndroid Build Coastguard Worker* LSAN = Leak SANitizer, finds memory leaks in a program. This is not really a
225*08b48e0bSAndroid Build Coastguard Worker  security issue, but for developers this can be very valuable. Note that unlike
226*08b48e0bSAndroid Build Coastguard Worker  the other sanitizers above this needs `__AFL_LEAK_CHECK();` added to all areas
227*08b48e0bSAndroid Build Coastguard Worker  of the target source code where you find a leak check necessary! Enabled with
228*08b48e0bSAndroid Build Coastguard Worker  `export AFL_USE_LSAN=1` before compiling. To ignore the memory-leaking check
229*08b48e0bSAndroid Build Coastguard Worker  for certain allocations, `__AFL_LSAN_OFF();` can be used before memory is
230*08b48e0bSAndroid Build Coastguard Worker  allocated, and `__AFL_LSAN_ON();` afterwards. Memory allocated between these
231*08b48e0bSAndroid Build Coastguard Worker  two macros will not be checked for memory leaks.
232*08b48e0bSAndroid Build Coastguard Worker
233*08b48e0bSAndroid Build Coastguard WorkerIt is possible to further modify the behavior of the sanitizers at run-time by
234*08b48e0bSAndroid Build Coastguard Workersetting `ASAN_OPTIONS=...`, `LSAN_OPTIONS` etc. - the available parameters can
235*08b48e0bSAndroid Build Coastguard Workerbe looked up in the sanitizer documentation of llvm/clang. afl-fuzz, however,
236*08b48e0bSAndroid Build Coastguard Workerrequires some specific parameters important for fuzzing to be set. If you want
237*08b48e0bSAndroid Build Coastguard Workerto set your own, it might bail and report what it is missing.
238*08b48e0bSAndroid Build Coastguard Worker
239*08b48e0bSAndroid Build Coastguard WorkerNote that some sanitizers cannot be used together, e.g., ASAN and MSAN, and
240*08b48e0bSAndroid Build Coastguard Workerothers often cannot work together because of target weirdness, e.g., ASAN and
241*08b48e0bSAndroid Build Coastguard WorkerCFISAN. You might need to experiment which sanitizers you can combine in a
242*08b48e0bSAndroid Build Coastguard Workertarget (which means more instances can be run without a sanitized target, which
243*08b48e0bSAndroid Build Coastguard Workeris more effective).
244*08b48e0bSAndroid Build Coastguard Worker
245*08b48e0bSAndroid Build Coastguard Worker### d) Modifying the target
246*08b48e0bSAndroid Build Coastguard Worker
247*08b48e0bSAndroid Build Coastguard WorkerIf the target has features that make fuzzing more difficult, e.g., checksums,
248*08b48e0bSAndroid Build Coastguard WorkerHMAC, etc., then modify the source code so that checks for these values are
249*08b48e0bSAndroid Build Coastguard Workerremoved. This can even be done safely for source code used in operational
250*08b48e0bSAndroid Build Coastguard Workerproducts by eliminating these checks within these AFL++ specific blocks:
251*08b48e0bSAndroid Build Coastguard Worker
252*08b48e0bSAndroid Build Coastguard Worker```
253*08b48e0bSAndroid Build Coastguard Worker#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
254*08b48e0bSAndroid Build Coastguard Worker  // say that the checksum or HMAC was fine - or whatever is required
255*08b48e0bSAndroid Build Coastguard Worker  // to eliminate the need for the fuzzer to guess the right checksum
256*08b48e0bSAndroid Build Coastguard Worker  return 0;
257*08b48e0bSAndroid Build Coastguard Worker#endif
258*08b48e0bSAndroid Build Coastguard Worker```
259*08b48e0bSAndroid Build Coastguard Worker
260*08b48e0bSAndroid Build Coastguard WorkerAll AFL++ compilers will set this preprocessor definition automatically.
261*08b48e0bSAndroid Build Coastguard Worker
262*08b48e0bSAndroid Build Coastguard Worker### e) Instrumenting the target
263*08b48e0bSAndroid Build Coastguard Worker
264*08b48e0bSAndroid Build Coastguard WorkerIn this step, the target source code is compiled so that it can be fuzzed.
265*08b48e0bSAndroid Build Coastguard Worker
266*08b48e0bSAndroid Build Coastguard WorkerBasically, you have to tell the target build system that the selected AFL++
267*08b48e0bSAndroid Build Coastguard Workercompiler is used. Also - if possible - you should always configure the build
268*08b48e0bSAndroid Build Coastguard Workersystem in such way that the target is compiled statically and not dynamically.
269*08b48e0bSAndroid Build Coastguard WorkerHow to do this is described below.
270*08b48e0bSAndroid Build Coastguard Worker
271*08b48e0bSAndroid Build Coastguard WorkerThe #1 rule when instrumenting a target is: avoid instrumenting shared libraries
272*08b48e0bSAndroid Build Coastguard Workerat all cost. You would need to set `LD_LIBRARY_PATH` to point to these, you
273*08b48e0bSAndroid Build Coastguard Workercould accidentally type "make install" and install them system wide - so don't.
274*08b48e0bSAndroid Build Coastguard WorkerReally don't. **Always compile libraries you want to have instrumented as static
275*08b48e0bSAndroid Build Coastguard Workerand link these to the target program!**
276*08b48e0bSAndroid Build Coastguard Worker
277*08b48e0bSAndroid Build Coastguard WorkerThen build the target. (Usually with `make`.)
278*08b48e0bSAndroid Build Coastguard Worker
279*08b48e0bSAndroid Build Coastguard Worker**NOTES**
280*08b48e0bSAndroid Build Coastguard Worker
281*08b48e0bSAndroid Build Coastguard Worker1. Sometimes configure and build systems are fickle and do not like stderr
282*08b48e0bSAndroid Build Coastguard Worker   output (and think this means a test failure) - which is something AFL++ likes
283*08b48e0bSAndroid Build Coastguard Worker   to do to show statistics. It is recommended to disable AFL++ instrumentation
284*08b48e0bSAndroid Build Coastguard Worker   reporting via `export AFL_QUIET=1`.
285*08b48e0bSAndroid Build Coastguard Worker
286*08b48e0bSAndroid Build Coastguard Worker2. Sometimes configure and build systems error on warnings - these should be
287*08b48e0bSAndroid Build Coastguard Worker   disabled (e.g., `--disable-werror` for some configure scripts).
288*08b48e0bSAndroid Build Coastguard Worker
289*08b48e0bSAndroid Build Coastguard Worker3. In case the configure/build system complains about AFL++'s compiler and
290*08b48e0bSAndroid Build Coastguard Worker   aborts, then set `export AFL_NOOPT=1` which will then just behave like the
291*08b48e0bSAndroid Build Coastguard Worker   real compiler and run the configure step separately. For building the target
292*08b48e0bSAndroid Build Coastguard Worker   afterwards this option has to be unset again!
293*08b48e0bSAndroid Build Coastguard Worker
294*08b48e0bSAndroid Build Coastguard Worker#### configure
295*08b48e0bSAndroid Build Coastguard Worker
296*08b48e0bSAndroid Build Coastguard WorkerFor `configure` build systems, this is usually done by:
297*08b48e0bSAndroid Build Coastguard Worker
298*08b48e0bSAndroid Build Coastguard Worker```
299*08b48e0bSAndroid Build Coastguard WorkerCC=afl-clang-fast CXX=afl-clang-fast++ ./configure --disable-shared
300*08b48e0bSAndroid Build Coastguard Worker```
301*08b48e0bSAndroid Build Coastguard Worker
302*08b48e0bSAndroid Build Coastguard WorkerNote that if you are using the (better) afl-clang-lto compiler, you also have to
303*08b48e0bSAndroid Build Coastguard Workerset `AR` to llvm-ar[-VERSION] and `RANLIB` to llvm-ranlib[-VERSION] - as is
304*08b48e0bSAndroid Build Coastguard Workerdescribed in [instrumentation/README.lto.md](../instrumentation/README.lto.md).
305*08b48e0bSAndroid Build Coastguard Worker
306*08b48e0bSAndroid Build Coastguard Worker#### CMake
307*08b48e0bSAndroid Build Coastguard Worker
308*08b48e0bSAndroid Build Coastguard WorkerFor CMake build systems, this is usually done by:
309*08b48e0bSAndroid Build Coastguard Worker
310*08b48e0bSAndroid Build Coastguard Worker```
311*08b48e0bSAndroid Build Coastguard Workermkdir build; cd build; cmake -DCMAKE_C_COMPILER=afl-cc -DCMAKE_CXX_COMPILER=afl-c++ ..
312*08b48e0bSAndroid Build Coastguard Worker```
313*08b48e0bSAndroid Build Coastguard Worker
314*08b48e0bSAndroid Build Coastguard WorkerNote that if you are using the (better) afl-clang-lto compiler you also have to
315*08b48e0bSAndroid Build Coastguard Workerset AR to llvm-ar[-VERSION] and RANLIB to llvm-ranlib[-VERSION] - as is
316*08b48e0bSAndroid Build Coastguard Workerdescribed in [instrumentation/README.lto.md](../instrumentation/README.lto.md).
317*08b48e0bSAndroid Build Coastguard Worker
318*08b48e0bSAndroid Build Coastguard Worker#### Meson Build System
319*08b48e0bSAndroid Build Coastguard Worker
320*08b48e0bSAndroid Build Coastguard WorkerFor the Meson Build System, you have to set the AFL++ compiler with the very
321*08b48e0bSAndroid Build Coastguard Workerfirst command!
322*08b48e0bSAndroid Build Coastguard Worker
323*08b48e0bSAndroid Build Coastguard Worker```
324*08b48e0bSAndroid Build Coastguard WorkerCC=afl-cc CXX=afl-c++ meson
325*08b48e0bSAndroid Build Coastguard Worker```
326*08b48e0bSAndroid Build Coastguard Worker
327*08b48e0bSAndroid Build Coastguard Worker#### Other build systems or if configure/cmake didn't work
328*08b48e0bSAndroid Build Coastguard Worker
329*08b48e0bSAndroid Build Coastguard WorkerSometimes `cmake` and `configure` do not pick up the AFL++ compiler or the
330*08b48e0bSAndroid Build Coastguard Worker`RANLIB`/`AR` that is needed - because this was just not foreseen by the
331*08b48e0bSAndroid Build Coastguard Workerdeveloper of the target. Or they have non-standard options. Figure out if there
332*08b48e0bSAndroid Build Coastguard Workeris a non-standard way to set this, otherwise set up the build normally and edit
333*08b48e0bSAndroid Build Coastguard Workerthe generated build environment afterwards manually to point it to the right
334*08b48e0bSAndroid Build Coastguard Workercompiler (and/or `RANLIB` and `AR`).
335*08b48e0bSAndroid Build Coastguard Worker
336*08b48e0bSAndroid Build Coastguard WorkerIn complex, weird, alien build systems you can try this neat project:
337*08b48e0bSAndroid Build Coastguard Worker[https://github.com/fuzzah/exeptor](https://github.com/fuzzah/exeptor)
338*08b48e0bSAndroid Build Coastguard Worker
339*08b48e0bSAndroid Build Coastguard Worker#### Linker scripts
340*08b48e0bSAndroid Build Coastguard Worker
341*08b48e0bSAndroid Build Coastguard WorkerIf the project uses linker scripts to hide the symbols exported by the
342*08b48e0bSAndroid Build Coastguard Workerbinary, then you may see errors such as:
343*08b48e0bSAndroid Build Coastguard Worker
344*08b48e0bSAndroid Build Coastguard Worker```
345*08b48e0bSAndroid Build Coastguard Workerundefined symbol: __afl_area_ptr
346*08b48e0bSAndroid Build Coastguard Worker```
347*08b48e0bSAndroid Build Coastguard Worker
348*08b48e0bSAndroid Build Coastguard WorkerThe solution is to modify the linker script to add:
349*08b48e0bSAndroid Build Coastguard Worker
350*08b48e0bSAndroid Build Coastguard Worker```
351*08b48e0bSAndroid Build Coastguard Worker{
352*08b48e0bSAndroid Build Coastguard Worker  global:
353*08b48e0bSAndroid Build Coastguard Worker    __afl_*;
354*08b48e0bSAndroid Build Coastguard Worker}
355*08b48e0bSAndroid Build Coastguard Worker```
356*08b48e0bSAndroid Build Coastguard Worker
357*08b48e0bSAndroid Build Coastguard Worker### f) Better instrumentation
358*08b48e0bSAndroid Build Coastguard Worker
359*08b48e0bSAndroid Build Coastguard WorkerIf you just fuzz a target program as-is, you are wasting a great opportunity for
360*08b48e0bSAndroid Build Coastguard Workermuch more fuzzing speed.
361*08b48e0bSAndroid Build Coastguard Worker
362*08b48e0bSAndroid Build Coastguard WorkerThis variant requires the usage of afl-clang-lto, afl-clang-fast or
363*08b48e0bSAndroid Build Coastguard Workerafl-gcc-fast.
364*08b48e0bSAndroid Build Coastguard Worker
365*08b48e0bSAndroid Build Coastguard WorkerIt is the so-called `persistent mode`, which is much, much faster but requires
366*08b48e0bSAndroid Build Coastguard Workerthat you code a source file that is specifically calling the target functions
367*08b48e0bSAndroid Build Coastguard Workerthat you want to fuzz, plus a few specific AFL++ functions around it. See
368*08b48e0bSAndroid Build Coastguard Worker[instrumentation/README.persistent_mode.md](../instrumentation/README.persistent_mode.md)
369*08b48e0bSAndroid Build Coastguard Workerfor details.
370*08b48e0bSAndroid Build Coastguard Worker
371*08b48e0bSAndroid Build Coastguard WorkerBasically, if you do not fuzz a target in persistent mode, then you are just
372*08b48e0bSAndroid Build Coastguard Workerdoing it for a hobby and not professionally :-).
373*08b48e0bSAndroid Build Coastguard Worker
374*08b48e0bSAndroid Build Coastguard Worker### g) libfuzzer fuzzer harnesses with LLVMFuzzerTestOneInput()
375*08b48e0bSAndroid Build Coastguard Worker
376*08b48e0bSAndroid Build Coastguard Workerlibfuzzer `LLVMFuzzerTestOneInput()` harnesses are the defacto standard for
377*08b48e0bSAndroid Build Coastguard Workerfuzzing, and they can be used with AFL++ (and honggfuzz) as well!
378*08b48e0bSAndroid Build Coastguard Worker
379*08b48e0bSAndroid Build Coastguard WorkerCompiling them is as simple as:
380*08b48e0bSAndroid Build Coastguard Worker
381*08b48e0bSAndroid Build Coastguard Worker```
382*08b48e0bSAndroid Build Coastguard Workerafl-clang-fast++ -fsanitize=fuzzer -o harness harness.cpp targetlib.a
383*08b48e0bSAndroid Build Coastguard Worker```
384*08b48e0bSAndroid Build Coastguard Worker
385*08b48e0bSAndroid Build Coastguard WorkerYou can even use advanced libfuzzer features like `FuzzedDataProvider`,
386*08b48e0bSAndroid Build Coastguard Worker`LLVMFuzzerInitialize()` etc. and they will work!
387*08b48e0bSAndroid Build Coastguard Worker
388*08b48e0bSAndroid Build Coastguard WorkerThe generated binary is fuzzed with afl-fuzz like any other fuzz target.
389*08b48e0bSAndroid Build Coastguard Worker
390*08b48e0bSAndroid Build Coastguard WorkerBonus: the target is already optimized for fuzzing due to persistent mode and
391*08b48e0bSAndroid Build Coastguard Workershared-memory test cases and hence gives you the fastest speed possible.
392*08b48e0bSAndroid Build Coastguard Worker
393*08b48e0bSAndroid Build Coastguard WorkerFor more information, see
394*08b48e0bSAndroid Build Coastguard Worker[utils/aflpp_driver/README.md](../utils/aflpp_driver/README.md).
395*08b48e0bSAndroid Build Coastguard Worker
396*08b48e0bSAndroid Build Coastguard Worker## 2. Preparing the fuzzing campaign
397*08b48e0bSAndroid Build Coastguard Worker
398*08b48e0bSAndroid Build Coastguard WorkerAs you fuzz the target with mutated input, having as diverse inputs for the
399*08b48e0bSAndroid Build Coastguard Workertarget as possible improves the efficiency a lot.
400*08b48e0bSAndroid Build Coastguard Worker
401*08b48e0bSAndroid Build Coastguard Worker### a) Collecting inputs
402*08b48e0bSAndroid Build Coastguard Worker
403*08b48e0bSAndroid Build Coastguard WorkerTo operate correctly, the fuzzer requires one or more starting files that
404*08b48e0bSAndroid Build Coastguard Workercontain a good example of the input data normally expected by the targeted
405*08b48e0bSAndroid Build Coastguard Workerapplication.
406*08b48e0bSAndroid Build Coastguard Worker
407*08b48e0bSAndroid Build Coastguard WorkerTry to gather valid inputs for the target from wherever you can. E.g., if it is
408*08b48e0bSAndroid Build Coastguard Workerthe PNG picture format, try to find as many PNG files as possible, e.g., from
409*08b48e0bSAndroid Build Coastguard Workerreported bugs, test suites, random downloads from the internet, unit test case
410*08b48e0bSAndroid Build Coastguard Workerdata - from all kind of PNG software.
411*08b48e0bSAndroid Build Coastguard Worker
412*08b48e0bSAndroid Build Coastguard WorkerIf the input format is not known, you can also modify a target program to write
413*08b48e0bSAndroid Build Coastguard Workernormal data it receives and processes to a file and use these.
414*08b48e0bSAndroid Build Coastguard Worker
415*08b48e0bSAndroid Build Coastguard WorkerYou can find many good examples of starting files in the
416*08b48e0bSAndroid Build Coastguard Worker[testcases/](../testcases) subdirectory that comes with this tool.
417*08b48e0bSAndroid Build Coastguard Worker
418*08b48e0bSAndroid Build Coastguard Worker### b) Making the input corpus unique
419*08b48e0bSAndroid Build Coastguard Worker
420*08b48e0bSAndroid Build Coastguard WorkerUse the AFL++ tool `afl-cmin` to remove inputs from the corpus that do not
421*08b48e0bSAndroid Build Coastguard Workerproduce a new path/coverage in the target:
422*08b48e0bSAndroid Build Coastguard Worker
423*08b48e0bSAndroid Build Coastguard Worker1. Put all files from [step a](#a-collecting-inputs) into one directory, e.g.,
424*08b48e0bSAndroid Build Coastguard Worker   `INPUTS`.
425*08b48e0bSAndroid Build Coastguard Worker2. Run afl-cmin:
426*08b48e0bSAndroid Build Coastguard Worker   * If the target program is to be called by fuzzing as `bin/target INPUTFILE`,
427*08b48e0bSAndroid Build Coastguard Worker     replace the INPUTFILE argument that the target program would read from with
428*08b48e0bSAndroid Build Coastguard Worker     `@@`:
429*08b48e0bSAndroid Build Coastguard Worker
430*08b48e0bSAndroid Build Coastguard Worker     ```
431*08b48e0bSAndroid Build Coastguard Worker     afl-cmin -i INPUTS -o INPUTS_UNIQUE -- bin/target -someopt @@
432*08b48e0bSAndroid Build Coastguard Worker     ```
433*08b48e0bSAndroid Build Coastguard Worker
434*08b48e0bSAndroid Build Coastguard Worker   * If the target reads from stdin (standard input) instead, just omit the `@@`
435*08b48e0bSAndroid Build Coastguard Worker     as this is the default:
436*08b48e0bSAndroid Build Coastguard Worker
437*08b48e0bSAndroid Build Coastguard Worker     ```
438*08b48e0bSAndroid Build Coastguard Worker     afl-cmin -i INPUTS -o INPUTS_UNIQUE -- bin/target -someopt
439*08b48e0bSAndroid Build Coastguard Worker     ```
440*08b48e0bSAndroid Build Coastguard Worker
441*08b48e0bSAndroid Build Coastguard WorkerThis step is highly recommended, because afterwards the testcase corpus is not
442*08b48e0bSAndroid Build Coastguard Workerbloated with duplicates anymore, which would slow down the fuzzing progress!
443*08b48e0bSAndroid Build Coastguard Worker
444*08b48e0bSAndroid Build Coastguard Worker### c) Minimizing all corpus files
445*08b48e0bSAndroid Build Coastguard Worker
446*08b48e0bSAndroid Build Coastguard WorkerThe shorter the input files that still traverse the same path within the target,
447*08b48e0bSAndroid Build Coastguard Workerthe better the fuzzing will be. This minimization is done with `afl-tmin`,
448*08b48e0bSAndroid Build Coastguard Workerhowever, it is a long process as this has to be done for every file:
449*08b48e0bSAndroid Build Coastguard Worker
450*08b48e0bSAndroid Build Coastguard Worker```
451*08b48e0bSAndroid Build Coastguard Workermkdir input
452*08b48e0bSAndroid Build Coastguard Workercd INPUTS_UNIQUE
453*08b48e0bSAndroid Build Coastguard Workerfor i in *; do
454*08b48e0bSAndroid Build Coastguard Worker  afl-tmin -i "$i" -o "../input/$i" -- bin/target -someopt @@
455*08b48e0bSAndroid Build Coastguard Workerdone
456*08b48e0bSAndroid Build Coastguard Worker```
457*08b48e0bSAndroid Build Coastguard Worker
458*08b48e0bSAndroid Build Coastguard WorkerThis step can also be parallelized, e.g., with `parallel`.
459*08b48e0bSAndroid Build Coastguard Worker
460*08b48e0bSAndroid Build Coastguard WorkerNote that this step is rather optional though.
461*08b48e0bSAndroid Build Coastguard Worker
462*08b48e0bSAndroid Build Coastguard Worker### Done!
463*08b48e0bSAndroid Build Coastguard Worker
464*08b48e0bSAndroid Build Coastguard WorkerThe INPUTS_UNIQUE/ directory from [step b](#b-making-the-input-corpus-unique) -
465*08b48e0bSAndroid Build Coastguard Workeror even better the directory input/ if you minimized the corpus in
466*08b48e0bSAndroid Build Coastguard Worker[step c](#c-minimizing-all-corpus-files) - is the resulting input corpus
467*08b48e0bSAndroid Build Coastguard Workerdirectory to be used in fuzzing! :-)
468*08b48e0bSAndroid Build Coastguard Worker
469*08b48e0bSAndroid Build Coastguard Worker## 3. Fuzzing the target
470*08b48e0bSAndroid Build Coastguard Worker
471*08b48e0bSAndroid Build Coastguard WorkerIn this final step, fuzz the target. There are not that many important options
472*08b48e0bSAndroid Build Coastguard Workerto run the target - unless you want to use many CPU cores/threads for the
473*08b48e0bSAndroid Build Coastguard Workerfuzzing, which will make the fuzzing much more useful.
474*08b48e0bSAndroid Build Coastguard Worker
475*08b48e0bSAndroid Build Coastguard WorkerIf you just use one instance for fuzzing, then you are fuzzing just for fun and
476*08b48e0bSAndroid Build Coastguard Workernot seriously :-)
477*08b48e0bSAndroid Build Coastguard Worker
478*08b48e0bSAndroid Build Coastguard Worker### a) Running afl-fuzz
479*08b48e0bSAndroid Build Coastguard Worker
480*08b48e0bSAndroid Build Coastguard WorkerBefore you do even a test run of afl-fuzz, execute `sudo afl-system-config` (on
481*08b48e0bSAndroid Build Coastguard Workerthe host if you execute afl-fuzz in a Docker container). This reconfigures the
482*08b48e0bSAndroid Build Coastguard Workersystem for optimal speed - which afl-fuzz checks and bails otherwise. Set
483*08b48e0bSAndroid Build Coastguard Worker`export AFL_SKIP_CPUFREQ=1` for afl-fuzz to skip this check if you cannot run
484*08b48e0bSAndroid Build Coastguard Workerafl-system-config with root privileges on the host for whatever reason.
485*08b48e0bSAndroid Build Coastguard Worker
486*08b48e0bSAndroid Build Coastguard WorkerNote:
487*08b48e0bSAndroid Build Coastguard Worker
488*08b48e0bSAndroid Build Coastguard Worker* There is also `sudo afl-persistent-config` which sets additional permanent
489*08b48e0bSAndroid Build Coastguard Worker  boot options for a much better fuzzing performance.
490*08b48e0bSAndroid Build Coastguard Worker* Both scripts improve your fuzzing performance but also decrease your system
491*08b48e0bSAndroid Build Coastguard Worker  protection against attacks! So set strong firewall rules and only expose SSH
492*08b48e0bSAndroid Build Coastguard Worker  as a network service if you use these (which is highly recommended).
493*08b48e0bSAndroid Build Coastguard Worker
494*08b48e0bSAndroid Build Coastguard WorkerIf you have an input corpus from [step 2](#2-preparing-the-fuzzing-campaign),
495*08b48e0bSAndroid Build Coastguard Workerthen specify this directory with the `-i` option. Otherwise, create a new
496*08b48e0bSAndroid Build Coastguard Workerdirectory and create a file with any content as test data in there.
497*08b48e0bSAndroid Build Coastguard Worker
498*08b48e0bSAndroid Build Coastguard WorkerIf you do not want anything special, the defaults are already usually best,
499*08b48e0bSAndroid Build Coastguard Workerhence all you need is to specify the seed input directory with the result of
500*08b48e0bSAndroid Build Coastguard Workerstep [2a) Collecting inputs](#a-collecting-inputs):
501*08b48e0bSAndroid Build Coastguard Worker
502*08b48e0bSAndroid Build Coastguard Worker```
503*08b48e0bSAndroid Build Coastguard Workerafl-fuzz -i input -o output -- bin/target -someopt @@
504*08b48e0bSAndroid Build Coastguard Worker```
505*08b48e0bSAndroid Build Coastguard Worker
506*08b48e0bSAndroid Build Coastguard WorkerNote that the directory specified with `-o` will be created if it does not
507*08b48e0bSAndroid Build Coastguard Workerexist.
508*08b48e0bSAndroid Build Coastguard Worker
509*08b48e0bSAndroid Build Coastguard WorkerIt can be valuable to run afl-fuzz in a `screen` or `tmux` shell so you can log
510*08b48e0bSAndroid Build Coastguard Workeroff, or afl-fuzz is not aborted if you are running it in a remote ssh session
511*08b48e0bSAndroid Build Coastguard Workerwhere the connection fails in between. Only do that though once you have
512*08b48e0bSAndroid Build Coastguard Workerverified that your fuzzing setup works! Run it like `screen -dmS afl-main --
513*08b48e0bSAndroid Build Coastguard Workerafl-fuzz -M main-$HOSTNAME -i ...` and it will start away in a screen session.
514*08b48e0bSAndroid Build Coastguard WorkerTo enter this session, type `screen -r afl-main`. You see - it makes sense to
515*08b48e0bSAndroid Build Coastguard Workername the screen session same as the afl-fuzz `-M`/`-S` naming :-) For more
516*08b48e0bSAndroid Build Coastguard Workerinformation on screen or tmux, check their documentation.
517*08b48e0bSAndroid Build Coastguard Worker
518*08b48e0bSAndroid Build Coastguard WorkerIf you need to stop and re-start the fuzzing, use the same command line options
519*08b48e0bSAndroid Build Coastguard Worker(or even change them by selecting a different power schedule or another mutation
520*08b48e0bSAndroid Build Coastguard Workermode!) and switch the input directory with a dash (`-`):
521*08b48e0bSAndroid Build Coastguard Worker
522*08b48e0bSAndroid Build Coastguard Worker```
523*08b48e0bSAndroid Build Coastguard Workerafl-fuzz -i - -o output -- bin/target -someopt @@
524*08b48e0bSAndroid Build Coastguard Worker```
525*08b48e0bSAndroid Build Coastguard Worker
526*08b48e0bSAndroid Build Coastguard WorkerAdding a dictionary is helpful. You have the following options:
527*08b48e0bSAndroid Build Coastguard Worker
528*08b48e0bSAndroid Build Coastguard Worker* See the directory
529*08b48e0bSAndroid Build Coastguard Worker[dictionaries/](../dictionaries/), if something is already included for your
530*08b48e0bSAndroid Build Coastguard Workerdata format, and tell afl-fuzz to load that dictionary by adding `-x
531*08b48e0bSAndroid Build Coastguard Workerdictionaries/FORMAT.dict`.
532*08b48e0bSAndroid Build Coastguard Worker* With `afl-clang-lto`, you have an autodictionary generation for which you need
533*08b48e0bSAndroid Build Coastguard Worker  to do nothing except to use afl-clang-lto as the compiler.
534*08b48e0bSAndroid Build Coastguard Worker* With `afl-clang-fast`, you can set
535*08b48e0bSAndroid Build Coastguard Worker  `AFL_LLVM_DICT2FILE=/full/path/to/new/file.dic` to automatically generate a
536*08b48e0bSAndroid Build Coastguard Worker  dictionary during target compilation.
537*08b48e0bSAndroid Build Coastguard Worker  Adding `AFL_LLVM_DICT2FILE_NO_MAIN=1` to not parse main (usually command line
538*08b48e0bSAndroid Build Coastguard Worker  parameter parsing) is often a good idea too.
539*08b48e0bSAndroid Build Coastguard Worker* You also have the option to generate a dictionary yourself during an
540*08b48e0bSAndroid Build Coastguard Worker  independent run of the target, see
541*08b48e0bSAndroid Build Coastguard Worker  [utils/libtokencap/README.md](../utils/libtokencap/README.md).
542*08b48e0bSAndroid Build Coastguard Worker* Finally, you can also write a dictionary file manually, of course.
543*08b48e0bSAndroid Build Coastguard Worker
544*08b48e0bSAndroid Build Coastguard Workerafl-fuzz has a variety of options that help to workaround target quirks like
545*08b48e0bSAndroid Build Coastguard Workervery specific locations for the input file (`-f`), performing deterministic
546*08b48e0bSAndroid Build Coastguard Workerfuzzing (`-D`) and many more. Check out `afl-fuzz -h`.
547*08b48e0bSAndroid Build Coastguard Worker
548*08b48e0bSAndroid Build Coastguard WorkerWe highly recommend that you set a memory limit for running the target with `-m`
549*08b48e0bSAndroid Build Coastguard Workerwhich defines the maximum memory in MB. This prevents a potential out-of-memory
550*08b48e0bSAndroid Build Coastguard Workerproblem for your system plus helps you detect missing `malloc()` failure
551*08b48e0bSAndroid Build Coastguard Workerhandling in the target. Play around with various `-m` values until you find one
552*08b48e0bSAndroid Build Coastguard Workerthat safely works for all your input seeds (if you have good ones and then
553*08b48e0bSAndroid Build Coastguard Workerdouble or quadruple that).
554*08b48e0bSAndroid Build Coastguard Worker
555*08b48e0bSAndroid Build Coastguard WorkerBy default, afl-fuzz never stops fuzzing. To terminate AFL++, press Control-C or
556*08b48e0bSAndroid Build Coastguard Workersend a signal SIGINT. You can limit the number of executions or approximate
557*08b48e0bSAndroid Build Coastguard Workerruntime in seconds with options also.
558*08b48e0bSAndroid Build Coastguard Worker
559*08b48e0bSAndroid Build Coastguard WorkerWhen you start afl-fuzz, you will see a user interface that shows what the
560*08b48e0bSAndroid Build Coastguard Workerstatus is:
561*08b48e0bSAndroid Build Coastguard Worker
562*08b48e0bSAndroid Build Coastguard Worker![resources/screenshot.png](resources/screenshot.png)
563*08b48e0bSAndroid Build Coastguard Worker
564*08b48e0bSAndroid Build Coastguard WorkerAll labels are explained in
565*08b48e0bSAndroid Build Coastguard Worker[afl-fuzz_approach.md#understanding-the-status-screen](afl-fuzz_approach.md#understanding-the-status-screen).
566*08b48e0bSAndroid Build Coastguard Worker
567*08b48e0bSAndroid Build Coastguard Worker### b) Keeping memory use and timeouts in check
568*08b48e0bSAndroid Build Coastguard Worker
569*08b48e0bSAndroid Build Coastguard WorkerMemory limits are not enforced by afl-fuzz by default and the system may run out
570*08b48e0bSAndroid Build Coastguard Workerof memory. You can decrease the memory with the `-m` option, the value is in MB.
571*08b48e0bSAndroid Build Coastguard WorkerIf this is too small for the target, you can usually see this by afl-fuzz
572*08b48e0bSAndroid Build Coastguard Workerbailing with the message that it could not connect to the forkserver.
573*08b48e0bSAndroid Build Coastguard Worker
574*08b48e0bSAndroid Build Coastguard WorkerConsider setting low values for `-m` and `-t`.
575*08b48e0bSAndroid Build Coastguard Worker
576*08b48e0bSAndroid Build Coastguard WorkerFor programs that are nominally very fast, but get sluggish for some inputs, you
577*08b48e0bSAndroid Build Coastguard Workercan also try setting `-t` values that are more punishing than what `afl-fuzz`
578*08b48e0bSAndroid Build Coastguard Workerdares to use on its own. On fast and idle machines, going down to `-t 5` may be
579*08b48e0bSAndroid Build Coastguard Workera viable plan.
580*08b48e0bSAndroid Build Coastguard Worker
581*08b48e0bSAndroid Build Coastguard WorkerThe `-m` parameter is worth looking at, too. Some programs can end up spending a
582*08b48e0bSAndroid Build Coastguard Workerfair amount of time allocating and initializing megabytes of memory when
583*08b48e0bSAndroid Build Coastguard Workerpresented with pathological inputs. Low `-m` values can make them give up sooner
584*08b48e0bSAndroid Build Coastguard Workerand not waste CPU time.
585*08b48e0bSAndroid Build Coastguard Worker
586*08b48e0bSAndroid Build Coastguard Worker### c) Using multiple cores
587*08b48e0bSAndroid Build Coastguard Worker
588*08b48e0bSAndroid Build Coastguard WorkerIf you want to seriously fuzz, then use as many cores/threads as possible to
589*08b48e0bSAndroid Build Coastguard Workerfuzz your target.
590*08b48e0bSAndroid Build Coastguard Worker
591*08b48e0bSAndroid Build Coastguard WorkerOn the same machine - due to the design of how AFL++ works - there is a maximum
592*08b48e0bSAndroid Build Coastguard Workernumber of CPU cores/threads that are useful, use more and the overall
593*08b48e0bSAndroid Build Coastguard Workerperformance degrades instead. This value depends on the target, and the limit is
594*08b48e0bSAndroid Build Coastguard Workerbetween 32 and 64 cores per machine.
595*08b48e0bSAndroid Build Coastguard Worker
596*08b48e0bSAndroid Build Coastguard WorkerIf you have the RAM, it is highly recommended run the instances with a caching
597*08b48e0bSAndroid Build Coastguard Workerof the test cases. Depending on the average test case size (and those found
598*08b48e0bSAndroid Build Coastguard Workerduring fuzzing) and their number, a value between 50-500MB is recommended. You
599*08b48e0bSAndroid Build Coastguard Workercan set the cache size (in MB) by setting the environment variable
600*08b48e0bSAndroid Build Coastguard Worker`AFL_TESTCACHE_SIZE`.
601*08b48e0bSAndroid Build Coastguard Worker
602*08b48e0bSAndroid Build Coastguard WorkerThere should be one main fuzzer (`-M main-$HOSTNAME` option - set also
603*08b48e0bSAndroid Build Coastguard Worker`AFL_FINAL_SYNC=1`) and as many secondary fuzzers (e.g., `-S variant1`) as you
604*08b48e0bSAndroid Build Coastguard Workerhave cores that you use. Every `-M`/`-S` entry needs a unique name (that can be
605*08b48e0bSAndroid Build Coastguard Workerwhatever), however, the same `-o` output directory location has to be used for
606*08b48e0bSAndroid Build Coastguard Workerall instances.
607*08b48e0bSAndroid Build Coastguard Worker
608*08b48e0bSAndroid Build Coastguard WorkerFor every secondary fuzzer there should be a variation, e.g.:
609*08b48e0bSAndroid Build Coastguard Worker* one should fuzz the target that was compiled with sanitizers activated
610*08b48e0bSAndroid Build Coastguard Worker  (`export AFL_USE_ASAN=1 ; export AFL_USE_UBSAN=1 ; export AFL_USE_CFISAN=1`)
611*08b48e0bSAndroid Build Coastguard Worker* one or two should fuzz the target with CMPLOG/redqueen (see above), at least
612*08b48e0bSAndroid Build Coastguard Worker  one cmplog instance should follow transformations (`-l 2AT`)
613*08b48e0bSAndroid Build Coastguard Worker* one to three fuzzers should fuzz a target compiled with laf-intel/COMPCOV (see
614*08b48e0bSAndroid Build Coastguard Worker  above). Important note: If you run more than one laf-intel/COMPCOV fuzzer and
615*08b48e0bSAndroid Build Coastguard Worker  you want them to share their intermediate results, the main fuzzer (`-M`) must
616*08b48e0bSAndroid Build Coastguard Worker  be one of them (although this is not really recommended).
617*08b48e0bSAndroid Build Coastguard Worker
618*08b48e0bSAndroid Build Coastguard WorkerThe other secondaries should be run like this:
619*08b48e0bSAndroid Build Coastguard Worker* 10% with the MOpt mutator enabled: `-L 0`
620*08b48e0bSAndroid Build Coastguard Worker* 10% should use the old queue cycling with `-Z`
621*08b48e0bSAndroid Build Coastguard Worker* 50-70% should run with `AFL_DISABLE_TRIM`
622*08b48e0bSAndroid Build Coastguard Worker* 40% should run with `-P explore` and 20% with `-P exploit`
623*08b48e0bSAndroid Build Coastguard Worker* If you use `-a` then set 30% of the instances to not use `-a`; if you did
624*08b48e0bSAndroid Build Coastguard Worker  not set `-a` (why??), then set 30% to `-a ascii` and 30% to `-a binary`.
625*08b48e0bSAndroid Build Coastguard Worker* run each with a different power schedule, recommended are: `fast` (default),
626*08b48e0bSAndroid Build Coastguard Worker  `explore`, `coe`, `lin`, `quad`, `exploit`, and `rare` which you can set with
627*08b48e0bSAndroid Build Coastguard Worker  the `-p` option, e.g., `-p explore`. See the
628*08b48e0bSAndroid Build Coastguard Worker  [FAQ](FAQ.md#what-are-power-schedules) for details.
629*08b48e0bSAndroid Build Coastguard Worker
630*08b48e0bSAndroid Build Coastguard WorkerIt can be useful to set `AFL_IGNORE_SEED_PROBLEMS=1` to skip over seeds that
631*08b48e0bSAndroid Build Coastguard Workercrash or timeout during startup.
632*08b48e0bSAndroid Build Coastguard Worker
633*08b48e0bSAndroid Build Coastguard WorkerAlso, it is recommended to set `export AFL_IMPORT_FIRST=1` to load test cases
634*08b48e0bSAndroid Build Coastguard Workerfrom other fuzzers in the campaign first. But note that can slow down the start
635*08b48e0bSAndroid Build Coastguard Workerof the first fuzz by quite a lot of you have many fuzzers and/or many seeds.
636*08b48e0bSAndroid Build Coastguard Worker
637*08b48e0bSAndroid Build Coastguard WorkerIf you have a large corpus, a corpus from a previous run or are fuzzing in a CI,
638*08b48e0bSAndroid Build Coastguard Workerthen also set `export AFL_CMPLOG_ONLY_NEW=1` and `export AFL_FAST_CAL=1`.
639*08b48e0bSAndroid Build Coastguard WorkerIf the queue in the CI is huge and/or the execution time is slow then you can
640*08b48e0bSAndroid Build Coastguard Workeralso add `AFL_NO_STARTUP_CALIBRATION=1` to skip the initial queue calibration
641*08b48e0bSAndroid Build Coastguard Workerphase and start fuzzing at once - but only do this if the calibration phase
642*08b48e0bSAndroid Build Coastguard Workerwould be too long for your fuzz run time.
643*08b48e0bSAndroid Build Coastguard Worker
644*08b48e0bSAndroid Build Coastguard WorkerYou can also use different fuzzers. If you are using AFL spinoffs or AFL
645*08b48e0bSAndroid Build Coastguard Workerconforming fuzzers, then just use the same -o directory and give it a unique
646*08b48e0bSAndroid Build Coastguard Worker`-S` name. Examples are:
647*08b48e0bSAndroid Build Coastguard Worker* [Fuzzolic](https://github.com/season-lab/fuzzolic)
648*08b48e0bSAndroid Build Coastguard Worker* [symcc](https://github.com/eurecom-s3/symcc/)
649*08b48e0bSAndroid Build Coastguard Worker* [Eclipser](https://github.com/SoftSec-KAIST/Eclipser/)
650*08b48e0bSAndroid Build Coastguard Worker* [AFLsmart](https://github.com/aflsmart/aflsmart)
651*08b48e0bSAndroid Build Coastguard Worker* [FairFuzz](https://github.com/carolemieux/afl-rb)
652*08b48e0bSAndroid Build Coastguard Worker* [Neuzz](https://github.com/Dongdongshe/neuzz)
653*08b48e0bSAndroid Build Coastguard Worker* [Angora](https://github.com/AngoraFuzzer/Angora)
654*08b48e0bSAndroid Build Coastguard Worker
655*08b48e0bSAndroid Build Coastguard WorkerA long list can be found at
656*08b48e0bSAndroid Build Coastguard Worker[https://github.com/Microsvuln/Awesome-AFL](https://github.com/Microsvuln/Awesome-AFL).
657*08b48e0bSAndroid Build Coastguard Worker
658*08b48e0bSAndroid Build Coastguard WorkerHowever, you can also sync AFL++ with honggfuzz, libfuzzer with `-entropic=1`,
659*08b48e0bSAndroid Build Coastguard Workeretc. Just show the main fuzzer (`-M`) with the `-F` option where the queue/work
660*08b48e0bSAndroid Build Coastguard Workerdirectory of a different fuzzer is, e.g., `-F /src/target/honggfuzz`. Using
661*08b48e0bSAndroid Build Coastguard Workerhonggfuzz (with `-n 1` or `-n 2`) and libfuzzer in parallel is highly
662*08b48e0bSAndroid Build Coastguard Workerrecommended!
663*08b48e0bSAndroid Build Coastguard Worker
664*08b48e0bSAndroid Build Coastguard Worker### d) Using multiple machines for fuzzing
665*08b48e0bSAndroid Build Coastguard Worker
666*08b48e0bSAndroid Build Coastguard WorkerMaybe you have more than one machine you want to fuzz the same target on. Start
667*08b48e0bSAndroid Build Coastguard Workerthe `afl-fuzz` (and perhaps libfuzzer, honggfuzz, ...) orchestra as you like,
668*08b48e0bSAndroid Build Coastguard Workerjust ensure that your have one and only one `-M` instance per server, and that
669*08b48e0bSAndroid Build Coastguard Workerits name is unique, hence the recommendation for `-M main-$HOSTNAME`.
670*08b48e0bSAndroid Build Coastguard Worker
671*08b48e0bSAndroid Build Coastguard WorkerNow there are three strategies on how you can sync between the servers:
672*08b48e0bSAndroid Build Coastguard Worker* never: sounds weird, but this makes every server an island and has the chance
673*08b48e0bSAndroid Build Coastguard Worker  that each follow different paths into the target. You can make this even more
674*08b48e0bSAndroid Build Coastguard Worker  interesting by even giving different seeds to each server.
675*08b48e0bSAndroid Build Coastguard Worker* regularly (~4h): this ensures that all fuzzing campaigns on the servers "see"
676*08b48e0bSAndroid Build Coastguard Worker  the same thing. It is like fuzzing on a huge server.
677*08b48e0bSAndroid Build Coastguard Worker* in intervals of 1/10th of the overall expected runtime of the fuzzing you
678*08b48e0bSAndroid Build Coastguard Worker  sync. This tries a bit to combine both. Have some individuality of the paths
679*08b48e0bSAndroid Build Coastguard Worker  each campaign on a server explores, on the other hand if one gets stuck where
680*08b48e0bSAndroid Build Coastguard Worker  another found progress this is handed over making it unstuck.
681*08b48e0bSAndroid Build Coastguard Worker
682*08b48e0bSAndroid Build Coastguard WorkerThe syncing process itself is very simple. As the `-M main-$HOSTNAME` instance
683*08b48e0bSAndroid Build Coastguard Workersyncs to all `-S` secondaries as well as to other fuzzers, you have to copy only
684*08b48e0bSAndroid Build Coastguard Workerthis directory to the other machines.
685*08b48e0bSAndroid Build Coastguard Worker
686*08b48e0bSAndroid Build Coastguard WorkerLet's say all servers have the `-o out` directory in /target/foo/out, and you
687*08b48e0bSAndroid Build Coastguard Workercreated a file `servers.txt` which contains the hostnames of all participating
688*08b48e0bSAndroid Build Coastguard Workerservers, plus you have an ssh key deployed to all of them, then run:
689*08b48e0bSAndroid Build Coastguard Worker
690*08b48e0bSAndroid Build Coastguard Worker```bash
691*08b48e0bSAndroid Build Coastguard Workerfor FROM in `cat servers.txt`; do
692*08b48e0bSAndroid Build Coastguard Worker  for TO in `cat servers.txt`; do
693*08b48e0bSAndroid Build Coastguard Worker    rsync -rlpogtz --rsh=ssh $FROM:/target/foo/out/main-$FROM $TO:target/foo/out/
694*08b48e0bSAndroid Build Coastguard Worker  done
695*08b48e0bSAndroid Build Coastguard Workerdone
696*08b48e0bSAndroid Build Coastguard Worker```
697*08b48e0bSAndroid Build Coastguard Worker
698*08b48e0bSAndroid Build Coastguard WorkerYou can run this manually, per cron job - as you need it. There is a more
699*08b48e0bSAndroid Build Coastguard Workercomplex and configurable script in
700*08b48e0bSAndroid Build Coastguard Worker[utils/distributed_fuzzing](../utils/distributed_fuzzing).
701*08b48e0bSAndroid Build Coastguard Worker
702*08b48e0bSAndroid Build Coastguard Worker### e) The status of the fuzz campaign
703*08b48e0bSAndroid Build Coastguard Worker
704*08b48e0bSAndroid Build Coastguard WorkerAFL++ comes with the `afl-whatsup` script to show the status of the fuzzing
705*08b48e0bSAndroid Build Coastguard Workercampaign.
706*08b48e0bSAndroid Build Coastguard Worker
707*08b48e0bSAndroid Build Coastguard WorkerJust supply the directory that afl-fuzz is given with the `-o` option and you
708*08b48e0bSAndroid Build Coastguard Workerwill see a detailed status of every fuzzer in that campaign plus a summary.
709*08b48e0bSAndroid Build Coastguard Worker
710*08b48e0bSAndroid Build Coastguard WorkerTo have only the summary, use the `-s` switch, e.g., `afl-whatsup -s out/`.
711*08b48e0bSAndroid Build Coastguard Worker
712*08b48e0bSAndroid Build Coastguard WorkerIf you have multiple servers, then use the command after a sync or you have to
713*08b48e0bSAndroid Build Coastguard Workerexecute this script per server.
714*08b48e0bSAndroid Build Coastguard Worker
715*08b48e0bSAndroid Build Coastguard WorkerAnother tool to inspect the current state and history of a specific instance is
716*08b48e0bSAndroid Build Coastguard Workerafl-plot, which generates an index.html file and graphs that show how the
717*08b48e0bSAndroid Build Coastguard Workerfuzzing instance is performing. The syntax is `afl-plot instance_dir web_dir`,
718*08b48e0bSAndroid Build Coastguard Workere.g., `afl-plot out/default /srv/www/htdocs/plot`.
719*08b48e0bSAndroid Build Coastguard Worker
720*08b48e0bSAndroid Build Coastguard Worker### f) Stopping fuzzing, restarting fuzzing, adding new seeds
721*08b48e0bSAndroid Build Coastguard Worker
722*08b48e0bSAndroid Build Coastguard WorkerTo stop an afl-fuzz run, press Control-C.
723*08b48e0bSAndroid Build Coastguard Worker
724*08b48e0bSAndroid Build Coastguard WorkerTo restart an afl-fuzz run, just reuse the same command line but replace the `-i
725*08b48e0bSAndroid Build Coastguard Workerdirectory` with `-i -` or set `AFL_AUTORESUME=1`.
726*08b48e0bSAndroid Build Coastguard Worker
727*08b48e0bSAndroid Build Coastguard WorkerIf you want to add new seeds to a fuzzing campaign, you can run a temporary
728*08b48e0bSAndroid Build Coastguard Workerfuzzing instance, e.g., when your main fuzzer is using `-o out` and the new
729*08b48e0bSAndroid Build Coastguard Workerseeds are in `newseeds/` directory:
730*08b48e0bSAndroid Build Coastguard Worker
731*08b48e0bSAndroid Build Coastguard Worker```
732*08b48e0bSAndroid Build Coastguard WorkerAFL_BENCH_JUST_ONE=1 AFL_FAST_CAL=1 afl-fuzz -i newseeds -o out -S newseeds -- ./target
733*08b48e0bSAndroid Build Coastguard Worker```
734*08b48e0bSAndroid Build Coastguard Worker
735*08b48e0bSAndroid Build Coastguard Worker### g) Checking the coverage of the fuzzing
736*08b48e0bSAndroid Build Coastguard Worker
737*08b48e0bSAndroid Build Coastguard WorkerThe `corpus count` value is a bad indicator for checking how good the coverage
738*08b48e0bSAndroid Build Coastguard Workeris.
739*08b48e0bSAndroid Build Coastguard Worker
740*08b48e0bSAndroid Build Coastguard WorkerA better indicator - if you use default llvm instrumentation with at least
741*08b48e0bSAndroid Build Coastguard Workerversion 9 - is to use `afl-showmap` with the collect coverage option `-C` on the
742*08b48e0bSAndroid Build Coastguard Workeroutput directory:
743*08b48e0bSAndroid Build Coastguard Worker
744*08b48e0bSAndroid Build Coastguard Worker```
745*08b48e0bSAndroid Build Coastguard Worker$ afl-showmap -C -i out -o /dev/null -- ./target -params @@
746*08b48e0bSAndroid Build Coastguard Worker...
747*08b48e0bSAndroid Build Coastguard Worker[*] Using SHARED MEMORY FUZZING feature.
748*08b48e0bSAndroid Build Coastguard Worker[*] Target map size: 9960
749*08b48e0bSAndroid Build Coastguard Worker[+] Processed 7849 input files.
750*08b48e0bSAndroid Build Coastguard Worker[+] Captured 4331 tuples (highest value 255, total values 67130596) in '/dev/nul
751*08b48e0bSAndroid Build Coastguard Workerl'.
752*08b48e0bSAndroid Build Coastguard Worker[+] A coverage of 4331 edges were achieved out of 9960 existing (43.48%) with 7849 input files.
753*08b48e0bSAndroid Build Coastguard Worker```
754*08b48e0bSAndroid Build Coastguard Worker
755*08b48e0bSAndroid Build Coastguard WorkerIt is even better to check out the exact lines of code that have been reached -
756*08b48e0bSAndroid Build Coastguard Workerand which have not been found so far.
757*08b48e0bSAndroid Build Coastguard Worker
758*08b48e0bSAndroid Build Coastguard WorkerAn "easy" helper script for this is
759*08b48e0bSAndroid Build Coastguard Worker[https://github.com/vanhauser-thc/afl-cov](https://github.com/vanhauser-thc/afl-cov),
760*08b48e0bSAndroid Build Coastguard Workerjust follow the README of that separate project.
761*08b48e0bSAndroid Build Coastguard Worker
762*08b48e0bSAndroid Build Coastguard WorkerIf you see that an important area or a feature has not been covered so far, then
763*08b48e0bSAndroid Build Coastguard Workertry to find an input that is able to reach that and start a new secondary in
764*08b48e0bSAndroid Build Coastguard Workerthat fuzzing campaign with that seed as input, let it run for a few minutes,
765*08b48e0bSAndroid Build Coastguard Workerthen terminate it. The main node will pick it up and make it available to the
766*08b48e0bSAndroid Build Coastguard Workerother secondary nodes over time. Set `export AFL_NO_AFFINITY=1` or `export
767*08b48e0bSAndroid Build Coastguard WorkerAFL_TRY_AFFINITY=1` if you have no free core.
768*08b48e0bSAndroid Build Coastguard Worker
769*08b48e0bSAndroid Build Coastguard WorkerNote that in nearly all cases you can never reach full coverage. A lot of
770*08b48e0bSAndroid Build Coastguard Workerfunctionality is usually dependent on exclusive options that would need
771*08b48e0bSAndroid Build Coastguard Workerindividual fuzzing campaigns each with one of these options set. E.g., if you
772*08b48e0bSAndroid Build Coastguard Workerfuzz a library to convert image formats and your target is the png to tiff API,
773*08b48e0bSAndroid Build Coastguard Workerthen you will not touch any of the other library APIs and features.
774*08b48e0bSAndroid Build Coastguard Worker
775*08b48e0bSAndroid Build Coastguard Worker### h) How long to fuzz a target?
776*08b48e0bSAndroid Build Coastguard Worker
777*08b48e0bSAndroid Build Coastguard WorkerThis is a difficult question. Basically, if no new path is found for a long time
778*08b48e0bSAndroid Build Coastguard Worker(e.g., for a day or a week), then you can expect that your fuzzing won't be
779*08b48e0bSAndroid Build Coastguard Workerfruitful anymore. However, often this just means that you should switch out
780*08b48e0bSAndroid Build Coastguard Workersecondaries for others, e.g., custom mutator modules, sync to very different
781*08b48e0bSAndroid Build Coastguard Workerfuzzers, etc.
782*08b48e0bSAndroid Build Coastguard Worker
783*08b48e0bSAndroid Build Coastguard WorkerKeep the queue/ directory (for future fuzzings of the same or similar targets)
784*08b48e0bSAndroid Build Coastguard Workerand use them to seed other good fuzzers like libfuzzer with the -entropic switch
785*08b48e0bSAndroid Build Coastguard Workeror honggfuzz.
786*08b48e0bSAndroid Build Coastguard Worker
787*08b48e0bSAndroid Build Coastguard Worker### i) Improve the speed!
788*08b48e0bSAndroid Build Coastguard Worker
789*08b48e0bSAndroid Build Coastguard Worker* Use [persistent mode](../instrumentation/README.persistent_mode.md) (x2-x20
790*08b48e0bSAndroid Build Coastguard Worker  speed increase).
791*08b48e0bSAndroid Build Coastguard Worker* If you do not use shmem persistent mode, use `AFL_TMPDIR` to point the input
792*08b48e0bSAndroid Build Coastguard Worker  file on a tempfs location, see [env_variables.md](env_variables.md).
793*08b48e0bSAndroid Build Coastguard Worker* Linux: Improve kernel performance: modify `/etc/default/grub`, set
794*08b48e0bSAndroid Build Coastguard Worker  `GRUB_CMDLINE_LINUX_DEFAULT="ibpb=off ibrs=off kpti=off l1tf=off mds=off
795*08b48e0bSAndroid Build Coastguard Worker  mitigations=off no_stf_barrier noibpb noibrs nopcid nopti
796*08b48e0bSAndroid Build Coastguard Worker  nospec_store_bypass_disable nospectre_v1 nospectre_v2 pcid=off pti=off
797*08b48e0bSAndroid Build Coastguard Worker  spec_store_bypass_disable=off spectre_v2=off stf_barrier=off"`; then
798*08b48e0bSAndroid Build Coastguard Worker  `update-grub` and `reboot` (warning: makes the system more insecure) - you can
799*08b48e0bSAndroid Build Coastguard Worker  also just run `sudo afl-persistent-config`.
800*08b48e0bSAndroid Build Coastguard Worker* Linux: Running on an `ext2` filesystem with `noatime` mount option will be a
801*08b48e0bSAndroid Build Coastguard Worker  bit faster than on any other journaling filesystem.
802*08b48e0bSAndroid Build Coastguard Worker* Use your cores! See [3c) Using multiple cores](#c-using-multiple-cores).
803*08b48e0bSAndroid Build Coastguard Worker* Run `sudo afl-system-config` before starting the first afl-fuzz instance after
804*08b48e0bSAndroid Build Coastguard Worker  a reboot.
805*08b48e0bSAndroid Build Coastguard Worker
806*08b48e0bSAndroid Build Coastguard Worker### j) Going beyond crashes
807*08b48e0bSAndroid Build Coastguard Worker
808*08b48e0bSAndroid Build Coastguard WorkerFuzzing is a wonderful and underutilized technique for discovering non-crashing
809*08b48e0bSAndroid Build Coastguard Workerdesign and implementation errors, too. Quite a few interesting bugs have been
810*08b48e0bSAndroid Build Coastguard Workerfound by modifying the target programs to call `abort()` when say:
811*08b48e0bSAndroid Build Coastguard Worker
812*08b48e0bSAndroid Build Coastguard Worker- Two bignum libraries produce different outputs when given the same
813*08b48e0bSAndroid Build Coastguard Worker  fuzzer-generated input.
814*08b48e0bSAndroid Build Coastguard Worker
815*08b48e0bSAndroid Build Coastguard Worker- An image library produces different outputs when asked to decode the same
816*08b48e0bSAndroid Build Coastguard Worker  input image several times in a row.
817*08b48e0bSAndroid Build Coastguard Worker
818*08b48e0bSAndroid Build Coastguard Worker- A serialization/deserialization library fails to produce stable outputs when
819*08b48e0bSAndroid Build Coastguard Worker  iteratively serializing and deserializing fuzzer-supplied data.
820*08b48e0bSAndroid Build Coastguard Worker
821*08b48e0bSAndroid Build Coastguard Worker- A compression library produces an output inconsistent with the input file when
822*08b48e0bSAndroid Build Coastguard Worker  asked to compress and then decompress a particular blob.
823*08b48e0bSAndroid Build Coastguard Worker
824*08b48e0bSAndroid Build Coastguard WorkerImplementing these or similar sanity checks usually takes very little time; if
825*08b48e0bSAndroid Build Coastguard Workeryou are the maintainer of a particular package, you can make this code
826*08b48e0bSAndroid Build Coastguard Workerconditional with `#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION` (a flag also
827*08b48e0bSAndroid Build Coastguard Workershared with libfuzzer and honggfuzz) or `#ifdef __AFL_COMPILER` (this one is
828*08b48e0bSAndroid Build Coastguard Workerjust for AFL++).
829*08b48e0bSAndroid Build Coastguard Worker
830*08b48e0bSAndroid Build Coastguard Worker### k) Known limitations & areas for improvement
831*08b48e0bSAndroid Build Coastguard Worker
832*08b48e0bSAndroid Build Coastguard WorkerHere are some of the most important caveats for AFL++:
833*08b48e0bSAndroid Build Coastguard Worker
834*08b48e0bSAndroid Build Coastguard Worker- AFL++ detects faults by checking for the first spawned process dying due to a
835*08b48e0bSAndroid Build Coastguard Worker  signal (SIGSEGV, SIGABRT, etc.). Programs that install custom handlers for
836*08b48e0bSAndroid Build Coastguard Worker  these signals may need to have the relevant code commented out. In the same
837*08b48e0bSAndroid Build Coastguard Worker  vein, faults in child processes spawned by the fuzzed target may evade
838*08b48e0bSAndroid Build Coastguard Worker  detection unless you manually add some code to catch that.
839*08b48e0bSAndroid Build Coastguard Worker
840*08b48e0bSAndroid Build Coastguard Worker- As with any other brute-force tool, the fuzzer offers limited coverage if
841*08b48e0bSAndroid Build Coastguard Worker  encryption, checksums, cryptographic signatures, or compression are used to
842*08b48e0bSAndroid Build Coastguard Worker  wholly wrap the actual data format to be tested.
843*08b48e0bSAndroid Build Coastguard Worker
844*08b48e0bSAndroid Build Coastguard Worker  To work around this, you can comment out the relevant checks (see
845*08b48e0bSAndroid Build Coastguard Worker  utils/libpng_no_checksum/ for inspiration); if this is not possible, you can
846*08b48e0bSAndroid Build Coastguard Worker  also write a postprocessor, one of the hooks of custom mutators. See
847*08b48e0bSAndroid Build Coastguard Worker  [custom_mutators.md](custom_mutators.md) on how to use
848*08b48e0bSAndroid Build Coastguard Worker  `AFL_CUSTOM_MUTATOR_LIBRARY`.
849*08b48e0bSAndroid Build Coastguard Worker
850*08b48e0bSAndroid Build Coastguard Worker- There are some unfortunate trade-offs with ASAN and 64-bit binaries. This
851*08b48e0bSAndroid Build Coastguard Worker  isn't due to any specific fault of afl-fuzz.
852*08b48e0bSAndroid Build Coastguard Worker
853*08b48e0bSAndroid Build Coastguard Worker- There is no direct support for fuzzing network services, background daemons,
854*08b48e0bSAndroid Build Coastguard Worker  or interactive apps that require UI interaction to work. You may need to make
855*08b48e0bSAndroid Build Coastguard Worker  simple code changes to make them behave in a more traditional way. Preeny or
856*08b48e0bSAndroid Build Coastguard Worker  libdesock may offer a relatively simple option, too - see:
857*08b48e0bSAndroid Build Coastguard Worker  [https://github.com/zardus/preeny](https://github.com/zardus/preeny) or
858*08b48e0bSAndroid Build Coastguard Worker  [https://github.com/fkie-cad/libdesock](https://github.com/fkie-cad/libdesock)
859*08b48e0bSAndroid Build Coastguard Worker
860*08b48e0bSAndroid Build Coastguard Worker  Some useful tips for modifying network-based services can be also found at:
861*08b48e0bSAndroid Build Coastguard Worker  [https://www.fastly.com/blog/how-to-fuzz-server-american-fuzzy-lop](https://www.fastly.com/blog/how-to-fuzz-server-american-fuzzy-lop)
862*08b48e0bSAndroid Build Coastguard Worker
863*08b48e0bSAndroid Build Coastguard Worker- Occasionally, sentient machines rise against their creators. If this happens
864*08b48e0bSAndroid Build Coastguard Worker  to you, please consult
865*08b48e0bSAndroid Build Coastguard Worker  [https://lcamtuf.coredump.cx/prep/](https://lcamtuf.coredump.cx/prep/).
866*08b48e0bSAndroid Build Coastguard Worker
867*08b48e0bSAndroid Build Coastguard WorkerBeyond this, see [INSTALL.md](INSTALL.md) for platform-specific tips.
868*08b48e0bSAndroid Build Coastguard Worker
869*08b48e0bSAndroid Build Coastguard Worker## 4. Triaging crashes
870*08b48e0bSAndroid Build Coastguard Worker
871*08b48e0bSAndroid Build Coastguard WorkerThe coverage-based grouping of crashes usually produces a small data set that
872*08b48e0bSAndroid Build Coastguard Workercan be quickly triaged manually or with a very simple GDB or Valgrind script.
873*08b48e0bSAndroid Build Coastguard WorkerEvery crash is also traceable to its parent non-crashing test case in the queue,
874*08b48e0bSAndroid Build Coastguard Workermaking it easier to diagnose faults.
875*08b48e0bSAndroid Build Coastguard Worker
876*08b48e0bSAndroid Build Coastguard WorkerHaving said that, it's important to acknowledge that some fuzzing crashes can be
877*08b48e0bSAndroid Build Coastguard Workerdifficult to quickly evaluate for exploitability without a lot of debugging and
878*08b48e0bSAndroid Build Coastguard Workercode analysis work. To assist with this task, afl-fuzz supports a very unique
879*08b48e0bSAndroid Build Coastguard Worker"crash exploration" mode enabled with the `-C` flag.
880*08b48e0bSAndroid Build Coastguard Worker
881*08b48e0bSAndroid Build Coastguard WorkerIn this mode, the fuzzer takes one or more crashing test cases as the input and
882*08b48e0bSAndroid Build Coastguard Workeruses its feedback-driven fuzzing strategies to very quickly enumerate all code
883*08b48e0bSAndroid Build Coastguard Workerpaths that can be reached in the program while keeping it in the crashing state.
884*08b48e0bSAndroid Build Coastguard Worker
885*08b48e0bSAndroid Build Coastguard WorkerMutations that do not result in a crash are rejected; so are any changes that do
886*08b48e0bSAndroid Build Coastguard Workernot affect the execution path.
887*08b48e0bSAndroid Build Coastguard Worker
888*08b48e0bSAndroid Build Coastguard WorkerThe output is a small corpus of files that can be very rapidly examined to see
889*08b48e0bSAndroid Build Coastguard Workerwhat degree of control the attacker has over the faulting address, or whether it
890*08b48e0bSAndroid Build Coastguard Workeris possible to get past an initial out-of-bounds read - and see what lies
891*08b48e0bSAndroid Build Coastguard Workerbeneath.
892*08b48e0bSAndroid Build Coastguard Worker
893*08b48e0bSAndroid Build Coastguard WorkerOh, one more thing: for test case minimization, give afl-tmin a try. The tool
894*08b48e0bSAndroid Build Coastguard Workercan be operated in a very simple way:
895*08b48e0bSAndroid Build Coastguard Worker
896*08b48e0bSAndroid Build Coastguard Worker```shell
897*08b48e0bSAndroid Build Coastguard Worker./afl-tmin -i test_case -o minimized_result -- /path/to/program [...]
898*08b48e0bSAndroid Build Coastguard Worker```
899*08b48e0bSAndroid Build Coastguard Worker
900*08b48e0bSAndroid Build Coastguard WorkerThe tool works with crashing and non-crashing test cases alike. In the crash
901*08b48e0bSAndroid Build Coastguard Workermode, it will happily accept instrumented and non-instrumented binaries. In the
902*08b48e0bSAndroid Build Coastguard Workernon-crashing mode, the minimizer relies on standard AFL++ instrumentation to
903*08b48e0bSAndroid Build Coastguard Workermake the file simpler without altering the execution path.
904*08b48e0bSAndroid Build Coastguard Worker
905*08b48e0bSAndroid Build Coastguard WorkerThe minimizer accepts the `-m`, `-t`, `-f`, and `@@` syntax in a manner
906*08b48e0bSAndroid Build Coastguard Workercompatible with afl-fuzz.
907*08b48e0bSAndroid Build Coastguard Worker
908*08b48e0bSAndroid Build Coastguard WorkerAnother tool in AFL++ is the afl-analyze tool. It takes an input file, attempts
909*08b48e0bSAndroid Build Coastguard Workerto sequentially flip bytes and observes the behavior of the tested program. It
910*08b48e0bSAndroid Build Coastguard Workerthen color-codes the input based on which sections appear to be critical and
911*08b48e0bSAndroid Build Coastguard Workerwhich are not; while not bulletproof, it can often offer quick insights into
912*08b48e0bSAndroid Build Coastguard Workercomplex file formats.
913*08b48e0bSAndroid Build Coastguard Worker
914*08b48e0bSAndroid Build Coastguard Worker`casr-afl` from [CASR](https://github.com/ispras/casr) tools provides
915*08b48e0bSAndroid Build Coastguard Workercomfortable triaging for crashes found by AFL++. Reports are clustered and
916*08b48e0bSAndroid Build Coastguard Workercontain severity and other information.
917*08b48e0bSAndroid Build Coastguard Worker```shell
918*08b48e0bSAndroid Build Coastguard Workercasr-afl -i /path/to/afl/out/dir -o /path/to/casr/out/dir
919*08b48e0bSAndroid Build Coastguard Worker```
920*08b48e0bSAndroid Build Coastguard Worker
921*08b48e0bSAndroid Build Coastguard Worker## 5. CI fuzzing
922*08b48e0bSAndroid Build Coastguard Worker
923*08b48e0bSAndroid Build Coastguard WorkerSome notes on continuous integration (CI) fuzzing - this fuzzing is different to
924*08b48e0bSAndroid Build Coastguard Workernormal fuzzing campaigns as these are much shorter runnings.
925*08b48e0bSAndroid Build Coastguard Worker
926*08b48e0bSAndroid Build Coastguard WorkerIf the queue in the CI is huge and/or the execution time is slow then you can
927*08b48e0bSAndroid Build Coastguard Workeralso add `AFL_NO_STARTUP_CALIBRATION=1` to skip the initial queue calibration
928*08b48e0bSAndroid Build Coastguard Workerphase and start fuzzing at once. But only do that if the calibration time is
929*08b48e0bSAndroid Build Coastguard Workertoo long for your overall available fuzz run time.
930*08b48e0bSAndroid Build Coastguard Worker
931*08b48e0bSAndroid Build Coastguard Worker1. Always:
932*08b48e0bSAndroid Build Coastguard Worker    * LTO has a much longer compile time which is diametrical to short fuzzing -
933*08b48e0bSAndroid Build Coastguard Worker      hence use afl-clang-fast instead.
934*08b48e0bSAndroid Build Coastguard Worker    * If you compile with CMPLOG, then you can save compilation time and reuse
935*08b48e0bSAndroid Build Coastguard Worker      that compiled target with the `-c` option and as the main fuzz target.
936*08b48e0bSAndroid Build Coastguard Worker      This will impact the speed by ~15% though.
937*08b48e0bSAndroid Build Coastguard Worker    * `AFL_FAST_CAL` - enables fast calibration, this halves the time the
938*08b48e0bSAndroid Build Coastguard Worker      saturated corpus needs to be loaded.
939*08b48e0bSAndroid Build Coastguard Worker    * `AFL_CMPLOG_ONLY_NEW` - only perform cmplog on new finds, not the initial
940*08b48e0bSAndroid Build Coastguard Worker      corpus as this very likely has been done for them already.
941*08b48e0bSAndroid Build Coastguard Worker    * Keep the generated corpus, use afl-cmin and reuse it every time!
942*08b48e0bSAndroid Build Coastguard Worker
943*08b48e0bSAndroid Build Coastguard Worker2. Additionally randomize the AFL++ compilation options, e.g.:
944*08b48e0bSAndroid Build Coastguard Worker    * 30% for `AFL_LLVM_CMPLOG`
945*08b48e0bSAndroid Build Coastguard Worker    * 5% for `AFL_LLVM_LAF_ALL`
946*08b48e0bSAndroid Build Coastguard Worker
947*08b48e0bSAndroid Build Coastguard Worker3. Also randomize the afl-fuzz runtime options, e.g.:
948*08b48e0bSAndroid Build Coastguard Worker    * 65% for `AFL_DISABLE_TRIM`
949*08b48e0bSAndroid Build Coastguard Worker    * 50% for `AFL_KEEP_TIMEOUTS`
950*08b48e0bSAndroid Build Coastguard Worker    * 50% use a dictionary generated by `AFL_LLVM_DICT2FILE` + `AFL_LLVM_DICT2FILE_NO_MAIN=1`
951*08b48e0bSAndroid Build Coastguard Worker    * 10% use MOpt (`-L 0`)
952*08b48e0bSAndroid Build Coastguard Worker    * 40% for `AFL_EXPAND_HAVOC_NOW`
953*08b48e0bSAndroid Build Coastguard Worker    * 20% for old queue processing (`-Z`)
954*08b48e0bSAndroid Build Coastguard Worker    * for CMPLOG targets, 70% for `-l 2`, 10% for `-l 3`, 20% for `-l 2AT`
955*08b48e0bSAndroid Build Coastguard Worker
956*08b48e0bSAndroid Build Coastguard Worker4. Do *not* run any `-M` modes, just running `-S` modes is better for CI
957*08b48e0bSAndroid Build Coastguard Worker   fuzzing. `-M` enables old queue handling etc. which is good for a fuzzing
958*08b48e0bSAndroid Build Coastguard Worker   campaign but not good for short CI runs.
959*08b48e0bSAndroid Build Coastguard Worker
960*08b48e0bSAndroid Build Coastguard WorkerHow this can look like can, e.g., be seen at AFL++'s setup in Google's
961*08b48e0bSAndroid Build Coastguard Worker[oss-fuzz](https://github.com/google/oss-fuzz/blob/master/infra/base-images/base-builder/compile_afl)
962*08b48e0bSAndroid Build Coastguard Workerand
963*08b48e0bSAndroid Build Coastguard Worker[clusterfuzz](https://github.com/google/clusterfuzz/blob/master/src/clusterfuzz/_internal/bot/fuzzers/afl/launcher.py).
964*08b48e0bSAndroid Build Coastguard Worker
965*08b48e0bSAndroid Build Coastguard Worker## The End
966*08b48e0bSAndroid Build Coastguard Worker
967*08b48e0bSAndroid Build Coastguard WorkerCheck out the [FAQ](FAQ.md). Maybe it answers your question (that you might not
968*08b48e0bSAndroid Build Coastguard Workereven have known you had ;-) ).
969*08b48e0bSAndroid Build Coastguard Worker
970*08b48e0bSAndroid Build Coastguard WorkerThis is basically all you need to know to professionally run fuzzing campaigns.
971*08b48e0bSAndroid Build Coastguard WorkerIf you want to know more, the tons of texts in [docs/](./) will have you
972*08b48e0bSAndroid Build Coastguard Workercovered.
973*08b48e0bSAndroid Build Coastguard Worker
974*08b48e0bSAndroid Build Coastguard WorkerNote that there are also a lot of tools out there that help fuzzing with AFL++
975*08b48e0bSAndroid Build Coastguard Worker(some might be deprecated or unsupported), see
976*08b48e0bSAndroid Build Coastguard Worker[third_party_tools.md](third_party_tools.md).
977