min_hook_rs 1.2.2

A Rust implementation of MinHook library for Windows x64 function hooking with enhanced precision instruction decoding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Memory buffer management for MinHook-rs
//!
//! This module handles allocation of executable memory blocks near the target functions.
//! In x64 mode, memory must be allocated within ±2GB range for relative jumps to work.

use crate::error::{HookError, Result};
use std::ffi::c_void;
use std::ptr;
use std::sync::Mutex;
use windows_sys::Win32::System::Memory::*;
use windows_sys::Win32::System::SystemInformation::*;

#[cfg(not(target_arch = "x86_64"))]
compile_error!("Buffer module only supports x86_64 architecture");

/// Size of each memory block (4KB = page size)
const MEMORY_BLOCK_SIZE: usize = 0x1000;

/// Maximum range for seeking memory blocks (±1GB, actual range is ±2GB)
const MAX_MEMORY_RANGE: usize = 0x40000000;

/// Size of each memory slot
const MEMORY_SLOT_SIZE: usize = 64;

/// Memory protection flags for executable addresses
const PAGE_EXECUTE_FLAGS: u32 =
    PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;

/// Memory slot structure
#[repr(C)]
union MemorySlot {
    /// Pointer to next free slot
    next: *mut MemorySlot,
    /// Buffer data
    buffer: [u8; MEMORY_SLOT_SIZE],
}

/// Memory block structure
#[repr(C)]
struct MemoryBlock {
    /// Pointer to next block
    next: *mut MemoryBlock,
    /// Pointer to first free slot
    free_list: *mut MemorySlot,
    /// Number of used slots
    used_count: u32,
}

unsafe impl Send for MemoryBlock {}
unsafe impl Sync for MemoryBlock {}
unsafe impl Send for MemorySlot {}
unsafe impl Sync for MemorySlot {}

/// Global memory buffer manager
struct BufferManager {
    /// First memory block in the chain
    blocks: *mut MemoryBlock,
}

// Implement Send and Sync to use Mutex
unsafe impl Send for BufferManager {}
unsafe impl Sync for BufferManager {}

impl BufferManager {
    const fn new() -> Self {
        Self {
            blocks: ptr::null_mut(),
        }
    }

    /// Initialize buffer manager
    fn initialize(&mut self) {
        // Nothing to do for now
    }

    /// Cleanup all allocated blocks
    fn uninitialize(&mut self) {
        let mut block = self.blocks;
        self.blocks = ptr::null_mut();

        while !block.is_null() {
            unsafe {
                let next = (*block).next;
                VirtualFree(block as *mut c_void, 0, MEM_RELEASE);
                block = next;
            }
        }
    }

    /// Allocate a buffer near the origin address
    fn allocate(&mut self, origin: *mut c_void) -> Result<*mut c_void> {
        let block = self.get_memory_block(origin)?;
        if block.is_null() {
            return Err(HookError::MemoryAlloc);
        }

        unsafe {
            // Get a free slot from the block
            let slot = (*block).free_list;
            if slot.is_null() {
                return Err(HookError::MemoryAlloc);
            }

            // Remove slot from free list
            (*block).free_list = (*slot).next;
            (*block).used_count += 1;

            #[cfg(debug_assertions)]
            {
                // Fill with INT3 for debugging
                ptr::write_bytes(slot as *mut u8, 0xCC, MEMORY_SLOT_SIZE);
            }

            Ok(slot as *mut c_void)
        }
    }

    /// Free an allocated buffer
    fn free(&mut self, buffer: *mut c_void) {
        if buffer.is_null() {
            return;
        }

        // Find the block containing this buffer
        let target_block_addr = (buffer as usize / MEMORY_BLOCK_SIZE) * MEMORY_BLOCK_SIZE;
        let mut block = self.blocks;
        let mut prev: *mut MemoryBlock = ptr::null_mut();

        while !block.is_null() {
            if block as usize == target_block_addr {
                unsafe {
                    let slot = buffer as *mut MemorySlot;

                    #[cfg(debug_assertions)]
                    {
                        // Clear for debugging
                        ptr::write_bytes(slot as *mut u8, 0x00, MEMORY_SLOT_SIZE);
                    }

                    // Return slot to free list
                    (*slot).next = (*block).free_list;
                    (*block).free_list = slot;
                    (*block).used_count -= 1;

                    // Free the block if completely unused
                    if (*block).used_count == 0 {
                        if !prev.is_null() {
                            (*prev).next = (*block).next;
                        } else {
                            self.blocks = (*block).next;
                        }

                        VirtualFree(block as *mut c_void, 0, MEM_RELEASE);
                    }
                }
                break;
            }

            unsafe {
                prev = block;
                block = (*block).next;
            }
        }
    }

    /// Get or create a memory block near the origin
    fn get_memory_block(&mut self, origin: *mut c_void) -> Result<*mut MemoryBlock> {
        let origin_addr = origin as usize;

        // Calculate address range for x64
        let (min_addr, max_addr) = unsafe {
            let mut si: SYSTEM_INFO = std::mem::zeroed();
            GetSystemInfo(&mut si);

            let mut min_addr = si.lpMinimumApplicationAddress as usize;
            let mut max_addr = si.lpMaximumApplicationAddress as usize;

            // origin ± 1GB (actual ±2GB for relative jumps)
            if origin_addr > MAX_MEMORY_RANGE && min_addr < origin_addr - MAX_MEMORY_RANGE {
                min_addr = origin_addr - MAX_MEMORY_RANGE;
            }

            if max_addr > origin_addr + MAX_MEMORY_RANGE {
                max_addr = origin_addr + MAX_MEMORY_RANGE;
            }

            // Reserve space for block header
            max_addr = max_addr.saturating_sub(MEMORY_BLOCK_SIZE - 1);

            (min_addr, max_addr)
        };

        // Look for existing reachable blocks with free slots
        let mut block = self.blocks;
        while !block.is_null() {
            let block_addr = block as usize;

            // Check if block is in range and has free slots
            if block_addr >= min_addr && block_addr < max_addr {
                unsafe {
                    if !(*block).free_list.is_null() {
                        return Ok(block);
                    }
                }
            }

            unsafe {
                block = (*block).next;
            }
        }

        // Need to allocate a new block
        self.allocate_new_block(origin, min_addr, max_addr)
    }

    /// Allocate a new memory block
    fn allocate_new_block(
        &mut self,
        origin: *mut c_void,
        min_addr: usize,
        max_addr: usize,
    ) -> Result<*mut MemoryBlock> {
        unsafe {
            let mut si: SYSTEM_INFO = std::mem::zeroed();
            GetSystemInfo(&mut si);

            // Try to allocate above the origin first
            let mut alloc_addr = origin;
            while alloc_addr as usize >= min_addr {
                alloc_addr = find_prev_free_region(
                    alloc_addr,
                    min_addr as *mut c_void,
                    si.dwAllocationGranularity,
                );
                if alloc_addr.is_null() {
                    break;
                }

                let block = VirtualAlloc(
                    alloc_addr,
                    MEMORY_BLOCK_SIZE,
                    MEM_COMMIT | MEM_RESERVE,
                    PAGE_EXECUTE_READWRITE,
                ) as *mut MemoryBlock;

                if !block.is_null() {
                    self.initialize_block(block);
                    return Ok(block);
                }
            }

            // Try to allocate below the origin
            let mut alloc_addr = origin;
            while (alloc_addr as usize) <= max_addr {
                alloc_addr = find_next_free_region(
                    alloc_addr,
                    max_addr as *mut c_void,
                    si.dwAllocationGranularity,
                );
                if alloc_addr.is_null() {
                    break;
                }

                let block = VirtualAlloc(
                    alloc_addr,
                    MEMORY_BLOCK_SIZE,
                    MEM_COMMIT | MEM_RESERVE,
                    PAGE_EXECUTE_READWRITE,
                ) as *mut MemoryBlock;

                if !block.is_null() {
                    self.initialize_block(block);
                    return Ok(block);
                }
            }

            Err(HookError::MemoryAlloc)
        }
    }

    /// Initialize a newly allocated block
    fn initialize_block(&mut self, block: *mut MemoryBlock) {
        unsafe {
            (*block).next = self.blocks;
            (*block).free_list = ptr::null_mut();
            (*block).used_count = 0;

            // Build free slot list
            let mut slot =
                (block as *mut u8).add(std::mem::size_of::<MemoryBlock>()) as *mut MemorySlot;
            let block_end = (block as *mut u8).add(MEMORY_BLOCK_SIZE);

            while (slot as *mut u8).add(MEMORY_SLOT_SIZE) <= block_end {
                (*slot).next = (*block).free_list;
                (*block).free_list = slot;
                slot = (slot as *mut u8).add(MEMORY_SLOT_SIZE) as *mut MemorySlot;
            }

            self.blocks = block;
        }
    }
}

/// Global buffer manager instance
static BUFFER_MANAGER: Mutex<BufferManager> = Mutex::new(BufferManager::new());

/// Find previous free region
fn find_prev_free_region(
    address: *mut c_void,
    min_addr: *mut c_void,
    granularity: u32,
) -> *mut c_void {
    let mut try_addr = address as usize;

    // Round down to allocation granularity
    try_addr -= try_addr % granularity as usize;
    try_addr = try_addr.saturating_sub(granularity as usize);

    while try_addr >= min_addr as usize {
        unsafe {
            let mut mbi: MEMORY_BASIC_INFORMATION = std::mem::zeroed();
            if VirtualQuery(
                try_addr as *const c_void,
                &mut mbi,
                std::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
            ) == 0
            {
                break;
            }

            if mbi.State == MEM_FREE {
                return try_addr as *mut c_void;
            }

            if (mbi.AllocationBase as usize) < granularity as usize {
                break;
            }

            try_addr = (mbi.AllocationBase as usize).saturating_sub(granularity as usize);
        }
    }

    ptr::null_mut()
}

/// Find next free region  
fn find_next_free_region(
    address: *mut c_void,
    max_addr: *mut c_void,
    granularity: u32,
) -> *mut c_void {
    let mut try_addr = address as usize;

    // Round down to allocation granularity
    try_addr -= try_addr % granularity as usize;
    try_addr += granularity as usize;

    while try_addr <= max_addr as usize {
        unsafe {
            let mut mbi: MEMORY_BASIC_INFORMATION = std::mem::zeroed();
            if VirtualQuery(
                try_addr as *const c_void,
                &mut mbi,
                std::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
            ) == 0
            {
                break;
            }

            if mbi.State == MEM_FREE {
                return try_addr as *mut c_void;
            }

            try_addr = mbi.BaseAddress as usize + mbi.RegionSize;

            // Round up to next allocation granularity
            try_addr += granularity as usize - 1;
            try_addr -= try_addr % granularity as usize;
        }
    }

    ptr::null_mut()
}

/// Initialize the buffer system
pub fn initialize_buffer() {
    if let Ok(mut manager) = BUFFER_MANAGER.lock() {
        manager.initialize();
    }
}

/// Uninitialize the buffer system
pub fn uninitialize_buffer() {
    if let Ok(mut manager) = BUFFER_MANAGER.lock() {
        manager.uninitialize();
    }
}

/// Allocate executable memory near the origin
pub fn allocate_buffer(origin: *mut c_void) -> Result<*mut c_void> {
    BUFFER_MANAGER
        .lock()
        .map_err(|_| HookError::MemoryAlloc)?
        .allocate(origin)
}

/// Free allocated buffer
pub fn free_buffer(buffer: *mut c_void) {
    if let Ok(mut manager) = BUFFER_MANAGER.lock() {
        manager.free(buffer);
    }
}

/// Check if address is executable
pub fn is_executable_address(address: *mut c_void) -> bool {
    unsafe {
        let mut mbi: MEMORY_BASIC_INFORMATION = std::mem::zeroed();

        if VirtualQuery(
            address as *const c_void,
            &mut mbi,
            std::mem::size_of::<MEMORY_BASIC_INFORMATION>(),
        ) == 0
        {
            return false;
        }

        mbi.State == MEM_COMMIT && (mbi.Protect & PAGE_EXECUTE_FLAGS) != 0
    }
}