Frida 是一个强大的动态插桩(Dynamic Instrumentation)工具,常用于逆向工程、安全测试、移动应用调试等。它支持在运行时对目标应用进行注入、自定义脚本执行,甚至篡改函数行为,广泛应用于 Android、iOS、Windows、macOS、Linux 等平台。
🧠 Frida 是什么?
- Frida 是一个动态二进制插桩框架
- 使用 JavaScript 或 Python 脚本与目标进程交互
- 可注入到本地或远程进程,实时修改函数参数、返回值
- 广泛用于 APP 抓包绕过、函数跟踪、反调试绕过、安全审计等
🚀 Frida 基本使用流程
1️⃣ 安装 Frida
pip install frida-tools # 安装命令行工具
pip install frida # 安装 Python 模块
2️⃣ 安装 Frida Server(Android 为例)
- 在 https://github.com/frida/frida/releases 下载对应架构的
frida-server
(如frida-server-16.x.x-android-arm64
) - 推送到 Android 设备:
adb push frida-server /data/local/tmp/
adb shell "chmod 755 /data/local/tmp/frida-server"
adb shell "/data/local/tmp/frida-server &"
🔍 常用命令
连接设备
frida-ps -U # 列出设备上的进程(-U 表示 USB 设备)
frida-trace -U -n com.example.app # 追踪某个包名
注入 JS 脚本
frida -U -n com.example.app -l hook.js
hook.js
可以是以下内容:
Java.perform(function () {
var MainActivity = Java.use("com.example.app.MainActivity");
MainActivity.secretMethod.implementation = function () {
console.log("secretMethod() 被调用!");
return "Hook 成功";
};
});
💡 示例:Hook 函数返回值(Android)
Java.perform(function () {
var targetClass = Java.use("com.example.secureapp.LoginManager");
targetClass.isUserLoggedIn.implementation = function () {
console.log("isUserLoggedIn 被调用,修改返回值为 true");
return true;
};
});
🛠️ 典型用例
用例 | 描述 |
---|---|
函数参数监控 | 打印函数输入参数 |
函数返回值修改 | 返回伪造数据 |
Bypass SSL Pinning | 绕过证书校验 |
Bypass Root 检测 | Hook 检测函数使其返回 false |
动态分析 Native 函数 | 使用 Interceptor.attach() hook native 层 |
🔧 Hook Native 函数(lib.so)
Interceptor.attach(Module.findExportByName("libnative-lib.so", "native_check"), {
onEnter: function (args) {
console.log("native_check 参数: " + args[0]);
},
onLeave: function (retval) {
console.log("修改前返回值: " + retval);
retval.replace(1); // 强制返回成功
}
});
⚙️ 配合 Python 使用(frida-python)
import frida
def on_message(message, data):
print("[*] Message:", message)
session = frida.get_usb_device().attach("com.example.app")
script = session.create_script(open("hook.js").read())
script.on("message", on_message)
script.load()
input("Press Enter to quit...")
📚 参考链接
- 官方网站:https://frida.re
- GitHub:https://github.com/frida/frida
- 教程文档:https://frida.re/docs/home/
- 安卓实战示例(中文):https://xz.aliyun.com/t/11814
发表回复