|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This file was automatically generated by s2e-env at 2021-10-06 14:21:59.710454 |
| 4 | +# |
| 5 | +# This bootstrap script is used to control the execution of the target program |
| 6 | +# in an S2E guest VM. |
| 7 | +# |
| 8 | +# When you run launch-s2e.sh, the guest VM calls s2eget to fetch and execute |
| 9 | +# this bootstrap script. This bootstrap script and the S2E config file |
| 10 | +# determine how the target program is analyzed. |
| 11 | +# |
| 12 | + |
| 13 | +set -x |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +mkdir -p guest-tools32 |
| 18 | +TARGET_TOOLS32_ROOT=guest-tools32 |
| 19 | + |
| 20 | + |
| 21 | +mkdir -p guest-tools64 |
| 22 | +TARGET_TOOLS64_ROOT=guest-tools64 |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +# 64-bit tools take priority on 64-bit architectures |
| 27 | +TARGET_TOOLS_ROOT=${TARGET_TOOLS64_ROOT} |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +# To save the hassle of rebuilding guest images every time you update S2E's guest tools, |
| 32 | +# the first thing that we do is get the latest versions of the guest tools. |
| 33 | +function update_common_tools { |
| 34 | + local OUR_S2EGET |
| 35 | + |
| 36 | + OUR_S2EGET=${S2EGET} |
| 37 | + OUR_S2ECMD=${S2ECMD} |
| 38 | + |
| 39 | + # First, download the common tools |
| 40 | + |
| 41 | + |
| 42 | + for TOOL in ${COMMON_TOOLS}; do |
| 43 | + ${OUR_S2EGET} ${TARGET_TOOLS_ROOT}/${TOOL} |
| 44 | + if [ ! -f ${TOOL} ]; then |
| 45 | + ${OUR_S2ECMD} kill 0 "Could not get ${TOOL} from the host. Make sure that guest tools are installed properly." |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + chmod +x ${TOOL} |
| 49 | + done |
| 50 | +} |
| 51 | + |
| 52 | +function update_target_tools { |
| 53 | + for TOOL in $(target_tools); do |
| 54 | + ${S2EGET} ${TOOL} ${TOOL} |
| 55 | + chmod +x ${TOOL} |
| 56 | + done |
| 57 | +} |
| 58 | + |
| 59 | +function prepare_target { |
| 60 | + # Make sure that the target is executable |
| 61 | + chmod +x "$1" |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +function get_ramdisk_root { |
| 69 | + echo '/tmp/' |
| 70 | +} |
| 71 | + |
| 72 | +function copy_file { |
| 73 | + SOURCE="$1" |
| 74 | + DEST="$2" |
| 75 | + |
| 76 | + cp ${SOURCE} ${DEST} |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +# This prepares the symbolic file inputs. |
| 81 | +# This function takes as input a seed file name and makes its content symbolic according to the symranges file. |
| 82 | +# It is up to the host to prepare all the required symbolic files. The bootstrap file does not make files |
| 83 | +# symbolic on its own. |
| 84 | +function download_symbolic_file { |
| 85 | + SYMBOLIC_FILE="$1" |
| 86 | + RAMDISK_ROOT="$(get_ramdisk_root)" |
| 87 | + |
| 88 | + ${S2EGET} "${SYMBOLIC_FILE}" |
| 89 | + if [ ! -f "${SYMBOLIC_FILE}" ]; then |
| 90 | + ${S2ECMD} kill 1 "Could not fetch symbolic file ${SYMBOLIC_FILE} from host" |
| 91 | + fi |
| 92 | + |
| 93 | + copy_file "${SYMBOLIC_FILE}" "${RAMDISK_ROOT}" |
| 94 | + |
| 95 | + SYMRANGES_FILE="${SYMBOLIC_FILE}.symranges" |
| 96 | + |
| 97 | + ${S2EGET} "${SYMRANGES_FILE}" > /dev/null |
| 98 | + |
| 99 | + # Make the file symbolic |
| 100 | + if [ -f "${SYMRANGES_FILE}" ]; then |
| 101 | + export S2E_SYMFILE_RANGES="${SYMRANGES_FILE}" |
| 102 | + fi |
| 103 | + |
| 104 | + |
| 105 | + # The symbolic file will be split into symbolic variables of up to 4k bytes each. |
| 106 | + ${S2ECMD} symbfile 4096 "${RAMDISK_ROOT}${SYMBOLIC_FILE}" > /dev/null |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +function download_symbolic_files { |
| 111 | + for f in "$@"; do |
| 112 | + download_symbolic_file "${f}" |
| 113 | + done |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +# This function executes the target program given in arguments. |
| 119 | +# |
| 120 | +# There are two versions of this function: |
| 121 | +# - without seed support |
| 122 | +# - with seed support (-s argument when creating projects with s2e_env) |
| 123 | +function execute { |
| 124 | + local TARGET |
| 125 | + |
| 126 | + TARGET="$1" |
| 127 | + shift |
| 128 | + |
| 129 | + execute_target "${TARGET}" "$@" |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +############################################################################### |
| 135 | +# This section contains target-specific code |
| 136 | + |
| 137 | +function make_seeds_symbolic { |
| 138 | + echo 1 |
| 139 | +} |
| 140 | + |
| 141 | +# This function executes the target program. |
| 142 | +# You can customize it if your program needs special invocation, |
| 143 | +# custom symbolic arguments, etc. |
| 144 | +function execute_target { |
| 145 | + local TARGET |
| 146 | + TARGET="$1" |
| 147 | + shift |
| 148 | + |
| 149 | + S2E_SO="${TARGET_TOOLS64_ROOT}/s2e.so" |
| 150 | + # sym_env is dynamically linked, so s2e.so has been preloaded to |
| 151 | + # provide symbolic arguments to the target if required. You can do so by |
| 152 | + # using the ``S2E_SYM_ARGS`` environment variable as required |
| 153 | + #S2E_SYM_ARGS="" LD_PRELOAD="${S2E_SO}" "${TARGET}" "$@" > /dev/null 2> /dev/null |
| 154 | + ./sym_env -- ./target |
| 155 | +} |
| 156 | + |
| 157 | +# Nothing more to initialize on Linux |
| 158 | +function target_init { |
| 159 | + # Start the LinuxMonitor kernel module |
| 160 | + sudo modprobe s2e |
| 161 | +} |
| 162 | + |
| 163 | +# Returns Linux-specific tools |
| 164 | +function target_tools { |
| 165 | + |
| 166 | + echo "${TARGET_TOOLS32_ROOT}/s2e.so" "${TARGET_TOOLS64_ROOT}/s2e.so" |
| 167 | + |
| 168 | +} |
| 169 | + |
| 170 | +S2ECMD=./s2ecmd |
| 171 | +S2EGET=./s2eget |
| 172 | +S2EPUT=./s2eput |
| 173 | +COMMON_TOOLS="s2ecmd s2eget s2eput" |
| 174 | + |
| 175 | +############################################################################### |
| 176 | + |
| 177 | + |
| 178 | +update_common_tools |
| 179 | +update_target_tools |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +# Don't print crashes in the syslog. This prevents unnecessary forking in the |
| 184 | +# kernel |
| 185 | +sudo sysctl -w debug.exception-trace=0 |
| 186 | + |
| 187 | +# Prevent core dumps from being created. This prevents unnecessary forking in |
| 188 | +# the kernel |
| 189 | +ulimit -c 0 |
| 190 | + |
| 191 | +# Ensure that /tmp is mounted in memory (if you built the image using s2e-env |
| 192 | +# then this should already be the case. But better to be safe than sorry!) |
| 193 | +if ! mount | grep "/tmp type tmpfs"; then |
| 194 | + sudo mount -t tmpfs -osize=10m tmpfs /tmp |
| 195 | +fi |
| 196 | + |
| 197 | +# Need to disable swap, otherwise there will be forced concretization if the |
| 198 | +# system swaps out symbolic data to disk. |
| 199 | +sudo swapoff -a |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +target_init |
| 204 | + |
| 205 | +# Download the target file to analyze |
| 206 | +${S2EGET} "sym_env" |
| 207 | +${S2EGET} "target" |
| 208 | +${S2EGET} "poc" |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | +download_symbolic_files |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +# Run the analysis |
| 217 | +TARGET_PATH='./sym_env' |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | +prepare_target "${TARGET_PATH}" |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +execute "${TARGET_PATH}" |
0 commit comments