frida_rs/
lib.rs

1//!Write [Frida](https://frida.re/) scripts in Rust thanks to the power of
2//!WebAssembly.
3//!
4//!For an example of how to structure an agent using this crate, please refer
5//!to [https://github.com/Ayrx/frida-rs-example](https://github.com/Ayrx/frida-rs-example).
6//!
7//!This crate is still a work-in-progress. The API is not stable and is
8//!subject to breaking changes until the crate reaches 1.0. Use with care.
9mod nativepointer;
10mod plumbing;
11
12pub mod console;
13pub mod cpu;
14pub mod interceptor;
15pub mod module;
16pub mod process;
17pub mod range;
18pub mod thread;
19
20pub use nativepointer::NativePointer;
21pub use plumbing::frida::ArrayBuffer;
22pub use plumbing::frida::RecvMessage;
23
24use wasm_bindgen::prelude::*;
25use wasm_bindgen::JsCast;
26
27// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
28// allocator.
29#[cfg(feature = "wee_alloc")]
30#[global_allocator]
31static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
32
33///Get the current Frida version.
34///
35///This is equivalent to calling `Frida.version` in the JavaScript API.
36pub fn version() -> &'static str {
37    &*crate::plumbing::frida::version
38}
39
40///Get the current size of Frida's private heap.
41///
42///This is equivalent to calling `Frida.heapSize` in the JavaScript API.
43pub fn heap_size() -> usize {
44    *crate::plumbing::frida::heap_size
45}
46
47///Get the runtime in use.
48///
49///This is equivalent to calling `Script.runtime` in the JavaScript API.
50pub fn runtime() -> &'static str {
51    &*crate::plumbing::script::runtime
52}
53
54///Generate a hexdump for the provided `target`.
55///
56///This is equivalent to calling `hexdump` in the JavaScript API.
57pub fn hexdump(target: &nativepointer::NativePointer) -> String {
58    crate::plumbing::frida::hexdump(target)
59}
60
61///Generate a hexdump for the provided `target`.
62///
63///This is equivalent to calling `hexdump` in the JavaScript API.
64pub fn hexdump_arraybuffer(target: &ArrayBuffer) -> String {
65    crate::plumbing::frida::hexdump_arraybuffer(target)
66}
67
68///Send a message to your Frida application.
69///
70///This is equivalent to calling `send` in the JavaScript API.
71pub fn send<T>(message: &T)
72where
73    T: serde::Serialize + ?Sized,
74{
75    crate::plumbing::frida::send(&JsValue::from_serde(&message).unwrap(), &JsValue::NULL);
76}
77
78///Send a message to your Frida application.
79///
80///This variant allows you to send raw bytes along with your message. This is
81///equivalent to calling `send` in the JavaScript API.
82pub fn send_with_byte_array<T>(message: &T, data: &[u8])
83where
84    T: serde::Serialize + ?Sized,
85{
86    let data = js_sys::Uint8Array::from(data);
87    crate::plumbing::frida::send(
88        &JsValue::from_serde(&message).unwrap(),
89        &data.unchecked_into(),
90    );
91}
92
93///Receive a message from your Frida application.
94///
95///This is equivalent to calling `recv` in the JavaScript API.
96pub fn recv(callback: Box<dyn FnMut(RecvMessage, Option<ArrayBuffer>)>) {
97    let c = Closure::wrap(callback);
98    let f: &js_sys::Function = c.as_ref().unchecked_ref();
99    crate::plumbing::frida::recv(f);
100    c.forget();
101}
102
103///Receive a message from your Frida application.
104///
105///This variant allows you to filter the received messages by the "type" field.
106///This is equivalent to calling `recv` in the JavaScript API.
107pub fn recv_with_type(
108    type_filter: &str,
109    callback: Box<dyn FnMut(RecvMessage, Option<ArrayBuffer>)>,
110) {
111    let c = Closure::wrap(callback);
112    let f: &js_sys::Function = c.as_ref().unchecked_ref();
113    crate::plumbing::frida::recv_with_type(type_filter, f);
114    c.forget();
115}