From b87f89ea952f502a8c008c0a9698d97d548d57b7 Mon Sep 17 00:00:00 2001 From: Idov31 Date: Fri, 29 Apr 2022 17:40:08 +0300 Subject: [PATCH] Adding api hooking example --- apihooking/Cargo.toml | 14 ++++++++++++++ apihooking/src/main.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 apihooking/Cargo.toml create mode 100644 apihooking/src/main.rs diff --git a/apihooking/Cargo.toml b/apihooking/Cargo.toml new file mode 100644 index 0000000..67d91ea --- /dev/null +++ b/apihooking/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "apihooking" +version = "0.1.0" +authors = ["Idov31 "] +edition = "2021" + +[[bin]] +name = "apihooking" +path = "src/main.rs" + +[dependencies] +winapi = {version = "0.3.9", features = ["winuser"]} +win32-error = "0.9.0" +detour = "0.8.1" \ No newline at end of file diff --git a/apihooking/src/main.rs b/apihooking/src/main.rs new file mode 100644 index 0000000..e7ce5b8 --- /dev/null +++ b/apihooking/src/main.rs @@ -0,0 +1,40 @@ +use std::ffi::CString; +use winapi::{ + um::{ + winuser::MessageBoxA, + }, + shared::{ + windef::HWND, + minwindef::{ + UINT + }, + ntdef::LPCSTR + } +}; +use detour::static_detour; + +static_detour! { + static MsgBox: unsafe extern "system" fn(HWND, LPCSTR, LPCSTR, UINT) -> i32; +} + +unsafe fn hooked_messagebox(hwnd: HWND, lp_text: LPCSTR, lp_caption: LPCSTR, u_type: UINT) -> i32 { + println!("Hooked MessageBoxA"); + + unsafe { + MsgBox.call(hwnd, lp_text, lp_caption, u_type) + } +} + + +fn main() { + unsafe { + MsgBox.initialize(MessageBoxA, |hwnd, lp_text, lp_caption, u_type| { + println!("Hooked MessageBoxA"); + MsgBox.call(hwnd, lp_text, lp_caption, u_type) + }); + MessageBoxA(0 as HWND, CString::new("Before").unwrap().as_ptr(), CString::new("Before").unwrap().as_ptr(), 0); + MsgBox.enable(); + MessageBoxA(0 as HWND, CString::new("After").unwrap().as_ptr(), CString::new("After").unwrap().as_ptr(), 0); + MsgBox.disable(); + } +}