1*08b48e0bSAndroid Build Coastguard Worker# strcmp() / memcmp() token capture library 2*08b48e0bSAndroid Build Coastguard Worker 3*08b48e0bSAndroid Build Coastguard Worker NOTE: libtokencap is only recommended for binary-only targets or targets that 4*08b48e0bSAndroid Build Coastguard Worker do not compile with afl-clang-fast/afl-clang-lto. 5*08b48e0bSAndroid Build Coastguard Worker The afl-clang-fast AFL_LLVM_DICT2FILE feature is much better, afl-clang-lto 6*08b48e0bSAndroid Build Coastguard Worker has that feature automatically integrated. 7*08b48e0bSAndroid Build Coastguard Worker 8*08b48e0bSAndroid Build Coastguard WorkerFor the general instruction manual, see [docs/README.md](../../docs/README.md). 9*08b48e0bSAndroid Build Coastguard Worker 10*08b48e0bSAndroid Build Coastguard WorkerThis companion library allows you to instrument `strcmp()`, `memcmp()`, 11*08b48e0bSAndroid Build Coastguard Workerand related functions to automatically extract syntax tokens passed to any of 12*08b48e0bSAndroid Build Coastguard Workerthese libcalls. The resulting list of tokens may be then given as a starting 13*08b48e0bSAndroid Build Coastguard Workerdictionary to afl-fuzz (the -x option) to improve coverage on subsequent 14*08b48e0bSAndroid Build Coastguard Workerfuzzing runs. 15*08b48e0bSAndroid Build Coastguard Worker 16*08b48e0bSAndroid Build Coastguard WorkerThis may help improving coverage in some targets, and do precisely nothing in 17*08b48e0bSAndroid Build Coastguard Workerothers. In some cases, it may even make things worse: if libtokencap picks up 18*08b48e0bSAndroid Build Coastguard Workersyntax tokens that are not used to process the input data, but that are a part 19*08b48e0bSAndroid Build Coastguard Workerof - say - parsing a config file... well, you're going to end up wasting a lot 20*08b48e0bSAndroid Build Coastguard Workerof CPU time on trying them out in the input stream. In other words, use this 21*08b48e0bSAndroid Build Coastguard Workerfeature with care. Manually screening the resulting dictionary is almost 22*08b48e0bSAndroid Build Coastguard Workeralways a necessity. 23*08b48e0bSAndroid Build Coastguard Worker 24*08b48e0bSAndroid Build Coastguard WorkerAs for the actual operation: the library stores tokens, without any deduping, 25*08b48e0bSAndroid Build Coastguard Workerby appending them to a file specified via AFL_TOKEN_FILE. If the variable is not 26*08b48e0bSAndroid Build Coastguard Workerset, the tool uses stderr (which is probably not what you want). 27*08b48e0bSAndroid Build Coastguard Worker 28*08b48e0bSAndroid Build Coastguard WorkerSimilarly to afl-tmin, the library is not "proprietary" and can be used with 29*08b48e0bSAndroid Build Coastguard Workerother fuzzers or testing tools without the need for any code tweaks. It does not 30*08b48e0bSAndroid Build Coastguard Workerrequire AFL-instrumented binaries to work. 31*08b48e0bSAndroid Build Coastguard Worker 32*08b48e0bSAndroid Build Coastguard WorkerTo use the library, you *need* to make sure that your fuzzing target is compiled 33*08b48e0bSAndroid Build Coastguard Workerwith -fno-builtin and is linked dynamically. If you wish to automate the first 34*08b48e0bSAndroid Build Coastguard Workerpart without mucking with CFLAGS in Makefiles, you can set `AFL_NO_BUILTIN=1` 35*08b48e0bSAndroid Build Coastguard Workerwhen using afl-gcc. This setting specifically adds the following flags: 36*08b48e0bSAndroid Build Coastguard Worker 37*08b48e0bSAndroid Build Coastguard Worker``` 38*08b48e0bSAndroid Build Coastguard Worker -fno-builtin-strcmp -fno-builtin-strncmp -fno-builtin-strcasecmp 39*08b48e0bSAndroid Build Coastguard Worker -fno-builtin-strcasencmp -fno-builtin-memcmp -fno-builtin-strstr 40*08b48e0bSAndroid Build Coastguard Worker -fno-builtin-strcasestr 41*08b48e0bSAndroid Build Coastguard Worker``` 42*08b48e0bSAndroid Build Coastguard Worker 43*08b48e0bSAndroid Build Coastguard WorkerThe next step is to load this library via LD_PRELOAD. The optimal usage pattern 44*08b48e0bSAndroid Build Coastguard Workeris to allow afl-fuzz to fuzz normally for a while and build up a corpus, and 45*08b48e0bSAndroid Build Coastguard Workerthen fire off the target binary, with libtokencap.so loaded, on every file found 46*08b48e0bSAndroid Build Coastguard Workerby AFL++ in that earlier run. This demonstrates the basic principle: 47*08b48e0bSAndroid Build Coastguard Worker 48*08b48e0bSAndroid Build Coastguard Worker``` 49*08b48e0bSAndroid Build Coastguard Worker export AFL_TOKEN_FILE=$PWD/temp_output.txt 50*08b48e0bSAndroid Build Coastguard Worker timeout_sec="5" 51*08b48e0bSAndroid Build Coastguard Worker 52*08b48e0bSAndroid Build Coastguard Worker for i in <out_dir>/queue/id*; do 53*08b48e0bSAndroid Build Coastguard Worker LD_PRELOAD=/path/to/libtokencap.so \ 54*08b48e0bSAndroid Build Coastguard Worker timeout -s SIGKILL ${timeout_sec} \ 55*08b48e0bSAndroid Build Coastguard Worker /path/to/target/program [...params, including $i...] 56*08b48e0bSAndroid Build Coastguard Worker done 57*08b48e0bSAndroid Build Coastguard Worker 58*08b48e0bSAndroid Build Coastguard Worker sort -u temp_output.txt >afl_dictionary.txt 59*08b48e0bSAndroid Build Coastguard Worker``` 60*08b48e0bSAndroid Build Coastguard Worker 61*08b48e0bSAndroid Build Coastguard WorkerIf you don't get any results, the target library is probably not using strcmp() 62*08b48e0bSAndroid Build Coastguard Workerand memcmp() to parse input; or you haven't compiled it with -fno-builtin; or 63*08b48e0bSAndroid Build Coastguard Workerthe whole thing isn't dynamically linked, and LD_PRELOAD is having no effect. 64*08b48e0bSAndroid Build Coastguard Worker 65*08b48e0bSAndroid Build Coastguard WorkerPortability hints: There is probably no particularly portable and non-invasive 66*08b48e0bSAndroid Build Coastguard Workerway to distinguish between read-only and read-write memory mappings. 67*08b48e0bSAndroid Build Coastguard WorkerThe `__tokencap_load_mappings()` function is the only thing that would 68*08b48e0bSAndroid Build Coastguard Workerneed to be changed for other OSes. 69*08b48e0bSAndroid Build Coastguard Worker 70*08b48e0bSAndroid Build Coastguard WorkerCurrent supported OSes are: Linux, Darwin, FreeBSD (thanks to @devnexen) 71*08b48e0bSAndroid Build Coastguard Worker 72