aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorvan Hauser <vh@thc.org>2020-08-03 13:39:55 +0200
committerGitHub <noreply@github.com>2020-08-03 13:39:55 +0200
commitd5d8d664d0d4b95792aaccd16264f3a3cff48cc8 (patch)
treefa82a04acca16ea3e088b0d7d3aaec4b01ddf8f9 /examples
parent4a51cb71fb8785325dedac693cdea4648f6e5279 (diff)
parent409e4ae945ab5aeb31b1e3a1497ce5fc65226f07 (diff)
downloadafl++-d5d8d664d0d4b95792aaccd16264f3a3cff48cc8.tar.gz
Merge pull request #477 from AFLplusplus/dev
Push to stable
Diffstat (limited to 'examples')
-rw-r--r--examples/afl_frida/GNUmakefile23
-rw-r--r--examples/afl_frida/Makefile2
-rw-r--r--examples/afl_frida/README.md34
-rw-r--r--examples/afl_frida/afl-frida.c541
-rw-r--r--examples/afl_frida/afl-frida.h53
-rw-r--r--examples/afl_frida/libtestinstr.c35
-rw-r--r--examples/afl_network_proxy/afl-network-client.c4
-rw-r--r--examples/afl_untracer/README.md3
-rw-r--r--examples/afl_untracer/TODO2
-rw-r--r--examples/afl_untracer/afl-untracer.c32
-rw-r--r--examples/aflpp_driver/GNUmakefile22
-rw-r--r--examples/aflpp_driver/aflpp_driver.c (renamed from examples/aflpp_driver/aflpp_driver.cpp)226
-rw-r--r--examples/aflpp_driver/aflpp_driver_test.c25
-rw-r--r--examples/aflpp_driver/aflpp_driver_test.cpp22
-rw-r--r--examples/custom_mutators/post_library_gif.so.c2
-rw-r--r--examples/defork/Makefile64
-rw-r--r--examples/defork/README.md11
-rw-r--r--examples/defork/defork.c50
-rw-r--r--examples/defork/forking_target.c48
-rw-r--r--examples/persistent_demo/persistent_demo_new.c14
20 files changed, 1066 insertions, 147 deletions
diff --git a/examples/afl_frida/GNUmakefile b/examples/afl_frida/GNUmakefile
new file mode 100644
index 00000000..c154f3a4
--- /dev/null
+++ b/examples/afl_frida/GNUmakefile
@@ -0,0 +1,23 @@
+ifdef DEBUG
+ OPT=-O0 -D_DEBUG=\"1\"
+else
+ OPT=-O3 -funroll-loops
+endif
+
+all: afl-frida libtestinstr.so
+
+libfrida-gum.a:
+ @echo Download and extract frida-gum-devkit-VERSION-PLATFORM.tar.xz for your platform from https://github.com/frida/frida/releases/latest
+ @exit 1
+
+afl-frida: afl-frida.c libfrida-gum.a
+ $(CC) -g $(OPT) -o afl-frida -Wno-format -Wno-pointer-sign -I. -fpermissive -fPIC afl-frida.c ../../afl-llvm-rt.o libfrida-gum.a -ldl -lresolv -pthread
+
+libtestinstr.so: libtestinstr.c
+ $(CC) -g -O0 -fPIC -o libtestinstr.so -shared libtestinstr.c
+
+clean:
+ rm -f afl-frida *~ core *.o libtestinstr.so
+
+deepclean: clean
+ rm -f libfrida-gum.a frida-gum*
diff --git a/examples/afl_frida/Makefile b/examples/afl_frida/Makefile
new file mode 100644
index 00000000..0b306dde
--- /dev/null
+++ b/examples/afl_frida/Makefile
@@ -0,0 +1,2 @@
+all:
+ @echo please use GNU make, thanks!
diff --git a/examples/afl_frida/README.md b/examples/afl_frida/README.md
new file mode 100644
index 00000000..7743479b
--- /dev/null
+++ b/examples/afl_frida/README.md
@@ -0,0 +1,34 @@
+# afl-frida - faster fuzzing of binary-only libraries
+
+## Introduction
+
+afl-frida is an example skeleton file which can easily be used to fuzz
+a closed source library.
+
+It requires less memory and is x5-10 faster than qemu_mode but does not
+provide interesting features like compcov or cmplog.
+
+## How-to
+
+### Modify afl-frida.c
+
+Read and modify afl-frida.c then `make`.
+To adapt afl-frida.c to your needs, read the header of the file and then
+search and edit the `STEP 1`, `STEP 2` and `STEP 3` locations.
+
+### Fuzzing
+
+Example (after modifying afl-frida.c to your needs and compile it):
+```
+LD_LIBRARY_PATH=/path/to/the/target/library afl-fuzz -i in -o out -- ./afl-frida
+```
+(or even remote via afl-network-proxy).
+
+# Speed and stability
+
+The speed is very good, about x12 of fork() qemu_mode.
+However the stability is low. Reason is currently unknown.
+
+# Background
+
+This code is copied for a larger part from https://github.com/meme/hotwax
diff --git a/examples/afl_frida/afl-frida.c b/examples/afl_frida/afl-frida.c
new file mode 100644
index 00000000..2ad5a72a
--- /dev/null
+++ b/examples/afl_frida/afl-frida.c
@@ -0,0 +1,541 @@
+/*
+ american fuzzy lop++ - afl-frida skeleton example
+ -------------------------------------------------
+
+ Copyright 2020 AFLplusplus Project. All rights reserved.
+
+ Written mostly by meme -> https://github.com/meme/hotwax
+
+ Modificationy by Marc Heuse <mh@mh-sec.de>
+
+ 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
+
+ HOW-TO
+ ======
+
+ You only need to change the following:
+
+ 1. set the defines and function call parameters.
+ 2. dl load the library you want to fuzz, lookup the functions you need
+ and setup the calls to these.
+ 3. in the while loop you call the functions in the necessary order -
+ incl the cleanup. the cleanup is important!
+
+ Just look these steps up in the code, look for "// STEP x:"
+
+*/
+
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <sys/shm.h>
+#include <dlfcn.h>
+
+#ifndef __APPLE__
+ #include <sys/wait.h>
+ #include <sys/personality.h>
+#endif
+
+int debug = 0;
+
+// STEP 1:
+
+// The presets are for the example libtestinstr.so:
+
+/* What is the name of the library to fuzz */
+#define TARGET_LIBRARY "libtestinstr.so"
+
+/* What is the name of the function to fuzz */
+#define TARGET_FUNCTION "testinstr"
+
+/* here you need to specify the parameter for the target function */
+static void *(*o_function)(uint8_t *, int);
+
+// END STEP 1
+
+#include "frida-gum.h"
+
+G_BEGIN_DECLS
+
+#define GUM_TYPE_FAKE_EVENT_SINK (gum_fake_event_sink_get_type())
+G_DECLARE_FINAL_TYPE(GumFakeEventSink, gum_fake_event_sink, GUM,
+ FAKE_EVENT_SINK, GObject)
+
+struct _GumFakeEventSink {
+
+ GObject parent;
+ GumEventType mask;
+
+};
+
+GumEventSink *gum_fake_event_sink_new(void);
+void gum_fake_event_sink_reset(GumFakeEventSink *self);
+
+G_END_DECLS
+
+static void gum_fake_event_sink_iface_init(gpointer g_iface,
+ gpointer iface_data);
+static void gum_fake_event_sink_finalize(GObject *obj);
+static GumEventType gum_fake_event_sink_query_mask(GumEventSink *sink);
+static void gum_fake_event_sink_process(GumEventSink *sink, const GumEvent *ev);
+void instr_basic_block(GumStalkerIterator *iterator, GumStalkerOutput *output,
+ gpointer user_data);
+void afl_setup(void);
+void afl_start_forkserver(void);
+int __afl_persistent_loop(unsigned int max_cnt);
+
+static void gum_fake_event_sink_class_init(GumFakeEventSinkClass *klass) {
+
+ GObjectClass *object_class = G_OBJECT_CLASS(klass);
+ object_class->finalize = gum_fake_event_sink_finalize;
+
+}
+
+static void gum_fake_event_sink_iface_init(gpointer g_iface,
+ gpointer iface_data) {
+
+ GumEventSinkInterface *iface = (GumEventSinkInterface *)g_iface;
+ iface->query_mask = gum_fake_event_sink_query_mask;
+ iface->process = gum_fake_event_sink_process;
+
+}
+
+G_DEFINE_TYPE_EXTENDED(GumFakeEventSink, gum_fake_event_sink, G_TYPE_OBJECT, 0,
+ G_IMPLEMENT_INTERFACE(GUM_TYPE_EVENT_SINK,
+ gum_fake_event_sink_iface_init))
+
+#include "../../config.h"
+
+// Shared memory fuzzing.
+int __afl_sharedmem_fuzzing = 1;
+extern unsigned int * __afl_fuzz_len;
+extern unsigned char *__afl_fuzz_ptr;
+
+// Notify AFL about persistent mode.
+static volatile char AFL_PERSISTENT[] = "##SIG_AFL_PERSISTENT##";
+int __afl_persistent_loop(unsigned int);
+
+// Notify AFL about deferred forkserver.
+static volatile char AFL_DEFER_FORKSVR[] = "##SIG_AFL_DEFER_FORKSRV##";
+void __afl_manual_init();
+
+// Because we do our own logging.
+extern uint8_t * __afl_area_ptr;
+static __thread guint64 previous_pc;
+
+// Frida stuff below.
+typedef struct {
+
+ GumAddress base_address;
+ guint64 code_start, code_end;
+
+} range_t;
+
+inline static void afl_maybe_log(guint64 current_pc) {
+
+ // fprintf(stderr, "PC: %p ^ %p\n", current_pc, previous_pc);
+
+ current_pc = (current_pc >> 4) ^ (current_pc << 8);
+ current_pc &= MAP_SIZE - 1;
+
+ __afl_area_ptr[current_pc ^ previous_pc]++;
+ previous_pc = current_pc >> 1;
+
+}
+
+static void on_basic_block(GumCpuContext *context, gpointer user_data) {
+
+ afl_maybe_log((guint64)user_data);
+
+}
+
+void instr_basic_block(GumStalkerIterator *iterator, GumStalkerOutput *output,
+ gpointer user_data) {
+
+ range_t *range = (range_t *)user_data;
+
+ const cs_insn *instr;
+ gboolean begin = TRUE;
+ while (gum_stalker_iterator_next(iterator, &instr)) {
+
+ if (begin) {
+
+ if (instr->address >= range->code_start &&
+ instr->address <= range->code_end) {
+
+ gum_stalker_iterator_put_callout(iterator, on_basic_block,
+ (gpointer)instr->address, NULL);
+ begin = FALSE;
+
+ }
+
+ }
+
+ gum_stalker_iterator_keep(iterator);
+
+ }
+
+}
+
+static void gum_fake_event_sink_init(GumFakeEventSink *self) {
+
+}
+
+static void gum_fake_event_sink_finalize(GObject *obj) {
+
+ G_OBJECT_CLASS(gum_fake_event_sink_parent_class)->finalize(obj);
+
+}
+
+GumEventSink *gum_fake_event_sink_new(void) {
+
+ GumFakeEventSink *sink;
+ sink = (GumFakeEventSink *)g_object_new(GUM_TYPE_FAKE_EVENT_SINK, NULL);
+ return GUM_EVENT_SINK(sink);
+
+}
+
+void gum_fake_event_sink_reset(GumFakeEventSink *self) {
+
+}
+
+static GumEventType gum_fake_event_sink_query_mask(GumEventSink *sink) {
+
+ return 0;
+
+}
+
+typedef struct library_list {
+
+ uint8_t *name;
+ uint64_t addr_start, addr_end;
+
+} library_list_t;
+
+#define MAX_LIB_COUNT 256
+static library_list_t liblist[MAX_LIB_COUNT];
+static u32 liblist_cnt;
+
+void read_library_information() {
+
+#if defined(__linux__)
+ FILE *f;
+ u8 buf[1024], *b, *m, *e, *n;
+
+ if ((f = fopen("/proc/self/maps", "r")) == NULL) {
+
+ fprintf(stderr, "Error: cannot open /proc/self/maps\n");
+ exit(-1);
+
+ }
+
+ if (debug) fprintf(stderr, "Library list:\n");
+ while (fgets(buf, sizeof(buf), f)) {
+
+ if (strstr(buf, " r-x")) {
+
+ if (liblist_cnt >= MAX_LIB_COUNT) {
+
+ fprintf(
+ stderr,
+ "Warning: too many libraries to old, maximum count of %d reached\n",
+ liblist_cnt);
+ return;
+
+ }
+
+ b = buf;
+ m = index(buf, '-');
+ e = index(buf, ' ');
+ if ((n = rindex(buf, '/')) == NULL) n = rindex(buf, ' ');
+ if (n &&
+ ((*n >= '0' && *n <= '9') || *n == '[' || *n == '{' || *n == '('))
+ n = NULL;
+ else
+ n++;
+ if (b && m && e && n && *n) {
+
+ *m++ = 0;
+ *e = 0;
+ if (n[strlen(n) - 1] == '\n') n[strlen(n) - 1] = 0;
+
+ if (rindex(n, '/') != NULL) {
+
+ n = rindex(n, '/');
+ n++;
+
+ }
+
+ liblist[liblist_cnt].name = strdup(n);
+ liblist[liblist_cnt].addr_start = strtoull(b, NULL, 16);
+ liblist[liblist_cnt].addr_end = strtoull(m, NULL, 16);
+ if (debug)
+ fprintf(
+ stderr, "%s:%llx (%llx-%llx)\n", liblist[liblist_cnt].name,
+ liblist[liblist_cnt].addr_end - liblist[liblist_cnt].addr_start,
+ liblist[liblist_cnt].addr_start,
+ liblist[liblist_cnt].addr_end - 1);
+ liblist_cnt++;
+
+ }
+
+ }
+
+ }
+
+ if (debug) fprintf(stderr, "\n");
+
+#elif defined(__FreeBSD__)
+ int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid()};
+ char * buf, *start, *end;
+ size_t miblen = sizeof(mib) / sizeof(mib[0]);
+ size_t len;
+
+ if (debug) fprintf(stderr, "Library list:\n");
+ if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) { return; }
+
+ len = len * 4 / 3;
+
+ buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
+ if (buf == MAP_FAILED) { return; }
+ if (sysctl(mib, miblen, buf, &len, NULL, 0) == -1) {
+
+ munmap(buf, len);
+ return;
+
+ }
+
+ start = buf;
+ end = buf + len;
+
+ while (start < end) {
+
+ struct kinfo_vmentry *region = (struct kinfo_vmentry *)start;
+ size_t size = region->kve_structsize;
+
+ if (size == 0) { break; }
+
+ if ((region->kve_protection & KVME_PROT_READ) &&
+ !(region->kve_protection & KVME_PROT_EXEC)) {
+
+ liblist[liblist_cnt].name =
+ region->kve_path[0] != '\0' ? strdup(region->kve_path) : 0;
+ liblist[liblist_cnt].addr_start = region->kve_start;
+ liblist[liblist_cnt].addr_end = region->kve_end;
+
+ if (debug) {
+
+ fprintf(stderr, "%s:%x (%lx-%lx)\n", liblist[liblist_cnt].name,
+ liblist[liblist_cnt].addr_end - liblist[liblist_cnt].addr_start,
+ liblist[liblist_cnt].addr_start,
+ liblist[liblist_cnt].addr_end - 1);
+
+ }
+
+ liblist_cnt++;
+
+ }
+
+ start += size;
+
+ }
+
+#endif
+
+}
+
+library_list_t *find_library(char *name) {
+
+ char *filename = rindex(name, '/');
+
+ if (filename)
+ filename++;
+ else
+ filename = name;
+
+#if defined(__linux__)
+ u32 i;
+ for (i = 0; i < liblist_cnt; i++)
+ if (strcmp(liblist[i].name, filename) == 0) return &liblist[i];
+#elif defined(__APPLE__) && defined(__LP64__)
+ kern_return_t err;
+ static library_list_t lib;
+
+ // get the list of all loaded modules from dyld
+ // the task_info mach API will get the address of the dyld all_image_info
+ // struct for the given task from which we can get the names and load
+ // addresses of all modules
+ task_dyld_info_data_t task_dyld_info;
+ mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
+ err = task_info(mach_task_self(), TASK_DYLD_INFO,
+ (task_info_t)&task_dyld_info, &count);
+
+ const struct dyld_all_image_infos *all_image_infos =
+ (const struct dyld_all_image_infos *)task_dyld_info.all_image_info_addr;
+ const struct dyld_image_info *image_infos = all_image_infos->infoArray;
+
+ for (size_t i = 0; i < all_image_infos->infoArrayCount; i++) {
+
+ const char * image_name = image_infos[i].imageFilePath;
+ mach_vm_address_t image_load_address =
+ (mach_vm_address_t)image_infos[i].imageLoadAddress;
+ if (strstr(image_name, name)) {
+
+ lib.name = name;
+ lib.addr_start = (u64)image_load_address;
+ lib.addr_end = 0;
+ return &lib;
+
+ }
+
+ }
+
+#endif
+
+ return NULL;
+
+}
+
+static void gum_fake_event_sink_process(GumEventSink * sink,
+ const GumEvent *ev) {
+
+}
+
+/* Because this CAN be called more than once, it will return the LAST range */
+static int enumerate_ranges(const GumRangeDetails *details,
+ gpointer user_data) {
+
+ GumMemoryRange *code_range = (GumMemoryRange *)user_data;
+ memcpy(code_range, details->range, sizeof(*code_range));
+ return 0;
+
+}
+
+int main() {
+
+#ifndef __APPLE__
+ (void)personality(ADDR_NO_RANDOMIZE); // disable ASLR
+#endif
+
+ // STEP 2: load the library you want to fuzz and lookup the functions,
+ // inclusive of the cleanup functions.
+ // If there is just one function, then there is nothing to change
+ // or add here.
+
+ void *dl = dlopen(TARGET_LIBRARY, RTLD_LAZY);
+ if (!dl) {
+
+ fprintf(stderr, "Could not load %s\n", TARGET_LIBRARY);
+ exit(-1);
+
+ }
+
+ if (!(o_function = dlsym(dl, TARGET_FUNCTION))) {
+
+ fprintf(stderr, "Could not find function %s\n", TARGET_FUNCTION);
+ exit(-1);
+
+ }
+
+ // END STEP 2
+
+ read_library_information();
+ library_list_t *lib = find_library(TARGET_LIBRARY);
+
+ if (lib == NULL) {
+
+ fprintf(stderr, "Could not find target library\n");
+ exit(-1);
+
+ }
+
+ gum_init_embedded();
+ if (!gum_stalker_is_supported()) {
+
+ gum_deinit_embedded();
+ return 1;
+
+ }
+
+ GumStalker *stalker = gum_stalker_new();
+
+ /*
+ This does not work here as we load a shared library. pretty sure this
+ would also be easily solvable with frida gum, but I already have all the
+ code I need from afl-untracer
+
+ GumAddress base_address = gum_module_find_base_address(TARGET_LIBRARY);
+ GumMemoryRange code_range;
+ gum_module_enumerate_ranges(TARGET_LIBRARY, GUM_PAGE_RX, enumerate_ranges,
+ &code_range);
+ guint64 code_start = code_range.base_address - base_address;
+ guint64 code_end = (code_range.base_address + code_range.size) - base_address;
+ range_t instr_range = {base_address, code_start, code_end};
+ */
+ range_t instr_range = {0, lib->addr_start, lib->addr_end};
+
+ GumStalkerTransformer *transformer =
+ gum_stalker_transformer_make_from_callback(instr_basic_block,
+ &instr_range, NULL);
+
+ GumEventSink *event_sink = gum_fake_event_sink_new();
+
+ // to ensure that the signatures are not optimized out
+ memcpy(__afl_area_ptr, (void *)AFL_PERSISTENT, sizeof(AFL_PERSISTENT) + 1);
+ memcpy(__afl_area_ptr + 32, (void *)AFL_DEFER_FORKSVR,
+ sizeof(AFL_DEFER_FORKSVR) + 1);
+ __afl_manual_init();
+
+ //
+ // any expensive target library initialization that has to be done just once
+ // - put that here
+ //
+
+ gum_stalker_follow_me(stalker, transformer, event_sink);
+
+ while (__afl_persistent_loop(UINT32_MAX) != 0) {
+
+ previous_pc = 0; // Required!
+
+#ifdef _DEBUG
+ fprintf(stderr, "CLIENT crc: %016llx len: %u\n", hash64(__afl_fuzz_ptr, *__a
+ fprintf(stderr, "RECV:");
+ for (int i = 0; i < *__afl_fuzz_len; i++)
+ fprintf(stderr, "%02x", __afl_fuzz_ptr[i]);
+ fprintf(stderr,"\n");
+#endif
+
+ // STEP 3: ensure the minimum length is present and setup the target
+ // function to fuzz.
+
+ if (*__afl_fuzz_len > 0) {
+
+ __afl_fuzz_ptr[*__afl_fuzz_len] = 0; // if you need to null terminate
+ (*o_function)(__afl_fuzz_ptr, *__afl_fuzz_len);
+
+ }
+
+ // END STEP 3
+
+ }
+
+ gum_stalker_unfollow_me(stalker);
+
+ while (gum_stalker_garbage_collect(stalker))
+ g_usleep(10000);
+
+ g_object_unref(stalker);
+ g_object_unref(transformer);
+ g_object_unref(event_sink);
+ gum_deinit_embedded();
+
+ return 0;
+
+}
+
diff --git a/examples/afl_frida/afl-frida.h b/examples/afl_frida/afl-frida.h
new file mode 100644
index 00000000..efa3440f
--- /dev/null
+++ b/examples/afl_frida/afl-frida.h
@@ -0,0 +1,53 @@
+extern int is_persistent;
+
+G_BEGIN_DECLS
+
+#define GUM_TYPE_FAKE_EVENT_SINK (gum_fake_event_sink_get_type())
+
+G_DECLARE_FINAL_TYPE(GumFakeEventSink, gum_fake_event_sink, GUM,
+ FAKE_EVENT_SINK, GObject)
+
+struct _GumFakeEventSink {
+
+ GObject parent;
+ GumEventType mask;
+
+};
+
+GumEventSink *gum_fake_event_sink_new(void);
+void gum_fake_event_sink_reset(GumFakeEventSink *self);
+
+G_END_DECLS
+
+typedef struct {
+
+ GumAddress base_address;
+ guint64 code_start, code_end;
+
+} range_t;
+
+void instr_basic_block(GumStalkerIterator *iterator, GumStalkerOutput *output,
+ gpointer user_data);
+#pragma once
+
+void afl_setup(void);
+void afl_start_forkserver(void);
+int __afl_persistent_loop(unsigned int max_cnt);
+
+inline static inline void afl_maybe_log(guint64 current_pc) {
+
+ extern unsigned int afl_instr_rms;
+ extern uint8_t * afl_area_ptr;
+
+ static __thread guint64 previous_pc;
+
+ current_pc = (current_pc >> 4) ^ (current_pc << 8);
+ current_pc &= MAP_SIZE - 1;
+
+ if (current_pc >= afl_instr_rms) return;
+
+ afl_area_ptr[current_pc ^ previous_pc]++;
+ previous_pc = current_pc >> 1;
+
+}
+
diff --git a/examples/afl_frida/libtestinstr.c b/examples/afl_frida/libtestinstr.c
new file mode 100644
index 00000000..96b1cf21
--- /dev/null
+++ b/examples/afl_frida/libtestinstr.c
@@ -0,0 +1,35 @@
+/*
+ american fuzzy lop++ - a trivial program to test the build
+ --------------------------------------------------------
+ Originally written by Michal Zalewski
+ Copyright 2014 Google Inc. All rights reserved.
+ Copyright 2019-2020 AFLplusplus Project. All rights reserved.
+ 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
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+void testinstr(char *buf, int len) {
+
+ if (len < 1) return;
+ buf[len] = 0;
+
+ // we support three input cases
+ if (buf[0] == '0')
+ printf("Looks like a zero to me!\n");
+ else if (buf[0] == '1')
+ printf("Pretty sure that is a one!\n");
+ else
+ printf("Neither one or zero? How quaint!\n");
+
+}
+
diff --git a/examples/afl_network_proxy/afl-network-client.c b/examples/afl_network_proxy/afl-network-client.c
index 5af41055..a2451fdc 100644
--- a/examples/afl_network_proxy/afl-network-client.c
+++ b/examples/afl_network_proxy/afl-network-client.c
@@ -34,7 +34,9 @@
#include <netinet/ip6.h>
#include <arpa/inet.h>
#include <sys/mman.h>
-#include <sys/shm.h>
+#ifndef USEMMAP
+ #include <sys/shm.h>
+#endif
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
diff --git a/examples/afl_untracer/README.md b/examples/afl_untracer/README.md
index e59792cb..ada0c916 100644
--- a/examples/afl_untracer/README.md
+++ b/examples/afl_untracer/README.md
@@ -32,13 +32,14 @@ To easily run the scripts without needing to run the GUI with Ghidra:
/opt/ghidra/support/analyzeHeadless /tmp/ tmp$$ -import libtestinstr.so -postscript ./ghidra_get_patchpoints.java
rm -rf /tmp/tmp$$
```
+The file is created at `~/Desktop/patches.txt`
### Fuzzing
Example (after modifying afl-untracer.c to your needs, compiling and creating
patches.txt):
```
-AFL_UNTRACER_FILE=./patches.txt afl-fuzz -i in -o out -- ./afl-untracer
+LD_LIBRARY_PATH=/path/to/target/library AFL_UNTRACER_FILE=./patches.txt afl-fuzz -i in -o out -- ./afl-untracer
```
(or even remote via afl-network-proxy).
diff --git a/examples/afl_untracer/TODO b/examples/afl_untracer/TODO
new file mode 100644
index 00000000..fffffacf
--- /dev/null
+++ b/examples/afl_untracer/TODO
@@ -0,0 +1,2 @@
+ * add shmem fuzzing
+ * add snapshot feature?
diff --git a/examples/afl_untracer/afl-untracer.c b/examples/afl_untracer/afl-untracer.c
index 664e691c..77b15eb8 100644
--- a/examples/afl_untracer/afl-untracer.c
+++ b/examples/afl_untracer/afl-untracer.c
@@ -56,6 +56,7 @@
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/types.h>
+#include <sys/personality.h>
#if defined(__linux__)
#include <sys/ucontext.h>
@@ -73,6 +74,9 @@
// STEP 1:
+/* here you need to specify the parameter for the target function */
+static void *(*o_function)(u8 *buf, int len);
+
/* use stdin (1) or a file on the commandline (0) */
static u32 use_stdin = 1;
@@ -111,10 +115,10 @@ static library_list_t liblist[MAX_LIB_COUNT];
static u32 liblist_cnt;
static void sigtrap_handler(int signum, siginfo_t *si, void *context);
-static void fuzz();
+static void fuzz(void);
/* read the library information */
-void read_library_information() {
+void read_library_information(void) {
#if defined(__linux__)
FILE *f;
@@ -280,7 +284,7 @@ library_list_t *find_library(char *name) {
// this seems to work for clang too. nice :) requires gcc 4.4+
#pragma GCC push_options
#pragma GCC optimize("O0")
-void breakpoint() {
+void breakpoint(void) {
if (debug) fprintf(stderr, "Breakpoint function \"breakpoint\" reached.\n");
@@ -395,7 +399,7 @@ static void __afl_map_shm(void) {
}
/* Fork server logic. */
-static void __afl_start_forkserver(void) {
+inline static void __afl_start_forkserver(void) {
u8 tmp[4] = {0, 0, 0, 0};
u32 status = 0;
@@ -411,7 +415,7 @@ static void __afl_start_forkserver(void) {
}
-static u32 __afl_next_testcase(u8 *buf, u32 max_len) {
+inline static u32 __afl_next_testcase(u8 *buf, u32 max_len) {
s32 status;
@@ -437,7 +441,7 @@ static u32 __afl_next_testcase(u8 *buf, u32 max_len) {
}
-static void __afl_end_testcase(int status) {
+inline static void __afl_end_testcase(int status) {
if (write(FORKSRV_FD + 1, &status, 4) != 4) do_exit = 1;
// fprintf(stderr, "write2 %d\n", do_exit);
@@ -457,7 +461,7 @@ static void __afl_end_testcase(int status) {
((uintptr_t)addr & 0x3) * 0x10000000000))
#endif
-void setup_trap_instrumentation() {
+void setup_trap_instrumentation(void) {
library_list_t *lib_base = NULL;
size_t lib_size = 0;
@@ -667,12 +671,11 @@ static void sigtrap_handler(int signum, siginfo_t *si, void *context) {
}
-/* here you need to specify the parameter for the target function */
-static void *(*o_function)(u8 *buf, int len);
-
/* the MAIN function */
int main(int argc, char *argv[]) {
+ (void)personality(ADDR_NO_RANDOMIZE); // disable ASLR
+
pid = getpid();
if (getenv("AFL_DEBUG")) debug = 1;
@@ -706,6 +709,9 @@ int main(int argc, char *argv[]) {
while (1) {
+ // instead of fork() we could also use the snapshot lkm or do our own mini
+ // snapshot feature like in https://github.com/marcinguy/fuzzer
+ // -> snapshot.c
if ((pid = fork()) == -1) PFATAL("fork failed");
if (pid) {
@@ -738,7 +744,11 @@ int main(int argc, char *argv[]) {
}
-static void fuzz() {
+#ifndef _DEBUG
+inline
+#endif
+ static void
+ fuzz(void) {
// STEP 3: call the function to fuzz, also the functions you might
// need to call to prepare the function and - important! -
diff --git a/examples/aflpp_driver/GNUmakefile b/examples/aflpp_driver/GNUmakefile
index a993c8a9..b118a8b5 100644
--- a/examples/aflpp_driver/GNUmakefile
+++ b/examples/aflpp_driver/GNUmakefile
@@ -7,25 +7,25 @@ ifneq "" "$(LLVM_BINDIR)"
LLVM_BINDIR := $(LLVM_BINDIR)/
endif
-FLAGS=-O3 -funroll-loops -g
+CFLAGS := -O3 -funroll-loops -g
all: libAFLDriver.a libAFLQemuDriver.a aflpp_qemu_driver_hook.so
-aflpp_driver.o: aflpp_driver.cpp
- $(LLVM_BINDIR)clang++ $(FLAGS) -stdlib=libc++ -std=c++11 -c aflpp_driver.cpp
+aflpp_driver.o: aflpp_driver.c
+ $(LLVM_BINDIR)clang -I. -I../../include $(CFLAGS) -c aflpp_driver.c
libAFLDriver.a: aflpp_driver.o
ar ru libAFLDriver.a aflpp_driver.o
debug:
- $(LLVM_BINDIR)clang++ -Wno-deprecated -I../../include $(FLAGS) -D_DEBUG=\"1\" -c -o afl-performance.o ../../src/afl-performance.c
- $(LLVM_BINDIR)clang++ -I../../include -D_DEBUG=\"1\" -g -stdlib=libc++ -funroll-loops -std=c++11 -c aflpp_driver.cpp
- #$(LLVM_BINDIR)clang++ -S -emit-llvm -Wno-deprecated -I../../include $(FLAGS) -D_DEBUG=\"1\" -c -o afl-performance.ll ../../src/afl-performance.c
- #$(LLVM_BINDIR)clang++ -S -emit-llvm -I../../include -D_DEBUG=\"1\" -g -stdlib=libc++ -funroll-loops -std=c++11 -c aflpp_driver.cpp
+ $(LLVM_BINDIR)clang -Wno-deprecated -I../../include $(CFLAGS) -D_DEBUG=\"1\" -c -o afl-performance.o ../../src/afl-performance.c
+ $(LLVM_BINDIR)clang -I../../include -D_DEBUG=\"1\" -g -funroll-loops -c aflpp_driver.c
+ #$(LLVM_BINDIR)clang -S -emit-llvm -Wno-deprecated -I../../include $(CFLAGS) -D_DEBUG=\"1\" -c -o afl-performance.ll ../../src/afl-performance.c
+ #$(LLVM_BINDIR)clang -S -emit-llvm -I../../include -D_DEBUG=\"1\" -g -funroll-loops -c aflpp_driver.c
ar ru libAFLDriver.a afl-performance.o aflpp_driver.o
aflpp_qemu_driver.o: aflpp_qemu_driver.c
- $(LLVM_BINDIR)clang $(FLAGS) -O0 -funroll-loops -c aflpp_qemu_driver.c
+ $(LLVM_BINDIR)clang $(CFLAGS) -O0 -funroll-loops -c aflpp_qemu_driver.c
libAFLQemuDriver.a: aflpp_qemu_driver.o
ar ru libAFLQemuDriver.a aflpp_qemu_driver.o
@@ -34,11 +34,11 @@ aflpp_qemu_driver_hook.so: aflpp_qemu_driver_hook.o
$(LLVM_BINDIR)clang -shared aflpp_qemu_driver_hook.o -o aflpp_qemu_driver_hook.so
aflpp_qemu_driver_hook.o: aflpp_qemu_driver_hook.c
- $(LLVM_BINDIR)clang -fPIC $(FLAGS) -funroll-loops -c aflpp_qemu_driver_hook.c
+ $(LLVM_BINDIR)clang -fPIC $(CFLAGS) -funroll-loops -c aflpp_qemu_driver_hook.c
test: debug
- #clang++ -S -emit-llvm -D_DEBUG=\"1\" -I../../include -Wl,--allow-multiple-definition -stdlib=libc++ -funroll-loops -std=c++11 -o aflpp_driver_test.ll aflpp_driver_test.cpp
- afl-clang-fast++ -D_DEBUG=\"1\" -I../../include -Wl,--allow-multiple-definition -stdlib=libc++ -funroll-loops -std=c++11 -o aflpp_driver_test aflpp_driver_test.cpp libAFLDriver.a
+ #clang -S -emit-llvm -D_DEBUG=\"1\" -I../../include -Wl,--allow-multiple-definition -funroll-loops -o aflpp_driver_test.ll aflpp_driver_test.c
+ afl-clang-fast -D_DEBUG=\"1\" -I../../include -Wl,--allow-multiple-definition -funroll-loops -o aflpp_driver_test aflpp_driver_test.c libAFLDriver.a afl-performance.o
clean:
rm -f *.o libAFLDriver*.a libAFLQemuDriver.a aflpp_qemu_driver_hook.so *~ core aflpp_driver_test
diff --git a/examples/aflpp_driver/aflpp_driver.cpp b/examples/aflpp_driver/aflpp_driver.c
index d6163bdf..86c7a69f 100644
--- a/examples/aflpp_driver/aflpp_driver.cpp
+++ b/examples/aflpp_driver/aflpp_driver.c
@@ -14,12 +14,15 @@ cat << EOF > test_fuzzer.cc
#include <stddef.h>
#include <stdint.h>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+
if (size > 0 && data[0] == 'H')
if (size > 1 && data[1] == 'I')
if (size > 2 && data[2] == '!')
__builtin_trap();
return 0;
+
}
+
EOF
# Build your target with -fsanitize-coverage=trace-pc-guard using fresh clang.
clang -g -fsanitize-coverage=trace-pc-guard test_fuzzer.cc -c
@@ -49,188 +52,195 @@ If 1, close stdout at startup. If 2 close stderr; if 3 close both.
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
-#include <fstream>
-#include <iostream>
-#include <vector>
+#include "config.h"
#ifdef _DEBUG
-#include "hash.h"
+ #include "hash.h"
#endif
// Platform detection. Copied from FuzzerInternal.h
#ifdef __linux__
-#define LIBFUZZER_LINUX 1
-#define LIBFUZZER_APPLE 0
-#define LIBFUZZER_NETBSD 0
-#define LIBFUZZER_FREEBSD 0
-#define LIBFUZZER_OPENBSD 0
+ #define LIBFUZZER_LINUX 1
+ #define LIBFUZZER_APPLE 0
+ #define LIBFUZZER_NETBSD 0
+ #define LIBFUZZER_FREEBSD 0
+ #define LIBFUZZER_OPENBSD 0
#elif __APPLE__
-#define LIBFUZZER_LINUX 0
-#define LIBFUZZER_APPLE 1
-#define LIBFUZZER_NETBSD 0
-#define LIBFUZZER_FREEBSD 0
-#define LIBFUZZER_OPENBSD 0
+ #define LIBFUZZER_LINUX 0
+ #define LIBFUZZER_APPLE 1
+ #define LIBFUZZER_NETBSD 0
+ #define LIBFUZZER_FREEBSD 0
+ #define LIBFUZZER_OPENBSD 0
#elif __NetBSD__
-#define LIBFUZZER_LINUX 0
-#define LIBFUZZER_APPLE 0
-#define LIBFUZZER_NETBSD 1
-#define LIBFUZZER_FREEBSD 0
-#define LIBFUZZER_OPENBSD 0
+ #define LIBFUZZER_LINUX 0
+ #define LIBFUZZER_APPLE 0
+ #define LIBFUZZER_NETBSD 1
+ #define LIBFUZZER_FREEBSD 0
+ #define LIBFUZZER_OPENBSD 0
#elif __FreeBSD__
-#define LIBFUZZER_LINUX 0
-#define LIBFUZZER_APPLE 0
-#define LIBFUZZER_NETBSD 0
-#define LIBFUZZER_FREEBSD 1
-#define LIBFUZZER_OPENBSD 0
+ #define LIBFUZZER_LINUX 0
+ #define LIBFUZZER_APPLE 0
+ #define LIBFUZZER_NETBSD 0
+ #define LIBFUZZER_FREEBSD 1
+ #define LIBFUZZER_OPENBSD 0
#elif __OpenBSD__
-#define LIBFUZZER_LINUX 0
-#define LIBFUZZER_APPLE 0
-#define LIBFUZZER_NETBSD 0
-#define LIBFUZZER_FREEBSD 0
-#define LIBFUZZER_OPENBSD 1
+ #define LIBFUZZER_LINUX 0
+ #define LIBFUZZER_APPLE 0
+ #define LIBFUZZER_NETBSD 0
+ #define LIBFUZZER_FREEBSD 0
+ #define LIBFUZZER_OPENBSD 1
#else
-#error "Support for your platform has not been implemented"
+ #error "Support for your platform has not been implemented"
#endif
-int __afl_sharedmem_fuzzing = 1;
-extern unsigned int *__afl_fuzz_len;
+int __afl_sharedmem_fuzzing = 1;
+extern unsigned int * __afl_fuzz_len;
extern unsigned char *__afl_fuzz_ptr;
// libFuzzer interface is thin, so we don't include any libFuzzer headers.
-extern "C" {
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
__attribute__((weak)) int LLVMFuzzerInitialize(int *argc, char ***argv);
-}
// Notify AFL about persistent mode.
static volatile char AFL_PERSISTENT[] = "##SIG_AFL_PERSISTENT##";
-extern "C" int __afl_persistent_loop(unsigned int);
-static volatile char suppress_warning2 = AFL_PERSISTENT[0];
+int __afl_persistent_loop(unsigned int);
// Notify AFL about deferred forkserver.
static volatile char AFL_DEFER_FORKSVR[] = "##SIG_AFL_DEFER_FORKSRV##";
-extern "C" void __afl_manual_init();
-static volatile char suppress_warning1 = AFL_DEFER_FORKSVR[0];
-
-// Input buffer.
-static const size_t kMaxAflInputSize = 1 << 20;
-static uint8_t AflInputBuf[kMaxAflInputSize];
+void __afl_manual_init();
// Use this optionally defined function to output sanitizer messages even if
// user asks to close stderr.
-__attribute__((weak)) extern "C" void __sanitizer_set_report_fd(void *);
+__attribute__((weak)) void __sanitizer_set_report_fd(void *);
// Keep track of where stderr content is being written to, so that
// dup_and_close_stderr can use the correct one.
-static FILE *output_file = stderr;
+static FILE *output_file;
// Experimental feature to use afl_driver without AFL's deferred mode.
// Needs to run before __afl_auto_init.
__attribute__((constructor(0))) static void __decide_deferred_forkserver(void) {
+
if (getenv("AFL_DRIVER_DONT_DEFER")) {
+
if (unsetenv("__AFL_DEFER_FORKSRV")) {
+
perror("Failed to unset __AFL_DEFER_FORKSRV");
abort();
+
}
+
}
+
}
// If the user asks us to duplicate stderr, then do it.
static void maybe_duplicate_stderr() {
+
char *stderr_duplicate_filename =
getenv("AFL_DRIVER_STDERR_DUPLICATE_FILENAME");
- if (!stderr_duplicate_filename)
- return;
+ if (!stderr_duplicate_filename) return;
FILE *stderr_duplicate_stream =
freopen(stderr_duplicate_filename, "a+", stderr);
if (!stderr_duplicate_stream) {
+
fprintf(
stderr,
"Failed to duplicate stderr to AFL_DRIVER_STDERR_DUPLICATE_FILENAME");
abort();
+
}
+
output_file = stderr_duplicate_stream;
+
}
// Most of these I/O functions were inspired by/copied from libFuzzer's code.
static void discard_output(int fd) {
+
FILE *temp = fopen("/dev/null", "w");
- if (!temp)
- abort();
+ if (!temp) abort();
dup2(fileno(temp), fd);
fclose(temp);
+
}
-static void close_stdout() { discard_output(STDOUT_FILENO); }
+static void close_stdout() {
+
+ discard_output(STDOUT_FILENO);
+
+}
// Prevent the targeted code from writing to "stderr" but allow sanitizers and
// this driver to do so.
static void dup_and_close_stderr() {
+
int output_fileno = fileno(output_file);
int output_fd = dup(output_fileno);
- if (output_fd <= 0)
- abort();
+ if (output_fd <= 0) abort();
FILE *new_output_file = fdopen(output_fd, "w");
- if (!new_output_file)
- abort();
- if (!__sanitizer_set_report_fd)
- return;
- __sanitizer_set_report_fd(reinterpret_cast<void *>(output_fd));
+ if (!new_output_file) abort();
+ if (!__sanitizer_set_report_fd) return;
+ __sanitizer_set_report_fd((void *)output_fd);
discard_output(output_fileno);
-}
-static void Printf(const char *Fmt, ...) {
- va_list ap;
- va_start(ap, Fmt);
- vfprintf(output_file, Fmt, ap);
- va_end(ap);
- fflush(output_file);
}
// Close stdout and/or stderr if user asks for it.
static void maybe_close_fd_mask() {
+
char *fd_mask_str = getenv("AFL_DRIVER_CLOSE_FD_MASK");
- if (!fd_mask_str)
- return;
+ if (!fd_mask_str) return;
int fd_mask = atoi(fd_mask_str);
- if (fd_mask & 2)
- dup_and_close_stderr();
- if (fd_mask & 1)
- close_stdout();
+ if (fd_mask & 2) dup_and_close_stderr();
+ if (fd_mask & 1) close_stdout();
+
}
// Define LLVMFuzzerMutate to avoid link failures for targets that use it
// with libFuzzer's LLVMFuzzerCustomMutator.
-extern "C" size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
- assert(false && "LLVMFuzzerMutate should not be called from afl_driver");
+size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
+
+ // assert(false && "LLVMFuzzerMutate should not be called from afl_driver");
return 0;
+
}
// Execute any files provided as parameters.
static int ExecuteFilesOnyByOne(int argc, char **argv) {
+
+ unsigned char *buf = malloc(MAX_FILE);
for (int i = 1; i < argc; i++) {
- std::ifstream in(argv[i], std::ios::binary);
- in.seekg(0, in.end);
- size_t length = in.tellg();
- in.seekg (0, in.beg);
- std::cout << "Reading " << length << " bytes from " << argv[i] << std::endl;
- // Allocate exactly length bytes so that we reliably catch buffer overflows.
- std::vector<char> bytes(length);
- in.read(bytes.data(), bytes.size());
- assert(in);
- LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t *>(bytes.data()),
- bytes.size());
- std::cout << "Execution successful" << std::endl;
+
+ int fd = open(argv[i], O_RDONLY);
+ if (fd == -1) continue;
+ ssize_t length = read(fd, buf, MAX_FILE);
+ if (length > 0) {
+
+ printf("Reading %zu bytes from %s\n", length, argv[i]);
+ LLVMFuzzerTestOneInput(buf, length);
+ printf("Execution successful.\n");
+
+ }
+
}
+
+ free(buf);
return 0;
+
}
int main(int argc, char **argv) {
- Printf(
+
+ printf(
"======================= INFO =========================\n"
"This binary is built for AFL-fuzz.\n"
"To run the target function on individual input(s) execute this:\n"
@@ -242,32 +252,39 @@ int main(int argc, char **argv) {
"afl-fuzz will run N iterations before "
"re-spawning the process (default: 1000)\n"
"======================================================\n",
- argv[0], argv[0], argv[0]);
+ argv[0], argv[0], argv[0]);
+ output_file = stderr;
maybe_duplicate_stderr();
maybe_close_fd_mask();
- if (LLVMFuzzerInitialize)
- LLVMFuzzerInitialize(&argc, &argv);
+ if (LLVMFuzzerInitialize) LLVMFuzzerInitialize(&argc, &argv);
+
// Do any other expensive one-time initialization here.
- uint8_t dummy_input[1] = {0};
- int N = 100000;
+ uint8_t dummy_input[64] = {0};
+ memcpy(dummy_input, (void *)AFL_PERSISTENT, sizeof(AFL_PERSISTENT));
+ memcpy(dummy_input + 32, (void *)AFL_DEFER_FORKSVR,
+ sizeof(AFL_DEFER_FORKSVR));
+ int N = INT_MAX;
if (argc == 2 && argv[1][0] == '-')
- N = atoi(argv[1] + 1);
- else if(argc == 2 && (N = atoi(argv[1])) > 0)
- Printf("WARNING: using the deprecated call style `%s %d`\n", argv[0], N);
+ N = atoi(argv[1] + 1);
+ else if (argc == 2 && (N = atoi(argv[1])) > 0)
+ printf("WARNING: using the deprecated call style `%s %d`\n", argv[0], N);
else if (argc > 1) {
-// if (!getenv("AFL_DRIVER_DONT_DEFER")) {
- __afl_sharedmem_fuzzing = 0;
- __afl_manual_init();
-// }
+
+ // if (!getenv("AFL_DRIVER_DONT_DEFER")) {
+
+ __afl_sharedmem_fuzzing = 0;
+ __afl_manual_init();
+ // }
return ExecuteFilesOnyByOne(argc, argv);
exit(0);
+
}
assert(N > 0);
-// if (!getenv("AFL_DRIVER_DONT_DEFER"))
+ // if (!getenv("AFL_DRIVER_DONT_DEFER"))
__afl_manual_init();
// Call LLVMFuzzerTestOneInput here so that coverage caused by initialization
@@ -276,17 +293,26 @@ int main(int argc, char **argv) {
int num_runs = 0;
while (__afl_persistent_loop(N)) {
+
#ifdef _DEBUG
- fprintf(stderr, "CLIENT crc: %016llx len: %u\n", hash64(__afl_fuzz_ptr, *__afl_fuzz_len, 0xa5b35705), *__afl_fuzz_len);
+ fprintf(stderr, "CLIENT crc: %016llx len: %u\n",
+ hash64(__afl_fuzz_ptr, *__afl_fuzz_len, 0xa5b35705),
+ *__afl_fuzz_len);
fprintf(stderr, "RECV:");
for (int i = 0; i < *__afl_fuzz_len; i++)
fprintf(stderr, "%02x", __afl_fuzz_ptr[i]);
- fprintf(stderr,"\n");
+ fprintf(stderr, "\n");
#endif
if (*__afl_fuzz_len) {
+
num_runs++;
LLVMFuzzerTestOneInput(__afl_fuzz_ptr, *__afl_fuzz_len);
+
}
+
}
- Printf("%s: successfully executed %d input(s)\n", argv[0], num_runs);
+
+ printf("%s: successfully executed %d input(s)\n", argv[0], num_runs);
+
}
+
diff --git a/examples/aflpp_driver/aflpp_driver_test.c b/examples/aflpp_driver/aflpp_driver_test.c
new file mode 100644
index 00000000..e4567bbf
--- /dev/null
+++ b/examples/aflpp_driver/aflpp_driver_test.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#include "hash.h"
+
+int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+
+ fprintf(stderr, "FUNC crc: %016llx len: %lu\n",
+ hash64((u8 *)Data, (unsigned int)Size,
+ (unsigned long long int)0xa5b35705),
+ Size);
+
+ if (Size < 5) return 0;
+
+ if (Data[0] == 'F')
+ if (Data[1] == 'A')
+ if (Data[2] == '$')
+ if (Data[3] == '$')
+ if (Data[4] == '$') abort();
+
+ return 0;
+
+}
+
diff --git a/examples/aflpp_driver/aflpp_driver_test.cpp b/examples/aflpp_driver/aflpp_driver_test.cpp
deleted file mode 100644
index 13dc09b9..00000000
--- a/examples/aflpp_driver/aflpp_driver_test.cpp
+++ /dev/null
@@ -1,22 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include "hash.h"
-
-extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
-
- fprintf(stderr, "FUNC crc: %016llx len: %lu\n", hash64((u8*)Data, (unsigned int) Size, (unsigned long long int) 0xa5b35705), Size);
-
- if (Size < 5)
- return 0;
-
- if (Data[0] == 'F')
- if (Data[1] == 'A')
- if (Data[2] == '$')
- if (Data[3] == '$')
- if (Data[4] == '$')
- abort();
-
- return 0;
-
-}
diff --git a/examples/custom_mutators/post_library_gif.so.c b/examples/custom_mutators/post_library_gif.so.c
index 9b76ead5..2d72400c 100644
--- a/examples/custom_mutators/post_library_gif.so.c
+++ b/examples/custom_mutators/post_library_gif.so.c
@@ -83,7 +83,7 @@ typedef struct post_state {
} post_state_t;
-void *afl afl_custom_init(void *afl) {
+void *afl_custom_init(void *afl) {
post_state_t *state = malloc(sizeof(post_state_t));
if (!state) {
diff --git a/examples/defork/Makefile b/examples/defork/Makefile
new file mode 100644
index 00000000..e8240dba
--- /dev/null
+++ b/examples/defork/Makefile
@@ -0,0 +1,64 @@
+#
+# american fuzzy lop++ - defork
+# ----------------------------------
+#
+# 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
+
+PREFIX ?= /usr/local
+BIN_PATH = $(PREFIX)/bin
+HELPER_PATH = $(PREFIX)/lib/afl
+
+CFLAGS = -fPIC -Wall -Wextra
+LDFLAGS = -shared
+
+UNAME_SAYS_LINUX=$(shell uname | grep -E '^Linux|^GNU' >/dev/null; echo $$?)
+UNAME_SAYS_LINUX:sh=uname | grep -E '^Linux|^GNU' >/dev/null; echo $$?
+
+_LDFLAGS_ADD=$(UNAME_SAYS_LINUX:1=)
+LDFLAGS_ADD=$(_LDFLAGS_ADD:0=-ldl)
+LDFLAGS += $(LDFLAGS_ADD)
+
+# on gcc for arm there is no -m32, but -mbe32
+M32FLAG = -m32
+M64FLAG = -m64
+
+CC_IS_GCC=$(shell $(CC) --version 2>/dev/null | grep -q gcc; echo $$?)
+CC_IS_GCC:sh=$(CC) --version 2>/dev/null | grep -q gcc; echo $$?
+CC_IS_ARMCOMPILER=$(shell $(CC) -v 2>&1 >/dev/null | grep -q arm; echo $$?)
+CC_IS_ARMCOMPILER:sh=$(CC) -v 2>&1 >/dev/null | grep -q arm; echo $$?
+
+_M32FLAG=$(CC_IS_GCC)$(CC_IS_ARMCOMPILER)
+__M32FLAG=$(_M32FLAG:00=-mbe32)
+___M32FLAG=$(__M32FLAG:$(CC_IS_GCC)$(CC_IS_ARMCOMPILER)=-m32)
+M32FLAG=$(___M32FLAG)
+#ifeq "$(findstring clang, $(shell $(CC) --version 2>/dev/null))" ""
+# ifneq (,$(findstring arm, "$(shell $(CC) -v 2>&1 >/dev/null)"))
+# M32FLAG = -mbe32
+# endif
+#endif
+
+all: defork32.so defork64.so
+
+defork32.so: defork.c
+ -@$(CC) $(M32FLAG) $(CFLAGS) $^ $(LDFLAGS) -o $@ 2>/dev/null || echo "defork32 build failure (that's fine)"
+
+defork64.so: defork.c
+ -@$(CC) $(M64FLAG) $(CFLAGS) $^ $(LDFLAGS) -o $@ 2>/dev/null || echo "defork64 build failure (that's fine)"
+
+install: defork32.so defork64.so
+ install -d -m 755 $(DESTDIR)$(HELPER_PATH)/
+ if [ -f defork32.so ]; then set -e; install -m 755 defork32.so $(DESTDIR)$(HELPER_PATH)/; fi
+ if [ -f defork64.so ]; then set -e; install -m 755 defork64.so $(DESTDIR)$(HELPER_PATH)/; fi
+
+target:
+ ../../afl-clang forking_target.c -o forking_target -Wall -Wextra -Werror
+
+clean:
+ rm -f defork32.so defork64.so forking_target
diff --git a/examples/defork/README.md b/examples/defork/README.md
new file mode 100644
index 00000000..7e950323
--- /dev/null
+++ b/examples/defork/README.md
@@ -0,0 +1,11 @@
+# defork
+
+when the target forks, this breaks all normal fuzzing runs.
+Sometimes, though, it is enough to just run the child process.
+If this is the case, then this LD_PRELOAD library will always return 0 on fork,
+the target will belive it is running as the child, post-fork.
+
+This is defork.c from the amazing preeny project
+https://github.com/zardus/preeny
+
+It is altered for afl++ to work with its fork-server: the initial fork will go through, the second fork will be blocked.
diff --git a/examples/defork/defork.c b/examples/defork/defork.c
new file mode 100644
index 00000000..f71d1124
--- /dev/null
+++ b/examples/defork/defork.c
@@ -0,0 +1,50 @@
+#define __GNU_SOURCE
+#include <dlfcn.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+#include "../../include/config.h"
+
+/* we want to fork once (for the afl++ forkserver),
+ then immediately return as child on subsequent forks. */
+static bool forked = 0;
+
+pid_t (*original_fork)(void);
+
+/* In case we are not running in afl, we use a dummy original_fork */
+static pid_t nop(void) {
+
+ return 0;
+
+}
+
+__attribute__((constructor)) void preeny_fork_orig() {
+
+ if (getenv(SHM_ENV_VAR)) {
+
+ printf("defork: running in AFL++. Allowing forkserver.\n");
+ original_fork = dlsym(RTLD_NEXT, "socket");
+
+ } else {
+
+ printf("defork: no AFL++ detected. Disabling fork from the start.\n");
+ original_fork = &nop;
+
+ }
+
+}
+
+pid_t fork(void) {
+
+ /* If we forked before, or if we're in the child (pid==0),
+ we don't want to fork anymore, else, we are still in the forkserver.
+ The forkserver parent needs to fork infinite times, each child should never
+ fork again. This can be written without branches and I hate myself for it.
+ */
+ pid_t ret = !forked && original_fork();
+ forked = !ret;
+ return ret;
+
+}
+
diff --git a/examples/defork/forking_target.c b/examples/defork/forking_target.c
new file mode 100644
index 00000000..98f6365a
--- /dev/null
+++ b/examples/defork/forking_target.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+/* This is an example target for defork.c - fuzz using
+```
+mkdir in; echo a > ./in/a
+AFL_PRELOAD=./defork64.so ../../afl-fuzz -i in -o out -- ./forking_target @@
+```
+*/
+
+int main(int argc, char **argv) {
+
+ if (argc < 2) {
+
+ printf("Example tool to test defork.\nUsage ./forking_target <input>\n");
+ return -1;
+
+ }
+
+ pid_t pid = fork();
+ if (pid == 0) {
+
+ printf("We're in the child.\n");
+ FILE *f = fopen(argv[1], "r");
+ char buf[4096];
+ fread(buf, 1, 4096, f);
+ uint32_t offset = buf[100] + (buf[101] << 8);
+ char test_val = buf[offset];
+ return test_val < 100;
+
+ } else if (pid < 0) {
+
+ perror("fork");
+ return -1;
+
+ } else {
+
+ printf("We are in the parent - defork didn't work! :( (pid=%d)\n",
+ (int)pid);
+
+ }
+
+ return 0;
+
+}
+
diff --git a/examples/persistent_demo/persistent_demo_new.c b/examples/persistent_demo/persistent_demo_new.c
index e4e328b0..7f878c0c 100644
--- a/examples/persistent_demo/persistent_demo_new.c
+++ b/examples/persistent_demo/persistent_demo_new.c
@@ -28,6 +28,20 @@
#include <signal.h>
#include <string.h>
+/* this lets the source compile without afl-clang-fast/lto */
+#ifndef __AFL_FUZZ_TESTCASE_LEN
+
+ssize_t fuzz_len;
+unsigned char fuzz_buf[1024000];
+
+ #define __AFL_FUZZ_TESTCASE_LEN fuzz_len
+ #define __AFL_FUZZ_TESTCASE_BUF fuzz_buf
+ #define __AFL_FUZZ_INIT() void sync(void);
+ #define __AFL_LOOP(x) ((fuzz_len = read(0, fuzz_buf, sizeof(fuzz_buf))) > 0 ?
+ #define __AFL_INIT() sync()
+
+#endif
+
__AFL_FUZZ_INIT();
/* Main entry point. */