symbols_aliases/
symbols_aliases.rs1mod common;
5
6use core::ffi::c_void;
7use dobby_rs_framework::hook_utils;
8use dobby_rs_framework::prelude::*;
9use std::ffi::CString;
10
11#[inline(never)]
12fn detour_add(x: i32) -> i32 {
13 let original = unsafe {
14 hook_utils::original::<fn(i32) -> i32>(detour_add as *const () as *mut c_void)
15 .expect("original not found")
16 };
17 original(x) + 1000
18}
19
20#[cfg(unix)]
23mod unix_demo {
24 use super::*;
25
26 pub fn run() -> dobby_rs_framework::Result<()> {
27 let sym = CString::new("puts").unwrap();
28 let img = CString::new(common::LIBC_IMAGE).unwrap();
29
30 let addr = resolve_and_register_alias("libc_puts", None, sym.as_c_str())?;
33 println!("resolved puts addr = {addr:p}");
34
35 let _ = resolve_and_register_alias_in("libc_puts_in", img.as_c_str(), sym.as_c_str())?;
38 register_alias_with_symbol_in("libc_puts_meta_in", img.as_c_str(), sym.as_c_str(), addr)?;
39
40 let h1 = unsafe {
43 hook_symbol_default(
44 sym.as_c_str(),
45 common::detours::unix::detour_puts as *const () as *mut c_void,
46 Some("puts"),
47 )?
48 };
49 unsafe { h1.unhook()? };
50
51 let h2 = unsafe {
54 hook_symbol(
55 None,
56 sym.as_c_str(),
57 common::detours::unix::detour_puts as *const () as *mut c_void,
58 Some("puts2"),
59 )?
60 };
61 unsafe { h2.unhook()? };
62
63 Ok(())
64 }
65}
66
67#[cfg(windows)]
70mod windows_demo {
71 use super::*;
72
73 pub fn run() -> dobby_rs_framework::Result<()> {
74 let image = CString::new("kernel32.dll").unwrap();
75 let sym = CString::new("GetTickCount").unwrap();
76
77 let _ = resolve_and_register_alias_in("k32_get_tick", image.as_c_str(), sym.as_c_str())?;
78 let h = unsafe {
79 hook_symbol_in(
80 image.as_c_str(),
81 sym.as_c_str(),
82 common::detours::windows::detour_get_tick_count as *const () as *mut c_void,
83 Some("GetTickCount"),
84 )?
85 };
86 unsafe { h.unhook()? };
87 Ok(())
88 }
89}
90
91fn main() -> dobby_rs_framework::Result<()> {
92 common::init_example_logging();
93
94 let add_addr = common::target_add as *const () as *mut c_void;
97 register_alias("demo_add", add_addr)?;
98 println!("alias demo_add addr = {:p}", get_alias("demo_add").unwrap());
99
100 let h = unsafe { hook_alias("demo_add", detour_add as *const () as *mut c_void)? };
103 println!("target_add(1) after hook_alias = {}", common::target_add(1));
104 unsafe { h.unhook()? };
105
106 let fake_sym = CString::new("target_add").unwrap();
108 register_alias_with_symbol("demo_add_meta", None, fake_sym.as_c_str(), add_addr)?;
109
110 #[cfg(unix)]
111 unix_demo::run()?;
112 #[cfg(windows)]
113 windows_demo::run()?;
114
115 Ok(())
116}