xref: /aosp_15_r20/external/AFLplusplus/docs/custom_mutators.md (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1*08b48e0bSAndroid Build Coastguard Worker# Custom Mutators in AFL++
2*08b48e0bSAndroid Build Coastguard Worker
3*08b48e0bSAndroid Build Coastguard WorkerThis file describes how you can implement custom mutations to be used in AFL.
4*08b48e0bSAndroid Build Coastguard WorkerFor now, we support C/C++ library and Python module, collectively named as the
5*08b48e0bSAndroid Build Coastguard Workercustom mutator.
6*08b48e0bSAndroid Build Coastguard Worker
7*08b48e0bSAndroid Build Coastguard WorkerThere is also experimental support for Rust in `custom_mutators/rust`. For
8*08b48e0bSAndroid Build Coastguard Workerdocumentation, refer to that directory. Run `cargo doc -p custom_mutator --open`
9*08b48e0bSAndroid Build Coastguard Workerin that directory to view the documentation in your web browser.
10*08b48e0bSAndroid Build Coastguard Worker
11*08b48e0bSAndroid Build Coastguard WorkerImplemented by
12*08b48e0bSAndroid Build Coastguard Worker- C/C++ library (`*.so`): Khaled Yakdan from Code Intelligence
13*08b48e0bSAndroid Build Coastguard Worker  (<[email protected]>)
14*08b48e0bSAndroid Build Coastguard Worker- Python module: Christian Holler from Mozilla (<[email protected]>)
15*08b48e0bSAndroid Build Coastguard Worker
16*08b48e0bSAndroid Build Coastguard Worker## 1) Introduction
17*08b48e0bSAndroid Build Coastguard Worker
18*08b48e0bSAndroid Build Coastguard WorkerCustom mutators can be passed to `afl-fuzz` to perform custom mutations on test
19*08b48e0bSAndroid Build Coastguard Workercases beyond those available in AFL. For example, to enable structure-aware
20*08b48e0bSAndroid Build Coastguard Workerfuzzing by using libraries that perform mutations according to a given grammar.
21*08b48e0bSAndroid Build Coastguard Worker
22*08b48e0bSAndroid Build Coastguard WorkerThe custom mutator is passed to `afl-fuzz` via the `AFL_CUSTOM_MUTATOR_LIBRARY`
23*08b48e0bSAndroid Build Coastguard Workeror `AFL_PYTHON_MODULE` environment variable, and must export a fuzz function.
24*08b48e0bSAndroid Build Coastguard WorkerNow AFL++ also supports multiple custom mutators which can be specified in the
25*08b48e0bSAndroid Build Coastguard Workersame `AFL_CUSTOM_MUTATOR_LIBRARY` environment variable like this.
26*08b48e0bSAndroid Build Coastguard Worker
27*08b48e0bSAndroid Build Coastguard Worker```bash
28*08b48e0bSAndroid Build Coastguard Workerexport AFL_CUSTOM_MUTATOR_LIBRARY="full/path/to/mutator_first.so;full/path/to/mutator_second.so"
29*08b48e0bSAndroid Build Coastguard Worker```
30*08b48e0bSAndroid Build Coastguard Worker
31*08b48e0bSAndroid Build Coastguard WorkerFor details, see [APIs](#2-apis) and [Usage](#3-usage).
32*08b48e0bSAndroid Build Coastguard Worker
33*08b48e0bSAndroid Build Coastguard WorkerThe custom mutation stage is set to be the first non-deterministic stage (right
34*08b48e0bSAndroid Build Coastguard Workerbefore the havoc stage).
35*08b48e0bSAndroid Build Coastguard Worker
36*08b48e0bSAndroid Build Coastguard WorkerNote: If `AFL_CUSTOM_MUTATOR_ONLY` is set, all mutations will solely be
37*08b48e0bSAndroid Build Coastguard Workerperformed with the custom mutator.
38*08b48e0bSAndroid Build Coastguard Worker
39*08b48e0bSAndroid Build Coastguard Worker## 2) APIs
40*08b48e0bSAndroid Build Coastguard Worker
41*08b48e0bSAndroid Build Coastguard Worker**IMPORTANT NOTE**: If you use our C/C++ API and you want to increase the size
42*08b48e0bSAndroid Build Coastguard Workerof an **out_buf buffer, you have to use `afl_realloc()` for this, so include
43*08b48e0bSAndroid Build Coastguard Worker`include/alloc-inl.h` - otherwise afl-fuzz will crash when trying to free
44*08b48e0bSAndroid Build Coastguard Workeryour buffers.
45*08b48e0bSAndroid Build Coastguard Worker
46*08b48e0bSAndroid Build Coastguard WorkerC/C++:
47*08b48e0bSAndroid Build Coastguard Worker
48*08b48e0bSAndroid Build Coastguard Worker```c
49*08b48e0bSAndroid Build Coastguard Workervoid *afl_custom_init(afl_state_t *afl, unsigned int seed);
50*08b48e0bSAndroid Build Coastguard Workerunsigned int afl_custom_fuzz_count(void *data, const unsigned char *buf, size_t buf_size);
51*08b48e0bSAndroid Build Coastguard Workervoid afl_custom_splice_optout(void *data);
52*08b48e0bSAndroid Build Coastguard Workersize_t afl_custom_fuzz(void *data, unsigned char *buf, size_t buf_size, unsigned char **out_buf, unsigned char *add_buf, size_t add_buf_size, size_t max_size);
53*08b48e0bSAndroid Build Coastguard Workerconst char *afl_custom_describe(void *data, size_t max_description_len);
54*08b48e0bSAndroid Build Coastguard Workersize_t afl_custom_post_process(void *data, unsigned char *buf, size_t buf_size, unsigned char **out_buf);
55*08b48e0bSAndroid Build Coastguard Workerint afl_custom_init_trim(void *data, unsigned char *buf, size_t buf_size);
56*08b48e0bSAndroid Build Coastguard Workersize_t afl_custom_trim(void *data, unsigned char **out_buf);
57*08b48e0bSAndroid Build Coastguard Workerint afl_custom_post_trim(void *data, unsigned char success);
58*08b48e0bSAndroid Build Coastguard Workersize_t afl_custom_havoc_mutation(void *data, unsigned char *buf, size_t buf_size, unsigned char **out_buf, size_t max_size);
59*08b48e0bSAndroid Build Coastguard Workerunsigned char afl_custom_havoc_mutation_probability(void *data);
60*08b48e0bSAndroid Build Coastguard Workerunsigned char afl_custom_queue_get(void *data, const unsigned char *filename);
61*08b48e0bSAndroid Build Coastguard Workervoid (*afl_custom_fuzz_send)(void *data, const u8 *buf, size_t buf_size);
62*08b48e0bSAndroid Build Coastguard Workeru8 afl_custom_queue_new_entry(void *data, const unsigned char *filename_new_queue, const unsigned int *filename_orig_queue);
63*08b48e0bSAndroid Build Coastguard Workerconst char* afl_custom_introspection(my_mutator_t *data);
64*08b48e0bSAndroid Build Coastguard Workervoid afl_custom_deinit(void *data);
65*08b48e0bSAndroid Build Coastguard Worker```
66*08b48e0bSAndroid Build Coastguard Worker
67*08b48e0bSAndroid Build Coastguard WorkerPython:
68*08b48e0bSAndroid Build Coastguard Worker
69*08b48e0bSAndroid Build Coastguard Worker```python
70*08b48e0bSAndroid Build Coastguard Workerdef init(seed):
71*08b48e0bSAndroid Build Coastguard Worker    pass
72*08b48e0bSAndroid Build Coastguard Worker
73*08b48e0bSAndroid Build Coastguard Workerdef fuzz_count(buf):
74*08b48e0bSAndroid Build Coastguard Worker    return cnt
75*08b48e0bSAndroid Build Coastguard Worker
76*08b48e0bSAndroid Build Coastguard Workerdef splice_optout():
77*08b48e0bSAndroid Build Coastguard Worker    pass
78*08b48e0bSAndroid Build Coastguard Worker
79*08b48e0bSAndroid Build Coastguard Workerdef fuzz(buf, add_buf, max_size):
80*08b48e0bSAndroid Build Coastguard Worker    return mutated_out
81*08b48e0bSAndroid Build Coastguard Worker
82*08b48e0bSAndroid Build Coastguard Workerdef describe(max_description_length):
83*08b48e0bSAndroid Build Coastguard Worker    return "description_of_current_mutation"
84*08b48e0bSAndroid Build Coastguard Worker
85*08b48e0bSAndroid Build Coastguard Workerdef post_process(buf):
86*08b48e0bSAndroid Build Coastguard Worker    return out_buf
87*08b48e0bSAndroid Build Coastguard Worker
88*08b48e0bSAndroid Build Coastguard Workerdef init_trim(buf):
89*08b48e0bSAndroid Build Coastguard Worker    return cnt
90*08b48e0bSAndroid Build Coastguard Worker
91*08b48e0bSAndroid Build Coastguard Workerdef trim():
92*08b48e0bSAndroid Build Coastguard Worker    return out_buf
93*08b48e0bSAndroid Build Coastguard Worker
94*08b48e0bSAndroid Build Coastguard Workerdef post_trim(success):
95*08b48e0bSAndroid Build Coastguard Worker    return next_index
96*08b48e0bSAndroid Build Coastguard Worker
97*08b48e0bSAndroid Build Coastguard Workerdef havoc_mutation(buf, max_size):
98*08b48e0bSAndroid Build Coastguard Worker    return mutated_out
99*08b48e0bSAndroid Build Coastguard Worker
100*08b48e0bSAndroid Build Coastguard Workerdef havoc_mutation_probability():
101*08b48e0bSAndroid Build Coastguard Worker    return probability # int in [0, 100]
102*08b48e0bSAndroid Build Coastguard Worker
103*08b48e0bSAndroid Build Coastguard Workerdef queue_get(filename):
104*08b48e0bSAndroid Build Coastguard Worker    return True
105*08b48e0bSAndroid Build Coastguard Worker
106*08b48e0bSAndroid Build Coastguard Workerdef fuzz_send(buf):
107*08b48e0bSAndroid Build Coastguard Worker    pass
108*08b48e0bSAndroid Build Coastguard Worker
109*08b48e0bSAndroid Build Coastguard Workerdef queue_new_entry(filename_new_queue, filename_orig_queue):
110*08b48e0bSAndroid Build Coastguard Worker    return False
111*08b48e0bSAndroid Build Coastguard Worker
112*08b48e0bSAndroid Build Coastguard Workerdef introspection():
113*08b48e0bSAndroid Build Coastguard Worker    return string
114*08b48e0bSAndroid Build Coastguard Worker
115*08b48e0bSAndroid Build Coastguard Workerdef deinit():  # optional for Python
116*08b48e0bSAndroid Build Coastguard Worker    pass
117*08b48e0bSAndroid Build Coastguard Worker```
118*08b48e0bSAndroid Build Coastguard Worker
119*08b48e0bSAndroid Build Coastguard Worker### Custom Mutation
120*08b48e0bSAndroid Build Coastguard Worker
121*08b48e0bSAndroid Build Coastguard Worker- `init` (optional in Python):
122*08b48e0bSAndroid Build Coastguard Worker
123*08b48e0bSAndroid Build Coastguard Worker    This method is called when AFL++ starts up and is used to seed RNG and set
124*08b48e0bSAndroid Build Coastguard Worker    up buffers and state.
125*08b48e0bSAndroid Build Coastguard Worker
126*08b48e0bSAndroid Build Coastguard Worker- `queue_get` (optional):
127*08b48e0bSAndroid Build Coastguard Worker
128*08b48e0bSAndroid Build Coastguard Worker    This method determines whether AFL++ should fuzz the current
129*08b48e0bSAndroid Build Coastguard Worker    queue entry or not: all defined custom mutators as well as
130*08b48e0bSAndroid Build Coastguard Worker    all AFL++'s mutators.
131*08b48e0bSAndroid Build Coastguard Worker
132*08b48e0bSAndroid Build Coastguard Worker- `fuzz_count` (optional):
133*08b48e0bSAndroid Build Coastguard Worker
134*08b48e0bSAndroid Build Coastguard Worker    When a queue entry is selected to be fuzzed, afl-fuzz selects the number of
135*08b48e0bSAndroid Build Coastguard Worker    fuzzing attempts with this input based on a few factors. If, however, the
136*08b48e0bSAndroid Build Coastguard Worker    custom mutator wants to set this number instead on how often it is called
137*08b48e0bSAndroid Build Coastguard Worker    for a specific queue entry, use this function. This function is most useful
138*08b48e0bSAndroid Build Coastguard Worker    if `AFL_CUSTOM_MUTATOR_ONLY` is **not** used.
139*08b48e0bSAndroid Build Coastguard Worker
140*08b48e0bSAndroid Build Coastguard Worker- `splice_optout` (optional):
141*08b48e0bSAndroid Build Coastguard Worker
142*08b48e0bSAndroid Build Coastguard Worker    If this function is present, no splicing target is passed to the `fuzz`
143*08b48e0bSAndroid Build Coastguard Worker    function. This saves time if splicing data is not needed by the custom
144*08b48e0bSAndroid Build Coastguard Worker    fuzzing function.
145*08b48e0bSAndroid Build Coastguard Worker    This function is never called, just needs to be present to activate.
146*08b48e0bSAndroid Build Coastguard Worker
147*08b48e0bSAndroid Build Coastguard Worker- `fuzz` (optional):
148*08b48e0bSAndroid Build Coastguard Worker
149*08b48e0bSAndroid Build Coastguard Worker    This method performs your custom mutations on a given input.
150*08b48e0bSAndroid Build Coastguard Worker    The add_buf is the contents of another queue item that can be used for
151*08b48e0bSAndroid Build Coastguard Worker    splicing - or anything else - and can also be ignored. If you are not
152*08b48e0bSAndroid Build Coastguard Worker    using this additional data then define `splice_optout` (see above).
153*08b48e0bSAndroid Build Coastguard Worker    This function is optional.
154*08b48e0bSAndroid Build Coastguard Worker    Returing a length of 0 is valid and is interpreted as skipping this
155*08b48e0bSAndroid Build Coastguard Worker    one mutation result.
156*08b48e0bSAndroid Build Coastguard Worker    For non-Python: the returned output buffer is under **your** memory
157*08b48e0bSAndroid Build Coastguard Worker    management!
158*08b48e0bSAndroid Build Coastguard Worker
159*08b48e0bSAndroid Build Coastguard Worker- `describe` (optional):
160*08b48e0bSAndroid Build Coastguard Worker
161*08b48e0bSAndroid Build Coastguard Worker    When this function is called, it shall describe the current test case,
162*08b48e0bSAndroid Build Coastguard Worker    generated by the last mutation. This will be called, for example, to name
163*08b48e0bSAndroid Build Coastguard Worker    the written test case file after a crash occurred. Using it can help to
164*08b48e0bSAndroid Build Coastguard Worker    reproduce crashing mutations.
165*08b48e0bSAndroid Build Coastguard Worker
166*08b48e0bSAndroid Build Coastguard Worker- `havoc_mutation` and `havoc_mutation_probability` (optional):
167*08b48e0bSAndroid Build Coastguard Worker
168*08b48e0bSAndroid Build Coastguard Worker    `havoc_mutation` performs a single custom mutation on a given input. This
169*08b48e0bSAndroid Build Coastguard Worker    mutation is stacked with other mutations in havoc. The other method,
170*08b48e0bSAndroid Build Coastguard Worker    `havoc_mutation_probability`, returns the probability that `havoc_mutation`
171*08b48e0bSAndroid Build Coastguard Worker    is called in havoc. By default, it is 6%.
172*08b48e0bSAndroid Build Coastguard Worker
173*08b48e0bSAndroid Build Coastguard Worker- `post_process` (optional):
174*08b48e0bSAndroid Build Coastguard Worker
175*08b48e0bSAndroid Build Coastguard Worker    For some cases, the format of the mutated data returned from the custom
176*08b48e0bSAndroid Build Coastguard Worker    mutator is not suitable to directly execute the target with this input. For
177*08b48e0bSAndroid Build Coastguard Worker    example, when using libprotobuf-mutator, the data returned is in a protobuf
178*08b48e0bSAndroid Build Coastguard Worker    format which corresponds to a given grammar. In order to execute the target,
179*08b48e0bSAndroid Build Coastguard Worker    the protobuf data must be converted to the plain-text format expected by the
180*08b48e0bSAndroid Build Coastguard Worker    target. In such scenarios, the user can define the `post_process` function.
181*08b48e0bSAndroid Build Coastguard Worker    This function is then transforming the data into the format expected by the
182*08b48e0bSAndroid Build Coastguard Worker    API before executing the target.
183*08b48e0bSAndroid Build Coastguard Worker
184*08b48e0bSAndroid Build Coastguard Worker    This can return any python object that implements the buffer protocol and
185*08b48e0bSAndroid Build Coastguard Worker    supports PyBUF_SIMPLE. These include bytes, bytearray, etc.
186*08b48e0bSAndroid Build Coastguard Worker
187*08b48e0bSAndroid Build Coastguard Worker    You can decide in the post_process mutator to not send the mutated data
188*08b48e0bSAndroid Build Coastguard Worker    to the target, e.g. if it is too short, too corrupted, etc. If so,
189*08b48e0bSAndroid Build Coastguard Worker    return a NULL buffer and zero length (or a 0 length string in Python).
190*08b48e0bSAndroid Build Coastguard Worker
191*08b48e0bSAndroid Build Coastguard Worker    NOTE: Do not make any random changes to the data in this function!
192*08b48e0bSAndroid Build Coastguard Worker
193*08b48e0bSAndroid Build Coastguard Worker    PERFORMANCE for C/C++: If possible make the changes in-place (so modify
194*08b48e0bSAndroid Build Coastguard Worker    the `*data` directly, and return it as `*outbuf = data`.
195*08b48e0bSAndroid Build Coastguard Worker
196*08b48e0bSAndroid Build Coastguard Worker- `fuzz_send` (optional):
197*08b48e0bSAndroid Build Coastguard Worker
198*08b48e0bSAndroid Build Coastguard Worker    This method can be used if you want to send data to the target yourself,
199*08b48e0bSAndroid Build Coastguard Worker    e.g. via IPC. This replaces some usage of utils/afl_proxy but requires
200*08b48e0bSAndroid Build Coastguard Worker    that you start the target with afl-fuzz.
201*08b48e0bSAndroid Build Coastguard Worker    Example: [custom_mutators/examples/custom_send.c](../custom_mutators/examples/custom_send.c)
202*08b48e0bSAndroid Build Coastguard Worker
203*08b48e0bSAndroid Build Coastguard Worker- `queue_new_entry` (optional):
204*08b48e0bSAndroid Build Coastguard Worker
205*08b48e0bSAndroid Build Coastguard Worker    This methods is called after adding a new test case to the queue. If the
206*08b48e0bSAndroid Build Coastguard Worker    contents of the file was changed, return True, False otherwise.
207*08b48e0bSAndroid Build Coastguard Worker
208*08b48e0bSAndroid Build Coastguard Worker- `introspection` (optional):
209*08b48e0bSAndroid Build Coastguard Worker
210*08b48e0bSAndroid Build Coastguard Worker    This method is called after a new queue entry, crash or timeout is
211*08b48e0bSAndroid Build Coastguard Worker    discovered if compiled with INTROSPECTION. The custom mutator can then
212*08b48e0bSAndroid Build Coastguard Worker    return a string (const char *) that reports the exact mutations used.
213*08b48e0bSAndroid Build Coastguard Worker
214*08b48e0bSAndroid Build Coastguard Worker- `deinit` (optional in Python):
215*08b48e0bSAndroid Build Coastguard Worker
216*08b48e0bSAndroid Build Coastguard Worker    The last method to be called, deinitializing the state.
217*08b48e0bSAndroid Build Coastguard Worker
218*08b48e0bSAndroid Build Coastguard WorkerNote that there are also three functions for trimming as described in the next
219*08b48e0bSAndroid Build Coastguard Workersection.
220*08b48e0bSAndroid Build Coastguard Worker
221*08b48e0bSAndroid Build Coastguard Worker### Trimming Support
222*08b48e0bSAndroid Build Coastguard Worker
223*08b48e0bSAndroid Build Coastguard WorkerThe generic trimming routines implemented in AFL++ can easily destroy the
224*08b48e0bSAndroid Build Coastguard Workerstructure of complex formats, possibly leading to a point where you have a lot
225*08b48e0bSAndroid Build Coastguard Workerof test cases in the queue that your Python module cannot process anymore but
226*08b48e0bSAndroid Build Coastguard Workeryour target application still accepts. This is especially the case when your
227*08b48e0bSAndroid Build Coastguard Workertarget can process a part of the input (causing coverage) and then errors out on
228*08b48e0bSAndroid Build Coastguard Workerthe remaining input.
229*08b48e0bSAndroid Build Coastguard Worker
230*08b48e0bSAndroid Build Coastguard WorkerIn such cases, it makes sense to implement a custom trimming routine. The API
231*08b48e0bSAndroid Build Coastguard Workerconsists of multiple methods because after each trimming step, we have to go
232*08b48e0bSAndroid Build Coastguard Workerback into the C code to check if the coverage bitmap is still the same for the
233*08b48e0bSAndroid Build Coastguard Workertrimmed input. Here's a quick API description:
234*08b48e0bSAndroid Build Coastguard Worker
235*08b48e0bSAndroid Build Coastguard Worker- `init_trim` (optional):
236*08b48e0bSAndroid Build Coastguard Worker
237*08b48e0bSAndroid Build Coastguard Worker    This method is called at the start of each trimming operation and receives
238*08b48e0bSAndroid Build Coastguard Worker    the initial buffer. It should return the amount of iteration steps possible
239*08b48e0bSAndroid Build Coastguard Worker    on this input (e.g., if your input has n elements and you want to remove
240*08b48e0bSAndroid Build Coastguard Worker    them one by one, return n, if you do a binary search, return log(n), and so
241*08b48e0bSAndroid Build Coastguard Worker    on).
242*08b48e0bSAndroid Build Coastguard Worker
243*08b48e0bSAndroid Build Coastguard Worker    If your trimming algorithm doesn't allow to determine the amount of
244*08b48e0bSAndroid Build Coastguard Worker    (remaining) steps easily (esp. while running), then you can alternatively
245*08b48e0bSAndroid Build Coastguard Worker    return 1 here and always return 0 in `post_trim` until you are finished and
246*08b48e0bSAndroid Build Coastguard Worker    no steps remain. In that case, returning 1 in `post_trim` will end the
247*08b48e0bSAndroid Build Coastguard Worker    trimming routine. The whole current index/max iterations stuff is only used
248*08b48e0bSAndroid Build Coastguard Worker    to show progress.
249*08b48e0bSAndroid Build Coastguard Worker
250*08b48e0bSAndroid Build Coastguard Worker- `trim` (optional)
251*08b48e0bSAndroid Build Coastguard Worker
252*08b48e0bSAndroid Build Coastguard Worker    This method is called for each trimming operation. It doesn't have any
253*08b48e0bSAndroid Build Coastguard Worker    arguments because there is already the initial buffer from `init_trim` and
254*08b48e0bSAndroid Build Coastguard Worker    we can memorize the current state in the data variables. This can also save
255*08b48e0bSAndroid Build Coastguard Worker    reparsing steps for each iteration. It should return the trimmed input
256*08b48e0bSAndroid Build Coastguard Worker    buffer.
257*08b48e0bSAndroid Build Coastguard Worker
258*08b48e0bSAndroid Build Coastguard Worker- `post_trim` (optional)
259*08b48e0bSAndroid Build Coastguard Worker
260*08b48e0bSAndroid Build Coastguard Worker    This method is called after each trim operation to inform you if your
261*08b48e0bSAndroid Build Coastguard Worker    trimming step was successful or not (in terms of coverage). If you receive a
262*08b48e0bSAndroid Build Coastguard Worker    failure here, you should reset your input to the last known good state. In
263*08b48e0bSAndroid Build Coastguard Worker    any case, this method must return the next trim iteration index (from 0 to
264*08b48e0bSAndroid Build Coastguard Worker    the maximum amount of steps you returned in `init_trim`).
265*08b48e0bSAndroid Build Coastguard Worker
266*08b48e0bSAndroid Build Coastguard WorkerOmitting any of three trimming methods will cause the trimming to be disabled
267*08b48e0bSAndroid Build Coastguard Workerand trigger a fallback to the built-in default trimming routine.
268*08b48e0bSAndroid Build Coastguard Worker
269*08b48e0bSAndroid Build Coastguard Worker### Environment Variables
270*08b48e0bSAndroid Build Coastguard Worker
271*08b48e0bSAndroid Build Coastguard WorkerOptionally, the following environment variables are supported:
272*08b48e0bSAndroid Build Coastguard Worker
273*08b48e0bSAndroid Build Coastguard Worker- `AFL_CUSTOM_MUTATOR_ONLY`
274*08b48e0bSAndroid Build Coastguard Worker
275*08b48e0bSAndroid Build Coastguard Worker    Disable all other mutation stages. This can prevent broken test cases (those
276*08b48e0bSAndroid Build Coastguard Worker    that your Python module can't work with anymore) to fill up your queue. Best
277*08b48e0bSAndroid Build Coastguard Worker    combined with a custom trimming routine (see below) because trimming can
278*08b48e0bSAndroid Build Coastguard Worker    cause the same test breakage like havoc and splice.
279*08b48e0bSAndroid Build Coastguard Worker
280*08b48e0bSAndroid Build Coastguard Worker- `AFL_PYTHON_ONLY`
281*08b48e0bSAndroid Build Coastguard Worker
282*08b48e0bSAndroid Build Coastguard Worker    Deprecated and removed, use `AFL_CUSTOM_MUTATOR_ONLY` instead.
283*08b48e0bSAndroid Build Coastguard Worker
284*08b48e0bSAndroid Build Coastguard Worker- `AFL_DEBUG`
285*08b48e0bSAndroid Build Coastguard Worker
286*08b48e0bSAndroid Build Coastguard Worker    When combined with `AFL_NO_UI`, this causes the C trimming code to emit
287*08b48e0bSAndroid Build Coastguard Worker    additional messages about the performance and actions of your custom
288*08b48e0bSAndroid Build Coastguard Worker    trimmer. Use this to see if it works :)
289*08b48e0bSAndroid Build Coastguard Worker
290*08b48e0bSAndroid Build Coastguard Worker## 3) Usage
291*08b48e0bSAndroid Build Coastguard Worker
292*08b48e0bSAndroid Build Coastguard Worker### Prerequisite
293*08b48e0bSAndroid Build Coastguard Worker
294*08b48e0bSAndroid Build Coastguard WorkerFor Python mutators, the python 3 or 2 development package is required. On
295*08b48e0bSAndroid Build Coastguard WorkerDebian/Ubuntu/Kali it can be installed like this:
296*08b48e0bSAndroid Build Coastguard Worker
297*08b48e0bSAndroid Build Coastguard Worker```bash
298*08b48e0bSAndroid Build Coastguard Workersudo apt install python3-dev
299*08b48e0bSAndroid Build Coastguard Worker# or
300*08b48e0bSAndroid Build Coastguard Workersudo apt install python-dev
301*08b48e0bSAndroid Build Coastguard Worker```
302*08b48e0bSAndroid Build Coastguard Worker
303*08b48e0bSAndroid Build Coastguard WorkerThen, AFL++ can be compiled with Python support. The AFL++ Makefile detects
304*08b48e0bSAndroid Build Coastguard WorkerPython3 through `python-config`/`python3-config` if it is in the PATH and
305*08b48e0bSAndroid Build Coastguard Workercompiles `afl-fuzz` with the feature if available.
306*08b48e0bSAndroid Build Coastguard Worker
307*08b48e0bSAndroid Build Coastguard WorkerNote: for some distributions, you might also need the package `python[3]-apt`.
308*08b48e0bSAndroid Build Coastguard WorkerIn case your setup is different, set the necessary variables like this:
309*08b48e0bSAndroid Build Coastguard Worker`PYTHON_INCLUDE=/path/to/python/include LDFLAGS=-L/path/to/python/lib make`.
310*08b48e0bSAndroid Build Coastguard Worker
311*08b48e0bSAndroid Build Coastguard Worker### Helpers
312*08b48e0bSAndroid Build Coastguard Worker
313*08b48e0bSAndroid Build Coastguard WorkerFor C/C++ custom mutators you get a pointer to `afl_state_t *afl` in the
314*08b48e0bSAndroid Build Coastguard Worker`afl_custom_init()` which contains all information that you need.
315*08b48e0bSAndroid Build Coastguard WorkerNote that if you access it, you need to recompile your custom mutator if
316*08b48e0bSAndroid Build Coastguard Workeryou update AFL++ because the structure might have changed!
317*08b48e0bSAndroid Build Coastguard Worker
318*08b48e0bSAndroid Build Coastguard WorkerFor mutators written in Python, Rust, GO, etc. there are a few environment
319*08b48e0bSAndroid Build Coastguard Workervariables set to help you to get started:
320*08b48e0bSAndroid Build Coastguard Worker
321*08b48e0bSAndroid Build Coastguard Worker`AFL_CUSTOM_INFO_PROGRAM` - the program name of the target that is executed.
322*08b48e0bSAndroid Build Coastguard WorkerIf your custom mutator is used with modes like Qemu (`-Q`), this will still
323*08b48e0bSAndroid Build Coastguard Workercontain the target program, not afl-qemu-trace.
324*08b48e0bSAndroid Build Coastguard Worker
325*08b48e0bSAndroid Build Coastguard Worker`AFL_CUSTOM_INFO_PROGRAM_INPUT` - if the `-f` parameter is used with afl-fuzz
326*08b48e0bSAndroid Build Coastguard Workerthen this value is found in this environment variable.
327*08b48e0bSAndroid Build Coastguard Worker
328*08b48e0bSAndroid Build Coastguard Worker`AFL_CUSTOM_INFO_PROGRAM_ARGV` - this contains the parameters given to the
329*08b48e0bSAndroid Build Coastguard Workertarget program and still has the `@@` identifier in there.
330*08b48e0bSAndroid Build Coastguard Worker
331*08b48e0bSAndroid Build Coastguard WorkerNote: If `AFL_CUSTOM_INFO_PROGRAM_INPUT` is empty and `AFL_CUSTOM_INFO_PROGRAM_ARGV`
332*08b48e0bSAndroid Build Coastguard Workeris either empty or does not contain `@@` then the target gets the input via
333*08b48e0bSAndroid Build Coastguard Worker`stdin`.
334*08b48e0bSAndroid Build Coastguard Worker
335*08b48e0bSAndroid Build Coastguard Worker`AFL_CUSTOM_INFO_OUT` - This is the output directory for this fuzzer instance,
336*08b48e0bSAndroid Build Coastguard Workerso if `afl-fuzz` was called with `-o out -S foobar`, then this will be set to
337*08b48e0bSAndroid Build Coastguard Worker`out/foobar`.
338*08b48e0bSAndroid Build Coastguard Worker
339*08b48e0bSAndroid Build Coastguard Worker### Custom Mutator Preparation
340*08b48e0bSAndroid Build Coastguard Worker
341*08b48e0bSAndroid Build Coastguard WorkerFor C/C++ mutators, the source code must be compiled as a shared object:
342*08b48e0bSAndroid Build Coastguard Worker
343*08b48e0bSAndroid Build Coastguard Worker```bash
344*08b48e0bSAndroid Build Coastguard Workergcc -shared -Wall -O3 example.c -o example.so
345*08b48e0bSAndroid Build Coastguard Worker```
346*08b48e0bSAndroid Build Coastguard Worker
347*08b48e0bSAndroid Build Coastguard WorkerNote that if you specify multiple custom mutators, the corresponding functions
348*08b48e0bSAndroid Build Coastguard Workerwill be called in the order in which they are specified. E.g., the first
349*08b48e0bSAndroid Build Coastguard Worker`post_process` function of `example_first.so` will be called and then that of
350*08b48e0bSAndroid Build Coastguard Worker`example_second.so`.
351*08b48e0bSAndroid Build Coastguard Worker
352*08b48e0bSAndroid Build Coastguard Worker### Run
353*08b48e0bSAndroid Build Coastguard Worker
354*08b48e0bSAndroid Build Coastguard WorkerC/C++
355*08b48e0bSAndroid Build Coastguard Worker
356*08b48e0bSAndroid Build Coastguard Worker```bash
357*08b48e0bSAndroid Build Coastguard Workerexport AFL_CUSTOM_MUTATOR_LIBRARY="/full/path/to/example_first.so;/full/path/to/example_second.so"
358*08b48e0bSAndroid Build Coastguard Workerafl-fuzz /path/to/program
359*08b48e0bSAndroid Build Coastguard Worker```
360*08b48e0bSAndroid Build Coastguard Worker
361*08b48e0bSAndroid Build Coastguard WorkerPython
362*08b48e0bSAndroid Build Coastguard Worker
363*08b48e0bSAndroid Build Coastguard Worker```bash
364*08b48e0bSAndroid Build Coastguard Workerexport PYTHONPATH=`dirname /full/path/to/example.py`
365*08b48e0bSAndroid Build Coastguard Workerexport AFL_PYTHON_MODULE=example
366*08b48e0bSAndroid Build Coastguard Workerafl-fuzz /path/to/program
367*08b48e0bSAndroid Build Coastguard Worker```
368*08b48e0bSAndroid Build Coastguard Worker
369*08b48e0bSAndroid Build Coastguard Worker## 4) Example
370*08b48e0bSAndroid Build Coastguard Worker
371*08b48e0bSAndroid Build Coastguard WorkerSee [example.c](../custom_mutators/examples/example.c) and
372*08b48e0bSAndroid Build Coastguard Worker[example.py](../custom_mutators/examples/example.py).
373*08b48e0bSAndroid Build Coastguard Worker
374*08b48e0bSAndroid Build Coastguard Worker## 5) Other Resources
375*08b48e0bSAndroid Build Coastguard Worker
376*08b48e0bSAndroid Build Coastguard Worker- AFL libprotobuf mutator
377*08b48e0bSAndroid Build Coastguard Worker    - [bruce30262/libprotobuf-mutator_fuzzing_learning](https://github.com/bruce30262/libprotobuf-mutator_fuzzing_learning/tree/master/4_libprotobuf_aflpp_custom_mutator)
378*08b48e0bSAndroid Build Coastguard Worker    - [thebabush/afl-libprotobuf-mutator](https://github.com/thebabush/afl-libprotobuf-mutator)
379*08b48e0bSAndroid Build Coastguard Worker- [XML Fuzzing@NullCon 2017](https://www.agarri.fr/docs/XML_Fuzzing-NullCon2017-PUBLIC.pdf)
380*08b48e0bSAndroid Build Coastguard Worker    - [A bug detected by AFL + XML-aware mutators](https://bugs.chromium.org/p/chromium/issues/detail?id=930663)
381