如何在 Frida 中使用 rpc 暴露?

How to use rpc expose in Frida?

我尝试使用 rpc.exports.dispose 但它不起作用。 这是我试过的代码:

import frida
import sys

device = frida.get_local_device()
session = device.attach('simple')
script = session.create_script("""
    rpc.exports.dispose = function() {
        console.log('dispose');
    };
""")
script.load()
sys.stdin.read()

根据 documentation 应在挂钩进程终止或脚本卸载之前执行处置。

我通过几种方式终止了 "simple" 进程(单击 Ctrl+C、运行 "kill -9 "、运行 "kill ")。 但我看不到处置日志。我正在使用 Ubuntu。 你能告诉我我的代码有什么问题吗? 谢谢!

在我看来,依赖在进程终止时执行的代码是一个坏主意。太多的事情可能会出错,代码不会被执行。

更好的方法是在 Python 端注册 detached 处理程序:

def on_detached():
    print("on_detached")

def on_detached_with_reason(reason):
    print("on_detached_with_reason:", reason)

def on_detached_with_varargs(*args):
    print("on_detached_with_varargs:", args)

session = frida.attach("simple")
print("attached")
session.on('detached', on_detached)
session.on('detached', on_detached_with_reason)
session.on('detached', on_detached_with_varargs)