
Generate Proxy DLLs in Rust
cargo install rustdllproxy
このクレートは現在、標準のDLL PE形式のみをサポートしています。
Rustdllproxy には2つのサブコマンドが用意されています:
| Command | Purpose |
|---|---|
rustdllproxy new | 1つ以上の既存DLLから新しいプロキシ cdylib クレートを生成します。 |
rustdllproxy build | .def ファイルを src/lib.rs と同期させ、クレートをビルドします。 |
rustdllproxy --help # top-level help
rustdllproxy new --help # generation flags
rustdllproxy build --help # build flags
クレートを生成する前に、プロキシをどのように動作させるかを決めてください。典型的なパターンは検索順序ハイジャックです。まず対象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 を実行してください。
マクロライブラリは、prehook、posthook、fullhook の3つの主要なフックタイプをサポートしています。
#[no_mangle] ディレクティブをフックマクロに置き換えます(末尾の //<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変数は自動的に mutable として定義されます。不要な場合は参照する必要はありません。
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
}
プロキシクレートのディレクトリから実行します(または最初の引数として渡します):
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にあります。
単一のクレートで複数のターゲットDLLをプロキシすることが可能です。この機能はほとんど使用されず、いくつかの重要な注意点があります。
複数のDLLをバンドルする場合:
リリースノートは CHANGELOG.md にあります。
貢献は歓迎します!お気軽に issue や pull request を送ってください。
| Flag | Default | Effect |
|---|
PATH | . | プロキシクレートのルートへのパス。 |
--profile <name> | release | Cargoビルドプロファイル(release、dev、カスタム)。 |
--no-build | off | .def ファイルを再生成しますが、cargo build はスキップします。 |
-- <args> | — | cargo build にそのまま転送されます。 |