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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use Error;
/// find_pattern attempts to find a matching pattern anywhere between
/// the start and end offsets.
///
/// This function was originally written by Jacob T. Read (jacobtread)
/// for their PocketRelay project (Copyright (c) 2023 - 2024 Jacobtread).
///
/// # Safety
///
/// This function is unsafe because it interacts with memory that may be
/// owned by other code or memory that is being operated on concurrently
/// by another thread.
///
/// # Arguments
///
/// * `start_offset` - The address to start matching from.
/// * `end_offset` - The address to stop matching at.
/// * `mask` - The mask to use when matching data. Refer to the
/// compare_mask function's rustdoc for an explanation of the mask string.
/// * `bytes` - The bytes to match against.
///
/// # Examples
///
/// Find the address of a local variable with
/// a value of 0xdeadbeef:
///
/// ```no_run
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let example: u64 = 0xdeadbeef;
///
/// let ptr = std::ptr::addr_of!(example) as usize;
///
/// eprintln!("ptr: {ptr:#x}");
///
/// let result = unsafe {
/// mrevise::find_pattern(ptr, ptr + 0xFF, "????xxxx", &[0xde, 0xad, 0xbe, 0xef])
/// };
///
/// match result {
/// Some(found_ptr) => {
/// let value = unsafe { *(found_ptr as *const u64) };
///
/// eprintln!("found target at: {found_ptr:#x?} - value: {value:#x}");
/// }
/// None => eprintln!("failed to find target :("),
/// }
///
/// Ok(())
/// }
/// ```
pub unsafe
/// compare_mask compares the bytes after the provided address using
/// the provided pattern.
///
/// This function was originally written by Jacob T. Read (jacobtread)
/// for their PocketRelay project (Copyright (c) 2023 - 2024 Jacobtread).
///
/// # Safety
///
/// This function is unsafe because it interacts with memory that may be
/// owned by other code or memory that is being operated on concurrently
/// by another thread.
///
/// # Arguments
///
/// * `addr` - The address to start matching from.
/// * `mask` - The mask to use when matching data. This string must be
/// the same length as the bytes argument. It can consist of a wildcard
/// character ("?") which indicates any byte can match at the given
/// position or a must-match character (this can be any character other
/// than "?", but is typically just "x").
/// * `bytes` - The bytes to match against.
pub unsafe
/// MopConfig defines the bounds of a memory chunk to operate on and
/// configures the behavior of the mop function.
/// MaybeProt specifies the memory protection behavior for the mop function.
/// Prot represents a memory protection setting.
/// mop (memory operation) handles the common toil involved in operating
/// on a memory chunk, such as setting the chunk's protection settings
/// before and after operating on it, aligning the chunk's boundaries to
/// a certain bit width, and reading from and writing to the chunk.
///
/// The function works by first applying the config.prot_before memory
/// protection setting to the target memory chunk. The op_func closure
/// is then executed. After op_func finishes running, config.prot_after
/// is applied to the memory chunk.
///
/// # Safety
///
/// This function is unsafe because it interacts with memory that may be
/// owned by other code or memory that is being operated on concurrently
/// by another thread.
///
/// # Arguments
///
/// * `config` - A struct that specifies the target memory chunk's
/// boundaries and this function's behavior.
/// * `op_func` - The closure to execute once the config has been
/// applied. The closure will receive an object representing the
/// final address of the memory chunk being operated on after the
/// optional alignment has been applied. The closure can return
/// a result with an error to communicate an error condition back
/// to the code that invoked mop.
///
/// # Examples
///
/// Change a read-only global variable's value to 0xdeadbeef.
///
/// ```no_run
/// static EXAMPLE: u64 = 0x00;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // This will output: 0x0.
/// eprintln!("value before: {EXAMPLE:#x?}");
///
/// unsafe {
/// mrevise::mop(
/// mrevise::MopConfig {
/// pointer: std::ptr::addr_of!(EXAMPLE),
/// size: 8,
/// align_to: Some(4096),
/// prot_before: mrevise::MaybeProt::ChangeTo(mrevise::Prot::ReadWrite),
/// prot_after: mrevise::MaybeProt::ChangeTo(mrevise::Prot::Read),
/// },
/// |example_ptr| {
/// *example_ptr = 0xdeadbeef;
///
/// Ok(())
/// },
/// )
/// }?;
///
/// // This outputs: 0xdeadbeef.
/// eprintln!("value after: {EXAMPLE:#x?}");
///
/// Ok(())
/// }
/// ```
pub unsafe
/// protect modifies the protection settings of a memory chunk for the
/// current process.
///
/// It provides identical functionality to the mprotect(2) system call
/// on Unix-like systems and the Windows VirtualProtect function.
///
/// # Safety
///
/// This function is unsafe because it interacts with memory that may be
/// owned by other code or memory that is being operated on concurrently
/// by another thread.
///
/// # Arguments
///
/// * `pointer` - The memory address to operate on.
/// * `size` - The size of the memory chunk to operate on.
/// * `prot` - The memory protection to apply to the memory chunk.
/// * `allign_with` - An optional boundary to align the chunk to. This is
/// typically be set to the platform's page size, which is commonly (but
/// not always!) 4096 bits. Or, in other words: `Some(4096)`
///
/// # Examples
///
/// Change a read-only global variable's value to 0xdeadbeef.
///
/// ```no_run
/// static EXAMPLE: u64 = 0x00;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let ptr = std::ptr::addr_of!(EXAMPLE) as *mut u64;
///
/// // This will output: 0x0.
/// eprintln!("value before: {EXAMPLE:#x?}");
///
/// // If we do not change the memory protection
/// // of the EXAMPLE global variable, this program
/// // will segfault when updating it.
/// mrevise::protect(
/// ptr,
/// std::mem::size_of::<u64>(),
/// mrevise::Prot::ReadWrite,
/// Some(4096),
/// )?;
///
/// unsafe { *ptr = 0xdeadbeef };
///
/// // This will output: 0xdeadbeef.
/// eprintln!("value after: {EXAMPLE:#x?}");
///
/// Ok(())
/// }
/// ```
/// ProtectResult captures information after a successful call to
/// the protect function.
/// AllocFlags are the flags to apply when calling the alloc function.
/// alloc allocates memory for the current process.
///
/// It provides identical functionality to the mmap(2) system call
/// on Unix-like systems and the Windows VirtualAlloc function.
///
/// # Arguments
///
/// * `addr` - An optional address to allocate memory on top of.
/// If None, then a new chunk is allocated.
/// * `size` - The size of the allocation in bytes.
/// * `prot` - The memory protection settings to apply to the new chunk.
/// * `flags` - The AllocFlags to use.
///
/// # Examples
///
/// Allocate a new chunk of memory and tell Rust to treat it as
/// an array of u8 with four elements:
///
/// ```no_run
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut chunk = unsafe {
/// *mrevise::alloc::<[u8; 4]>(
/// None,
/// 4,
/// mrevise::Prot::ReadWrite,
/// mrevise::AllocFlags::Default,
/// )?
/// };
///
/// chunk[0] = 0xde;
/// chunk[1] = 0xad;
/// chunk[2] = 0xbe;
/// chunk[3] = 0xef;
///
/// // This outputs: [0xde, 0xad, 0xbe, 0xef].
/// eprintln!("{chunk:#x?}");
///
/// Ok(())
/// }
/// ```
/// align_chunk aligns a pointer to an arbitrary chunk of memory according
/// to the specified bits. It returns a new pointer and the size of the
/// the new chunk (that is: the original chunk's size plus the length
/// between the aligned pointer and the original pointer).
///
/// # Arguments
///
/// * `pointer` - A pointer to a chunk of memory.
/// * `bits` - The bits to align the pointer to (usually the system's
/// page size - e.g., 4096 or 0x1000).
/// * `chunk_size` - The size of the chunk that pointer is pointing at.
///
/// # Examples
///
/// Align an 8-byte chunk to a boundary of 4096 bits:
///
/// ```no_run
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut example: u64 = 0x8badf00d;
///
/// let aligned = mrevise::align_chunk(std::ptr::addr_of_mut!(example), 4096, 8);
///
/// // This outputs:
/// // old ptr: 0x00002bd810fe49e8 | new ptr: 0x00002bd810fe4000 | new chunk size: 2544
/// eprintln!(
/// "old ptr: {:#x?} | new ptr: {:#x?} | new chunk size: {}",
/// std::ptr::addr_of!(example),
/// aligned.new_ptr,
/// aligned.new_size
/// );
///
/// Ok(())
/// }
/// ```
/// AlignedChunk represents a memory chunk that has been aligned to
/// a bit width and the chunk's new size. The new size is the sum of
/// the old chunk's size plus the length from the aligned chunk pointer
/// to the old pointer.