|
| 1 | +# AFL++ Plugin: Data-Generator Extractor |
| 2 | +## Overview |
| 3 | +This plugin allows AFL++ to use LibFuzzer's data generators for fuzzing. The plugin extracts generators from LibFuzzer and integrates them into AFL++. |
| 4 | + |
| 5 | +## Example Building |
| 6 | + |
| 7 | +1. Build the AFL++ compiler. |
| 8 | +This will create the patched `afl-cc`. |
| 9 | + |
| 10 | +2. Build the plugin: |
| 11 | +``` |
| 12 | +cd utils/aflpp_extractor |
| 13 | +make |
| 14 | +``` |
| 15 | + |
| 16 | +This will compile and create the files: |
| 17 | +- `libAFLExtractor.a`: The static library containing the AFL++ Extractor plugin. |
| 18 | +- `extractor_test_fuzzer`: The library that demonstrates how to extract generator using this plugin. |
| 19 | +- `reproducer`: The program that reads an input file, processes it with the extracted generator, and writes the result to an output file. |
| 20 | + |
| 21 | +## For Your Use |
| 22 | +Assume you have a generation-based libfuzzer `fuzzer_harness.c`: |
| 23 | +``` |
| 24 | +size_t generate_magic_data(const uint8_t *data, size_t size){ |
| 25 | +
|
| 26 | + /* 1. Consume the random data */ |
| 27 | + /* 2. Construct random data into more complex data to */ |
| 28 | + /* satisfy a certain structure, syntax or semantics */ |
| 29 | +} |
| 30 | +
|
| 31 | +/* libFuzzer's required entry point. */ |
| 32 | +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 33 | + /* Generate magic data */ |
| 34 | + uint8_t * magic_data = generate_magic_data(data, size); |
| 35 | +
|
| 36 | + /* Call function to be fuzzed using the magic_data */ |
| 37 | + target_function(modified_data); |
| 38 | +
|
| 39 | + /* Reset state. e.g. free something */ |
| 40 | +} |
| 41 | +``` |
| 42 | +And this is the clang compile command to build for libfuzzer: |
| 43 | +``` |
| 44 | +clang -o fuzz -fsanitize=fuzzer fuzzer_harness.cc |
| 45 | +``` |
| 46 | +OK, if you find the data generator to be quite ingenious and you want to use it in AFL++, set it up as follows: |
| 47 | + |
| 48 | +#### 1) Include the header before `LLVMFuzzerTestOneInput`: |
| 49 | +``` |
| 50 | +#include "fuzzer_ext.h" |
| 51 | +``` |
| 52 | +or just add this: |
| 53 | +``` |
| 54 | +typedef struct mutate_helper { |
| 55 | + uint8_t *buf; |
| 56 | + size_t len; |
| 57 | +} mutate_helper_t; |
| 58 | +
|
| 59 | +#ifdef __cplusplus |
| 60 | +extern "C" { |
| 61 | +#endif |
| 62 | + extern void mutate_helper_buffer_copy(unsigned char *buf, size_t buf_size); |
| 63 | +#ifdef __cplusplus |
| 64 | +} |
| 65 | +#endif |
| 66 | +``` |
| 67 | + |
| 68 | +#### 2) Call `mutate_helper_buffer_copy` after the `generate_magic_data`: |
| 69 | +``` |
| 70 | +mutate_helper_buffer_copy(modified_data, size + 2); |
| 71 | +``` |
| 72 | + |
| 73 | +#### 3) Remove the `target_function` call. |
| 74 | +``` |
| 75 | +# target_function(modified_data); |
| 76 | +``` |
| 77 | + |
| 78 | +#### 4) Recompile this libfuzzer, replacing clang with `afl-clang-fast`, with `AFL_PATH` needed. |
| 79 | +``` |
| 80 | +afl-clang-fast -o fuzz -fsanitize=fuzzer fuzzer_harness.cc |
| 81 | +``` |
| 82 | +The afl-clang-fast will automatically produce a shared library with `libAFLExtractor.a` linked to it, which will be used by `afl-fuzz` later on. |
| 83 | + |
0 commit comments