Android 反检测与 SSL Pinning 绕过 (Frida/Objection)

Reading time: 12 minutes

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

本页提供了一个实用流程,用以在 Android 应用检测/阻止 instrumentation 或强制 TLS pinning 时恢复动态分析。侧重快速调查、常见检测点,以及在可能的情况下无需重打包的可复制粘贴 hook/策略。

Detection Surface (what apps check)

  • 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

Step 1 — Quick win: hide root with Magisk DenyList

  • Enable Zygisk in Magisk
  • Enable DenyList, add the target package
  • Reboot and retest

许多应用只查找明显的指标(su/Magisk 路径/getprop)。DenyList 往往能中和这些初级检测。

References:

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

Step 2 — 30‑second Frida Codeshare tests

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

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

Example:

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

这些通常会 stub Java 的 root/debug checks、process/service scans 和 native ptrace()。在保护较轻的 apps 上很有用;对于加固的 targets 可能需要定制的 hooks。

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

使用 Medusa 自动化 (Frida framework)

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

bash
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 非常适合在编写 custom hooks 之前快速取得成果。你也可以 cherry-pick modules,并把它们与自己的 scripts 结合使用。

Step 3 — 通过延迟 attach 绕过 init-time 检测器

许多检测只在 process spawn/onCreate() 期间运行。Spawn‑time injection (-f) 或 gadgets 会被捕获;在 UI 加载后再 attach 往往能绕过这些检测。

bash
# 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 检查。

Step 4 — 通过 Jadx 和字符串搜寻映射检测逻辑

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

  • "frida", "gum", "root", "magisk", "ptrace", "su", "getprop", "debugger"

典型的 Java 模式:

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 (native bridge)
  • java.lang.Runtime.exec / ProcessBuilder (probing commands)
  • android.os.SystemProperties.get (root/emulator heuristics)

第5步 — Runtime stubbing with Frida (Java)

覆盖自定义 guards 以返回安全值,避免 repacking:

js
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(); };
});

在排查早期崩溃吗?在它崩溃前转储类,以找出可能的检测命名空间:

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

// Quick root detection stub example (adapt to target package/class names) Java.perform(() => { try { const RootChecker = Java.use('com.target.security.RootCheck'); RootChecker.isDeviceRooted.implementation = function () { return false; }; } catch (e) {} });

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

js
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。

快速伪造 Build 字段:

js
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 contexts:

js
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

第 6 步 — 当 Java hooks 失败时,跟随 JNI/native 轨迹

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

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

快速对捆绑的 .so 文件进行本地初步筛查:

bash
# 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:

js
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

Step 7 — Objection patching (embed gadget / strip basics)

当你更偏好 repacking 而不是 runtime hooks 时,尝试:

bash
objection patchapk --source app.apk

注意:

  • 需要 apktool;请根据官方指南确保使用最新版本以避免构建问题: https://apktool.org/docs/install
  • Gadget injection 允许在无需 root 的情况下进行 instrumentation,但仍可能被更严格的初始化时检查(init‑time checks)发现。

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

参考:

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

第 8 步 — 备用:修补 TLS pinning 以便查看网络流量

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

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

Make APK Accept CA Certificate

Install Burp Certificate

便捷命令速查表

bash
# 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

Universal proxy forcing + TLS unpinning (HTTP Toolkit Frida hooks)

现代应用常常忽略系统代理并实施多层 pinning(Java + native),即使安装了用户/系统 CA,也会使流量捕获变得困难。一个实用的做法是结合 universal TLS unpinning 与 proxy forcing,使用现成的 Frida hooks,并将所有流量通过 mitmproxy/Burp 转发。

工作流程

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

示例

bash
# 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 回避时,此技术非常理想。
  • 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