frida / frida-rust

Frida Rust bindings
Other
188 stars 53 forks source link

feat (script): Add post method to Script #134

Closed charleywright closed 5 months ago

charleywright commented 6 months ago

This PR adds support for sending data to scripts which can be read using the recv function as detailed in the frida docs. Until RPC support is implemented (#105) this can be used to achieve a similar effect.

EDIT: RPC appears to be implemented using messages, see the python implementation here which could make this PR a requirement for an RPC implementation

s1341 commented 5 months ago

Can you please rebase on main?

charleywright commented 5 months ago

Done

s1341 commented 5 months ago

Thanks!

Xoffio commented 2 months ago

Hi @charleywright, Could you add an example?

I have been trying the post function but I can't get it to work..

I have something like this:

        // .....
        let session = device.attach(pid).unwrap();

            let script_source = r#"
                var globalVar = 0;

                recv('input', function(message) {
                    console.log("--->", message);
                    if (message.payload === 'increment') {
                        globalVar += 1;
                        console.log("- gv: ", globalVar);
                        send('incremented: ' + globalVar);
                    } else if (message.payload === 'getvalue') {
                        send('globalVar value: ' + globalVar);
                        console.log("- gv: ", globalVar);
                    }
                });
            "#;
            let mut script_option = frida::ScriptOption::new()
                .set_name("example")
                .set_runtime(frida::ScriptRuntime::QJS);
            let script = session
                .create_script(script_source, &mut script_option)
                .unwrap();

            script.handle_message(&mut Handler).unwrap();

            script.load().unwrap();
            println!("[*] Script loaded.");

            // Post messages to the script
            script.post("input", Some("increment".as_bytes())).unwrap();
            script.post("input", Some("getvalue".as_bytes())).unwrap();

            script.unload().unwrap();
            println!("[*] Script unloaded");
           //....

But it doesn't show the changes.. I also tried adding .wait() to recv but the program seems to hang and then fail.

I can also add an example if I can get some guidance. Really appreciate the time. Thanks!

Xoffio commented 2 months ago

Never mind, I have a working example. I will create a pull request with it.