Adding api hooking example

This commit is contained in:
Idov31
2022-04-29 17:40:08 +03:00
parent f011fb616a
commit b87f89ea95
2 changed files with 54 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
[package]
name = "apihooking"
version = "0.1.0"
authors = ["Idov31 <github.com/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"
+40
View File
@@ -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();
}
}