#64-bit #hook #32 #windows

trampoline

A Rust library for function hooking that supports both 32 and 64 bit

1 unstable release

0.1.0 Aug 1, 2021

#4 in #32

MIT license

15KB
235 lines

trampoline

trampoline is a function hooking library that supports both 32 and 64 bit.

OS support

OS Architecture
Windows x32, x64

License

This project is licensed under MIT License (LICENSE-MIT)


lib.rs:

trampoline - A Rust library for function hooking that supports both 32 and 64 bit.

Example

[dependencies]
windows = "0.18.0"
once_cell = "1.8.0"
trampoline = "0.1.0"

[build-dependencies]
windows = "0.18.0"
fn main() {
    windows::build!(
        Windows::Win32::Foundation::{HANDLE, BOOL},
        Windows::Win32::System::LibraryLoader::{GetProcAddress, GetModuleHandleA},
    );
}
use crate::bindings::Windows::Win32::Foundation::{HANDLE, BOOL};
use crate::bindings::Windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
use std::ffi::c_void;
use std::sync::Mutex;
use std::mem::transmute;
use once_cell::sync::Lazy;
use trampoline::TrampolineHook;

mod bindings {
    windows::include_bindings!();
}

static HOOK: Lazy<Mutex<Option<TrampolineHook>>> = Lazy::new(|| {
    Mutex::new(None)
});

pub extern "stdcall" fn wgl_swap_buffers(hdc: HANDLE) -> BOOL {
    let gateway = HOOK
        .lock()
        .unwrap()
        .as_ref()
        .unwrap()
        .gateway();

    let gateway_call: extern "stdcall" fn(hdc: HANDLE) -> BOOL;
    gateway_call = unsafe { transmute(gateway) };
    gateway_call(hdc);

    BOOL::from(true)
}

fn main() {
    let module = unsafe { GetModuleHandleA("opengl32.dll") };
    let src_wgl_swap_buffers = unsafe {
        GetProcAddress(module, "wglSwapBuffers")
    }.unwrap();

    let hook = TrampolineHook::hook(
        src_wgl_swap_buffers as *mut c_void,
        wgl_swap_buffers as *mut c_void,
        21
    ).unwrap();

    *HOOK
        .lock()
        .unwrap() = Some(hook);
}

Dependencies

~123MB
~2M SLoC