|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# QEMU build script for Eclipser's instrumentation |
| 4 | +# |
| 5 | +# Modified codes from AFL's QEMU mode (original license below). |
| 6 | +# -------------------------------------- |
| 7 | +# |
| 8 | +# Written by Andrew Griffiths <agriffiths@google.com> and |
| 9 | +# Michal Zalewski <lcamtuf@google.com> |
| 10 | +# |
| 11 | +# Copyright 2015, 2016 Google Inc. All rights reserved. |
| 12 | +# |
| 13 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | +# you may not use this file except in compliance with the License. |
| 15 | +# You may obtain a copy of the License at: |
| 16 | +# |
| 17 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | + |
| 19 | +build_qemu () { |
| 20 | + if [ $2 = "x86" ]; then |
| 21 | + CPU_TARGET="i386" |
| 22 | + elif [ $2 = "x64" ]; then |
| 23 | + CPU_TARGET="x86_64" |
| 24 | + else |
| 25 | + echo "Invalid CPU architecture provided" |
| 26 | + exit 0 |
| 27 | + fi |
| 28 | + |
| 29 | + echo "[*] Configuring QEMU for $CPU_TARGET..." |
| 30 | + |
| 31 | + cd qemu-2.3.0-$1-$2 || exit 1 |
| 32 | + |
| 33 | + CFLAGS="-O3" ./configure --disable-system --enable-linux-user \ |
| 34 | + --enable-guest-base --disable-gtk --disable-sdl --disable-vnc \ |
| 35 | + --target-list="${CPU_TARGET}-linux-user" || exit 1 |
| 36 | + |
| 37 | + echo "[+] Configuration complete." |
| 38 | + |
| 39 | + echo "[*] Attempting to build QEMU (fingers crossed!)..." |
| 40 | + |
| 41 | + make || exit 1 |
| 42 | + |
| 43 | + echo "[+] Build process successful!" |
| 44 | + |
| 45 | + echo "[*] Copying binary..." |
| 46 | + cp -f "${CPU_TARGET}-linux-user/qemu-${CPU_TARGET}" "../qemu-trace" || exit 1 |
| 47 | + cd .. |
| 48 | +} |
| 49 | + |
| 50 | +QEMU_URL="https://download.qemu.org/qemu-2.3.0.tar.bz2" |
| 51 | +QEMU_SHA384="7a0f0c900f7e2048463cc32ff3e904965ab466c8428847400a0f2dcfe458108a68012c4fddb2a7e7c822b4fd1a49639b" |
| 52 | + |
| 53 | +echo "=========================================" |
| 54 | +echo "Chatkey instrumentation QEMU build script" |
| 55 | +echo "=========================================" |
| 56 | +echo |
| 57 | + |
| 58 | +echo "[*] Performing basic sanity checks..." |
| 59 | + |
| 60 | +if [ ! "`uname -s`" = "Linux" ]; then |
| 61 | + |
| 62 | + echo "[-] Error: QEMU instrumentation is supported only on Linux." |
| 63 | + exit 1 |
| 64 | + |
| 65 | +fi |
| 66 | + |
| 67 | +if [ ! -f "patches-pathcov/chatkey.cc" -o ! -f "patches-syscall/chatkey.c" -o ! -f "patches-feedback/chatkey.c" ]; then |
| 68 | + |
| 69 | + echo "[-] Error: key files not found - wrong working directory?" |
| 70 | + exit 1 |
| 71 | + |
| 72 | +fi |
| 73 | + |
| 74 | +for i in libtool wget python automake autoconf sha384sum bison iconv; do |
| 75 | + |
| 76 | + T=`which "$i" 2>/dev/null` |
| 77 | + |
| 78 | + if [ "$T" = "" ]; then |
| 79 | + |
| 80 | + echo "[-] Error: '$i' not found, please install first." |
| 81 | + exit 1 |
| 82 | + |
| 83 | + fi |
| 84 | + |
| 85 | +done |
| 86 | + |
| 87 | +if [ ! -d "/usr/include/glib-2.0/" -a ! -d "/usr/local/include/glib-2.0/" ]; then |
| 88 | + |
| 89 | + echo "[-] Error: devel version of 'glib2' not found, please install first." |
| 90 | + exit 1 |
| 91 | + |
| 92 | +fi |
| 93 | + |
| 94 | +echo "[+] All checks passed!" |
| 95 | + |
| 96 | +ARCHIVE="`basename -- "$QEMU_URL"`" |
| 97 | + |
| 98 | +CKSUM=`sha384sum -- "$ARCHIVE" 2>/dev/null | cut -d' ' -f1` |
| 99 | + |
| 100 | +if [ ! "$CKSUM" = "$QEMU_SHA384" ]; then |
| 101 | + |
| 102 | + echo "[*] Downloading QEMU 2.3.0 from the web..." |
| 103 | + rm -f "$ARCHIVE" |
| 104 | + wget -O "$ARCHIVE" -- "$QEMU_URL" || exit 1 |
| 105 | + |
| 106 | + CKSUM=`sha384sum -- "$ARCHIVE" 2>/dev/null | cut -d' ' -f1` |
| 107 | + |
| 108 | +fi |
| 109 | + |
| 110 | +if [ "$CKSUM" = "$QEMU_SHA384" ]; then |
| 111 | + |
| 112 | + echo "[+] Cryptographic signature on $ARCHIVE checks out." |
| 113 | + |
| 114 | +else |
| 115 | + |
| 116 | + echo "[-] Error: signature mismatch on $ARCHIVE (perhaps download error?)." |
| 117 | + exit 1 |
| 118 | + |
| 119 | +fi |
| 120 | + |
| 121 | +echo "[*] Uncompressing archive (this will take a while)..." |
| 122 | + |
| 123 | +rm -rf "qemu-2.3.0" || exit 1 |
| 124 | +rm -rf "qemu-2.3.0-pathcov" || exit 1 |
| 125 | +rm -rf "qemu-2.3.0-syscall" || exit 1 |
| 126 | +rm -rf "qemu-2.3.0-feedback" || exit 1 |
| 127 | +rm -rf "qemu-2.3.0-bbcount" || exit 1 |
| 128 | +rm -rf "qemu-2.3.0-pathcov-x86" || exit 1 |
| 129 | +rm -rf "qemu-2.3.0-pathcov-x64" || exit 1 |
| 130 | +rm -rf "qemu-2.3.0-syscall-x86" || exit 1 |
| 131 | +rm -rf "qemu-2.3.0-syscall-x64" || exit 1 |
| 132 | +rm -rf "qemu-2.3.0-feedback-x86" || exit 1 |
| 133 | +rm -rf "qemu-2.3.0-feedback-x64" || exit 1 |
| 134 | +rm -rf "qemu-2.3.0-bbcount-x86" || exit 1 |
| 135 | +rm -rf "qemu-2.3.0-bbcount-x64" || exit 1 |
| 136 | +tar xf "$ARCHIVE" || exit 1 |
| 137 | + |
| 138 | +echo "[+] Unpacking successful." |
| 139 | + |
| 140 | +echo "[*] Backup target files of patches-common/ (for later use)" |
| 141 | +cp qemu-2.3.0/linux-user/elfload.c qemu-2.3.0/linux-user/elfload.c.orig |
| 142 | +cp qemu-2.3.0/linux-user/linuxload.c qemu-2.3.0/linux-user/linuxload.c.orig |
| 143 | +cp qemu-2.3.0/linux-user/signal.c qemu-2.3.0/linux-user/signal.c.orig |
| 144 | +cp qemu-2.3.0/translate-all.c qemu-2.3.0/translate-all.c.orig |
| 145 | +cp qemu-2.3.0/scripts/texi2pod.pl qemu-2.3.0/scripts/texi2pod.pl.orig |
| 146 | +cp qemu-2.3.0/user-exec.c qemu-2.3.0/user-exec.c.orig |
| 147 | + |
| 148 | +echo "[*] Applying common patches..." |
| 149 | +patch -p0 <patches-common/elfload.diff || exit 1 |
| 150 | +patch -p0 <patches-common/linuxload.diff || exit 1 |
| 151 | +patch -p0 <patches-common/signal.diff || exit 1 |
| 152 | +patch -p0 <patches-common/translate-all.diff || exit 1 |
| 153 | +patch -p0 <patches-common/texi2pod.diff || exit 1 |
| 154 | +patch -p0 <patches-common/user-exec.diff || exit 1 |
| 155 | + |
| 156 | +cp -r "qemu-2.3.0" "qemu-2.3.0-pathcov" |
| 157 | +cp -r "qemu-2.3.0" "qemu-2.3.0-syscall" |
| 158 | +cp -r "qemu-2.3.0" "qemu-2.3.0-feedback" |
| 159 | +cp -r "qemu-2.3.0" "qemu-2.3.0-bbcount" |
| 160 | + |
| 161 | +### Patch for pathcov tracer |
| 162 | + |
| 163 | +echo "[*] Applying patches for pathcov..." |
| 164 | + |
| 165 | +patch -p0 <patches-pathcov/syscall.diff || exit 1 |
| 166 | +patch -p0 <patches-pathcov/cpu-exec.diff || exit 1 |
| 167 | +patch -p0 <patches-pathcov/exec-all.diff || exit 1 |
| 168 | +patch -p0 <patches-pathcov/translate.diff || exit 1 |
| 169 | +patch -p0 <patches-pathcov/makefile-target.diff || exit 1 |
| 170 | +cp patches-pathcov/chatkey.cc qemu-2.3.0-pathcov/ |
| 171 | +cp patches-pathcov/afl-qemu-cpu-inl.h qemu-2.3.0-pathcov/ |
| 172 | +cp patches-pathcov/chatkey-utils.h qemu-2.3.0-pathcov/ |
| 173 | + |
| 174 | +echo "[+] Patching done." |
| 175 | + |
| 176 | +### Patch for syscall tracer |
| 177 | + |
| 178 | +echo "[*] Applying patches for syscall..." |
| 179 | + |
| 180 | +patch -p0 <patches-syscall/cpu-exec.diff || exit 1 |
| 181 | +patch -p0 <patches-syscall/syscall.diff || exit 1 |
| 182 | +patch -p0 <patches-syscall/makefile-objs.diff || exit 1 |
| 183 | +cp patches-syscall/chatkey.c qemu-2.3.0-syscall/linux-user/ |
| 184 | + |
| 185 | +echo "[+] Patching done." |
| 186 | + |
| 187 | +### Patch for feedback tracer |
| 188 | + |
| 189 | +echo "[*] Applying patches for feedback..." |
| 190 | + |
| 191 | +patch -p0 <patches-feedback/cpu-exec.diff || exit 1 |
| 192 | +patch -p0 <patches-feedback/syscall.diff || exit 1 |
| 193 | +patch -p0 <patches-feedback/makefile-target.diff || exit 1 |
| 194 | +patch -p0 <patches-feedback/translate.diff || exit 1 |
| 195 | +patch -p0 <patches-feedback/tcg-target.diff || exit 1 |
| 196 | +patch -p0 <patches-feedback/tcg-op.diff || exit 1 |
| 197 | +patch -p0 <patches-feedback/tcg-opc.diff || exit 1 |
| 198 | +patch -p0 <patches-feedback/tcg.diff || exit 1 |
| 199 | +patch -p0 <patches-feedback/optimize.diff || exit 1 |
| 200 | +cp patches-feedback/chatkey.c qemu-2.3.0-feedback/tcg/ |
| 201 | +cp patches-feedback/afl-qemu-cpu-inl.h qemu-2.3.0-feedback/ |
| 202 | + |
| 203 | +echo "[+] Patching done." |
| 204 | + |
| 205 | +### Patch for basic block count tracer |
| 206 | + |
| 207 | +echo "[*] Applying patches for bbcount..." |
| 208 | + |
| 209 | +patch -p0 <patches-bbcount/syscall.diff || exit 1 |
| 210 | +patch -p0 <patches-bbcount/cpu-exec.diff || exit 1 |
| 211 | +patch -p0 <patches-bbcount/makefile-target.diff || exit 1 |
| 212 | +patch -p0 <patches-bbcount/makefile-objs.diff || exit 1 |
| 213 | +patch -p0 <patches-bbcount/main.diff || exit 1 |
| 214 | +cp patches-bbcount/chatkey.cc qemu-2.3.0-bbcount/linux-user/ |
| 215 | +echo "[+] Patching done." |
| 216 | + |
| 217 | +### Copy directories, one for x86 and the other for x64 |
| 218 | + |
| 219 | +cp -r "qemu-2.3.0-pathcov" "qemu-2.3.0-pathcov-x86" |
| 220 | +mv "qemu-2.3.0-pathcov" "qemu-2.3.0-pathcov-x64" |
| 221 | +cp -r "qemu-2.3.0-syscall" "qemu-2.3.0-syscall-x86" |
| 222 | +mv "qemu-2.3.0-syscall" "qemu-2.3.0-syscall-x64" |
| 223 | +cp -r "qemu-2.3.0-feedback" "qemu-2.3.0-feedback-x86" |
| 224 | +mv "qemu-2.3.0-feedback" "qemu-2.3.0-feedback-x64" |
| 225 | +cp -r "qemu-2.3.0-bbcount" "qemu-2.3.0-bbcount-x86" |
| 226 | +mv "qemu-2.3.0-bbcount" "qemu-2.3.0-bbcount-x64" |
| 227 | + |
| 228 | +### Build QEMU tracers |
| 229 | + |
| 230 | +build_qemu pathcov x86 |
| 231 | +mv "./qemu-trace" "../../build/qemu-trace-pathcov-x86" || exit 1 |
| 232 | +echo "[+] Successfully created 'qemu-trace-pathcov-x86'." |
| 233 | + |
| 234 | +build_qemu pathcov x64 |
| 235 | +mv "./qemu-trace" "../../build/qemu-trace-pathcov-x64" || exit 1 |
| 236 | +echo "[+] Successfully created 'qemu-trace-pathcov-x64'." |
| 237 | + |
| 238 | +build_qemu syscall x86 |
| 239 | +mv "./qemu-trace" "../../build/qemu-trace-syscall-x86" || exit 1 |
| 240 | +echo "[+] Successfully created 'qemu-trace-syscall-x86'." |
| 241 | + |
| 242 | +build_qemu syscall x64 |
| 243 | +mv "./qemu-trace" "../../build/qemu-trace-syscall-x64" || exit 1 |
| 244 | +echo "[+] Successfully created 'qemu-trace-syscall-x64'." |
| 245 | + |
| 246 | +build_qemu feedback x86 |
| 247 | +mv "./qemu-trace" "../../build/qemu-trace-feedback-x86" || exit 1 |
| 248 | +echo "[+] Successfully created 'qemu-trace-feedback-x86'." |
| 249 | + |
| 250 | +build_qemu feedback x64 |
| 251 | +mv "./qemu-trace" "../../build/qemu-trace-feedback-x64" || exit 1 |
| 252 | +echo "[+] Successfully created 'qemu-trace-feedback-x64'." |
| 253 | + |
| 254 | +build_qemu bbcount x86 |
| 255 | +mv "./qemu-trace" "../../build/qemu-trace-bbcount-x86" || exit 1 |
| 256 | +echo "[+] Successfully created 'qemu-trace-bbcount-x86'." |
| 257 | + |
| 258 | +build_qemu bbcount x64 |
| 259 | +mv "./qemu-trace" "../../build/qemu-trace-bbcount-x64" || exit 1 |
| 260 | +echo "[+] Successfully created 'qemu-trace-bbcount-x64'." |
| 261 | + |
| 262 | +exit 0 |
0 commit comments