mem_rs/pointer.rs
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
// This file is part of the mem-rs distribution (https://github.com/FrankvdStam/mem-rs).
// Copyright (c) 2022 Frank van der Stam.
// https://github.com/FrankvdStam/mem-rs/blob/main/LICENSE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use std::cell::RefCell;
use std::rc::Rc;
use crate::read_write::{BaseReadWrite, ReadWrite};
use crate::process_data::ProcessData;
/// Represents a pointer path that is dynamically resolved each read/write operation.
/// This ensures that the pointer is always valid. Race conditions can occur and the pointer could encounter
/// a null pointer along the path. Should always be constructed via the Process struct.
///
/// # Example
///
/// ```
/// use mem_rs::prelude::*;
///
/// let mut process = Process::new("name_of_process.exe");
/// process.refresh()?;
/// let pointer = process.create_pointer(0x1234, vec![0]);
/// let data = pointer.read_u8_rel(Some(0x1234));
/// ```
pub struct Pointer
{
process_data: Rc<RefCell<ProcessData>>,
is_64_bit: bool,
base_address: usize,
offsets: Vec<usize>,
/// Set this to true to print each memory address while resolving the pointer path.
pub debug: bool,
}
impl Default for Pointer
{
fn default() -> Self
{
Pointer
{
process_data: Rc::new(RefCell::new(ProcessData::default())),
is_64_bit: true,
base_address: 0,
offsets: Vec::new(),
debug: false,
}
}
}
impl Pointer
{
pub(crate) fn new(process_data: Rc<RefCell<ProcessData>>, is_64_bit: bool, base_address: usize, offsets: Vec<usize>) -> Self
{
Pointer
{
process_data,
is_64_bit,
base_address,
offsets,
debug: false,
}
}
/// Get the base address of this pointer, without resolving offsets.
pub fn get_base_address(&self) -> usize
{
return self.base_address;
}
fn resolve_offsets(&self, offsets: &Vec<usize>) -> usize
{
let mut path = String::from(format!(" {:#010x}", self.base_address));
let mut ptr = self.base_address;
for i in 0..offsets.len()
{
let offset = offsets[i];
//Create a copy for debug output
let debug_copy = ptr;
//Resolve an offset
let address = ptr + offset;
//Not the last offset = resolve as pointer
if i + 1 < offsets.len()
{
if self.is_64_bit
{
let mut buffer = [0; 8];
self.read_memory_abs(address, &mut buffer);
ptr = u64::from_ne_bytes(buffer) as usize;
}
else
{
let mut buffer = [0; 4];
self.read_memory_abs(address, &mut buffer);
ptr = u32::from_ne_bytes(buffer) as usize;
}
path.push_str(format!("\n[{:#010x} + {:#010x}]: {:#010x}", debug_copy, offset, ptr).as_str());
if ptr == 0
{
if self.debug
{
println!("{}", path);
}
return 0;
}
}
else
{
ptr = address;
path.push_str(format!("\n{:#010x} + {:#010x}: {:#010x}", debug_copy, offset, ptr).as_str());
}
}
if self.debug
{
println!("{}", path);
}
return ptr;
}
}
impl BaseReadWrite for Pointer
{
fn read_memory_rel(&self, offset: Option<usize>, buffer: &mut [u8]) -> bool
{
let mut copy = self.offsets.clone();
if offset.is_some()
{
copy.push(offset.unwrap());
}
let address = self.resolve_offsets(©);
return self.read_with_handle(self.process_data.borrow().handle, address, buffer);
}
fn write_memory_rel(&self, offset: Option<usize>, buffer: &[u8]) -> bool
{
let mut copy = self.offsets.clone();
if offset.is_some()
{
copy.push(offset.unwrap());
}
let address = self.resolve_offsets(©);
return self.write_with_handle(self.process_data.borrow().handle, address, buffer);
}
fn read_memory_abs(&self, address: usize, buffer: &mut [u8]) -> bool
{
return self.read_with_handle(self.process_data.borrow().handle, address, buffer);
}
fn write_memory_abs(&self, address: usize, buffer: &[u8]) -> bool
{
return self.write_with_handle(self.process_data.borrow().handle, address, buffer);
}
}
impl ReadWrite for Pointer{}