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

本页面提供一个实用的工作流程,用于恢复对检测/阻止 instrumentation 或强制 TLS pinning 的 Android 应用的动态分析。重点是快速初筛、常见检测点,以及尽可能在不重打包的情况下可复制粘贴的 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

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

许多应用只查找明显的指示器(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:

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

这些通常会对 Java 的 root/debug checks、process/service scans 和 native ptrace() 进行 stub。对防护较弱的 apps 很有效;对于 hardened targets 可能需要定制的 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 之前快速取得成果。你也可以挑选模块并将它们与自己的脚本结合使用。

第3步 — 延迟附加以绕过初始化时检测器

许多检测只在进程 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 检查。

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

Static triage keywords in Jadx:

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

Typical Java patterns:

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:

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')
});
});
// 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) {}
});

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

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 字段:

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';
});

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

SSL pinning bypass quick hook (Java)

中和自定义 TrustManagers 并强制使用宽松的 SSL contexts:

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:

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'

Interactive/native reversing:

  • 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

Notes:

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

可选择添加 LSPosed 模块和 Shamiko,以在 Zygisk 环境中实现更强的 root 隐藏,并调整 DenyList 以覆盖子进程。

For a complete workflow including script-mode Gadget configuration and bundling your Frida 17+ agent into the APK, see:

Frida Tutorial — Self-contained agent + Gadget embedding

References:

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

Step 8 — Fallback: Patch TLS pinning for network visibility

如果 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+ 用户 CA 信任),请参见:

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

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

现代应用经常忽略系统代理并强制多层 pinning(Java + native),即使安装了用户/系统 CAs,也会使流量捕获变得困难。一种实用方法是结合通用 TLS unpinning 和通过现成的 Frida hooks 强制代理,然后将所有流量通过 mitmproxy/Burp 转发。

Workflow

  • 在主机上运行 mitmproxy(或 Burp)。确保设备可以访问主机的 IP/端口。
  • 加载 HTTP Toolkit 的整合 Frida hooks,以同时进行 TLS unpinning 并在常见栈(OkHttp/OkHttp3、HttpsURLConnection、Conscrypt、WebView 等)中强制使用代理。该方法会绕过 CertificatePinner/TrustManager 检查并覆盖代理选择器,因此即使应用显式禁用代理,流量也会始终通过你的代理发送。
  • 使用 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 避免时,此技术非常适用。
  • 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