Search code examples
iosfrida

How to pass bytes(ArrayBuffer) between python & js in frida?


javascript code:

    rpc.exports = {
        something: function (data, size) {
            // data maybe ArrayBuffer
        }
    }

python code:

zip = gzip.compress(json.dumps(info).encode('utf8')) # zip -> bytes
script.exports.something(zip, len(zip))

According to frida's document, JavaScript can pass byte arrays to python through the send method, so is there any way to pass byte arrays from python to JavaScript?


Solution

  • To answer your question

    python

    script.exports.something([0x33, 0x34, 0x35], 3)
    

    js

        rpc.exports = {
            something: function (data, size) {
                console.log(JSON.stringify(data), size);
                // output: [51,52,53] 3
            }
        }
    
    

    More complicated example that hopefully will answer future questions;

    python side:

    def on_message(msg, data):
      if msg['payload'] == 'tag1':
        print('bytes from js:', data)
        # sending bytes to js
        script.post({'type': 'tag2', 'payload': list([0x33, 0x34, 0x35])})
    

    javascript side:

      // send bytes to python
      send("tag1", ptr(0x1234).readByteArray(64));
      recv('tag2', function (value) {
        var data = value.payload;
        console.log('received bytes from python:', data, 'size:', data.length);
      })
      .wait(); // block until python respond