cargo install rustdllproxy
该 crate 目前仅支持标准 DLL PE 格式。
Rustdllproxy 附带两个子命令:
| 命令 | 用途 |
|---|---|
rustdllproxy new | 根据一个或多个现有 DLL 生成一个新的代理 cdylib crate。 |
rustdllproxy build | 将 .def 文件与 src/lib.rs 同步并构建该 crate。 |
rustdllproxy --help # top-level help
rustdllproxy new --help # generation flags
rustdllproxy build --help # build flags
在生成 crate 之前,请决定你希望代理如何工作。一种典型模式是搜索顺序劫持:首先将目标 DLL 重命名为类似 target_.dll 的名称,然后将编译后的代理用作 target.dll。这会形成一个类似 binary -> target.dll -> target_.dll 的调用流。
根据你的使用场景,有多种路径可选。但如果需要重命名被代理的底层 DLL,请相应更新生成的 .def 文件。
rustdllproxy new -p path/to/target_.dll -n my_proxy
提示: rustdllproxy 是使用 clap 构建的 CLI。运行 rustdllproxy --help 查看所有选项和标志。
该宏库支持 3 种主要 Hook 类型:prehook、posthook 和 fullhook。
将 #[no_mangle] 指令替换为 Hook 宏(保留 //<dllname>.dll 尾部注释)
#[prehook("dllbeingproxied.dll", "function_name")] //dllbeingproxied.dll
填写函数签名(将输入声明为 mut 以便修改它们)
使用 rustdllproxy build 构建。
prehook在原始函数之前执行代码。允许你添加功能或修改输入变量。
#[prehook("target.dll", "my_function")] //target.dll
fn my_function(mut param1: i32, mut param2: &str) {
// Your code here - executes before original function
param1 *= 2; // Modify parameters if needed
}
posthook在原始函数之后执行代码。使用神奇的 ret 变量查看和修改返回值。
#[posthook("target.dll", "calculate")] //target.dll
fn calculate(input: i32) -> i32 {
// Original function executes first
// Then your code runs with access to 'ret'
ret = ret * 2; // Modify return value
}
注意:
ret变量会自动定义为可变的。如果不需要,则无需引用它。
fullhook提供对函数执行的完全控制。手动管理返回值和函数调用。
#[fullhook("target.dll", "do_multi_add")] //target.dll
fn do_multi_add(mut a: i32, mut b: i32, mut c: i32) -> i32 {
// Pre-processing
a += 10;
b += 20;
// Call original function with magic func()
let mut return_value: i32 = func(a, b, c);
// Post-processing
return_value *= 2;
// Must explicitly return the value
return_value
}
在代理 crate 目录中运行(或将其作为第一个参数传入):
rustdllproxy build [PATH] [--profile <name>] [--no-build] [-- <extra cargo args>]
.def 文件在每次构建时都会完全重新生成,手动修改将被覆盖。如果你需要对 rustdllproxy 的构建方式进行手动修改,可以使用 cargo 来实现。假设你想通过 DLL 搜索顺序劫持来修改办公软件中使用的 office.dll:
# Rename the original DLL
mv office.dll office_.dll
rustdllproxy new -p office_.dll -n office_proxy
#[prehook("office_.dll", "open_window")] //office_.dll
fn open_window() {
// Your custom code here...
println!("Window is about to open!");
}
cd office_proxy
rustdllproxy build
构建文件位于
/target目录下
可以用单个 crate 代理多个目标 DLL。该功能很少使用,并且带有一些重要注意事项。
捆绑多个 DLL 时:
发布说明位于 CHANGELOG.md。
欢迎贡献!请随时提交 issue 和 pull request。
| 标志 | 默认值 | 作用 |
|---|
PATH | . | 代理 crate 根目录的路径。 |
--profile <name> | release | Cargo 构建配置文件(release、dev、自定义)。 |
--no-build | off | 重新生成 .def 文件,但跳过 cargo build。 |
-- <args> | — | 原样转发给 cargo build。 |