MSHookFunction

Function MSHookFunction 

Source
#[unsafe(no_mangle)]
pub unsafe extern "C" fn MSHookFunction( symbol: *mut c_void, replace: *mut c_void, result: *mut *mut c_void, )
Examples found in repository?
examples/multi_arch.rs (lines 43-47)
10fn main() {
11    println!("=== Multi-Architecture Hook Example ===\n");
12
13    #[cfg(target_arch = "x86_64")]
14    println!("Architecture: x86-64 (64-bit Intel/AMD)");
15
16    #[cfg(target_arch = "x86")]
17    println!("Architecture: x86 (32-bit Intel/AMD)");
18
19    #[cfg(target_arch = "arm")]
20    println!("Architecture: ARMv7 (32-bit ARM with Thumb)");
21
22    #[cfg(target_arch = "aarch64")]
23    println!("Architecture: ARM64/AArch64 (64-bit ARM)");
24
25    println!("\nThis example shows architecture-specific behavior.\n");
26
27    unsafe {
28        let target: *mut c_void = std::ptr::null_mut();
29
30        #[cfg(target_arch = "aarch64")]
31        {
32            println!("Using A64HookFunction for ARM64...");
33            substrate::A64HookFunction(
34                target,
35                my_hook as *mut c_void,
36                &mut ORIGINAL
37            );
38        }
39
40        #[cfg(not(target_arch = "aarch64"))]
41        {
42            println!("Using MSHookFunction...");
43            substrate::MSHookFunction(
44                target,
45                my_hook as *mut c_void,
46                &mut ORIGINAL
47            );
48        }
49
50        println!("Hook API called successfully!");
51        println!("\nNote: This example uses null pointers for demonstration.");
52        println!("In real usage, provide valid function addresses.");
53    }
54}
More examples
Hide additional examples
examples/library_hook.rs (lines 38-42)
18fn main() {
19    println!("=== Library Function Hook Example ===\n");
20
21    println!("This example demonstrates hooking a function in a shared library.");
22    println!("Note: This requires a target library to be loaded.\n");
23
24    let library_name = "libexample.so";
25    let function_offset = 0x1234;
26
27    unsafe {
28        println!("Checking if {} is loaded...", library_name);
29
30        if utils::is_library_loaded(library_name) {
31            println!("✓ Library is loaded!");
32
33            match utils::get_absolute_address(library_name, function_offset) {
34                Ok(addr) => {
35                    println!("Target address: 0x{:x}", addr);
36                    println!("Installing hook...");
37
38                    MSHookFunction(
39                        addr as *mut c_void,
40                        my_hooked_function as *mut c_void,
41                        &mut OLD_FUNCTION
42                    );
43
44                    println!("✓ Hook installed!");
45                    println!("Original function: {:p}", OLD_FUNCTION);
46                }
47                Err(e) => {
48                    eprintln!("✗ Failed to get address: {}", e);
49                }
50            }
51        } else {
52            println!("✗ Library '{}' is not loaded", library_name);
53            println!("\nTo use this example:");
54            println!("1. Replace 'libexample.so' with your target library");
55            println!("2. Replace 0x1234 with the actual function offset");
56            println!("3. Make sure the library is loaded before running");
57        }
58    }
59}
examples/android_game_hook.rs (lines 67-71)
45fn main() {
46    println!("=== Android Game Hook Example (IL2CPP/Unity) ===\n");
47
48    let library = "libil2cpp.so";
49
50    let update_offset_str = "0x123456";
51    let fixed_update_offset_str = "0x789ABC";
52
53    println!("Waiting for {} to load...", library);
54
55    if wait_for_library(library, 30) {
56        println!("✓ {} loaded!", library);
57
58        unsafe {
59            match string_to_offset(update_offset_str) {
60                Ok(update_offset) => {
61                    println!("\nHooking Update() at offset: 0x{:X}", update_offset);
62
63                    match get_absolute_address(library, update_offset) {
64                        Ok(addr) => {
65                            println!("Absolute address: 0x{:x}", addr);
66
67                            MSHookFunction(
68                                addr as *mut c_void,
69                                hooked_update as *mut c_void,
70                                &mut OLD_UPDATE
71                            );
72
73                            if !OLD_UPDATE.is_null() {
74                                println!("✓ Update() hooked successfully!");
75                            }
76                        }
77                        Err(e) => eprintln!("✗ Failed to get address: {}", e),
78                    }
79                }
80                Err(e) => eprintln!("✗ Invalid offset: {}", e),
81            }
82
83            match string_to_offset(fixed_update_offset_str) {
84                Ok(fixed_offset) => {
85                    println!("\nHooking FixedUpdate() at offset: 0x{:X}", fixed_offset);
86
87                    match get_absolute_address(library, fixed_offset) {
88                        Ok(addr) => {
89                            println!("Absolute address: 0x{:x}", addr);
90
91                            MSHookFunction(
92                                addr as *mut c_void,
93                                hooked_fixed_update as *mut c_void,
94                                &mut OLD_FIXED_UPDATE
95                            );
96
97                            if !OLD_FIXED_UPDATE.is_null() {
98                                println!("✓ FixedUpdate() hooked successfully!");
99                            }
100                        }
101                        Err(e) => eprintln!("✗ Failed to get address: {}", e),
102                    }
103                }
104                Err(e) => eprintln!("✗ Invalid offset: {}", e),
105            }
106
107            println!("\n=== Hooks Installed ===");
108            println!("The game functions will now call your hooks.");
109            println!("\nNote: Replace the offset values with real ones from your game!");
110        }
111    } else {
112        eprintln!("✗ Timeout waiting for {} to load", library);
113        eprintln!("\nTo use this example:");
114        eprintln!("1. Find function offsets using IDA Pro, Ghidra, or similar");
115        eprintln!("2. Replace the offset strings with actual values");
116        eprintln!("3. Run as part of an injected library in the game process");
117    }
118}