FridaSetupScript.sh 3.88 KB
#!/system/bin/sh
# =============================================================================
# FridaSetupScript.sh — frida-server device initialisation
#
# Purpose:
#   Download the correct frida-server binary for the device architecture,
#   install it to /data/local/tmp/, and start it.
#
# Usage (executed via RootShell on the Android device):
#   su -c "sh /data/local/tmp/FridaSetupScript.sh [frida_version]"
#
# Arguments:
#   $1  (optional) frida-server version to install, e.g. "16.2.1".
#       Defaults to the value of FRIDA_VERSION below.
#
# Requirements:
#   - Root shell (su)
#   - curl or wget available on the device
#   - Internet access (or a local mirror set via FRIDA_MIRROR)
# =============================================================================

set -e

FRIDA_VERSION="${1:-16.2.1}"
FRIDA_INSTALL_DIR="/data/local/tmp"
FRIDA_SERVER_BIN="$FRIDA_INSTALL_DIR/frida-server"
FRIDA_MIRROR="https://github.com/frida/frida/releases/download"

# ---------------------------------------------------------------------------
# 1. Detect device ABI
# ---------------------------------------------------------------------------

ARCH="$(getprop ro.product.cpu.abi)"

case "$ARCH" in
    arm64-v8a)   FRIDA_ARCH="android-arm64"   ;;
    armeabi-v7a) FRIDA_ARCH="android-arm"     ;;
    x86_64)      FRIDA_ARCH="android-x86_64"  ;;
    x86)         FRIDA_ARCH="android-x86"     ;;
    *)
        echo "[frida-setup] ERROR: unsupported ABI: $ARCH" >&2
        exit 1
        ;;
esac

echo "[frida-setup] Device ABI: $ARCH → frida arch: $FRIDA_ARCH"

# ---------------------------------------------------------------------------
# 2. Download frida-server
# ---------------------------------------------------------------------------

FILENAME="frida-server-${FRIDA_VERSION}-${FRIDA_ARCH}.xz"
DOWNLOAD_URL="$FRIDA_MIRROR/${FRIDA_VERSION}/${FILENAME}"
COMPRESSED_PATH="$FRIDA_INSTALL_DIR/$FILENAME"

echo "[frida-setup] Downloading $DOWNLOAD_URL …"

if command -v curl > /dev/null 2>&1; then
    curl -fSL -o "$COMPRESSED_PATH" "$DOWNLOAD_URL"
elif command -v wget > /dev/null 2>&1; then
    wget -q -O "$COMPRESSED_PATH" "$DOWNLOAD_URL"
else
    echo "[frida-setup] ERROR: neither curl nor wget found" >&2
    exit 1
fi

echo "[frida-setup] Download complete: $COMPRESSED_PATH"

# ---------------------------------------------------------------------------
# 3. Decompress
# ---------------------------------------------------------------------------

if command -v xz > /dev/null 2>&1; then
    xz -d "$COMPRESSED_PATH"
    DECOMPRESSED="$FRIDA_INSTALL_DIR/frida-server-${FRIDA_VERSION}-${FRIDA_ARCH}"
else
    # Fallback: use busybox xz or a pre-extracted binary pushed via adb.
    echo "[frida-setup] WARNING: xz not available, assuming binary was pre-extracted" >&2
    DECOMPRESSED="$COMPRESSED_PATH"
fi

mv "$DECOMPRESSED" "$FRIDA_SERVER_BIN"

# ---------------------------------------------------------------------------
# 4. Set executable permission
# ---------------------------------------------------------------------------

chmod 755 "$FRIDA_SERVER_BIN"
echo "[frida-setup] Installed: $FRIDA_SERVER_BIN"

# ---------------------------------------------------------------------------
# 5. Start frida-server
# ---------------------------------------------------------------------------

# Kill any previous instance before starting a fresh one.
pkill -f frida-server 2>/dev/null || true
sleep 0.5

setsid "$FRIDA_SERVER_BIN" &>/dev/null &
sleep 1

# ---------------------------------------------------------------------------
# 6. Verify the process is running
# ---------------------------------------------------------------------------

if ps | grep -v grep | grep -q frida-server; then
    VERSION_OUT="$($FRIDA_SERVER_BIN --version 2>/dev/null || echo 'unknown')"
    echo "[frida-setup] frida-server $VERSION_OUT is running  (port 27042)"
    exit 0
else
    echo "[frida-setup] ERROR: frida-server did not start" >&2
    exit 1
fi