|
1 | 1 | use dll_syringe::{ |
2 | 2 | Syringe, |
3 | | - process::OwnedProcess, |
| 3 | + process::{BorrowedProcessModule, OwnedProcess, Process}, |
4 | 4 | rpc::{RawRpcFunctionPtr, RemoteRawProcedure}, |
5 | 5 | }; |
6 | | -use std::env; |
| 6 | +use std::error; |
| 7 | +use std::{env, path::PathBuf}; |
7 | 8 | use windows::{ |
8 | 9 | Win32::{ |
9 | 10 | Foundation::{HWND, LPARAM, TRUE, WPARAM}, |
@@ -185,62 +186,72 @@ pub fn get_top_level_windows() -> Vec<WindowInfo> { |
185 | 186 | pub struct Injector {} |
186 | 187 |
|
187 | 188 | impl Injector { |
188 | | - pub fn inject_and_get_remote_proc<F>( |
189 | | - syringe: &Syringe, |
190 | | - proc_name: &str, |
191 | | - ) -> RemoteRawProcedure<F> |
192 | | - where |
193 | | - F: RawRpcFunctionPtr, |
194 | | - { |
195 | | - let mut dll_path = env::current_exe().unwrap(); |
| 189 | + fn get_dll_path(process: &OwnedProcess) -> Result<PathBuf, Box<dyn error::Error>> { |
| 190 | + let mut dll_path = env::current_exe()?; |
196 | 191 | dll_path.pop(); |
197 | | - dll_path.push("utils.dll"); |
198 | 192 |
|
199 | | - let injected_payload = syringe.find_or_inject(dll_path).unwrap(); |
| 193 | + if cfg!(debug_assertions) && process.runs_under_wow64()? { |
| 194 | + dll_path.push("../i686-pc-windows-msvc/debug/utils.dll"); |
| 195 | + } else if process.is_x86()? { |
| 196 | + dll_path.push("utils32.dll"); |
| 197 | + } else { |
| 198 | + dll_path.push("utils.dll"); |
| 199 | + } |
| 200 | + |
| 201 | + Ok(dll_path) |
| 202 | + } |
200 | 203 |
|
201 | | - return unsafe { syringe.get_raw_procedure::<F>(injected_payload, proc_name) } |
202 | | - .unwrap() |
203 | | - .unwrap(); |
| 204 | + pub fn get_remote_proc<F: RawRpcFunctionPtr>( |
| 205 | + syringe: &Syringe, |
| 206 | + module: BorrowedProcessModule<'_>, |
| 207 | + proc_name: &str, |
| 208 | + ) -> Result<RemoteRawProcedure<F>, Box<dyn error::Error>> { |
| 209 | + match unsafe { syringe.get_raw_procedure::<F>(module, proc_name) }? { |
| 210 | + Some(remote_proc) => Ok(remote_proc), |
| 211 | + None => Err(format!("Failed to find procedure {}", proc_name).into()), |
| 212 | + } |
204 | 213 | } |
205 | 214 |
|
206 | 215 | pub fn set_window_props( |
207 | 216 | target_process: OwnedProcess, |
208 | 217 | hwnds: &[u32], |
209 | 218 | hide: bool, |
210 | 219 | hide_from_taskbar: Option<bool>, |
211 | | - ) { |
| 220 | + ) -> Result<(), Box<dyn error::Error>> { |
| 221 | + let dll_path = Self::get_dll_path(&target_process)?; |
212 | 222 | let syringe = Syringe::for_process(target_process); |
| 223 | + let module = syringe.find_or_inject(dll_path)?; |
213 | 224 |
|
214 | | - let remote_proc = Self::inject_and_get_remote_proc::<extern "system" fn(HWND, bool) -> bool>( |
| 225 | + let remote_proc = Self::get_remote_proc::<extern "system" fn(u32, bool) -> bool>( |
215 | 226 | &syringe, |
| 227 | + module, |
216 | 228 | "SetWindowVisibility", |
217 | | - ); |
| 229 | + )?; |
218 | 230 |
|
219 | | - let remote_proc2 = Self::inject_and_get_remote_proc::<extern "system" fn(HWND, bool) -> bool>( |
| 231 | + let remote_proc2 = Self::get_remote_proc::<extern "system" fn(u32, bool) -> bool>( |
220 | 232 | &syringe, |
| 233 | + module, |
221 | 234 | "HideFromTaskbar", |
222 | | - ); |
| 235 | + )?; |
223 | 236 |
|
224 | 237 | for hwnd in hwnds { |
225 | | - remote_proc |
226 | | - .call(HWND(hwnd.clone() as *mut _), hide) |
227 | | - .unwrap(); |
| 238 | + remote_proc.call(*hwnd, hide).unwrap(); |
228 | 239 |
|
229 | 240 | if let Some(hide_from_taskbar) = hide_from_taskbar { |
230 | | - remote_proc2 |
231 | | - .call(HWND(hwnd.clone() as *mut _), hide_from_taskbar) |
232 | | - .unwrap(); |
| 241 | + remote_proc2.call(*hwnd, hide_from_taskbar).unwrap(); |
233 | 242 | } |
234 | 243 | } |
| 244 | + Ok(()) |
235 | 245 | } |
236 | 246 |
|
237 | 247 | pub fn set_window_props_with_pid( |
238 | 248 | pid: u32, |
239 | 249 | hwnd: u32, |
240 | 250 | hide: bool, |
241 | 251 | hide_from_taskbar: Option<bool>, |
242 | | - ) { |
243 | | - let target_process = OwnedProcess::from_pid(pid).unwrap(); |
244 | | - Self::set_window_props(target_process, &[hwnd], hide, hide_from_taskbar); |
| 252 | + ) -> Result<(), Box<dyn error::Error>> { |
| 253 | + let target_process = OwnedProcess::from_pid(pid)?; |
| 254 | + Self::set_window_props(target_process, &[hwnd], hide, hide_from_taskbar)?; |
| 255 | + Ok(()) |
245 | 256 | } |
246 | 257 | } |
0 commit comments