Search code examples
pythonfrida

Frida python return values and write them into file


I'm currently using frida-python to hook onto a function in an ios application and I want the output of the function to be written in a CSV file. How do I do that?

Python script:

import frida, sys

script = 'script.js'
bundle = 'application'
f = open(script, "r")
s = f.read()

device = frida.get_usb_device(1000)
pid = device.spawn([bundle])
session = device.attach(pid)
script = session.create_script(s)
script.load()
device.resume(pid)
sys.stdin.read()

script.js

Interceptor.attach(intercept.implementation, {
        onEnter: function (args) {
            var instance = ObjC.chooseSync(ObjC.classes.CLASS)[0];
            send(instance.toString());
            }
        },

Currently, my script can only console log out the values when I have intercepted the function. Is there any ways I can return the values to python so that I can write them into CSV files?


Solution

  • Yes there is a way. You change the on_message() function that handles the send() function in javascript.

    i.e default on_message in python:

    def on_message(message, data):
        if message['type'] == 'send':
            print("[* ] + message)
    
    .... 
    
    
    device = frida.get_usb_device()
    pid = device.spawn(["pgk"])
    session = device.attach(pid)
    
    script = open("filepath")
    
    drop = session.create_script(script.read())
    drop.on('message', on_message)
    drop.load()
    time.sleep(1)  # fails without this sleep
    device.resume(pid)
    sys.stdin.read()
    

    in your javascript you simply call

    send("Method called") // Same as console log just handled through frida

    You can find the documentation for this here:

    https://frida.re/docs/messages/