frida如何hook多进程app
时间: 2023-11-29 12:05:50 浏览: 90
Frida 可以通过在多个进程之间切换来 hook 多进程应用程序。以下是一些步骤:
1. 在脚本中使用 `frida.get_device_manager().enumerate_devices()` 枚举所有设备,并找到你要 hook 的设备。
2. 使用 `frida.get_device(target).attach(pid)` 来 attach 目标进程。
3. 在脚本中设置 `on_enter` 和 `on_leave` 回调函数。这些回调函数将在进程切换时被调用。
4. 使用 `frida.get_device(target).resume(pid)` 来使进程继续执行。
当进程切换时,`on_leave` 回调函数将被调用,然后 `on_enter` 回调函数将被调用。你可以在这些回调函数中执行你的 hook 代码。
以下是一个示例代码,它演示了如何在多个进程之间切换并 hook 应用程序:
```
import frida
def on_message(message, data):
print(message)
def on_enter():
print("Entering process")
def on_leave():
print("Leaving process")
def main():
session = None
target = frida.get_usb_device()
for process in target.enumerate_processes():
if process.name == "target_app":
pid = process.pid
session = target.attach(pid)
break
if session is None:
print("Could not find target process")
return
script = session.create_script("""
console.log("Script loaded");
function hook_function() {
console.log("Function hooked");
}
Interceptor.attach(Module.findExportByName(null, "function_name"), {
onEnter: function(args) {
hook_function();
}
});
""")
script.on("message", on_message)
script.load()
while True:
target.resume(pid)
target.on("stopped", on_leave)
target.on("resumed", on_enter)
print("Waiting for process to stop")
target.wait_for_stopped()
print("Process stopped")
```
在这个示例中,我们首先枚举了所有设备,并找到了要 hook 的设备。然后,我们 attach 目标进程,并创建了一个 script 对象。在 script 对象中,我们定义了一个回调函数 `hook_function()`,然后通过 `Interceptor.attach()` 函数 hook 了一个函数。最后,我们使用 `target.wait_for_stopped()` 来等待进程停止,并在进程切换时打印一些信息。