--- title: Frida Android Setup and Hooking description: Install and configure frida on Android/Termux for dynamic instrumentation name: frida-android-setup version: 1.0 triggers: - frida - android hook - dynamic instrumentation - reverse engineering --- # Frida Android Setup and Hooking ## Overview Install frida-server and frida Python client on Android/Termux for dynamic instrumentation of Android apps. ## Prerequisites - Android device with root access (Magisk) - Termux installed - Internet connection for downloading binaries ## Installation Steps ### 1. Install frida-server ```bash # Download frida-server for Android arm64 VERSION=17.15.3 curl -L -o frida-server-${VERSION}-android-arm64.xz \ "https://github.com/frida/frida/releases/download/${VERSION}/frida-server-${VERSION}-android-arm64.xz" # Extract unxz frida-server-${VERSION}-android-arm64.xz # Install to /data/local/tmp (requires root) su -c "cp frida-server-${VERSION}-android-arm64 /data/local/tmp/frida-server" su -c "chmod 755 /data/local/tmp/frida-server" # Start frida-server su -c "/data/local/tmp/frida-server -D" # Verify su -c "/data/local/tmp/frida-server --version" ``` ### 2. Install frida Python Client #### 2.1 Download frida-core-devkit ```bash VERSION=17.15.3 curl -L -o frida-core-devkit-${VERSION}-android-arm64.tar.xz \ "https://github.com/frida/frida/releases/download/${VERSION}/frida-core-devkit-${VERSION}-android-arm64.tar.xz" # Extract to home directory tar -xf frida-core-devkit-${VERSION}-android-arm64.tar.xz # Produces: libfrida-core.a, frida-core.h, frida-core-example.c ``` #### 2.2 Install Build Dependencies ```bash pkg install clang make pkg-config glib libffi pip install meson ``` #### 2.3 Compile ninja (if not available) ```bash # Download ninja source curl -L -o ninja-src.tar.gz \ "https://github.com/ninja-build/ninja/archive/refs/tags/v1.12.1.tar.gz" tar -xzf ninja-src.tar.gz cd ninja-1.12.1 # Fix Android Bionic compatibility in src/util.cc # The GetLoadAverage() function uses sysinfo/SI_LOAD_SHIFT which are not available on Android Bionic # Replace the function with a simple return -0.0f python3 configure.py --bootstrap cp ninja $PREFIX/bin/ ``` #### 2.4 Download frida Python Source ```bash pip download frida --no-deps -d frida_wheel cd frida_wheel tar -xzf frida-*.tar.gz ``` #### 2.5 Compile frida Python Extension ```bash mkdir frida_build && cd frida_build # Create meson.build cat > meson.build << 'EOF' project('frida-python-ext', 'c', version: '17.15.3') python = import('python').find_installation() python_dep = python.dependency() cc = meson.get_compiler('c') frida_core_dep = cc.find_library('frida-core', dirs: '/data/data/com.termux/files/home') frida_core_inc = include_directories('/data/data/com.termux/files/home') glib_dep = dependency('glib-2.0', required: false) gio_dep = dependency('gio-2.0', required: false) os_deps = [] if glib_dep.found() os_deps += glib_dep endif if gio_dep.found() os_deps += gio_dep endif extension = python.extension_module('_frida', '/path/to/frida-17.15.3/frida/_frida/extension.c', limited_api: '3.7', include_directories: frida_core_inc, dependencies: [python_dep, frida_core_dep, os_deps], install: true, subdir: 'frida', ) EOF # Build meson setup build meson compile -C build # The output will be at build/_frida.abi3.so ``` #### 2.6 Install frida Python Package ```bash cd frida-17.15.3 FRIDA_EXTENSION=/path/to/frida_build/build/_frida.abi3.so \ pip install . --no-build-isolation ``` #### 2.7 Verify Installation ```bash python3 -c "import frida; print(frida.__version__)" ``` ## Usage ### Connect to frida-server ```python import frida # Connect to local frida-server device = frida.get_device_manager().add_remote_device('127.0.0.1:27042') # List processes processes = device.enumerate_processes() for p in processes: print(f'{p.pid}: {p.name}') ``` ### Attach to a Process ```python # Attach by process name session = device.attach('com.example.app') # Attach by PID session = device.attach(1234) ``` ### Load a Script ```python script = session.create_script(''' Java.perform(function() { console.log('Java VM available: ' + Java.available); // Hook a Java method var TargetClass = Java.use('com.example.app.TargetClass'); TargetClass.targetMethod.implementation = function(arg1, arg2) { console.log('Hook called with: ' + arg1 + ', ' + arg2); return this.targetMethod(arg1, arg2); }; }); ''') script.on('message', lambda msg, data: print(msg)) script.load() ``` ## Pitfalls ### Python 3.14 无预编译 wheel(Termux 2026-07 验证) - Termux 默认 Python 升级到 3.14 后,`pip install frida` 和 `pip install frida-tools` 均失败 - PyPI 上 frida 没有 cp314 预编译 wheel,`--only-binary :all:` 也找不到匹配版本 - 源码编译需要 ninja + meson + glib + frida-core-devkit,在 Android 16 Bionic 上极易失败 - **替代方案**(按推荐顺序): 1. 使用 frida-server 的 CLI 模式 + `frida-inject` 二进制(不需要 Python 客户端) 2. 在另一台机器上运行 frida Python 客户端,通过网络连接 frida-server(`frida -H 127.0.0.1:27042`) 3. 放弃 Frida,改用 UI 自动化方案(适用于只需触发 App 功能、不需要 hook 内部逻辑的场景) 4. 安装 Termux 旧版 Python 3.12 并行使用(如有) ### Version Mismatch - **frida-server** and **frida Python client** must be the same version - Mismatched versions cause Java VM detection failures ### Java VM Not Available - On some Android versions (e.g., Android 16), Java VM may not be available after attach - This is a known limitation; use native hooking instead ### Android 16 / Bionic Compatibility - Android 16 uses Bionic libc which lacks `sysinfo` structure and `SI_LOAD_SHIFT` - ninja build fails with `sysinfo` and `SI_LOAD_SHIFT` errors - Fix: Patch `src/util.cc` in ninja source to replace `GetLoadAverage()` with simple `return -0.0f` - See `references/frida-termux-compilation-recipe.md` for full patch ### frida-server Version Mismatch Symptoms - Java VM unavailable after attach - Process list shows wrong process names (e.g., "mmd" instead of "com.tencent.mm") - Fix: Ensure frida-server and frida Python client versions match exactly - Download matching versions from GitHub releases ### Process Name Resolution - frida matches processes by human-readable label, not package name - WeChat main process may show as "WeChat" or "mmd" instead of "com.tencent.mm" - Multi-process apps may resolve to wrong sub-process ### /data/local/tmp Permission - frida needs write access to /data/local/tmp for helper files - Run frida-server as root or ensure /data/local/tmp is writable ### Frida 17.x Spawn Bug on Android 16 - `frida -f ` (spawn mode) fails on Android 16: ``` Failed to spawn: unable to locate android.os.Process.setArgV0() slot ``` - This is a known issue with Frida 17.x on newer Android versions - Workaround: Use attach mode (`frida -p `) instead of spawn mode - Start app normally (`am start`), wait, then attach by PID ### Anti-Frida Detection (Xiaohongshu, banking apps, etc.) - Apps like Xiaohongshu actively detect Frida. Symptoms: ``` Failed to attach: agent connection closed unexpectedly ``` - The app detects the frida agent in-process and kills the connection - Attempted bypasses (limited success): 1. Rename frida-server binary: `cp frida-server fsrv` 2. Change port: `fsrv -l 0.0.0.0:31337` 3. Attach with: `frida -H 127.0.0.1:31337 -p ` - These may still fail if the app checks for Frida's in-process artifacts - For apps with strong anti-Frida, prefer static analysis (jadx, apktool, MMKV extraction) ## References - [frida releases](https://github.com/frida/frida/releases) - [frida docs](https://frida.re/docs/) - [apktool-android-apk-decompilation](references/apktool-android-apk-decompilation.md) - [wechat-send-message-mechanism](references/wechat-send-message-mechanism.md) - [wechat-internal-send-api](references/wechat-internal-send-api.md) — sk.p0() method analysis and Frida hook target - [frida-termux-compilation-recipe](references/frida-termux-compilation-recipe.md)