Is it any good?
- Install python2 & python3 (ucf uses python3, however qemu/unicorn needs python2 to build)
./setup.sh, preferrably inside a Virtualenv (else python deps will be installed using
--user). During install, afl++ and uDdbg as well as python deps will be pulled and installed.
When upgrading from an early version of ucf:
- Unicorefuzz will notify you of config changes and new options automatically.
- Alternatively, run
ucf specto output a commented
harness.pyis now named
- The song remains the same.
Debug Kernel Setup (Skip this if you know how this works)
- Create a qemu-img and install your preferred OS on there through qemu
- An easy way to get a working userspace up and running in QEMU is to follow the steps described by syzkaller, namely create-image.sh
- For kernel customization you might want to clone your preferred kernel version and compile it on the host. This way you can also compile your own kernel modules (e.g. example_module).
- In order to find out the address of a loaded module in the guest OS you can use
cat /proc/modulesto find out the base address of the module location. Use this as the offset for the function where you want to break. If you specify
config.py, it should use
./get_mod_addr.shto start it automated.
- You can compile the kernel with debug info. When you have compiled the linux kernel you can start gdb from the kernel folder with
gdb vmlinux. After having loaded other modules you can use the
lx-symbolscommand in gdb to load the symbols for the other modules (make sure the .ko files of the modules are in your kernel folder). This way you can just use something like
break function_to_breakto set breakpoints for the required functions.
- In order to compile a custom kernel for Arch, download the current Arch kernel and set the .config to the Arch default. Then set
KASAN_EXTRA=y. For convenience, we added a working example_config that can be place to the linux dir.
- To only get necessary kernel modules boot the current system and execute
lsmod > mylsmodand copy the mylsmod file to your host system into the linux kernel folder that you downloaded. Then you can use
make LSMOD=mylsmod localmodconfigto only make the kernel modules that are actually needed by the guest system. Then you can compile the kernel like normal with
make. Then mount the guest file system to
make modules_install INSTALL_MOD_PATH=/mnt. At last you have to create a new initramfs, which apparently has to be done on the guest system. Here use
mkinitcpio -k <folder in /lib/modules/...> -g <where to put initramfs>. Then you just need to copy that back to the host and let qemu know where your kernel and the initramfs are located.
- Setting breakpoints anywhere else is possible. For this, set
- For fancy debugging, ucf uses uDdbg
- Before fuzzing, run
sudo ./setaflops.shto initialize your system for fuzzing.
- ensure a target gdbserver is reachable, for example via
- provide the target's gdbserver network address in the config to the probe wrapper
- provide the target's target function to the probe wrapper and harness
- make the harness put AFL's input to the desired memory location by adopting the
- add all EXITs
ucf attach, it will (try to) connect to gdb.
- make the target execute the target function (by using it inside the vm)
- after the breakpoint was hit, run
ucf fuzz. Make sure afl++ is in the PATH. (Use
./resumeafl.shto resume using the same input folder)
Putting afl's input to the correct location must be coded invididually for most targets. However with modern binary analysis frameworks like IDA or Ghidra it's possible to find the desired location's address.
place_input method places at the data section of
# read input into param xyz here: rdx = uc.reg_read(UC_X86_REG_RDX) utils.map_page(uc, rdx) # ensure sk_buf is mapped bufferPtr = struct.unpack("<Q",uc.mem_read(rdx + 0xd8, 8)) utils.map_page(uc, bufferPtr) # ensure the buffer is mapped uc.mem_write(rdx, input) # insert afl input uc.mem_write(rdx + 0xc4, b"\xdc\x05") # fix tail
QEMUing the Kernel
A few general pointers.
./startvm.sh, the VM can be debugged via gdb.
$gdb >file ./linux/vmlinux >target remote :1234
This dynamic method makes it rather easy to find out breakpoints and that can then be fed to
startvm.sh will forward port 22 (ssh) to 8022 - you can use it to ssh into the VM.
This makes it easier to interact with it.
You can step through the code, starting at the breakpoint, with any given input.
The fancy debugging makes use of uDdbg.
To do so, run
ucf emu -d $inputfile.
Possible inputs to the harness (the thing wrapping afl-unicorn) that help debugging:
-d flag loads the target inside the unicorn debugger (uDdbg)
-t flag enables the afl-unicorn tracer. It prints every emulated instruction, as well as displays memory accesses.
A few things to consider.
FS_BASE and GS_BASE
Unicorn did not offer a way to directly set model specific registers directly. The forked unicornafl version of AFL++ finally supports it. Most ugly code of earlier versions was scrapped.
Improve Fuzzing Speed
Right now, the Unicorefuzz
ucf attach harness might need to be manually restarted after an amount of pages has been allocated.
Allocated pages should propagate back to the forkserver parent automatically but might still get reloaded from disk for each iteration.
It's generally a good idea to nop out kprintf or kernel printing functionality if possible, when the program is loaded into the emulator.
If you got trouble running unicorefuzz, follow these rulse, worst case feel free to reach out to us, for example to @domenuk on twitter. For some notes on debugging and developing ucf and afl-unicorn further, read DEVELOPMENT.md
Just won't start
Run the harness without afl (
ucf emu -t ./sometestcase).
Make sure you are not in a virtualenv or in the correct one.
If this works but it still crashes in AFL, set
AFL_DEBUG_CHILD_OUTPUT=1 to see some harness output while fuzzing.
All testcases time out
ucf attach is running, in the same folder, and breakpoint has been triggered.