Skip to main content

symbols_aliases/
symbols_aliases.rs

1//! EN: Symbol resolution + alias registry + hook-by-alias/symbol.
2//! CN: 符号解析 + 别名注册表 + 按 alias/symbol 安装 hook。
3
4mod 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// EN: --- Unix demo: hook libc `puts` by symbol name.
21// CN: --- Unix 演示:按 symbol 名 hook libc 的 `puts`。
22#[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        // EN: Resolve address and register an alias.
31        // CN: 解析地址并注册 alias。
32        let addr = resolve_and_register_alias("libc_puts", None, sym.as_c_str())?;
33        println!("resolved puts addr = {addr:p}");
34
35        // EN: Resolve with explicit image.
36        // CN: 指定 image 名解析。
37        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        // EN: Hook by symbol (default image search).
41        // CN: 按 symbol 安装 hook(默认 image 搜索)。
42        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        // EN: Fully generic form.
52        // CN: 完整通用形式。
53        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// EN: --- Windows demo: hook a simple kernel32 API by symbol.
68// CN: --- Windows 演示:按 symbol hook kernel32 的简单 API。
69#[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    // EN: Alias registry is just a map: alias -> address (+optional metadata).
95    // CN: alias 注册表本质就是:alias -> 地址(以及可选的元信息)。
96    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    // EN: Hook by alias.
101    // CN: 按 alias 安装 hook。
102    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    // EN/CN: Optional: store richer alias metadata.
107    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}