From 14e6572521de397d15e97547ee91d4e9dbdea586 Mon Sep 17 00:00:00 2001 From: Dimitris Zervas Date: Sat, 27 May 2023 21:45:02 +0300 Subject: [PATCH] Call LoadLibraryA on DllMain to fix crash --- Cargo.toml | 2 +- build.rs | 1 + src/lib.rs | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e0bfa4..7afa1a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ lazy_static = "1.4.0" ctor = "0.2.0" [target.'cfg(windows)'.dependencies] -winapi = { version = "0.3.9", features = ["winnt"] } +winapi = { version = "0.3.9", features = ["winnt", "libloaderapi"] } [build-dependencies] goblin = "0.6.1" diff --git a/build.rs b/build.rs index 79680b2..8d9a5b8 100644 --- a/build.rs +++ b/build.rs @@ -33,5 +33,6 @@ fn main() { // println!("cargo:rustc-link-lib=dylib={}-orig", lib_name); } println!("cargo:warning=Expected library name: {}-orig.dll", lib_name); + println!("cargo:rustc-env=LIB_NAME={}-orig.dll", lib_name); } } diff --git a/src/lib.rs b/src/lib.rs index 49e07d1..248743e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,8 @@ use std::ffi::c_void; #[cfg(windows)] use winapi::um::winnt::DLL_PROCESS_ATTACH; +#[cfg(windows)] +use winapi::um::libloaderapi::LoadLibraryA; #[cfg(windows)] #[no_mangle] @@ -27,8 +29,9 @@ extern "system" fn DllMain(dll_module: *mut c_void, call_reason: u32, _: *mut () match call_reason { DLL_PROCESS_ATTACH => { println!("[+] frida-deepfreeze-rs DLL injected"); + unsafe { LoadLibraryA(env!("LIB_NAME").as_ptr() as *const i8); } + println!("[+] Original DLL {} loaded", env!("LIB_NAME")); attach_self(); - } // Maybe we should detach? Is it useful? _ => ()