Skip to main content

frida_rs/plumbing/
interceptor.rs

1use crate::plumbing::nativepointer::NativePointer;
2use std::fmt;
3use wasm_bindgen::prelude::*;
4use wasm_bindgen::JsCast;
5
6#[wasm_bindgen]
7extern "C" {
8    #[wasm_bindgen(js_namespace = Interceptor, js_name = attach)]
9    pub fn attach(target: NativePointer, callbacks: js_sys::Object);
10
11    ///Arguments to an invocation of the intercepted function.
12    ///
13    ///Use the [`get`][get] method to access the arguments by index. Frida
14    ///makes no guarantees about the validty of the index and it is possible
15    ///to index into more arguments than the intercepted function accepts.
16    ///
17    ///[get]: InvocationArgs::get
18    #[wasm_bindgen(js_name = InvocationArg)]
19    #[derive(Debug)]
20    pub type InvocationArgs;
21
22    #[wasm_bindgen(js_name = InvocationContext)]
23    #[derive(Debug)]
24    pub type InvocationContext;
25
26    #[wasm_bindgen(method, getter, js_name = returnAddress)]
27    pub fn return_address(this: &InvocationContext) -> NativePointer;
28
29    #[wasm_bindgen(method, getter, js_name = context)]
30    pub fn context(this: &InvocationContext) -> crate::plumbing::cpu::CpuContext;
31
32    #[wasm_bindgen(method, getter, js_name = threadId)]
33    pub fn thread_id(this: &InvocationContext) -> u32;
34
35    #[wasm_bindgen(method, getter, js_name = depth)]
36    pub fn depth(this: &InvocationContext) -> u32;
37
38    #[wasm_bindgen(method, structural, indexing_getter)]
39    pub fn get(this: &InvocationContext, prop: &str) -> JsValue;
40
41    #[wasm_bindgen(method, structural, indexing_setter)]
42    pub fn set(this: &InvocationContext, prop: &str, val: JsValue);
43
44    ///Return value for an invocation of the intercepted function.
45    ///
46    ///This is an extension of [`NativePointer`][NativePointer] with an
47    ///additional [`replace`][replace] method.
48    ///
49    ///[NativePointer]: NativePointer
50    ///[replace]: InvocationReturnValue::replace
51    #[wasm_bindgen(js_name = InvocationReturnValue, extends = NativePointer)]
52    #[derive(Debug)]
53    pub type InvocationReturnValue;
54
55    #[wasm_bindgen(constructor)]
56    fn new(s: &str) -> InvocationReturnValue;
57
58    ///Replace the return value for an invocation of the intercepted function.
59    #[wasm_bindgen(method, js_name = replace)]
60    pub fn replace(this: &InvocationReturnValue, value: NativePointer);
61}
62
63impl InvocationArgs {
64    pub fn get(&self, index: u32) -> NativePointer {
65        let a: &js_sys::Array = &self.unchecked_ref();
66        a.get(index).unchecked_into()
67    }
68}
69
70impl fmt::Display for InvocationReturnValue {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        let p: &NativePointer = self.as_ref();
73        write!(f, "{}", p.to_string())
74    }
75}