Struct RawHook

Source
pub struct RawHook { /* private fields */ }

Implementations§

Source§

impl RawHook

Source

pub unsafe fn new( struct_vtable_field_ptr: *mut RawVTable, original_vtable: Option<VTable>, ) -> Self

Examples found in repository?
examples/copy_raw.rs (lines 39-42)
31fn main() {
32    unsafe {
33        /* The same classes, but one is our victim, and the other is unaffected. */
34        let mut victim_cpp_class = CppClass::default();
35        let unaffected_cpp_class = CppClass::default();
36
37        /* Setting up raw hook */
38        let original_vtable = vtable_hook::VTable::new_with_size(victim_cpp_class.vtable as _, 1);
39        let mut raw_hook = vtable_hook::hook::copy::raw::RawHook::new(
40            &mut victim_cpp_class.vtable as *mut _ as _,
41            Some(original_vtable),
42        );
43        eprintln!("Raw hook: {raw_hook:#?}");
44
45        /* Hooks are unset now, let's test that its true */
46        eprintln!("-- Hook is disabled -- ");
47        eprintln!(
48            "victim_cpp_class's raw_hook is_enabled {}",
49            raw_hook.is_enabled()
50        );
51        eprintln!(
52            "victim_cpp_class foo() result = {}",
53            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
54        );
55        eprintln!(
56            "unaffected_cpp_class foo() result = {}",
57            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
58        );
59        /* victim_cpp_class's raw_hook is_enabled false
60         * victim_cpp_class foo() result = 0
61         * unaffected_cpp_class foo() result = 0 */
62
63        /* Replacing foo inside raw_hook */
64        raw_hook.replace_method(0, foo_hooked as _);
65        /* Replacing victim_cpp_class's VTable */
66        raw_hook.enable();
67        /* Testing */
68        eprintln!("-- Hook is enabled -- ");
69        eprintln!(
70            "victim_cpp_class's raw_hook is_enabled {}",
71            raw_hook.is_enabled()
72        );
73        eprintln!(
74            "victim_cpp_class foo() result = {}",
75            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
76        );
77        eprintln!(
78            "unaffected_cpp_class foo() result = {}",
79            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
80        );
81        /* victim_cpp_class's raw_hook is_enabled true
82         * victim_cpp_class foo() result = 1
83         * unaffected_cpp_class foo() result = 0 */
84
85        /* Restoring victim_cpp_class's VTable */
86        raw_hook.disable();
87        /* NOTE: raw_hook's foo method still points to our function,
88         * but we have restored original VTable inside victim_cpp_class
89         * To restore methods inside raw_hook use raw_hook.restore_method()
90         * or raw_hook.restore_all() */
91        eprintln!("-- Hook is disabled -- ");
92        eprintln!(
93            "victim_cpp_class's raw_hook is_enabled {}",
94            raw_hook.is_enabled()
95        );
96        eprintln!(
97            "victim_cpp_class foo() result = {}",
98            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
99        );
100        eprintln!(
101            "unaffected_cpp_class foo() result = {}",
102            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
103        );
104        /* victim_cpp_class's raw_hook is_enabled false
105         * victim_cpp_class foo() result = 0
106         * unaffected_cpp_class foo() result = 0 */
107    }
108}
Source

pub unsafe fn is_enabled(&self) -> bool

Examples found in repository?
examples/copy_raw.rs (line 49)
31fn main() {
32    unsafe {
33        /* The same classes, but one is our victim, and the other is unaffected. */
34        let mut victim_cpp_class = CppClass::default();
35        let unaffected_cpp_class = CppClass::default();
36
37        /* Setting up raw hook */
38        let original_vtable = vtable_hook::VTable::new_with_size(victim_cpp_class.vtable as _, 1);
39        let mut raw_hook = vtable_hook::hook::copy::raw::RawHook::new(
40            &mut victim_cpp_class.vtable as *mut _ as _,
41            Some(original_vtable),
42        );
43        eprintln!("Raw hook: {raw_hook:#?}");
44
45        /* Hooks are unset now, let's test that its true */
46        eprintln!("-- Hook is disabled -- ");
47        eprintln!(
48            "victim_cpp_class's raw_hook is_enabled {}",
49            raw_hook.is_enabled()
50        );
51        eprintln!(
52            "victim_cpp_class foo() result = {}",
53            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
54        );
55        eprintln!(
56            "unaffected_cpp_class foo() result = {}",
57            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
58        );
59        /* victim_cpp_class's raw_hook is_enabled false
60         * victim_cpp_class foo() result = 0
61         * unaffected_cpp_class foo() result = 0 */
62
63        /* Replacing foo inside raw_hook */
64        raw_hook.replace_method(0, foo_hooked as _);
65        /* Replacing victim_cpp_class's VTable */
66        raw_hook.enable();
67        /* Testing */
68        eprintln!("-- Hook is enabled -- ");
69        eprintln!(
70            "victim_cpp_class's raw_hook is_enabled {}",
71            raw_hook.is_enabled()
72        );
73        eprintln!(
74            "victim_cpp_class foo() result = {}",
75            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
76        );
77        eprintln!(
78            "unaffected_cpp_class foo() result = {}",
79            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
80        );
81        /* victim_cpp_class's raw_hook is_enabled true
82         * victim_cpp_class foo() result = 1
83         * unaffected_cpp_class foo() result = 0 */
84
85        /* Restoring victim_cpp_class's VTable */
86        raw_hook.disable();
87        /* NOTE: raw_hook's foo method still points to our function,
88         * but we have restored original VTable inside victim_cpp_class
89         * To restore methods inside raw_hook use raw_hook.restore_method()
90         * or raw_hook.restore_all() */
91        eprintln!("-- Hook is disabled -- ");
92        eprintln!(
93            "victim_cpp_class's raw_hook is_enabled {}",
94            raw_hook.is_enabled()
95        );
96        eprintln!(
97            "victim_cpp_class foo() result = {}",
98            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
99        );
100        eprintln!(
101            "unaffected_cpp_class foo() result = {}",
102            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
103        );
104        /* victim_cpp_class's raw_hook is_enabled false
105         * victim_cpp_class foo() result = 0
106         * unaffected_cpp_class foo() result = 0 */
107    }
108}
Source

pub unsafe fn enable(&mut self) -> bool

Examples found in repository?
examples/copy_raw.rs (line 66)
31fn main() {
32    unsafe {
33        /* The same classes, but one is our victim, and the other is unaffected. */
34        let mut victim_cpp_class = CppClass::default();
35        let unaffected_cpp_class = CppClass::default();
36
37        /* Setting up raw hook */
38        let original_vtable = vtable_hook::VTable::new_with_size(victim_cpp_class.vtable as _, 1);
39        let mut raw_hook = vtable_hook::hook::copy::raw::RawHook::new(
40            &mut victim_cpp_class.vtable as *mut _ as _,
41            Some(original_vtable),
42        );
43        eprintln!("Raw hook: {raw_hook:#?}");
44
45        /* Hooks are unset now, let's test that its true */
46        eprintln!("-- Hook is disabled -- ");
47        eprintln!(
48            "victim_cpp_class's raw_hook is_enabled {}",
49            raw_hook.is_enabled()
50        );
51        eprintln!(
52            "victim_cpp_class foo() result = {}",
53            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
54        );
55        eprintln!(
56            "unaffected_cpp_class foo() result = {}",
57            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
58        );
59        /* victim_cpp_class's raw_hook is_enabled false
60         * victim_cpp_class foo() result = 0
61         * unaffected_cpp_class foo() result = 0 */
62
63        /* Replacing foo inside raw_hook */
64        raw_hook.replace_method(0, foo_hooked as _);
65        /* Replacing victim_cpp_class's VTable */
66        raw_hook.enable();
67        /* Testing */
68        eprintln!("-- Hook is enabled -- ");
69        eprintln!(
70            "victim_cpp_class's raw_hook is_enabled {}",
71            raw_hook.is_enabled()
72        );
73        eprintln!(
74            "victim_cpp_class foo() result = {}",
75            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
76        );
77        eprintln!(
78            "unaffected_cpp_class foo() result = {}",
79            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
80        );
81        /* victim_cpp_class's raw_hook is_enabled true
82         * victim_cpp_class foo() result = 1
83         * unaffected_cpp_class foo() result = 0 */
84
85        /* Restoring victim_cpp_class's VTable */
86        raw_hook.disable();
87        /* NOTE: raw_hook's foo method still points to our function,
88         * but we have restored original VTable inside victim_cpp_class
89         * To restore methods inside raw_hook use raw_hook.restore_method()
90         * or raw_hook.restore_all() */
91        eprintln!("-- Hook is disabled -- ");
92        eprintln!(
93            "victim_cpp_class's raw_hook is_enabled {}",
94            raw_hook.is_enabled()
95        );
96        eprintln!(
97            "victim_cpp_class foo() result = {}",
98            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
99        );
100        eprintln!(
101            "unaffected_cpp_class foo() result = {}",
102            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
103        );
104        /* victim_cpp_class's raw_hook is_enabled false
105         * victim_cpp_class foo() result = 0
106         * unaffected_cpp_class foo() result = 0 */
107    }
108}
Source

pub unsafe fn disable(&mut self) -> bool

Examples found in repository?
examples/copy_raw.rs (line 86)
31fn main() {
32    unsafe {
33        /* The same classes, but one is our victim, and the other is unaffected. */
34        let mut victim_cpp_class = CppClass::default();
35        let unaffected_cpp_class = CppClass::default();
36
37        /* Setting up raw hook */
38        let original_vtable = vtable_hook::VTable::new_with_size(victim_cpp_class.vtable as _, 1);
39        let mut raw_hook = vtable_hook::hook::copy::raw::RawHook::new(
40            &mut victim_cpp_class.vtable as *mut _ as _,
41            Some(original_vtable),
42        );
43        eprintln!("Raw hook: {raw_hook:#?}");
44
45        /* Hooks are unset now, let's test that its true */
46        eprintln!("-- Hook is disabled -- ");
47        eprintln!(
48            "victim_cpp_class's raw_hook is_enabled {}",
49            raw_hook.is_enabled()
50        );
51        eprintln!(
52            "victim_cpp_class foo() result = {}",
53            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
54        );
55        eprintln!(
56            "unaffected_cpp_class foo() result = {}",
57            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
58        );
59        /* victim_cpp_class's raw_hook is_enabled false
60         * victim_cpp_class foo() result = 0
61         * unaffected_cpp_class foo() result = 0 */
62
63        /* Replacing foo inside raw_hook */
64        raw_hook.replace_method(0, foo_hooked as _);
65        /* Replacing victim_cpp_class's VTable */
66        raw_hook.enable();
67        /* Testing */
68        eprintln!("-- Hook is enabled -- ");
69        eprintln!(
70            "victim_cpp_class's raw_hook is_enabled {}",
71            raw_hook.is_enabled()
72        );
73        eprintln!(
74            "victim_cpp_class foo() result = {}",
75            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
76        );
77        eprintln!(
78            "unaffected_cpp_class foo() result = {}",
79            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
80        );
81        /* victim_cpp_class's raw_hook is_enabled true
82         * victim_cpp_class foo() result = 1
83         * unaffected_cpp_class foo() result = 0 */
84
85        /* Restoring victim_cpp_class's VTable */
86        raw_hook.disable();
87        /* NOTE: raw_hook's foo method still points to our function,
88         * but we have restored original VTable inside victim_cpp_class
89         * To restore methods inside raw_hook use raw_hook.restore_method()
90         * or raw_hook.restore_all() */
91        eprintln!("-- Hook is disabled -- ");
92        eprintln!(
93            "victim_cpp_class's raw_hook is_enabled {}",
94            raw_hook.is_enabled()
95        );
96        eprintln!(
97            "victim_cpp_class foo() result = {}",
98            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
99        );
100        eprintln!(
101            "unaffected_cpp_class foo() result = {}",
102            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
103        );
104        /* victim_cpp_class's raw_hook is_enabled false
105         * victim_cpp_class foo() result = 0
106         * unaffected_cpp_class foo() result = 0 */
107    }
108}
Source

pub unsafe fn replace_method( &mut self, index: usize, our_method: Method, ) -> Option<()>

Examples found in repository?
examples/copy_raw.rs (line 64)
31fn main() {
32    unsafe {
33        /* The same classes, but one is our victim, and the other is unaffected. */
34        let mut victim_cpp_class = CppClass::default();
35        let unaffected_cpp_class = CppClass::default();
36
37        /* Setting up raw hook */
38        let original_vtable = vtable_hook::VTable::new_with_size(victim_cpp_class.vtable as _, 1);
39        let mut raw_hook = vtable_hook::hook::copy::raw::RawHook::new(
40            &mut victim_cpp_class.vtable as *mut _ as _,
41            Some(original_vtable),
42        );
43        eprintln!("Raw hook: {raw_hook:#?}");
44
45        /* Hooks are unset now, let's test that its true */
46        eprintln!("-- Hook is disabled -- ");
47        eprintln!(
48            "victim_cpp_class's raw_hook is_enabled {}",
49            raw_hook.is_enabled()
50        );
51        eprintln!(
52            "victim_cpp_class foo() result = {}",
53            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
54        );
55        eprintln!(
56            "unaffected_cpp_class foo() result = {}",
57            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
58        );
59        /* victim_cpp_class's raw_hook is_enabled false
60         * victim_cpp_class foo() result = 0
61         * unaffected_cpp_class foo() result = 0 */
62
63        /* Replacing foo inside raw_hook */
64        raw_hook.replace_method(0, foo_hooked as _);
65        /* Replacing victim_cpp_class's VTable */
66        raw_hook.enable();
67        /* Testing */
68        eprintln!("-- Hook is enabled -- ");
69        eprintln!(
70            "victim_cpp_class's raw_hook is_enabled {}",
71            raw_hook.is_enabled()
72        );
73        eprintln!(
74            "victim_cpp_class foo() result = {}",
75            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
76        );
77        eprintln!(
78            "unaffected_cpp_class foo() result = {}",
79            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
80        );
81        /* victim_cpp_class's raw_hook is_enabled true
82         * victim_cpp_class foo() result = 1
83         * unaffected_cpp_class foo() result = 0 */
84
85        /* Restoring victim_cpp_class's VTable */
86        raw_hook.disable();
87        /* NOTE: raw_hook's foo method still points to our function,
88         * but we have restored original VTable inside victim_cpp_class
89         * To restore methods inside raw_hook use raw_hook.restore_method()
90         * or raw_hook.restore_all() */
91        eprintln!("-- Hook is disabled -- ");
92        eprintln!(
93            "victim_cpp_class's raw_hook is_enabled {}",
94            raw_hook.is_enabled()
95        );
96        eprintln!(
97            "victim_cpp_class foo() result = {}",
98            (victim_cpp_class.vtable.read().foo)(&victim_cpp_class)
99        );
100        eprintln!(
101            "unaffected_cpp_class foo() result = {}",
102            (unaffected_cpp_class.vtable.read().foo)(&unaffected_cpp_class)
103        );
104        /* victim_cpp_class's raw_hook is_enabled false
105         * victim_cpp_class foo() result = 0
106         * unaffected_cpp_class foo() result = 0 */
107    }
108}
Source

pub unsafe fn restore_method(&mut self, index: usize) -> Option<()>

Source

pub unsafe fn restore_all(&mut self)

Trait Implementations§

Source§

impl Debug for RawHook

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.