|
| 1 | +use std::ffi::CString; |
| 2 | +use std::mem; |
| 3 | +use std::ptr; |
| 4 | +use thiserror::Error; |
| 5 | +use winapi::shared::minwindef::LPVOID; |
| 6 | +use winapi::um::errhandlingapi::GetLastError; |
| 7 | +use winapi::um::handleapi::CloseHandle; |
| 8 | +use winapi::um::libloaderapi::{GetModuleHandleA, GetProcAddress}; |
| 9 | +use winapi::um::memoryapi::{VirtualAllocEx, VirtualFreeEx, WriteProcessMemory}; |
| 10 | +use winapi::um::processthreadsapi::{CreateRemoteThread, OpenProcess}; |
| 11 | +use winapi::um::synchapi::WaitForSingleObject; |
| 12 | +use winapi::um::winbase::INFINITE; |
| 13 | +use winapi::um::winnt::{ |
| 14 | + HANDLE, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, PROCESS_ALL_ACCESS, |
| 15 | +}; |
| 16 | + |
| 17 | +#[derive(Debug, Error)] |
| 18 | +pub enum InjectionError { |
| 19 | + #[error("failed to create remote thread, error code: {0}")] |
| 20 | + CreateRemoteThreadFailed(u32), |
| 21 | + #[error("failed to get kernel32 handle, error code: {0}")] |
| 22 | + GetModuleHandleFailed(u32), |
| 23 | + #[error("failed to get LoadLibraryA address, error code: {0}")] |
| 24 | + GetProcAddressFailed(u32), |
| 25 | + #[error("invalid DLL path")] |
| 26 | + InvalidPath, |
| 27 | + #[error("failed to open process, error code: {0}")] |
| 28 | + OpenProcessFailed(u32), |
| 29 | + #[error("process not found")] |
| 30 | + ProcessNotFound, |
| 31 | + #[error("failed to write process memory, error code: {0}")] |
| 32 | + WriteProcessMemoryFailed(u32), |
| 33 | + #[error("failed to allocate memory, error code: {0}")] |
| 34 | + VirtualAllocFailed(u32), |
| 35 | +} |
| 36 | + |
| 37 | +pub struct DllInjector { |
| 38 | + target_pid: u32, |
| 39 | +} |
| 40 | + |
| 41 | +impl DllInjector { |
| 42 | + pub fn new(pid: u32) -> Self { |
| 43 | + Self { target_pid: pid } |
| 44 | + } |
| 45 | + |
| 46 | + pub fn inject(&self, dll_path: &str) -> Result<(), InjectionError> { |
| 47 | + unsafe { |
| 48 | + let process_handle = OpenProcess(PROCESS_ALL_ACCESS, 0, self.target_pid); |
| 49 | + if process_handle.is_null() { |
| 50 | + return Err(InjectionError::OpenProcessFailed(GetLastError())); |
| 51 | + } |
| 52 | + |
| 53 | + let _process_guard = ProcessHandleGuard(process_handle); |
| 54 | + let dll_path_cstring = |
| 55 | + CString::new(dll_path).map_err(|_| InjectionError::InvalidPath)?; |
| 56 | + let dll_path_bytes = dll_path_cstring.as_bytes_with_nul(); |
| 57 | + let dll_path_size = dll_path_bytes.len(); |
| 58 | + |
| 59 | + let remote_memory = VirtualAllocEx( |
| 60 | + process_handle, |
| 61 | + ptr::null_mut(), |
| 62 | + dll_path_size, |
| 63 | + MEM_COMMIT | MEM_RESERVE, |
| 64 | + PAGE_READWRITE, |
| 65 | + ); |
| 66 | + |
| 67 | + if remote_memory.is_null() { |
| 68 | + return Err(InjectionError::VirtualAllocFailed(GetLastError())); |
| 69 | + } |
| 70 | + |
| 71 | + let _memory_guard = RemoteMemoryGuard { |
| 72 | + process_handle, |
| 73 | + address: remote_memory, |
| 74 | + }; |
| 75 | + |
| 76 | + let mut bytes_written: usize = 0; |
| 77 | + let result = WriteProcessMemory( |
| 78 | + process_handle, |
| 79 | + remote_memory, |
| 80 | + dll_path_bytes.as_ptr() as LPVOID, |
| 81 | + dll_path_size, |
| 82 | + &mut bytes_written as *mut usize, |
| 83 | + ); |
| 84 | + |
| 85 | + if result == 0 { |
| 86 | + return Err(InjectionError::WriteProcessMemoryFailed(GetLastError())); |
| 87 | + } |
| 88 | + |
| 89 | + let kernel32_cstring = CString::new("kernel32.dll").unwrap(); |
| 90 | + let kernel32_handle = GetModuleHandleA(kernel32_cstring.as_ptr()); |
| 91 | + if kernel32_handle.is_null() { |
| 92 | + return Err(InjectionError::GetModuleHandleFailed(GetLastError())); |
| 93 | + } |
| 94 | + |
| 95 | + let load_library_cstring = CString::new("LoadLibraryA").unwrap(); |
| 96 | + let load_library_addr = GetProcAddress(kernel32_handle, load_library_cstring.as_ptr()); |
| 97 | + |
| 98 | + if load_library_addr.is_null() { |
| 99 | + return Err(InjectionError::GetProcAddressFailed(GetLastError())); |
| 100 | + } |
| 101 | + |
| 102 | + let thread_handle = CreateRemoteThread( |
| 103 | + process_handle, |
| 104 | + ptr::null_mut(), |
| 105 | + 0, |
| 106 | + Some(mem::transmute(load_library_addr)), |
| 107 | + remote_memory, |
| 108 | + 0, |
| 109 | + ptr::null_mut(), |
| 110 | + ); |
| 111 | + |
| 112 | + if thread_handle.is_null() { |
| 113 | + return Err(InjectionError::CreateRemoteThreadFailed(GetLastError())); |
| 114 | + } |
| 115 | + |
| 116 | + WaitForSingleObject(thread_handle, INFINITE); |
| 117 | + CloseHandle(thread_handle); |
| 118 | + |
| 119 | + Ok(()) |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +struct ProcessHandleGuard(HANDLE); |
| 125 | + |
| 126 | +impl Drop for ProcessHandleGuard { |
| 127 | + fn drop(&mut self) { |
| 128 | + unsafe { |
| 129 | + if !self.0.is_null() { |
| 130 | + CloseHandle(self.0); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +struct RemoteMemoryGuard { |
| 137 | + process_handle: HANDLE, |
| 138 | + address: LPVOID, |
| 139 | +} |
| 140 | + |
| 141 | +impl Drop for RemoteMemoryGuard { |
| 142 | + fn drop(&mut self) { |
| 143 | + unsafe { |
| 144 | + if !self.address.is_null() { |
| 145 | + VirtualFreeEx(self.process_handle, self.address, 0, MEM_RELEASE); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments