ITPub博客

首页 > 数据库 > 数据库开发技术 > frida Python与js脚本交互

frida Python与js脚本交互

原创 数据库开发技术 作者:kanadeblisst 时间:2021-03-03 15:20:22 0 删除 编辑


前言

上一篇文章说了怎么hook获取参数和修改参数:https://blog.csdn.net/Qwertyuiop2016/article/details/114284618?spm=1001.2014.3001.5502

这篇看官方文档的下一个内容: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。

原文作者:Python成长路;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2019-05-15

  • 博文量
    117
  • 访问量
    67421