#!/bin/sh set -eu APP_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) ROOT_DIR=$(CDPATH= cd -- "$APP_DIR/.." && pwd) SDK="${ANDROID_HOME:-$ROOT_DIR/android-sdk}" NDK="${ANDROID_NDK:-$SDK/ndk/28.2.13676358}" BUILD_TOOLS="$SDK/build-tools/35.0.0" PLATFORM="$SDK/platforms/android-23/android.jar" BUILD="$APP_DIR/build" ASSETS="$APP_DIR/assets" NATIVE="$APP_DIR/native" CC="$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" if [ ! -x "$BUILD_TOOLS/aapt2" ]; then echo "aapt2 not found at $BUILD_TOOLS/aapt2" >&2 exit 1 fi if [ ! -f "$PLATFORM" ]; then echo "android.jar not found at $PLATFORM" >&2 exit 1 fi if [ ! -x "$CC" ]; then echo "clang not found at $CC" >&2 exit 1 fi # Always build SuManager to keep assets up to date echo "Building SuManager..." "$ROOT_DIR/sumanager/build.sh" rm -rf "$BUILD" mkdir -p "$BUILD/res" "$BUILD/classes" "$BUILD/dex" "$BUILD/gen" mkdir -p "$ASSETS/common" "$ASSETS/arm" "$ASSETS/armv6" "$ASSETS/armv5" "$ASSETS/x86" "$ASSETS/arm64" "$ASSETS/x86_64" build_exploit() { directory="$1" march="$2" target="$3" extra_flags="${4:-}" echo "Building exploit payload for $directory ($march)..." "$CC" --target="$target" -nostdlib -static -fno-pie -fno-stack-protector \ -fno-builtin -Os -march="$march" $extra_flags -s -Wl,--build-id=none -Wl,-e,_start \ -Wl,--gc-sections -ffunction-sections -fdata-sections \ "$NATIVE/start_dcow.S" "$NATIVE/dirtycow.c" -o "$ASSETS/$directory/dirtycow" "$CC" --target="$target" -nostdlib -static -fno-pie -fno-stack-protector \ -fno-builtin -Os -march="$march" $extra_flags -s -Wl,--build-id=none -Wl,-e,_start \ -Wl,--gc-sections -ffunction-sections -fdata-sections \ "$NATIVE/start_payload.S" "$NATIVE/payload.c" -o "$ASSETS/$directory/payload" } build_su() { directory="$1" march="$2" target="$3" echo "Building su binary for $directory ($march)..." "$CC" --target="$target" -static -fno-stack-protector \ -Os -march="$march" -s -Wl,--build-id=none \ -Wl,--gc-sections -ffunction-sections -fdata-sections \ -I"$NATIVE/su" "$NATIVE/su/su.c" -o "$ASSETS/$directory/su" } # ARMv5 and ARMv6 cannot execute the NDK libc, which contains ARMv7 movw. # Link against the device libc with a minimal _start instead. This is a # non-PIE executable, valid on the Android 2.x through 4.4 devices that # use these CPUs. build_su_legacy() { directory="$1" march="$2" target="$3" echo "Building su binary for $directory ($march, device libc)..." "$CC" --target="$target" -nostdlib -fno-pie -no-pie -fno-stack-protector \ -fno-builtin -D_FORTIFY_SOURCE=0 -Os -march="$march" -marm -s \ -Wl,--build-id=none -Wl,-e,_start -Wl,--dynamic-linker=/system/bin/linker \ -Wl,--gc-sections -ffunction-sections -fdata-sections \ -I"$NATIVE/su" "$NATIVE/start_su.S" "$NATIVE/su/su.c" \ -o "$ASSETS/$directory/su" -lc } # Compile exploit binaries for 32-bit platforms build_exploit arm armv7-a armv7a-linux-androideabi21 "-marm" build_exploit armv6 armv6 arm-linux-androideabi21 "-marm" build_exploit armv5 armv5te arm-linux-androideabi21 "-marm" build_exploit x86 i686 i686-linux-android21 "" # Compile su binary for all platforms build_su arm armv7-a armv7a-linux-androideabi21 build_su_legacy armv6 armv6 arm-linux-androideabi21 build_su_legacy armv5 armv5te arm-linux-androideabi21 build_su x86 i686 i686-linux-android21 build_su arm64 armv8-a aarch64-linux-android21 build_su x86_64 x86-64 x86_64-linux-android21 # Setup common assets cp "$NATIVE/install.sh" "$ASSETS/common/install.sh" cp "$ROOT_DIR/sumanager/build/sumanager.apk" "$ASSETS/common/sumanager.apk" # Also link into arm for compatibility cp "$NATIVE/install.sh" "$ASSETS/arm/install.sh" cp "$ROOT_DIR/sumanager/build/sumanager.apk" "$ASSETS/arm/sumanager.apk" echo "Compiling resources..." "$BUILD_TOOLS/aapt2" compile --dir "$APP_DIR/res" -o "$BUILD/res.zip" "$BUILD_TOOLS/aapt2" link \ -o "$BUILD/unsigned.apk" \ -I "$PLATFORM" \ --manifest "$APP_DIR/AndroidManifest.xml" \ "$BUILD/res.zip" \ -A "$ASSETS" \ --min-sdk-version 8 \ --target-sdk-version 19 \ --version-code 1 \ --version-name 1.0 \ --java "$BUILD/gen" find "$APP_DIR/src" -name '*.java' > "$BUILD/sources.txt" find "$BUILD/gen" -name '*.java' >> "$BUILD/sources.txt" echo "Compiling Java sources..." javac -source 8 -target 8 -bootclasspath "$PLATFORM" -Xlint:all -d "$BUILD/classes" @"$BUILD/sources.txt" find "$BUILD/classes" -name '*.class' > "$BUILD/classes.txt" echo "Running d8 dex compiler with release optimization..." "$BUILD_TOOLS/d8" --release --min-api 8 --lib "$PLATFORM" --output "$BUILD/dex" @"$BUILD/classes.txt" (cd "$BUILD/dex" && zip -q "$BUILD/unsigned.apk" classes.dex) "$BUILD_TOOLS/zipalign" -f 4 "$BUILD/unsigned.apk" "$BUILD/aligned.apk" KEYSTORE="$APP_DIR/debug.keystore" if [ ! -f "$KEYSTORE" ]; then keytool -genkeypair -keystore "$KEYSTORE" -alias sloproot -keyalg RSA \ -keysize 2048 -validity 10000 -storepass android -keypass android \ -dname "CN=SlopRoot, OU=Legacy, O=SlopRoot, L=Local, ST=Local, C=US" fi echo "Signing APK with apksigner (min SDK 8)..." "$BUILD_TOOLS/apksigner" sign \ --ks "$KEYSTORE" \ --ks-pass pass:android \ --min-sdk-version 8 \ --out "$BUILD/sloproot.apk" \ "$BUILD/aligned.apk" "$BUILD_TOOLS/apksigner" verify --verbose "$BUILD/sloproot.apk" echo "Built and verified $BUILD/sloproot.apk"