-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmod.rs
More file actions
311 lines (284 loc) · 12.9 KB
/
Copy pathmod.rs
File metadata and controls
311 lines (284 loc) · 12.9 KB
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
310
311
//! Support for attaching to Unity games that are using the standard Mono
//! backend.
#[cfg(feature = "alloc")]
use crate::file_format::macho;
use crate::{
file_format::{elf, pe},
future::retry,
signature::Signature,
Address, Address32, Address64, PointerSize, Process,
};
use core::iter::{self, FusedIterator};
mod assembly;
use assembly::Assembly;
mod image;
pub use image::Image;
mod class;
pub use class::{Class, Object};
mod field;
use field::Field;
mod version;
pub use version::Version;
mod pointer;
pub use pointer::UnityPointer;
mod offsets;
use offsets::MonoOffsets;
use super::{BinaryFormat, CSTR};
/// Represents access to a Unity game that is using the standard Mono backend.
pub struct Module {
assemblies: Address,
version: Version,
offsets: &'static MonoOffsets,
pointer_size: PointerSize,
}
impl Module {
/// Tries attaching to a Unity game that is using the standard Mono backend.
/// This function automatically detects the [Mono version](Version). If you
/// know the version in advance or it fails detecting it, use
/// [`attach`](Self::attach) instead.
pub fn attach_auto_detect(process: &Process) -> Option<Self> {
let version = Version::detect(process)?;
Self::attach(process, version)
}
/// Tries attaching to a Unity game that is using the standard Mono backend
/// with the [Mono version](Version) provided. The version needs to be
/// correct for this function to work. If you don't know the version in
/// advance, use [`attach_auto_detect`](Self::attach_auto_detect) instead.
pub fn attach(process: &Process, version: Version) -> Option<Self> {
let (module_range, format) = [
("mono.dll", BinaryFormat::PE),
("libmono.so", BinaryFormat::ELF),
#[cfg(feature = "alloc")]
("libmono.0.dylib", BinaryFormat::MachO),
("mono-2.0-bdwgc.dll", BinaryFormat::PE),
("libmonobdwgc-2.0.so", BinaryFormat::ELF),
#[cfg(feature = "alloc")]
("libmonobdwgc-2.0.dylib", BinaryFormat::MachO),
]
.into_iter()
.find_map(|(name, format)| Some((process.get_module_range(name).ok()?, format)))?;
let (mono_module, _) = module_range;
let pointer_size = match format {
BinaryFormat::PE => pe::MachineType::read(process, mono_module)?.pointer_size()?,
BinaryFormat::ELF => elf::pointer_size(process, mono_module)?,
#[cfg(feature = "alloc")]
BinaryFormat::MachO => macho::pointer_size(process, module_range)?,
#[allow(unreachable_patterns)]
_ => return None,
};
let offsets = MonoOffsets::new(version, pointer_size, format)?;
let root_domain_function_address = match format {
BinaryFormat::PE => {
pe::symbols(process, mono_module)
.find(|symbol| {
symbol
.get_name::<22>(process)
.is_ok_and(|name| name.matches("mono_assembly_foreach"))
})?
.address
}
BinaryFormat::ELF => {
elf::symbols(process, mono_module)
.find(|symbol| {
symbol
.get_name::<22>(process)
.is_ok_and(|name| name.matches("mono_assembly_foreach"))
})?
.address
}
#[cfg(feature = "alloc")]
BinaryFormat::MachO => {
macho::symbols(process, module_range)
.find(|symbol| {
symbol
.get_name::<26>(process)
.is_ok_and(|name| name.matches("_mono_assembly_foreach"))
})?
.address
}
#[allow(unreachable_patterns)]
_ => return None,
};
let assemblies: Address = match (pointer_size, format) {
(PointerSize::Bit64, BinaryFormat::PE) => {
const SIG_MONO_64: Signature<3> = Signature::new("48 8B 0D");
SIG_MONO_64
.scan_process_range(process, (root_domain_function_address, 0x100))
.map(|addr| addr + 3)
.and_then(|addr| Some(addr + 0x4 + process.read::<i32>(addr).ok()?))?
}
(PointerSize::Bit64, BinaryFormat::ELF) => {
const SIG_MONO_64_ELF: Signature<3> = Signature::new("48 8B 3D");
SIG_MONO_64_ELF
.scan_process_range(process, (root_domain_function_address, 0x100))
.map(|addr| addr + 3)
.and_then(|addr| Some(addr + 0x4 + process.read::<i32>(addr).ok()?))?
}
#[cfg(feature = "alloc")]
(PointerSize::Bit64, BinaryFormat::MachO) => {
const SIG_MONO_X86_64_MACHO: Signature<3> = Signature::new("48 8B 3D");
// 57 0f 00 d0 adrp x23,(page + 0x1ea000)
// e0 da 47 f9 ldr x0,[x23, #0xfb0]=>(page + 0x1eafb0)
// adrp ldr
// 57 0f 00 d0 e0 da 47 f9
// ???10111 ???????? ???????? 1??10000 11100000 ??????10 01?????? 11111001
// hi0 hi1 hi2 lo i0 i1
const SIG_MONO_ARM_64_MACHO: Signature<8> = Signature::Complex {
needle: [
0b00010111, 0, 0, 0b10010000, 0xE0, 0b00000010, 0b01000000, 0xF9,
],
mask: [
0b00011111, 0, 0, 0b10011111, 0xFF, 0b00000011, 0b11000000, 0xFF,
],
anchor_pos: Some(4),
anchor_byte: 0xE0,
check_pos: Some(7),
check_byte: 0xF9,
};
if let Some(scan_address) = SIG_MONO_X86_64_MACHO
.scan_process_range(process, (root_domain_function_address, 0x100))
.map(|a| a + 3)
{
scan_address + 0x4 + process.read::<i32>(scan_address).ok()?
} else if let Some(scan_address) = SIG_MONO_ARM_64_MACHO
.scan_process_range(process, (root_domain_function_address, 0x100))
{
let page = scan_address.value() & 0xfffffffffffff000;
let bs = process.read::<[u8; 8]>(scan_address).ok()?;
// adrp
let lo = ((bs[3] >> 5) & 0b11) as u64;
let hi0 = (bs[0] >> 5) as u64;
let hi1 = bs[1] as u64;
let hi2 = bs[2] as u64;
let adrp = (lo << 12) | (hi0 << 14) | (hi1 << 17) | (hi2 << 25);
// ldr
let i0 = (bs[5] >> 2) as u64;
let i1 = (bs[6] & 0b111111) as u64;
let ldr = (i0 << 3) | (i1 << 9);
(page + adrp + ldr).into()
} else {
return None;
}
}
(PointerSize::Bit32, BinaryFormat::PE) => {
const SIG_32_1: Signature<2> = Signature::new("FF 35");
const SIG_32_2: Signature<2> = Signature::new("8B 0D");
let ptr = [SIG_32_1, SIG_32_2].iter().find_map(|sig| {
sig.scan_process_range(process, (root_domain_function_address, 0x100))
})? + 2;
process.read::<Address32>(ptr).ok()?.into()
}
_ => return None,
};
Some(Self {
assemblies,
version,
offsets,
pointer_size,
})
}
/// Retrieve the [Mono version](Version) of the module.
pub const fn get_version(&self) -> Version {
self.version
}
/// Retrieve the [pointer size](PointerSize) of the process/module.
pub const fn get_pointer_size(&self) -> PointerSize {
self.pointer_size
}
fn assemblies<'a>(&'a self, process: &'a Process) -> impl FusedIterator<Item = Assembly> + 'a {
let mut assembly = process
.read_pointer(self.assemblies, self.pointer_size)
.ok()
.filter(|val| !val.is_null());
iter::from_fn(move || {
let [data, next_assembly]: [Address; 2] = match self.pointer_size {
PointerSize::Bit64 => process
.read::<[Address64; 2]>(assembly?)
.ok()?
.map(|item| item.into()),
_ => process
.read::<[Address32; 2]>(assembly?)
.ok()?
.map(|item| item.into()),
};
assembly = Some(next_assembly);
Some(Assembly { assembly: data })
})
.fuse()
}
/// Looks for the specified binary [image](Image) inside the target process.
/// An [image](Image) is a .NET DLL that is loaded
/// by the game. The `Assembly-CSharp` [image](Image) is the main game
/// assembly, and contains all the game logic. The
/// [`get_default_image`](Self::get_default_image) function is a shorthand
/// for this function that accesses the `Assembly-CSharp` [image](Image).
pub fn get_image(&self, process: &Process, assembly_name: &str) -> Option<Image> {
self.assemblies(process)
.find(|assembly| {
assembly
.get_name::<CSTR>(process, self)
.is_ok_and(|name| name.matches(assembly_name))
})
.and_then(|assembly| assembly.get_image(process, self))
}
/// Looks for the `Assembly-CSharp` binary [image](Image) inside the target
/// process. An [image](Image) is a .NET DLL that is loaded
/// by the game. The `Assembly-CSharp` [image](Image) is the main
/// game assembly, and contains all the game logic. This function is a
/// shorthand for [`get_image`](Self::get_image) that accesses the
/// `Assembly-CSharp` [image](Image).
pub fn get_default_image(&self, process: &Process) -> Option<Image> {
self.get_image(process, "Assembly-CSharp")
}
/// Attaches to a Unity game that is using the standard Mono backend. This
/// function automatically detects the [Mono version](Version). If you
/// know the version in advance or it fails detecting it, use
/// [`wait_attach`](Self::wait_attach) instead.
///
/// This is the `await`able version of the
/// [`attach_auto_detect`](Self::attach_auto_detect) function, yielding back
/// to the runtime between each try.
pub async fn wait_attach_auto_detect(process: &Process) -> Module {
retry(|| Self::attach_auto_detect(process)).await
}
/// Attaches to a Unity game that is using the standard Mono backend with the
/// [Mono version](Version) provided. The version needs to be correct
/// for this function to work. If you don't know the version in advance, use
/// [`wait_attach_auto_detect`](Self::wait_attach_auto_detect) instead.
///
/// This is the `await`able version of the [`attach`](Self::attach)
/// function, yielding back to the runtime between each try.
pub async fn wait_attach(process: &Process, version: Version) -> Self {
retry(|| Self::attach(process, version)).await
}
/// Looks for the specified binary [image](Image) inside the target process.
/// An [image](Image) is a .NET DLL that is loaded
/// by the game. The `Assembly-CSharp` [image](Image) is the main game
/// assembly, and contains all the game logic. The
/// [`wait_get_default_image`](Self::wait_get_default_image) function is a
/// shorthand for this function that accesses the `Assembly-CSharp`
/// [image](Image).
///
/// This is the `await`able version of the [`get_image`](Self::get_image)
/// function, yielding back to the runtime between each try.
pub async fn wait_get_image(&self, process: &Process, assembly_name: &str) -> Image {
retry(|| self.get_image(process, assembly_name)).await
}
/// Looks for the `Assembly-CSharp` binary [image](Image) inside the target
/// process. An [image](Image) is a .NET DLL that
/// is loaded by the game. The `Assembly-CSharp` [image](Image) is the main
/// game assembly, and contains all the game logic. This function is a
/// shorthand for [`wait_get_image`](Self::wait_get_image) that accesses the
/// `Assembly-CSharp` [image](Image).
///
/// This is the `await`able version of the
/// [`get_default_image`](Self::get_default_image) function, yielding back
/// to the runtime between each try.
pub async fn wait_get_default_image(&self, process: &Process) -> Image {
retry(|| self.get_default_image(process)).await
}
#[inline]
const fn size_of_ptr(&self) -> u64 {
self.pointer_size as u64
}
}