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
use std::path::PathBuf;
use super::{error::ProcessError, signature::Signature};
use paste::paste;
#[cfg(target_os = "windows")]
use windows::Win32::Foundation::HANDLE;
#[derive(Debug)]
pub struct MemoryRegion {
pub from: usize,
pub size: usize,
}
macro_rules! read_generic {
($t: ty, $self: expr, $addr: expr) => {{
paste! {
let mut bytes = vec![0u8; std::mem::size_of::<T>()];
$self.read($addr, std::mem::size_of::<T>(), &mut bytes)?;
unsafe { std::ptr::read(bytes.as_ptr() as *const T) }
}
}};
}
macro_rules! prim_read_impl {
($t: ident) => {
paste! {
fn [<read_ $t>]<T: TryInto<usize>>(
&self,
addr: T
) -> Result<$t, ProcessError> {
let mut bytes = [0u8; std::mem::size_of::<$t>()];
self.read(addr, std::mem::size_of::<$t>(), &mut bytes)?;
Ok($t::from_le_bytes(bytes))
}
}
};
}
macro_rules! prim_read_array_impl {
($t: ident) => {
paste! {
fn [<read_ $t _array>]<T: TryInto<usize>>(
&self,
addr: T,
buff: &mut Vec<$t>
) -> Result<(), ProcessError> {
let addr: usize = addr.try_into()
.map_err(|_| ProcessError::AddressConvertError)?;
let items_ptr = self.read_i32(addr + 4)?;
let size = self.read_i32(addr + 12)? as usize;
buff.resize(size, 0 as $t);
let byte_buff = unsafe { std::slice::from_raw_parts_mut(
buff.as_mut_ptr() as *mut u8,
buff.len() * std::mem::size_of::<$t>()
) };
self.read(
items_ptr + 8,
size * std::mem::size_of::<$t>(),
byte_buff
)?;
Ok(())
}
}
};
}
pub struct Process {
#[cfg(target_os = "linux")]
pub pid: i32,
#[cfg(target_os = "windows")]
pub pid: u32,
#[cfg(target_os = "windows")]
pub handle: HANDLE,
pub maps: Vec<MemoryRegion>,
pub executable_dir: Option<PathBuf>,
}
pub trait ProcessTraits
where
Self: Sized,
{
/// Initialize a `Process` struct
///
/// * `proc_name` - Name of the process or key words
/// * `exclude` - Key words to avoid when searching for process name
///
/// Notes:
/// For more details of searching the process name see [`find_process`]
/// method
fn initialize(
proc_name: &str,
exclude: &[&str],
) -> Result<Self, ProcessError>;
/// Attemp to find a process
///
/// * `proc_name` - Name of the process or key words
/// * `exclude` - Keywords to avoid when searching for process name
///
/// # Notes
/// It's going try to search process name by using [`str::contains`] function
/// with `proc_name` argument on process name. Same applies to `exclude`
fn find_process(
proc_name: &str,
exclude: &[&str],
) -> Result<Self, ProcessError>;
/// Collect memory regions offsets into itself.
///
/// Notes:
/// * Function isn't whole memory just their offsets.
/// Check out [`MemoryRegion`] for more info
fn read_regions(self) -> Result<Self, ProcessError>;
fn read_signature<T: TryFrom<usize>>(
&self,
sign: &Signature,
) -> Result<T, ProcessError>;
fn read<T: TryInto<usize>>(
&self,
addr: T,
len: usize,
buff: &mut [u8],
) -> Result<(), ProcessError>;
fn read_uleb128<T: TryInto<usize>>(
&self,
addr: T,
) -> Result<u64, ProcessError> {
let mut addr: usize = addr
.try_into()
.map_err(|_| ProcessError::AddressConvertError)?;
let mut value: u64 = 0;
let mut bytes_read = 0;
loop {
let byte = self.read_u8(addr)?;
addr += 1;
let byte_value = (byte & 0b0111_1111) as u64;
value |= byte_value << (7 * bytes_read);
bytes_read += 1;
if (byte & !0b0111_1111) == 0 {
break;
}
}
Ok(value)
}
/// Same behaviour as [`ProcessTraits::read_string_from_ptr()`]
///
/// The only diffrence is that function will throw
/// a [`ProcessError::StringTooLarge`] error if readed string length
/// is over a provided limit
///
/// Notes:
/// * `*_from_ptr()` functions usually will result in additional
/// heap allocation, due to generic behaviour. If you need to avoid
/// heap allocations at all costs, read pointer manually and then pass
/// address to the [`ProcessTraits::read_string()`] function
fn read_string_with_limit_from_ptr<T: TryInto<usize>>(
&self,
addr: T,
limit: usize,
) -> Result<String, ProcessError> {
let addr = read_generic!(T, self, addr);
self.read_string_with_limit(addr, limit)
}
/// Reads a C# string. For more info checkout [`ProcessTraits::read_string()`]
///
/// The only diffrence is that function will throw
/// a [`ProcessError::StringTooLarge`] error if readed string length
/// is over a provided limit
fn read_string_with_limit<T: TryInto<usize>>(
&self,
addr: T,
limit: usize,
) -> Result<String, ProcessError> {
let mut addr: usize = addr
.try_into()
.map_err(|_| ProcessError::AddressConvertError)?;
addr += std::mem::size_of::<T>();
let len = self.read_u32(addr)? as usize; // Reading 4B str len
if len > limit {
return Err(ProcessError::StringTooLarge);
}
addr += 0x4; // Since we read length skipping it too
let mut buff = vec![0u16; len];
let byte_buff = unsafe {
std::slice::from_raw_parts_mut(
buff.as_mut_ptr() as *mut u8,
buff.len() * 2,
)
};
self.read(addr, byte_buff.len(), byte_buff)?;
Ok(String::from_utf16_lossy(&buff))
}
/// Reads a C# string based on C# string structure
/// Assumes passed `addr` is a pointer, so it's gonna make
/// additional pointer read.
///
/// Notes:
/// * `*_from_ptr()` functions usually will result in additional
/// heap allocation, due to generic behaviour. If you need to avoid
/// heap allocations at all costs, read pointer manually and then pass
/// address to the [`ProcessTraits::read_string()`] function
fn read_string_from_ptr<T: TryInto<usize>>(
&self,
addr: T,
) -> Result<String, ProcessError> {
let addr = read_generic!(T, self, addr);
self.read_string(addr)
}
/// Reads a C# string based on C# string structure
/// Assumes passed `addr` is not a pointer, so no additional
/// pointer reads is gonna be made.
///
/// If you have a pointer to string either read that pointer youself
/// or use [`ProcessTraits::read_string_from_ptr()`]
fn read_string<T: TryInto<usize>>(
&self,
addr: T,
) -> Result<String, ProcessError> {
let mut addr: usize = addr
.try_into()
.map_err(|_| ProcessError::AddressConvertError)?;
// C# string structure: 4B/8B obj header, 4B str len, str itself
addr += std::mem::size_of::<T>(); // Skipping 4B/8B obj header depending on endiness
let len = self.read_u32(addr)? as usize; // Reading 4B str len
addr += 0x4; // Since we read length skipping it too
let mut buff = vec![0u16; len];
let byte_buff = unsafe {
std::slice::from_raw_parts_mut(
buff.as_mut_ptr() as *mut u8,
buff.len() * 2,
)
};
self.read(addr, byte_buff.len(), byte_buff)?;
Ok(String::from_utf16_lossy(&buff))
}
prim_read_impl!(i8);
prim_read_impl!(i16);
prim_read_impl!(i32);
prim_read_impl!(i64);
prim_read_impl!(i128);
prim_read_impl!(u8);
prim_read_impl!(u16);
prim_read_impl!(u32);
prim_read_impl!(u64);
prim_read_impl!(u128);
prim_read_impl!(f32);
prim_read_impl!(f64);
prim_read_array_impl!(i8);
prim_read_array_impl!(i16);
prim_read_array_impl!(i32);
prim_read_array_impl!(i64);
prim_read_array_impl!(i128);
prim_read_array_impl!(u8);
prim_read_array_impl!(u16);
prim_read_array_impl!(u32);
prim_read_array_impl!(u64);
prim_read_array_impl!(u128);
prim_read_array_impl!(f32);
prim_read_array_impl!(f64);
}