about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrea Fioraldi <andreafioraldi@gmail.com>2019-11-17 11:39:21 +0100
committerAndrea Fioraldi <andreafioraldi@gmail.com>2019-11-17 11:39:21 +0100
commit7e022a09ccd88cf91cfafbc3d61cdb93b961bbe7 (patch)
tree647c7d7ec9c0614b5fb82449e5904f641cd2eb87
parent0071e537f3aa1ccb6dbc38a2e88bd53595021775 (diff)
downloadafl++-7e022a09ccd88cf91cfafbc3d61cdb93b961bbe7.tar.gz
adjust qbdi mode
-rw-r--r--include/config.h2
-rwxr-xr-xqbdi_mode/README.md130
-rwxr-xr-xqbdi_mode/assets/image-20191114222336674.pngbin170505 -> 0 bytes
-rw-r--r--qbdi_mode/assets/screen1.pngbin0 -> 88333 bytes
-rwxr-xr-xqbdi_mode/build.sh28
-rwxr-xr-xqbdi_mode/template.cpp48
6 files changed, 123 insertions, 85 deletions
diff --git a/include/config.h b/include/config.h
index 144d3810..edfaf9ad 100644
--- a/include/config.h
+++ b/include/config.h
@@ -41,7 +41,9 @@
 
 /* Comment out to disable fancy ANSI boxes and use poor man's 7-bit UI: */
 
+#ifndef ANDROID_DISABLE_FANCY // Fancy boxes are ugly from adb
 #define FANCY_BOXES
+#endif
 
 /* Default timeout for fuzzed code (milliseconds). This is the upper bound,
    also used for detecting hangs; the actual value is auto-scaled: */
diff --git a/qbdi_mode/README.md b/qbdi_mode/README.md
index 8891d99e..49b4ecfb 100755
--- a/qbdi_mode/README.md
+++ b/qbdi_mode/README.md
@@ -30,6 +30,16 @@ For x86 standalone-toolchain
 ./build/tools/make_standalone_toolchain.py --arch x86 --api 21 --install-dir ../android-standalone-toolchain-x86
 ```
 
+In alternative you can also use the prebuilt toolchain, in that case make sure to set the proper CC and CXX env variables because there are many different compilers for each API version in the prebuilt toolchain.
+
+For example:
+
+```
+export STANDALONE_TOOLCHAIN_PATH=~/Android/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/linux-x86_64/
+export CC=x86_64-linux-android21-clang
+export CXX=x86_64-linux-android21-clang++
+```
+
 Then download the QBDI SDK from website
 
 ```
@@ -76,35 +86,40 @@ this could build the afl-fuzz and also the qbdi template for android x86_64
 The demo-so.c is an vulnerable library, it has a function for test
 
 ```
-int target_func(char *buf, int size)
-{
-    printf("buffer:%p, size:%p\n", buf, size);
-    switch (buf[0])
-    {
+int target_func(char *buf, int size) {
+
+  printf("buffer:%p, size:%p\n", buf, size);
+  switch (buf[0]) {
+
     case 1:
-        puts("222");
-        if (buf[1] == '\x44')
-        {
-            puts("null ptr deference");
-            *(char *)(0) = 1;
-        }
-        break;
+      puts("222");
+      if (buf[1] == '\x44') {
+
+        puts("null ptr deference");
+        *(char *)(0) = 1;
+
+      }
+
+      break;
     case 0xff:
-        if (buf[2] == '\xff')
-        {
-            if (buf[1] == '\x44')
-            {
-                puts("crash....");
-                *(char *)(0xdeadbeef) = 1;
-            }
+      if (buf[2] == '\xff') {
+
+        if (buf[1] == '\x44') {
+
+          puts("crash....");
+          *(char *)(0xdeadbeef) = 1;
+
         }
-        break;
-    default:
-        puts("default action");
-        break;
-    }
 
-    return 1;
+      }
+
+      break;
+    default: puts("default action"); break;
+
+  }
+
+  return 1;
+
 }
 ```
 
@@ -122,24 +137,18 @@ Then we should load the library in template.cpp and find the `target` function a
 then we read the data from file and call the function in `fuzz_func`
 
 ```
-QBDI_NOINLINE int fuzz_func()
-{
-	// afl forkserver stuff
-    if (afl_setup())
-    {
-        afl_forkserver();
-    }
+QBDI_NOINLINE int fuzz_func() {
 
-	// read the data from file(argv[2])
-    unsigned long len = 0;
-    char *data = read_file(FPATH, &len);
+  if (afl_setup()) { afl_forkserver(); }
 
+  /* Read the input from file */
+  unsigned long len = 0;
+  char *        data = read_file(input_pathname, &len);
 
-    printf("In fuzz_func\n");
+  /* Call the target function with the input data */
+  p_target_func(data, len);
+  return 1;
 
-	// call the target function with input data.
-    p_target_func(data, len);
-    return 1;
 }
 ```
 
@@ -159,39 +168,32 @@ adb push ../../android-standalone-toolchain-x86_64/sysroot/usr/lib/x86_64-linux-
 /data/local/tmp
 ```
 
-In android adb shell, we could try to run the loader
+In android adb shell, run the loader to test if it runs
 ```
+cd /data/local/tmp
 export LD_LIBRARY_PATH=/data/local/tmp
-./loader /data/local/tmp/libdemo.so init
-```
-the normal output like
-
-```
-# ./loader /data/local/tmp/libdemo.so init                                        p_target_func:0x7b41ac26e600
-In fuzz_func
-        offset:0x600
-        offset:0x580
-buffer:0x7b41abe2b050, size:0x4
-        offset:0x628
-        offset:0x646
-        offset:0x64b
-        offset:0x65c
-        offset:0x6df
-        offset:0x590
+mkdir in
+echo 0000 > in/1
+./loader libdemo.so in/1
+p_target_func:0x716d96a98600
+	offset:0x600
+	offset:0x580
+buffer:0x716d96609050, size:0x5
+	offset:0x628
+	offset:0x646
+	offset:0x64b
+	offset:0x65c
+	offset:0x6df
+	offset:0x590
 default action
-        offset:0x6eb
+	offset:0x6eb
 ```
 
-now run `afl-fuzz` to fuzz the library
+Now run `afl-fuzz` to fuzz the demo library
 
 ```
-mkdir in
-echo xxxx > in/1
 ./afl-fuzz -i in -o out -- ./loader /data/local/tmp/libdemo.so @@
 ```
 
-the snapshot
-
-![image-20191114222336674](assets/image-20191114222336674.png)
+![screen1](assets/screen1.png)
 
-good job.
\ No newline at end of file
diff --git a/qbdi_mode/assets/image-20191114222336674.png b/qbdi_mode/assets/image-20191114222336674.png
deleted file mode 100755
index 7df805c9..00000000
--- a/qbdi_mode/assets/image-20191114222336674.png
+++ /dev/null
Binary files differdiff --git a/qbdi_mode/assets/screen1.png b/qbdi_mode/assets/screen1.png
new file mode 100644
index 00000000..3cf1cb76
--- /dev/null
+++ b/qbdi_mode/assets/screen1.png
Binary files differdiff --git a/qbdi_mode/build.sh b/qbdi_mode/build.sh
index 7ac4c75d..c2912e94 100755
--- a/qbdi_mode/build.sh
+++ b/qbdi_mode/build.sh
@@ -1,3 +1,4 @@
+
 if [ -z ${STANDALONE_TOOLCHAIN_PATH} ]; then
     echo "please set the android-standalone-toolchain path in STANDALONE_TOOLCHAIN_PATH environmental variable" 
     echo "for example: "
@@ -16,10 +17,22 @@ fi
 
 if [ "$1" = "x86" ]; then
   echo "build x86 qbdi"
-  compiler_prefix="${STANDALONE_TOOLCHAIN_PATH}/bin/i686-linux-android-"
+  compiler_prefix="${STANDALONE_TOOLCHAIN_PATH}/bin/"
+  if [ -z ${CC} ]; then
+      export CC=i686-linux-android-gcc
+  fi
+  if [ -z ${CXX} ]; then
+      export CXX=i686-linux-android-g++
+  fi
 elif [ "$1" = "x86_64" ]; then
     echo "build x86_64 qbdi"
-    compiler_prefix="${STANDALONE_TOOLCHAIN_PATH}/bin/x86_64-linux-android-"
+    compiler_prefix="${STANDALONE_TOOLCHAIN_PATH}/bin/"
+    if [ -z ${CC} ]; then
+        export CC=x86_64-linux-android-gcc
+    fi
+    if [ -z ${CXX} ]; then
+        export CXX=x86_64-linux-android-g++
+    fi
 else
     echo "usage: ./build.sh arch[x86, x86_64]"
     exit
@@ -28,12 +41,17 @@ fi
 
 CFLAGS="-I${QBDI_SDK_PATH}/usr/local/include/ -L${QBDI_SDK_PATH}/usr/local/lib/"
 
+echo "[+] Building the QBDI template"
 # build the qbdi template 
-${compiler_prefix}g++ -o loader template.cpp -lQBDI -ldl -w  -g ${CFLAGS}
+${compiler_prefix}${CXX} -o loader template.cpp -lQBDI -ldl -w  -g ${CFLAGS}
 
+echo "[+] Building the demo library"
 # build the demo share library
-${compiler_prefix}gcc -shared -o libdemo.so demo-so.c -w -g
+${compiler_prefix}${CC} -shared -o libdemo.so demo-so.c -w -g
 
+echo "[+] Building afl-fuzz for Android"
 # build afl-fuzz
 cd ..
-${compiler_prefix}gcc -O3 -funroll-loops -Wall -D_FORTIFY_SOURCE=2 -g -Wno-pointer-sign -I include/ -DAFL_PATH=\"/usr/local/lib/afl\" -DBIN_PATH=\"/usr/local/bin\" -DDOC_PATH=\"/usr/local/share/doc/afl\" -Wno-unused-function src/afl-fuzz-misc.c src/afl-fuzz-extras.c src/afl-fuzz-queue.c src/afl-fuzz-one.c src/afl-fuzz-python.c src/afl-fuzz-stats.c src/afl-fuzz-init.c src/afl-fuzz.c src/afl-fuzz-bitmap.c src/afl-fuzz-run.c src/afl-fuzz-globals.c src/afl-common.c src/afl-sharedmem.c src/afl-forkserver.c -o qbdi_mode/afl-fuzz  -ldl -w
+${compiler_prefix}${CC} -DANDROID_DISABLE_FANCY=1 -O3 -funroll-loops -Wall -D_FORTIFY_SOURCE=2 -g -Wno-pointer-sign -I include/ -DAFL_PATH=\"/usr/local/lib/afl\" -DBIN_PATH=\"/usr/local/bin\" -DDOC_PATH=\"/usr/local/share/doc/afl\" -Wno-unused-function src/afl-fuzz-misc.c src/afl-fuzz-extras.c src/afl-fuzz-queue.c src/afl-fuzz-one.c src/afl-fuzz-python.c src/afl-fuzz-stats.c src/afl-fuzz-init.c src/afl-fuzz.c src/afl-fuzz-bitmap.c src/afl-fuzz-run.c src/afl-fuzz-globals.c src/afl-common.c src/afl-sharedmem.c src/afl-forkserver.c -o qbdi_mode/afl-fuzz  -ldl -w
+
+echo "[+] All done. Enjoy!"
diff --git a/qbdi_mode/template.cpp b/qbdi_mode/template.cpp
index 601860d2..18766d31 100755
--- a/qbdi_mode/template.cpp
+++ b/qbdi_mode/template.cpp
@@ -20,6 +20,20 @@
 
 #include <QBDI.h>
 
+/* NeverZero */
+
+#if (defined(__x86_64__) || defined(__i386__)) && defined(AFL_QEMU_NOT_ZERO)
+#define INC_AFL_AREA(loc)           \
+  asm volatile(                     \
+      "incb (%0, %1, 1)\n"          \
+      "adcb $0, (%0, %1, 1)\n"      \
+      : /* no out */                \
+      : "r"(afl_area_ptr), "r"(loc) \
+      : "memory", "eax")
+#else
+#define INC_AFL_AREA(loc) afl_area_ptr[loc]++
+#endif
+
 using namespace QBDI;
 
 typedef int (*target_func)(char *buf, int size);
@@ -35,6 +49,8 @@ unsigned char *afl_area_ptr = NULL;           /* Exported for afl_gen_trace */
 
 unsigned long afl_prev_loc = 0;
 
+char input_pathname[PATH_MAX];
+
 /* Set up SHM region and initialize other stuff. */
 
 int afl_setup(void) {
@@ -98,36 +114,37 @@ void afl_maybe_log(unsigned long cur_loc) {
 
   if (afl_area_ptr == NULL) { return; }
   unsigned long afl_idx = cur_loc ^ afl_prev_loc;
-  afl_area_ptr[afl_idx % MAP_SIZE]++;
+  afl_idx &= MAP_SIZE -1;
+  INC_AFL_AREA(afl_idx);
   afl_prev_loc = cur_loc >> 1;
 
 }
 
 char *read_file(char *path, unsigned long *length) {
 
-  FILE *pFile = fopen(path, "rb");
-  char *pBuf;
-  fseek(pFile, 0, SEEK_END);
-  unsigned long len = ftell(pFile);
-  pBuf = (char *)malloc(len);
-  rewind(pFile);
-  fread(pBuf, 1, len, pFile);
-  fclose(pFile);
+  unsigned long len;
+  char * buf;
+
+  FILE *fp = fopen(path, "rb");
+  fseek(fp, 0, SEEK_END);
+  len = ftell(fp);
+  buf = (char *)malloc(len);
+  rewind(fp);
+  fread(buf, 1, len, fp);
+  fclose(fp);
   *length = len;
-  return pBuf;
+  return buf;
 
 }
 
-char FPATH[200];
-
 QBDI_NOINLINE int fuzz_func() {
 
   if (afl_setup()) { afl_forkserver(); }
 
   unsigned long len = 0;
-  char *        data = read_file(FPATH, &len);
+  char *        data = read_file(input_pathname, &len);
 
-  printf("In fuzz_func\n");
+  // printf("In fuzz_func\n");
   p_target_func(data, len);
   return 1;
 
@@ -172,8 +189,7 @@ int main(int argc, char **argv) {
 
   const char *lib_path;
   lib_path = argv[1];
-  // FPATH = argv[2];
-  strcpy(FPATH, argv[2]);
+  strcpy(input_pathname, argv[2]);
   void *handle = dlopen(lib_path, RTLD_LAZY);
 
   if (handle == nullptr) {