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
use winapi::shared::minwindef;
use std::os::windows::io::AsRawHandle;
use std::process::Child;
use std::ptr;
use super::{Architecture, CopyAddress, ProcessHandleExt, PutAddress, TryIntoProcessHandle};
pub type Pid = minwindef::DWORD;
pub type ProcessHandle = (winapi::um::winnt::HANDLE, Architecture);
impl ProcessHandleExt for ProcessHandle {
#[must_use]
fn check_handle(&self) -> bool {
self.0.is_null()
}
#[must_use]
fn null_type() -> ProcessHandle {
(ptr::null_mut(), Architecture::from_native())
}
#[must_use]
fn set_arch(self, arch: Architecture) -> Self {
(self.0, arch)
}
}
impl TryIntoProcessHandle for minwindef::DWORD {
fn try_into_process_handle(&self) -> std::io::Result<ProcessHandle> {
let handle = unsafe {
winapi::um::processthreadsapi::OpenProcess(
winapi::um::winnt::PROCESS_CREATE_THREAD
| winapi::um::winnt::PROCESS_QUERY_INFORMATION
| winapi::um::winnt::PROCESS_VM_READ
| winapi::um::winnt::PROCESS_VM_WRITE
| winapi::um::winnt::PROCESS_VM_OPERATION,
winapi::shared::minwindef::FALSE,
*self,
)
};
if handle == (0 as winapi::um::winnt::HANDLE) {
Err(std::io::Error::last_os_error())
} else {
Ok((handle, Architecture::from_native()))
}
}
}
impl TryIntoProcessHandle for Child {
fn try_into_process_handle(&self) -> std::io::Result<ProcessHandle> {
Ok((self.as_raw_handle() as _, Architecture::from_native()))
}
}
impl CopyAddress for ProcessHandle {
#[allow(clippy::inline_always)]
#[inline(always)]
fn get_pointer_width(&self) -> Architecture {
self.1
}
fn copy_address(&self, addr: usize, buf: &mut [u8]) -> std::io::Result<()> {
if buf.is_empty() {
return Ok(());
}
if unsafe {
winapi::um::memoryapi::ReadProcessMemory(
self.0,
addr as minwindef::LPVOID,
buf.as_mut_ptr() as minwindef::LPVOID,
buf.len() as winapi::shared::basetsd::SIZE_T,
ptr::null_mut(),
)
} == winapi::shared::minwindef::FALSE
{
Err(std::io::Error::last_os_error())
} else {
Ok(())
}
}
}
impl PutAddress for ProcessHandle {
fn put_address(&self, addr: usize, buf: &[u8]) -> std::io::Result<()> {
if buf.is_empty() {
return Ok(());
}
if unsafe {
winapi::um::memoryapi::WriteProcessMemory(
self.0,
addr as minwindef::LPVOID,
buf.as_ptr() as minwindef::LPCVOID,
buf.len() as winapi::shared::basetsd::SIZE_T,
ptr::null_mut(),
)
} == winapi::shared::minwindef::FALSE
{
Err(std::io::Error::last_os_error())
} else {
Ok(())
}
}
}