Android Anti-Instrumentation & SSL Pinning Bypass (Frida/Objection)

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

This page provides a practical workflow to regain dynamic analysis against Android apps that detect/root‑block instrumentation or enforce TLS pinning. It focuses on fast triage, common detections, and copy‑pasteable hooks/tactics to bypass them without repacking when possible.

检测面(应用会检查的项)

  • Root checks: su binary, Magisk paths, getprop values, common root packages
  • Frida/debugger checks (Java): Debug.isDebuggerConnected(), ActivityManager.getRunningAppProcesses(), getRunningServices(), scanning /proc, classpath, loaded libs
  • Native anti‑debug: ptrace(), syscalls, anti‑attach, breakpoints, inline hooks
  • Early init checks: Application.onCreate() or process start hooks that crash if instrumentation is present
  • TLS pinning: custom TrustManager/HostnameVerifier, OkHttp CertificatePinner, Conscrypt pinning, native pins

绕过 Anti‑Frida 检测 / 隐蔽 Frida 服务器

phantom-frida rebuilds Frida from source and applies ~90 patches so common Frida fingerprints disappear while the stock Frida protocol remains compatible (frida-tools can still connect). Target: apps that grep /proc (cmdline, maps, task comm, fd readlink), D-Bus service names, default ports, or exported symbols.

阶段:

  • 源码补丁: 全局重命名 frida 标识符(server/agent/helper),并重建带有重命名 Java 包的 helper DEX。
  • 针对性构建/运行时补丁: meson 调整,将 memfd 标签改为 jit-cache,重命名 SELinux 标签(例如 frida_file),禁用 libc 在 exit/signal 上的 hooks 以避免 hook 检测器。
  • 构建后重命名: 导出符号 frida_agent_main 在首次编译后被重命名(Vala 会生成它),因此需要第二次增量编译。
  • 二进制十六进制补丁: 替换线程名(gmaingdbuspool-spawner);可选的清理会移除残留的 frida/Frida 字符串。

覆盖的检测向量:

  • Base (1–8): 进程名 frida-server、映射的 libfrida-agent.so、线程名、memfd 标签、导出 frida_agent_main、SELinux 标签、libc hook 副作用,以及 D-Bus 服务 re.frida.server 被重命名/中和。
  • Extended (9–16): 更改监听端口(--port)、重命名 D-Bus 接口/内部 C 符号/GType 名称、临时路径如 .frida/frida-、清理二进制字符串、重命名构建时宏和资产路径(libdir/frida)。作为协议一部分的 D-Bus 接口名称在 base 模式下保持不变以避免破坏 stock 客户端。

构建/使用(Android arm64 示例):

python3 build.py --version 17.7.2 --name myserver --port 27142 --extended --verify
adb push output/myserver-server-17.7.2-android-arm64 /data/local/tmp/myserver-server
adb shell chmod 755 /data/local/tmp/myserver-server
adb shell /data/local/tmp/myserver-server -D &
adb forward tcp:27142 tcp:27142
frida -H 127.0.0.1:27142 -f com.example.app

Flags: --skip-build (patch only), --skip-clone, --arch, --ndk-path, --temp-fixes; WSL helper: wsl -d Ubuntu bash build-wsl.sh.

Step 1 — Quick win: hide root with Magisk DenyList

  • 在 Magisk 中启用 Zygisk
  • 启用 DenyList,添加目标包
  • 重启并重新测试

许多应用只检查明显的指示器(su/Magisk paths/getprop)。DenyList 常能中和简单的检测。

References:

  • Magisk (Zygisk & DenyList): https://github.com/topjohnwu/Magisk

Play Integrity / Zygisk detections (post‑SafetyNet)

较新的银行/ID 应用将运行时检查与 Google Play Integrity(SafetyNet 的替代方案)关联,如果检测到 Zygisk 本身也可能导致崩溃。快速排查提示:

  • 临时禁用 Zygisk(切换关闭 + 重启)并重试;一些应用在 Zygote injection 加载时就会崩溃。
  • 如果 attestation 阻止登录,可以用 PlayIntegrityFix/Fork + TrickyStore 打补丁 Google Play Services,或仅在测试时使用 ReZygisk/Zygisk‑Next。将目标保持在 DenyList 中,避免使用会 leak props 的 LSPosed 模块。
  • 对于一次性运行,使用 KernelSU/APatch(无 Zygote injection)以规避 Zygisk 启发式检测,然后附加 Frida。

Step 2 — 30‑second Frida Codeshare tests

在深入之前试试常见的即插即用脚本:

  • anti-root-bypass.js
  • anti-frida-detection.js
  • hide_frida_gum.js

Example:

frida -U -f com.example.app -l anti-frida-detection.js

这些通常会对 Java 的 root/debug checks、process/service scans 和 native ptrace() 做 stub 处理。适用于防护较弱的应用;对加固目标可能需要定制化 hooks。

  • Codeshare: https://codeshare.frida.re/

使用 Medusa (Frida framework) 自动化

Medusa 提供 90+ 现成的模块,用于 SSL unpinning、root/emulator detection bypass、HTTP comms logging、crypto key interception 等。

git clone https://github.com/Ch0pin/medusa
cd medusa
pip install -r requirements.txt
python medusa.py

# Example interactive workflow
show categories
use http_communications/multiple_unpinner
use root_detection/universal_root_detection_bypass
run com.target.app

提示:Medusa 非常适合在编写自定义 hooks 之前快速取得成效。你也可以挑选 modules 并将它们与自己的 scripts 结合使用。

第 3 步 — 通过延后 attaching 绕过 init-time detectors

许多检测仅在 process spawn/onCreate() 期间运行。Spawn‑time injection (-f) 或 gadgets 会被捕获;在 UI 加载后再进行 attaching 可以绕过。

# Launch the app normally (launcher/adb), wait for UI, then attach
frida -U -n com.example.app
# Or with Objection to attach to running process
aobjection --gadget com.example.app explore  # if using gadget

如果这有效,保持会话稳定并继续进行 map 和 stub 检查。

第4步 — 通过 Jadx 和字符串搜索映射检测逻辑

在 Jadx 中的静态初筛关键字:

  • “frida”, “gum”, “root”, “magisk”, “ptrace”, “su”, “getprop”, “debugger”

典型的 Java 模式:

public boolean isFridaDetected() {
return getRunningServices().contains("frida");
}

常见需要审查/hook 的 API:

  • android.os.Debug.isDebuggerConnected
  • android.app.ActivityManager.getRunningAppProcesses / getRunningServices
  • java.lang.System.loadLibrary / System.load (本地桥接)
  • java.lang.Runtime.exec / ProcessBuilder (探测命令)
  • android.os.SystemProperties.get (root/模拟器 启发式检测)

第5步 — 使用 Frida (Java) 进行运行时替换(stubbing)

覆写自定义 guards 以返回安全值,而无需重新打包:

Java.perform(() => {
const Checks = Java.use('com.example.security.Checks');
Checks.isFridaDetected.implementation = function () { return false; };

// Neutralize debugger checks
const Debug = Java.use('android.os.Debug');
Debug.isDebuggerConnected.implementation = function () { return false; };

// Example: kill ActivityManager scans
const AM = Java.use('android.app.ActivityManager');
AM.getRunningAppProcesses.implementation = function () { return java.util.Collections.emptyList(); };
});

在排查早期崩溃时?在应用崩溃前 dump classes 以发现可能的检测命名空间:

Java.perform(() => {
Java.enumerateLoadedClasses({
onMatch: n => console.log(n),
onComplete: () => console.log('Done')
});
});

快速 root 检测存根示例(根据目标 package/class 名称进行调整):

Java.perform(() => {
try {
const RootChecker = Java.use('com.target.security.RootCheck');
RootChecker.isDeviceRooted.implementation = function () { return false; };
} catch (e) {}
});

记录并使可疑方法失效以确认执行流程:

Java.perform(() => {
const Det = Java.use('com.example.security.DetectionManager');
Det.checkFrida.implementation = function () {
console.log('checkFrida() called');
return false;
};
});

Bypass emulator/VM detection (Java stubs)

常见启发式检测:Build.FINGERPRINT/MODEL/MANUFACTURER/HARDWARE 包含 generic/goldfish/ranchu/sdk;QEMU 痕迹例如 /dev/qemu_pipe、/dev/socket/qemud;默认 MAC 02:00:00:00:00:00;10.0.2.x NAT;缺少 telephony/sensors。

快速 spoof Build 字段示例:

Java.perform(function(){
var Build = Java.use('android.os.Build');
Build.MODEL.value = 'Pixel 7 Pro';
Build.MANUFACTURER.value = 'Google';
Build.BRAND.value = 'google';
Build.FINGERPRINT.value = 'google/panther/panther:14/UP1A.231105.003/1234567:user/release-keys';
});

补充用于文件存在性检查和标识符的存根(TelephonyManager.getDeviceId/SubscriberId、WifiInfo.getMacAddress、SensorManager.getSensorList),以返回真实的值。

SSL pinning bypass quick hook (Java)

使自定义 TrustManagers 无效并强制使用宽松的 SSL 上下文:

Java.perform(function(){
var X509TrustManager = Java.use('javax.net.ssl.X509TrustManager');
var SSLContext = Java.use('javax.net.ssl.SSLContext');

// No-op validations
X509TrustManager.checkClientTrusted.implementation = function(){ };
X509TrustManager.checkServerTrusted.implementation = function(){ };

// Force permissive TrustManagers
var TrustManagers = [ X509TrustManager.$new() ];
var SSLContextInit = SSLContext.init.overload('[Ljavax.net.ssl.KeyManager;','[Ljavax.net.ssl.TrustManager;','java.security.SecureRandom');
SSLContextInit.implementation = function(km, tm, sr){
return SSLContextInit.call(this, km, TrustManagers, sr);
};
});

注意事项

  • 针对 OkHttp 扩展:按需 hook okhttp3.CertificatePinner 和 HostnameVerifier,或使用来自 CodeShare 的通用 unpinning 脚本。
  • 运行示例:frida -U -f com.target.app -l ssl-bypass.js --no-pause

OkHttp4 / gRPC / Cronet pinning (2024+)

现代栈会在较新的 API 内部进行 pin(OkHttp4+、gRPC over Cronet/BoringSSL)。当基本的 SSLContext hook 失效时,添加这些 hooks:

Java.perform(() => {
try {
const Pinner = Java.use('okhttp3.CertificatePinner');
Pinner.check.overload('java.lang.String', 'java.util.List').implementation = function(){};
Pinner.check$okhttp.implementation = function(){};
} catch (e) {}

try {
const CronetB = Java.use('org.chromium.net.CronetEngine$Builder');
CronetB.enablePublicKeyPinningBypassForLocalTrustAnchors.overload('boolean').implementation = function(){ return this; };
CronetB.setPublicKeyPins.overload('java.lang.String', 'java.util.Set', 'boolean').implementation = function(){ return this; };
} catch (e) {}
});

如果 TLS 仍然失败,则切换到 native 并修补 Cronet/gRPC 使用的 BoringSSL 验证入口点:

const customVerify = Module.findExportByName(null, 'SSL_CTX_set_custom_verify');
if (customVerify) {
Interceptor.attach(customVerify, {
onEnter(args){
// arg0 = SSL_CTX*, arg1 = mode, arg2 = callback
args[1] = ptr(0); // SSL_VERIFY_NONE
args[2] = NULL;  // disable callback
}
});
}

第6步 — 在 Java hooks 失败时沿着 JNI/native 路径追踪

追踪 JNI 入口点以定位 native loaders 和 detection init:

frida-trace -n com.example.app -i "JNI_OnLoad"

已捆绑 .so 文件的快速本地初步分析:

# List exported symbols & JNI
nm -D libfoo.so | head
objdump -T libfoo.so | grep Java_
strings -n 6 libfoo.so | egrep -i 'frida|ptrace|gum|magisk|su|root'

交互式/本地逆向:

  • Ghidra: https://ghidra-sre.org/
  • r2frida: https://github.com/nowsecure/r2frida

示例:禁用 ptrace 以绕过 libc 中的简单 anti‑debug:

const ptrace = Module.findExportByName(null, 'ptrace');
if (ptrace) {
Interceptor.replace(ptrace, new NativeCallback(function () {
return -1; // pretend failure
}, 'int', ['int', 'int', 'pointer', 'pointer']));
}

另见: Reversing Native Libraries

第7步 — Objection patching (embed gadget / strip basics)

如果你更倾向于 repacking 而不是 runtime hooks,请尝试:

objection patchapk --source app.apk

注意:

  • 需要 apktool;请按照官方指南确保使用最新版本以避免构建问题: https://apktool.org/docs/install
  • Gadget 注入允许在无需 root 的情况下进行 instrumentation,但仍可能被更严格的 init‑time 检测捕获。

可选地,添加 LSPosed 模块和 Shamiko 以在 Zygisk 环境中增强 root 隐藏,并维护 DenyList 以覆盖子进程。

有关完整工作流程(包括 script-mode Gadget 配置和将 Frida 17+ agent 打包进 APK),请参见:

Frida Tutorial — Self-contained agent + Gadget embedding

参考:

  • Objection: https://github.com/sensepost/objection

第 8 步 — 备用:修补 TLS pinning 以便网络可见性

如果 instrumentation 被阻止,你仍然可以通过静态移除 pinning 来检查流量:

apk-mitm app.apk
# Then install the patched APK and proxy via Burp/mitmproxy
  • 工具: https://github.com/shroudedcode/apk-mitm
  • 有关网络配置 CA‑trust 技巧(以及 Android 7+ user CA trust),请参阅:

Make APK Accept CA Certificate

Install Burp Certificate

常用命令速查表

# List processes and attach
frida-ps -Uai
frida -U -n com.example.app

# Spawn with a script (may trigger detectors)
frida -U -f com.example.app -l anti-frida-detection.js

# Trace native init
frida-trace -n com.example.app -i "JNI_OnLoad"

# Objection runtime
objection --gadget com.example.app explore

# Static TLS pinning removal
apk-mitm app.apk

通用 proxy 强制 + TLS unpinning (HTTP Toolkit Frida hooks)

现代应用常常忽略系统 proxies 并在多个层面实施 pinning(Java + native),即使安装了用户/系统 CA,抓取流量仍然很困难。一个实用的方法是结合通用 TLS unpinning 和通过现成的 Frida hooks 强制 proxy,并将所有流量通过 mitmproxy/Burp 转发。

Workflow

  • 在主机上运行 mitmproxy(或 Burp)。确保设备可以访问主机的 IP/端口。
  • 加载 HTTP Toolkit 的整合 Frida hooks 来同时 unpin TLS 并在常见栈(OkHttp/OkHttp3, HttpsURLConnection, Conscrypt, WebView 等)上强制使用 proxy。此操作可绕过 CertificatePinner/TrustManager 检查并覆盖 proxy selectors,因此即使应用显式禁用 proxies,流量仍会通过你的 proxy 发送。
  • 使用 Frida 和 hook 脚本启动目标应用,并在 mitmproxy 中捕获请求。

Example

# Device connected via ADB or over network (-U)
# See the repo for the exact script names & options
frida -U -f com.vendor.app \
-l ./android-unpinning-with-proxy.js \
--no-pause

# mitmproxy listening locally
mitmproxy -p 8080

注意

  • 在可能的情况下,通过 adb shell settings put global http_proxy <host>:<port> 将其与系统范围代理结合使用。Frida hooks 会强制使用代理,即使应用绕过全局设置。
  • 当需要对 mobile-to-IoT onboarding 流程进行 MITM(这类流程常见 pinning/proxy avoidance)时,此技术非常适用。
  • Hooks: https://github.com/httptoolkit/frida-interception-and-unpinning

参考资料

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks