1*08b48e0bSAndroid Build Coastguard Worker# Building and installing AFL++ 2*08b48e0bSAndroid Build Coastguard Worker 3*08b48e0bSAndroid Build Coastguard Worker## Linux on x86 4*08b48e0bSAndroid Build Coastguard Worker 5*08b48e0bSAndroid Build Coastguard WorkerAn easy way to install AFL++ with everything compiled is available via docker: 6*08b48e0bSAndroid Build Coastguard WorkerYou can use the [Dockerfile](../Dockerfile) or just pull directly from the 7*08b48e0bSAndroid Build Coastguard WorkerDocker Hub (for x86_64 and arm64): 8*08b48e0bSAndroid Build Coastguard Worker 9*08b48e0bSAndroid Build Coastguard Worker```shell 10*08b48e0bSAndroid Build Coastguard Workerdocker pull aflplusplus/aflplusplus:latest 11*08b48e0bSAndroid Build Coastguard Workerdocker run -ti -v /location/of/your/target:/src aflplusplus/aflplusplus 12*08b48e0bSAndroid Build Coastguard Worker``` 13*08b48e0bSAndroid Build Coastguard Worker 14*08b48e0bSAndroid Build Coastguard WorkerThis image is automatically generated when a push to the stable branch happens. 15*08b48e0bSAndroid Build Coastguard WorkerYou will find your target source code in `/src` in the container. 16*08b48e0bSAndroid Build Coastguard Worker 17*08b48e0bSAndroid Build Coastguard WorkerNote: you can also pull `aflplusplus/aflplusplus:dev` which is the most current 18*08b48e0bSAndroid Build Coastguard Workerdevelopment state of AFL++. 19*08b48e0bSAndroid Build Coastguard Worker 20*08b48e0bSAndroid Build Coastguard WorkerIf you want to build AFL++ yourself, you have many options. The easiest choice 21*08b48e0bSAndroid Build Coastguard Workeris to build and install everything: 22*08b48e0bSAndroid Build Coastguard Worker 23*08b48e0bSAndroid Build Coastguard WorkerNOTE: depending on your Debian/Ubuntu/Kali/... release, replace `-14` with 24*08b48e0bSAndroid Build Coastguard Workerwhatever llvm version is available. We recommend llvm 13, 14, 15 or 16. 25*08b48e0bSAndroid Build Coastguard Worker 26*08b48e0bSAndroid Build Coastguard Worker```shell 27*08b48e0bSAndroid Build Coastguard Workersudo apt-get update 28*08b48e0bSAndroid Build Coastguard Workersudo apt-get install -y build-essential python3-dev automake cmake git flex bison libglib2.0-dev libpixman-1-dev python3-setuptools cargo libgtk-3-dev 29*08b48e0bSAndroid Build Coastguard Worker# try to install llvm 14 and install the distro default if that fails 30*08b48e0bSAndroid Build Coastguard Workersudo apt-get install -y lld-14 llvm-14 llvm-14-dev clang-14 || sudo apt-get install -y lld llvm llvm-dev clang 31*08b48e0bSAndroid Build Coastguard Workersudo apt-get install -y gcc-$(gcc --version|head -n1|sed 's/\..*//'|sed 's/.* //')-plugin-dev libstdc++-$(gcc --version|head -n1|sed 's/\..*//'|sed 's/.* //')-dev 32*08b48e0bSAndroid Build Coastguard Workersudo apt-get install -y ninja-build # for QEMU mode 33*08b48e0bSAndroid Build Coastguard Workergit clone https://github.com/AFLplusplus/AFLplusplus 34*08b48e0bSAndroid Build Coastguard Workercd AFLplusplus 35*08b48e0bSAndroid Build Coastguard Workermake distrib 36*08b48e0bSAndroid Build Coastguard Workersudo make install 37*08b48e0bSAndroid Build Coastguard Worker``` 38*08b48e0bSAndroid Build Coastguard Worker 39*08b48e0bSAndroid Build Coastguard WorkerIt is recommended to install the newest available gcc, clang and llvm-dev 40*08b48e0bSAndroid Build Coastguard Workerpossible in your distribution! 41*08b48e0bSAndroid Build Coastguard Worker 42*08b48e0bSAndroid Build Coastguard WorkerNote that `make distrib` also builds FRIDA mode, QEMU mode, unicorn_mode, and 43*08b48e0bSAndroid Build Coastguard Workermore. If you just want plain AFL++, then do `make all`. If you want some 44*08b48e0bSAndroid Build Coastguard Workerassisting tooling compiled but are not interested in binary-only targets, then 45*08b48e0bSAndroid Build Coastguard Workerinstead choose: 46*08b48e0bSAndroid Build Coastguard Worker 47*08b48e0bSAndroid Build Coastguard Worker```shell 48*08b48e0bSAndroid Build Coastguard Workermake source-only 49*08b48e0bSAndroid Build Coastguard Worker``` 50*08b48e0bSAndroid Build Coastguard Worker 51*08b48e0bSAndroid Build Coastguard WorkerThese build targets exist: 52*08b48e0bSAndroid Build Coastguard Worker 53*08b48e0bSAndroid Build Coastguard Worker* all: the main AFL++ binaries and llvm/gcc instrumentation 54*08b48e0bSAndroid Build Coastguard Worker* binary-only: everything for binary-only fuzzing: frida_mode, nyx_mode, 55*08b48e0bSAndroid Build Coastguard Worker qemu_mode, frida_mode, unicorn_mode, coresight_mode, libdislocator, 56*08b48e0bSAndroid Build Coastguard Worker libtokencap 57*08b48e0bSAndroid Build Coastguard Worker* source-only: everything for source code fuzzing: nyx_mode, libdislocator, 58*08b48e0bSAndroid Build Coastguard Worker libtokencap 59*08b48e0bSAndroid Build Coastguard Worker* distrib: everything (for both binary-only and source code fuzzing) 60*08b48e0bSAndroid Build Coastguard Worker* man: creates simple man pages from the help option of the programs 61*08b48e0bSAndroid Build Coastguard Worker* install: installs everything you have compiled with the build options above 62*08b48e0bSAndroid Build Coastguard Worker* clean: cleans everything compiled, not downloads (unless not on a checkout) 63*08b48e0bSAndroid Build Coastguard Worker* deepclean: cleans everything including downloads 64*08b48e0bSAndroid Build Coastguard Worker* code-format: format the code, do this before you commit and send a PR please! 65*08b48e0bSAndroid Build Coastguard Worker* tests: runs test cases to ensure that all features are still working as they 66*08b48e0bSAndroid Build Coastguard Worker should 67*08b48e0bSAndroid Build Coastguard Worker* unit: perform unit tests (based on cmocka) 68*08b48e0bSAndroid Build Coastguard Worker* help: shows these build options 69*08b48e0bSAndroid Build Coastguard Worker 70*08b48e0bSAndroid Build Coastguard Worker[Unless you are on Mac OS X](https://developer.apple.com/library/archive/qa/qa1118/_index.html), 71*08b48e0bSAndroid Build Coastguard Workeryou can also build statically linked versions of the AFL++ binaries by passing 72*08b48e0bSAndroid Build Coastguard Workerthe `STATIC=1` argument to make: 73*08b48e0bSAndroid Build Coastguard Worker 74*08b48e0bSAndroid Build Coastguard Worker```shell 75*08b48e0bSAndroid Build Coastguard Workermake STATIC=1 76*08b48e0bSAndroid Build Coastguard Worker``` 77*08b48e0bSAndroid Build Coastguard Worker 78*08b48e0bSAndroid Build Coastguard WorkerThese build options exist: 79*08b48e0bSAndroid Build Coastguard Worker 80*08b48e0bSAndroid Build Coastguard Worker* STATIC - compile AFL++ static 81*08b48e0bSAndroid Build Coastguard Worker* CODE_COVERAGE - compile the target for code coverage (see docs/instrumentation/README.llvm.md) 82*08b48e0bSAndroid Build Coastguard Worker* ASAN_BUILD - compiles AFL++ with memory sanitizer for debug purposes 83*08b48e0bSAndroid Build Coastguard Worker* UBSAN_BUILD - compiles AFL++ tools with undefined behaviour sanitizer for debug purposes 84*08b48e0bSAndroid Build Coastguard Worker* DEBUG - no optimization, -ggdb3, all warnings and -Werror 85*08b48e0bSAndroid Build Coastguard Worker* LLVM_DEBUG - shows llvm deprecation warnings 86*08b48e0bSAndroid Build Coastguard Worker* PROFILING - compile afl-fuzz with profiling information 87*08b48e0bSAndroid Build Coastguard Worker* INTROSPECTION - compile afl-fuzz with mutation introspection 88*08b48e0bSAndroid Build Coastguard Worker* NO_PYTHON - disable python support 89*08b48e0bSAndroid Build Coastguard Worker* NO_SPLICING - disables splicing mutation in afl-fuzz, not recommended for normal fuzzing 90*08b48e0bSAndroid Build Coastguard Worker* NO_UTF - do not use UTF-8 for line rendering in status screen (fallback to G1 box drawing, of vanilla AFL) 91*08b48e0bSAndroid Build Coastguard Worker* NO_NYX - disable building nyx mode dependencies 92*08b48e0bSAndroid Build Coastguard Worker* NO_CORESIGHT - disable building coresight (arm64 only) 93*08b48e0bSAndroid Build Coastguard Worker* NO_UNICORN_ARM64 - disable building unicorn on arm64 94*08b48e0bSAndroid Build Coastguard Worker* AFL_NO_X86 - if compiling on non-intel/amd platforms 95*08b48e0bSAndroid Build Coastguard Worker* LLVM_CONFIG - if your distro doesn't use the standard name for llvm-config (e.g., Debian) 96*08b48e0bSAndroid Build Coastguard Worker 97*08b48e0bSAndroid Build Coastguard Workere.g.: `make LLVM_CONFIG=llvm-config-14` 98*08b48e0bSAndroid Build Coastguard Worker 99*08b48e0bSAndroid Build Coastguard Worker## MacOS X on x86 and arm64 (M1) 100*08b48e0bSAndroid Build Coastguard Worker 101*08b48e0bSAndroid Build Coastguard WorkerMacOS has some gotchas due to the idiosyncrasies of the platform. 102*08b48e0bSAndroid Build Coastguard Worker 103*08b48e0bSAndroid Build Coastguard WorkerTo build AFL, install llvm (and perhaps gcc) from brew and follow the general 104*08b48e0bSAndroid Build Coastguard Workerinstructions for Linux. If possible, avoid Xcode at all cost. 105*08b48e0bSAndroid Build Coastguard Worker 106*08b48e0bSAndroid Build Coastguard Worker```shell 107*08b48e0bSAndroid Build Coastguard Workerbrew install wget git make cmake llvm gdb coreutils 108*08b48e0bSAndroid Build Coastguard Worker``` 109*08b48e0bSAndroid Build Coastguard Worker 110*08b48e0bSAndroid Build Coastguard WorkerBe sure to setup `PATH` to point to the correct clang binaries and use the 111*08b48e0bSAndroid Build Coastguard Workerfreshly installed clang, clang++, llvm-config, gmake and coreutils, e.g.: 112*08b48e0bSAndroid Build Coastguard Worker 113*08b48e0bSAndroid Build Coastguard Worker```shell 114*08b48e0bSAndroid Build Coastguard Worker# Depending on your MacOS system + brew version it is either 115*08b48e0bSAndroid Build Coastguard Workerexport PATH="/opt/homebrew/opt/llvm/bin:$PATH" 116*08b48e0bSAndroid Build Coastguard Worker# or 117*08b48e0bSAndroid Build Coastguard Workerexport PATH="/usr/local/opt/llvm/bin:/usr/local/opt/coreutils/libexec/gnubin:$PATH" 118*08b48e0bSAndroid Build Coastguard Worker# you can check with "brew info llvm" 119*08b48e0bSAndroid Build Coastguard Worker 120*08b48e0bSAndroid Build Coastguard Workerexport PATH="/usr/local/bin:$PATH" 121*08b48e0bSAndroid Build Coastguard Workerexport CC=clang 122*08b48e0bSAndroid Build Coastguard Workerexport CXX=clang++ 123*08b48e0bSAndroid Build Coastguard Workergmake 124*08b48e0bSAndroid Build Coastguard Workercd frida_mode 125*08b48e0bSAndroid Build Coastguard Workergmake 126*08b48e0bSAndroid Build Coastguard Workercd .. 127*08b48e0bSAndroid Build Coastguard Workersudo gmake install 128*08b48e0bSAndroid Build Coastguard Worker``` 129*08b48e0bSAndroid Build Coastguard Worker 130*08b48e0bSAndroid Build Coastguard Worker`afl-gcc` will fail unless you have GCC installed, but that is using outdated 131*08b48e0bSAndroid Build Coastguard Workerinstrumentation anyway. `afl-clang` might fail too depending on your PATH setup. 132*08b48e0bSAndroid Build Coastguard WorkerBut you don't want neither, you want `afl-clang-fast` anyway :) Note that 133*08b48e0bSAndroid Build Coastguard Worker`afl-clang-lto`, `afl-gcc-fast` and `qemu_mode` are not working on MacOS. 134*08b48e0bSAndroid Build Coastguard Worker 135*08b48e0bSAndroid Build Coastguard WorkerThe crash reporting daemon that comes by default with MacOS X will cause 136*08b48e0bSAndroid Build Coastguard Workerproblems with fuzzing. You need to turn it off: 137*08b48e0bSAndroid Build Coastguard Worker 138*08b48e0bSAndroid Build Coastguard Worker``` 139*08b48e0bSAndroid Build Coastguard Workerlaunchctl unload -w /System/Library/LaunchAgents/com.apple.ReportCrash.plist 140*08b48e0bSAndroid Build Coastguard Workersudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.ReportCrash.Root.plist 141*08b48e0bSAndroid Build Coastguard Worker``` 142*08b48e0bSAndroid Build Coastguard Worker 143*08b48e0bSAndroid Build Coastguard WorkerThe `fork()` semantics on OS X are a bit unusual compared to other unix systems 144*08b48e0bSAndroid Build Coastguard Workerand definitely don't look POSIX-compliant. This means two things: 145*08b48e0bSAndroid Build Coastguard Worker 146*08b48e0bSAndroid Build Coastguard Worker - Fuzzing will be probably slower than on Linux. In fact, some folks report 147*08b48e0bSAndroid Build Coastguard Worker considerable performance gains by running the jobs inside a Linux VM on 148*08b48e0bSAndroid Build Coastguard Worker MacOS X. 149*08b48e0bSAndroid Build Coastguard Worker - Some non-portable, platform-specific code may be incompatible with the AFL++ 150*08b48e0bSAndroid Build Coastguard Worker forkserver. If you run into any problems, set `AFL_NO_FORKSRV=1` in the 151*08b48e0bSAndroid Build Coastguard Worker environment before starting afl-fuzz. 152*08b48e0bSAndroid Build Coastguard Worker 153*08b48e0bSAndroid Build Coastguard WorkerUser emulation mode of QEMU does not appear to be supported on MacOS X, so 154*08b48e0bSAndroid Build Coastguard Workerblack-box instrumentation mode (`-Q`) will not work. However, FRIDA mode (`-O`) 155*08b48e0bSAndroid Build Coastguard Workerworks on both x86 and arm64 MacOS boxes. 156*08b48e0bSAndroid Build Coastguard Worker 157*08b48e0bSAndroid Build Coastguard WorkerMacOS X supports SYSV shared memory used by AFL's instrumentation, but the 158*08b48e0bSAndroid Build Coastguard Workerdefault settings aren't usable with AFL++. The default settings on 10.14 seem to 159*08b48e0bSAndroid Build Coastguard Workerbe: 160*08b48e0bSAndroid Build Coastguard Worker 161*08b48e0bSAndroid Build Coastguard Worker```bash 162*08b48e0bSAndroid Build Coastguard Worker$ ipcs -M 163*08b48e0bSAndroid Build Coastguard WorkerIPC status from <running system> as of XXX 164*08b48e0bSAndroid Build Coastguard Workershminfo: 165*08b48e0bSAndroid Build Coastguard Worker shmmax: 4194304 (max shared memory segment size) 166*08b48e0bSAndroid Build Coastguard Worker shmmin: 1 (min shared memory segment size) 167*08b48e0bSAndroid Build Coastguard Worker shmmni: 32 (max number of shared memory identifiers) 168*08b48e0bSAndroid Build Coastguard Worker shmseg: 8 (max shared memory segments per process) 169*08b48e0bSAndroid Build Coastguard Worker shmall: 1024 (max amount of shared memory in pages) 170*08b48e0bSAndroid Build Coastguard Worker``` 171*08b48e0bSAndroid Build Coastguard Worker 172*08b48e0bSAndroid Build Coastguard WorkerTo temporarily change your settings to something minimally usable with AFL++, 173*08b48e0bSAndroid Build Coastguard Workerrun these commands as root: 174*08b48e0bSAndroid Build Coastguard Worker 175*08b48e0bSAndroid Build Coastguard Worker```bash 176*08b48e0bSAndroid Build Coastguard Workersysctl kern.sysv.shmmax=8388608 177*08b48e0bSAndroid Build Coastguard Workersysctl kern.sysv.shmall=4096 178*08b48e0bSAndroid Build Coastguard Worker``` 179*08b48e0bSAndroid Build Coastguard Worker 180*08b48e0bSAndroid Build Coastguard WorkerIf you're running more than one instance of AFL, you likely want to make 181*08b48e0bSAndroid Build Coastguard Worker`shmall` bigger and increase `shmseg` as well: 182*08b48e0bSAndroid Build Coastguard Worker 183*08b48e0bSAndroid Build Coastguard Worker```bash 184*08b48e0bSAndroid Build Coastguard Workersysctl kern.sysv.shmmax=8388608 185*08b48e0bSAndroid Build Coastguard Workersysctl kern.sysv.shmseg=48 186*08b48e0bSAndroid Build Coastguard Workersysctl kern.sysv.shmall=98304 187*08b48e0bSAndroid Build Coastguard Worker``` 188*08b48e0bSAndroid Build Coastguard Worker 189*08b48e0bSAndroid Build Coastguard WorkerSee 190*08b48e0bSAndroid Build Coastguard Worker[http://www.spy-hill.com/help/apple/SharedMemory.html](http://www.spy-hill.com/help/apple/SharedMemory.html) 191*08b48e0bSAndroid Build Coastguard Workerfor documentation for these settings and how to make them permanent. 192