win-auto-utils 0.2.1

Universal Windows automation utilities with memory, window, input, and color operations
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Inline Hook Implementation
//!
//! Provides functionality to redirect function execution by modifying the target function's entry point.
//! This is a simple hooking technique that directly patches the target address with a jump instruction.
//!
//! # Architecture Support
//! - **x86**: Uses 5-byte relative JMP (`E9 xx xx xx xx`)
//! - **x64**: Uses 14-byte absolute JMP (`MOV RAX, addr` + `JMP RAX`)
//!
//! # Use Cases
//! - Function interception and replacement
//! - API hooking for monitoring or modification
//! - Simple code redirection without preserving original behavior
//!
//! # Limitations
//! - Does not automatically preserve original instructions
//! - If you need to call the original function, use [`TrampolineHook`](super::TrampolineHook) instead
//! - Must manually handle register preservation in detour function if needed
//!
//! # Quick Start
//! ```no_run
//! use win_auto_utils::memory_hook::InlineHook;
//! use win_auto_utils::handle::open_process_handle;
//! use windows::Win32::System::Threading::PROCESS_ALL_ACCESS;
//!
//! fn example() -> Result<(), Box<dyn std::error::Error>> {
//!     let handle = open_process_handle(12345, PROCESS_ALL_ACCESS).unwrap();
//!     
//!     // For x86 process
//!     let mut hook = InlineHook::new_x86(handle, 0x1000, 0x2000);
//!     
//!     // For x64 process (or use InlineHook::new for default)
//!     let mut hook = InlineHook::new_x64(handle, 0x7FF6A1B2C3D4, 0x7FF6A1B2E5F6);
//!     
//!     // Install the hook
//!     hook.install()?;
//!     
//!     // Now all calls to target will jump to detour
//!     // ... your code here ...
//!     
//!     // Remove hook when done (automatically restores original bytes)
//!     hook.uninstall()?;
//!     Ok(())
//! }
//! ```
use windows::Win32::Foundation::HANDLE;
use crate::memory::{read_memory_bytes, write_memory_bytes, MemoryError};
use crate::memory_hook::shellcode::ShellcodeBuilder;
use crate::memory_hook::utils::{ProtectionGuard, SendableHandle};

/// Inline hook for redirecting function execution
///
/// # Quick Start
/// ```no_run
/// use win_auto_utils::memory_hook::InlineHook;
/// use win_auto_utils::handle::open_process_handle;
/// use windows::Win32::System::Threading::PROCESS_ALL_ACCESS;
///
/// fn example() -> Result<(), Box<dyn std::error::Error>> {
///     let handle = open_process_handle(12345, PROCESS_ALL_ACCESS).unwrap();
///     
///     // Hook target function at 0x1000 to redirect to 0x2000
///     let mut hook = InlineHook::new(handle, 0x1000, 0x2000);
///     hook.install()?;
///     
///     // Now calls to 0x1000 will jump to 0x2000
///     
///     // Remove hook when done
///     hook.uninstall()?;
///     Ok(())
/// }
/// ```
pub struct InlineHook {
    handle: SendableHandle,
    target_address: usize,
    detour_address: usize,
    original_bytes: Vec<u8>,
    is_installed: bool,
    architecture: HookArchitecture,
}

/// Architecture detection for hook generation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookArchitecture {
    X86,
    X64,
}

impl InlineHook {
    /// Create a builder for precise configuration
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_hook::{InlineHook, HookArchitecture};
    ///```
    pub fn builder() -> InlineHookBuilder {
        InlineHookBuilder::new()
    }

    /// Create a new inline hook for x86 (32-bit) processes
    ///
    /// This is the recommended constructor for 32-bit target processes.
    /// It creates a simple inline hook that redirects execution from the target
    /// address to the detour address.
    ///
    /// # Arguments
    /// * `handle` - Process handle with full access rights
    /// * `target_address` - Address of the function to hook
    /// * `detour_address` - Address of the replacement function
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_hook::InlineHook;
    ///
    /// let mut hook = InlineHook::new_x86(handle, 0x1000, 0x2000);
    /// hook.install()?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn new_x86(handle: HANDLE, target_address: usize, detour_address: usize) -> Self {
        Self {
            handle: SendableHandle(handle),
            target_address,
            detour_address,
            original_bytes: Vec::new(),
            is_installed: false,
            architecture: HookArchitecture::X86,
        }
    }

    /// Create a new inline hook for x64 (64-bit) processes
    ///
    /// This is the recommended constructor for 64-bit target processes.
    /// It creates a simple inline hook that redirects execution from the target
    /// address to the detour address using absolute jump (MOV RAX + JMP RAX).
    ///
    /// # Arguments
    /// * `handle` - Process handle with full access rights
    /// * `target_address` - Address of the function to hook
    /// * `detour_address` - Address of the replacement function
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_hook::InlineHook;
    ///
    /// let mut hook = InlineHook::new_x64(handle, 0x7FF6A1B2C3D4, 0x7FF6A1B2E5F6);
    /// hook.install()?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn new_x64(handle: HANDLE, target_address: usize, detour_address: usize) -> Self {
        Self {
            handle: SendableHandle(handle),
            target_address,
            detour_address,
            original_bytes: Vec::new(),
            is_installed: false,
            architecture: HookArchitecture::X64,
        }
    }

    /// Create a new inline hook (defaults to x64 architecture)
    ///
    /// This is a convenience constructor for quick hooking when you don't need
    /// to specify the architecture explicitly. Defaults to x64 for modern applications.
    ///
    /// # Arguments
    /// * `handle` - Process handle with full access rights
    /// * `target_address` - Address of the function to hook
    /// * `detour_address` - Address of the replacement function
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_hook::InlineHook;
    ///
    /// // For 64-bit process (default)
    /// let mut hook = InlineHook::new(handle, 0x7FF6A1B2C3D4, 0x7FF6A1B2E5F6);
    /// hook.install()?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn new(handle: HANDLE, target_address: usize, detour_address: usize) -> Self {
        Self::new_x64(handle, target_address, detour_address)
    }

    /// Set the architecture explicitly
    ///
    /// # Arguments
    /// * `is_64bit` - true for x64, false for x86
    pub fn set_architecture(&mut self, is_64bit: bool) {
        self.architecture = if is_64bit {
            HookArchitecture::X64
        } else {
            HookArchitecture::X86
        };
    }

    /// Reset the hook to its initial state (safe for program restart)
    ///
    /// This method safely cleans up all internal state, allowing the hook
    /// to be reused after the target program restarts.
    ///
    /// # What it does:
    /// 1. If installed, uninstalls the hook first (restores original bytes)
    /// 2. Clears cached data (original_bytes, etc.)
    /// 3. Resets all flags to initial state
    ///
    /// # Safety
    /// - Safe to call multiple times (idempotent)
    /// - Safe to call even if never installed
    /// - After reset, the object can be used as if newly created
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_hook::InlineHook;
    ///
    /// // First usage
    /// let mut hook = InlineHook::new_x86(handle, target, detour);
    /// hook.install()?;
    /// hook.uninstall()?;
    ///
    /// // Program restarted - reset state
    /// hook.reset();
    ///
    /// // Second usage (no side effects)
    /// hook.install()?; // Will read fresh original bytes
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn reset(&mut self) {
        // If still installed, uninstall first (this will restore bytes)
        if self.is_installed {
            let _ = self.uninstall();
        }

        // Clear all cached state
        self.original_bytes.clear();
        self.is_installed = false;
    }

    /// Install the hook
    ///
    /// This will:
    /// 1. Save the original bytes at the target address
    /// 2. Generate jump code to the detour function
    /// 3. Write the jump code to the target address
    ///
    /// # Errors
    /// Returns `MemoryError` if reading or writing fails
    pub fn install(&mut self) -> Result<(), MemoryError> {
        if self.is_installed {
            return Err(MemoryError::WriteFailed("Hook already installed".to_string()));
        }
        
        // Determine how many bytes we need to overwrite
        let required_bytes = match self.architecture {
            HookArchitecture::X86 => 5,  // JMP rel32
            HookArchitecture::X64 => 14, // MOV RAX + JMP RAX (for absolute address)
        };
        
        // Save original bytes
        self.original_bytes = read_memory_bytes(self.handle.0, self.target_address, required_bytes)?;
        
        // Generate shellcode for the jump
        let jump_code = self.generate_jump_code()?;
        
        // Change memory protection to allow writing
        let _guard = ProtectionGuard::new(self.handle.0, self.target_address, required_bytes)?;
        
        // Write the jump code
        write_memory_bytes(self.handle.0, self.target_address, &jump_code)?;
        
        self.is_installed = true;
        Ok(())
    }
    
    /// Uninstall the hook and restore original bytes
    ///
    /// # Errors
    /// Returns `MemoryError` if restoration fails
    pub fn uninstall(&mut self) -> Result<(), MemoryError> {
        if !self.is_installed {
            return Err(MemoryError::WriteFailed("Hook not installed".to_string()));
        }
        
        if self.original_bytes.is_empty() {
            return Err(MemoryError::WriteFailed("No original bytes to restore".to_string()));
        }
        
        // Change memory protection to allow writing
        let _guard = ProtectionGuard::new(
            self.handle.0, 
            self.target_address, 
            self.original_bytes.len()
        )?;
        
        // Restore original bytes
        write_memory_bytes(self.handle.0, self.target_address, &self.original_bytes)?;
        
        self.is_installed = false;
        Ok(())
    }
    
    /// Check if the hook is currently installed
    pub fn is_installed(&self) -> bool {
        self.is_installed
    }

    /// Get the original bytes that were overwritten
    pub fn get_original_bytes(&self) -> &[u8] {
        &self.original_bytes
    }
    
    /// Generate the jump code based on architecture
    fn generate_jump_code(&self) -> Result<Vec<u8>, MemoryError> {
        let mut builder = match self.architecture {
            HookArchitecture::X86 => ShellcodeBuilder::new_x86(),
            HookArchitecture::X64 => ShellcodeBuilder::new_x64(),
        };
        
        // For x64 with large addresses, use absolute jump
        match self.architecture {
            HookArchitecture::X86 => {
                // Try relative jump first
                let offset = (self.detour_address as i64) - (self.target_address as i64) - 5;
                
                if offset >= i32::MIN as i64 && offset <= i32::MAX as i64 {
                    // Use relative jump
                    builder.jmp_relative(self.target_address, self.detour_address);
                } else {
                    // Use absolute jump (PUSH + RET)
                    builder.jmp_absolute(self.detour_address);
                }
            }
            HookArchitecture::X64 => {
                // Always use absolute jump for x64 to handle full 64-bit addresses
                builder.jmp_absolute(self.detour_address);
            }
        }
        
        Ok(builder.build())
    }
}

impl Drop for InlineHook {
    fn drop(&mut self) {
        if self.is_installed {
            let _ = self.uninstall();
        }
    }
}

/// Builder for configuring InlineHook
///
/// Provides a fluent API for building InlineHook with optional configuration.
#[derive(Debug)]
pub struct InlineHookBuilder {
    handle: Option<HANDLE>,
    target_address: Option<usize>,
    detour_address: Option<usize>,
    architecture: Option<HookArchitecture>,
}

impl InlineHookBuilder {
    fn new() -> Self {
        Self {
            handle: None,
            target_address: None,
            detour_address: None,
            architecture: None,
        }
    }

    /// Set process handle (required for install)
    pub fn handle(mut self, handle: HANDLE) -> Self {
        self.handle = Some(handle);
        self
    }

    /// Set target address (required for install)
    pub fn target_address(mut self, addr: usize) -> Self {
        self.target_address = Some(addr);
        self
    }

    /// Set detour address (required for install)
    pub fn detour_address(mut self, addr: usize) -> Self {
        self.detour_address = Some(addr);
        self
    }

    /// Set the target architecture
    pub fn architecture(mut self, arch: HookArchitecture) -> Self {
        self.architecture = Some(arch);
        self
    }

    /// Convenience method for x86 architecture
    pub fn x86(self) -> Self {
        self.architecture(HookArchitecture::X86)
    }

    /// Convenience method for x64 architecture
    pub fn x64(self) -> Self {
        self.architecture(HookArchitecture::X64)
    }

    /// Build the InlineHook with configured settings
    ///
    /// # Validation
    /// This method validates that all required parameters are set:
    /// - `handle`: Process handle
    /// - `target_address`: Address to hook
    /// - `detour_address`: Address of replacement function
    ///
    /// # Errors
    /// Returns error if any required parameter is missing.
    pub fn build(self) -> Result<InlineHook, MemoryError> {
        // Validate required parameters
        let handle = self.handle.ok_or_else(|| {
            MemoryError::WriteFailed(
                "handle must be set. Call .handle(handle) before build().".to_string()
            )
        })?;

        let target_address = self.target_address.ok_or_else(|| {
            MemoryError::WriteFailed(
                "target_address must be set. Call .target_address(addr) before build().".to_string()
            )
        })?;

        let detour_address = self.detour_address.ok_or_else(|| {
            MemoryError::WriteFailed(
                "detour_address must be set. Call .detour_address(addr) before build().".to_string()
            )
        })?;

        Ok(InlineHook {
            handle: SendableHandle(handle),
            target_address,
            detour_address,
            original_bytes: Vec::new(),
            is_installed: false,
            architecture: self.architecture.unwrap_or(HookArchitecture::X64),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_inline_hook_creation() {
        let hook = InlineHook::new_x64(HANDLE::default(), 0x1000, 0x2000);
        
        assert!(!hook.is_installed());
        assert_eq!(hook.get_original_bytes().len(), 0);
    }
    
    #[test]
    fn test_builder_api() {
        let result = InlineHook::builder()
            .handle(HANDLE::default())
            .target_address(0x1000)
            .detour_address(0x2000)
            .x86()
            .build();
        
        assert!(result.is_ok());
        let hook = result.unwrap();
        assert_eq!(hook.architecture, HookArchitecture::X86);
    }
}