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
//! Memory Hook Module
//!
//! Provides memory manipulation capabilities including:
//! - Memory locking (continuous monitoring and restoration)
//! - Inline hooking (redirect function calls)
//! - Trampoline hooking (preserve original function while hooking)
//! - Bytes switching (quick enable/disable of byte sequences)
//! - Register extraction (capture CPU register values at hook points)
//!
//! # Feature Flag
//! Enable with: `--features "memory_hook"`
//!
//! # Quick Start
//!
//! ## Memory Lock Example
//! ```no_run
//! use win_auto_utils::memory_hook::MemoryLock;
//! use win_auto_utils::handle::open_process_handle;
//! use windows::Win32::System::Threading::{PROCESS_VM_READ, PROCESS_VM_WRITE, PROCESS_VM_OPERATION};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let pid = 12345;
//! let address = 0x7FF6A1B2C3D4;
//!
//! let handle = open_process_handle(pid,
//! PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION)
//! .ok_or("Failed to open process")?;
//!
//! // Lock a value (e.g., health = 100)
//! let mut lock = MemoryLock::builder()
//! .handle(handle)
//! .address(address)
//! .value(100u32)
//! .build()?;
//! lock.start()?;
//!
//! // The value will be continuously restored to 100
//! // ... your code here ...
//!
//! // Stop locking
//! lock.stop()?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Inline Hook Example
//! ```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 main() -> Result<(), Box<dyn std::error::Error>> {
//! let pid = 12345;
//! let target_func = 0x7FF6A1B2C3D4;
//! let detour_func = 0x7FF6A1B2D4E5;
//!
//! let handle = open_process_handle(pid, PROCESS_ALL_ACCESS)
//! .ok_or("Failed to open process")?;
//!
//! // Install inline hook (defaults to x64)
//! let mut hook = InlineHook::new(handle, target_func, detour_func);
//! hook.install()?;
//!
//! // Now calls to target_func will jump to detour_func
//! // ... your code here ...
//!
//! // Remove hook
//! hook.uninstall()?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Trampoline Hook Example
//! ```no_run
//! use win_auto_utils::memory_hook::TrampolineHook;
//! use win_auto_utils::handle::open_process_handle;
//! use windows::Win32::System::Threading::{PROCESS_ALL_ACCESS};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let pid = 12345;
//! let target_addr = 0x0041FAF2;
//!
//! let handle = open_process_handle(pid, PROCESS_ALL_ACCESS)
//! .ok_or("Failed to open process")?;
//!
//! // Define custom shellcode
//! let shellcode = vec![
//! 0x01, 0xD2, // add edx,edx
//! 0x01, 0x91, 0x08, 0x03, 0x00, 0x00, // add [ecx+0x308],edx
//! ];
//!
//! // Create trampoline hook for x86 process
//! let mut hook = TrampolineHook::new_x86(handle, target_addr, shellcode);
//! hook.install()?;
//!
//! // Use the hook...
//!
//! // Remove hook (auto-cleanup)
//! hook.uninstall()?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Bytes Switch Example
//! ```no_run
//! use win_auto_utils::memory_hook::BytesSwitch;
//! use win_auto_utils::handle::open_process_handle;
//! use windows::Win32::System::Threading::{PROCESS_ALL_ACCESS};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let pid = 12345;
//! let target_addr = 0x7FF6A1B2C3D4;
//!
//! let handle = open_process_handle(pid, PROCESS_ALL_ACCESS)
//! .ok_or("Failed to open process")?;
//!
//! // Create a NOP switch (disable functionality)
//! let mut switch = BytesSwitch::new_nop(handle, target_addr, 6)?;
//!
//! // Enable switch (write NOPs)
//! switch.enable()?;
//!
//! // ... functionality is now disabled ...
//!
//! // Disable switch (restore original bytes)
//! switch.disable()?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Register Extractor Example
//! ```no_run
//! use win_auto_utils::memory_hook::register_extractor::{RegisterExtractor, Register};
//! use win_auto_utils::handle::open_process_handle;
//! use windows::Win32::System::Threading::{PROCESS_ALL_ACCESS};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let pid = 12345;
//! let target_addr = 0x1C17E514F90;
//!
//! let handle = open_process_handle(pid, PROCESS_ALL_ACCESS)
//! .ok_or("Failed to open process")?;
//!
//! // Create register extractor to capture RDI at hook point
//! let mut extractor = RegisterExtractor::builder()
//! .handle(handle)
//! .target_address(target_addr)
//! .bytes_to_overwrite(15)
//! .extract_register(Register::RDI)
//! .x64()
//! .build()?;
//!
//! extractor.install()?;
//!
//! // Read captured register value
//! let player_ptr: u64 = extractor.read_register::<u64>(Register::RDI)?;
//!
//! // Read through pointer chain
//! let health: f32 = extractor.read_chain::<f32>(Register::RDI, &[0x34])?;
//!
//! extractor.uninstall()?;
//!
//! Ok(())
//! }
//! ```
pub use ;
pub use ;
pub use TrampolineHook;
pub use HookArchitecture as TrampolineHookArchitecture;
pub use BytesSwitch;
pub use ;
pub use ;
pub use ;