Writing process memory in python

Frida's writeByteArray failed with me to write in read only memory.

A quick solution was to send the bytes and the address I want to write at, to python.

Using ReadWriteMemory library it's really easy to write into process memory, also it has good documentation.


import frida
from ReadWriteMemory import ReadWriteMemory

rwm = ReadWriteMemory()
process = rwm.get_process_by_id(YOUR_PROCESS_ID_HERE)

def onmessage(message, data):
    global process
    message = json.loads(message['payload'])
    if message['type'] == "patch":
        ptr = message['address']
        for idx, byte in enumerate(message['patch']):
            process.write(ptr + idx, byte)

device = frida.get_local_device()
session = device.attach(YOUR_PROCESS_ID_HERE)
script = session.create_script(YOUR_JAVASCRIPT_CODE_STRING)
script.on("message", onmessage)

and now in your Frida's JavaScript code, you can send a JSON like this

    type: 'patch',
    address: ptr.toInt32(),
    patch: [0x90, 0x90, 0x90]