快捷搜索: 王者荣耀 脱发

frida Python与js脚本交互

前言

这篇看官方文档的下一个内容:https://frida.re/docs/messages/。使用的示例程序还是原来的程序。Python与js交互其实就是我们的程序和我们hook的程序进行交互,因为js代码是作用于hook的进程。

js发送消息

send.py

from __future__ import print_function
import frida
import sys

session = frida.attach("hello.exe")
script = session.create_script("send(1337);")

def on_message(message, data):
    print(message)
script.on(message, on_message)
script.load()
sys.stdin.read()

session.create_script的参数就是js代码段,只是调用了一个send函数,而on_message函数就可以接收这个send发送的值的。所以运行python send.py会打印 {‘type’: ‘send’, ‘payload’: 1337} 这个字典。当然除了send还有其他类型,比如error,报错信息。

Python发送消息

post.py

from __future__ import print_function
import frida
import sys

session = frida.attach("hello.exe")
script = session.create_script("""
    recv(poke, function onMessage(pokeMessage) { send(pokeBack); });
""")
def on_message(message, data):
    print(message)
script.on(message, on_message)
script.load()
script.post({
          
   "type": "poke"})
sys.stdin.read()

script.post就相当于js中的send,而recv就相当于Python中的on_message,recv的第一个参数为消息类型,第二个参数为消息回调,也就是收到消息执行的函数。运行python post.py会打印 {‘type’: ‘send’, ‘payload’: ‘pokeBack’}

js中等待接收消息

rpc.py

from __future__ import print_function
import frida
import sys

session = frida.attach("hello.exe")
script = session.create_script("""
Interceptor.attach(ptr("%s"), {
    onEnter: function(args) {
        send(args[0].toString());
        var op = recv(input, function(value) {
            args[0] = ptr(value.payload);
        });
        op.wait();
    }
});
""" % int(sys.argv[1], 16))
def on_message(message, data):
    print(message)
    val = int(message[payload], 16)
    script.post({
          
   type: input, payload: str(val * 2)})
script.on(message, on_message)
script.load()
sys.stdin.read()

op.wait()为阻塞函数,如果没有收到Python发送的消息,就一直等待接收。可以把script.post({type: input, payload: str(val * 2)})这行代码注释掉,你会发现hello.exe一直卡在那里。

cmd运行python rpc.py f函数地址就可以看到,这个代码的效果是将hello.py打印的值乘以2。

经验分享 程序员 微信小程序 职场和发展