From 7db87ec74b62c4cd59c27e177903b6bd4dac4880 Mon Sep 17 00:00:00 2001 From: Kjell Braden Date: Sat, 21 Dec 2019 20:21:07 +0100 Subject: argvfuzz preload for fuzzing binaries' argv --- experimental/argv_fuzzing/Makefile | 48 +++++++++++++++++++++++++++++++++++ experimental/argv_fuzzing/README.md | 16 ++++++++++++ experimental/argv_fuzzing/argvfuzz.c | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 experimental/argv_fuzzing/Makefile create mode 100644 experimental/argv_fuzzing/README.md create mode 100644 experimental/argv_fuzzing/argvfuzz.c (limited to 'experimental/argv_fuzzing') diff --git a/experimental/argv_fuzzing/Makefile b/experimental/argv_fuzzing/Makefile new file mode 100644 index 00000000..da0eaed6 --- /dev/null +++ b/experimental/argv_fuzzing/Makefile @@ -0,0 +1,48 @@ +# +# american fuzzy lop - argvfuzz +# -------------------------------- +# +# Copyright 2019 Kjell Braden +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# + +.PHONY: all install clean + +ifndef AFL_NO_X86 +PREFIX ?= /usr/local +BIN_PATH = $(PREFIX)/bin +HELPER_PATH = $(PREFIX)/lib/afl + +CFLAGS = -fPIC -Wall -Wextra +LDFLAGS = -shared -ldl + +all: argvfuzz32.so argvfuzz64.so + +argvfuzz32.so: argvfuzz.c + $(CC) -m32 $(CFLAGS) $^ $(LDFLAGS) -o $@ || echo "argvfuzz32 build failure (that's fine)" + +argvfuzz64.so: argvfuzz.c + $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@ + +install: argvfuzz32.so argvfuzz64.so + if [ -f argvfuzz32.so ]; then set -e; install -m 755 argvfuzz32.so $(DESTDIR)$(HELPER_PATH)/; fi + install -m 755 argvfuzz64.so $(DESTDIR)$(HELPER_PATH)/ + +clean: + rm -f argvfuzz32.so argvfuzz64.so + +else + +all: + @echo "[!] Note: skipping compilation of argvfuzz (AFL_NO_X86 set)." + +install: + +clean: + +endif diff --git a/experimental/argv_fuzzing/README.md b/experimental/argv_fuzzing/README.md new file mode 100644 index 00000000..fa8cad80 --- /dev/null +++ b/experimental/argv_fuzzing/README.md @@ -0,0 +1,16 @@ +# argvfuzz + +afl supports fuzzing file inputs or stdin. When source is available, +`argv-fuzz-inl.h` can be used to change `main()` to build argv from stdin. + +`argvfuzz` tries to provide the same functionality for binaries. When loaded +using `LD_PRELOAD`, it will hook the call to `__libc_start_main` and replace +argv using the same logic of `argv-fuzz-inl.h`. + +A few conditions need to be fulfilled for this mechanism to work correctly: + +1. As it relies on hooking the loader, it cannot work on static binaries. +2. If the target binary does not use the default libc's `_start` implementation + (crt1.o), the hook may not run. +3. The hook will replace argv with pointers to `.data` of `argvfuzz.so`. If the + target binary expects argv to be living on the stack, things may go wrong. diff --git a/experimental/argv_fuzzing/argvfuzz.c b/experimental/argv_fuzzing/argvfuzz.c new file mode 100644 index 00000000..65fb5e13 --- /dev/null +++ b/experimental/argv_fuzzing/argvfuzz.c @@ -0,0 +1,49 @@ +/* + american fuzzy lop - LD_PRELOAD for fuzzing argv in binaries + ------------------------------------------------------------ + + Copyright 2019 Kjell Braden + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at: + + http://www.apache.org/licenses/LICENSE-2.0 + + */ + +#define _GNU_SOURCE /* for RTLD_NEXT */ +#include +#include +#include +#include +#include "argv-fuzz-inl.h" + +int __libc_start_main(int (*main)(int, char **, char **), int argc, char **argv, + void (*init)(void), void (*fini)(void), + void (*rtld_fini)(void), void *stack_end) { + + int (*orig)(int (*main)(int, char **, char **), int argc, char **argv, + void (*init)(void), void (*fini)(void), void (*rtld_fini)(void), + void *stack_end); + int sub_argc; + char **sub_argv; + + (void)argc; + (void)argv; + + orig = dlsym(RTLD_NEXT, __func__); + + if (!orig) { + + fprintf(stderr, "hook did not find original %s: %s\n", __func__, dlerror()); + exit(EXIT_FAILURE); + + } + + sub_argv = afl_init_argv(&sub_argc); + + return orig(main, sub_argc, sub_argv, init, fini, rtld_fini, stack_end); + +} + -- cgit 1.4.1 From 5aa089d1b22b092a2cc2e797abe755a717f0afc4 Mon Sep 17 00:00:00 2001 From: Kjell Braden Date: Tue, 24 Dec 2019 16:09:48 +0100 Subject: argv_fuzzing: should also compile with AFL_NO_X86 --- experimental/argv_fuzzing/Makefile | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'experimental/argv_fuzzing') diff --git a/experimental/argv_fuzzing/Makefile b/experimental/argv_fuzzing/Makefile index da0eaed6..a8858a39 100644 --- a/experimental/argv_fuzzing/Makefile +++ b/experimental/argv_fuzzing/Makefile @@ -13,7 +13,6 @@ .PHONY: all install clean -ifndef AFL_NO_X86 PREFIX ?= /usr/local BIN_PATH = $(PREFIX)/bin HELPER_PATH = $(PREFIX)/lib/afl @@ -35,14 +34,3 @@ install: argvfuzz32.so argvfuzz64.so clean: rm -f argvfuzz32.so argvfuzz64.so - -else - -all: - @echo "[!] Note: skipping compilation of argvfuzz (AFL_NO_X86 set)." - -install: - -clean: - -endif -- cgit 1.4.1