python-3.xfrida

enumerateModules with Frida Python


I use Frida with Python.

Connect to remote process

import frida


frida.get_device_manager().add_remote_device('192.168.3.55:2222').attach(1234)

How can I enumerate modules like I did in Frida cli(Javascript)

Process.enumerateModules();

But with Python?


Solution

  • The process is the same as when using frida command, the Python code can only be used on client side. For server side you have to load Frida JavaScript code.

    You can do this by integrating a JavaScript file as String into your Python code:

    Frida Python Script with integrated JavaScript source

    import frida, sys
     
    script = """
        Process.enumerateModules({
            onMatch: function(module){
                console.log('Module name: ' + module.name + " - " + "Base Address: " + module.base.toString());
            }, 
            onComplete: function(){}
        });
    """
    session = frida.get_device_manager().add_remote_device('192.168.3.55:2222').attach(1234)
    script = session.create_script(script)
    script.load()
    sys.stdin.read()
    

    Or the way I prefer, having a separate JavaScript file that is loaded in the Python code:

    Frida Python Script and external JavaScript source file

    script.js

    rpc.exports = {
        modules: function() {
            return Process.enumerateModules();
        }
    }
    

    my-frida.py

    import frida, sys
    import codecs
    
    frida_js = './script.js'
    with codecs.open(frida_js, 'r', 'utf-8') as f:
        source = f.read()
    
    session = frida.get_device_manager().add_remote_device('192.168.3.55:2222').attach(1234)
    script = session.create_script(source)
    script.load()
    modules = script.exports_sync.modules() # execute rpc exported modules function in loaded script
    for m in modules:
        print(f'name={m["name"]} base={m["base"]} size={m["size"]} path={m["path"]}')
    
    sys.stdin.read()