Remove Address struct

This commit is contained in:
a2x
2024-03-02 01:21:34 +10:00
parent 0bc7cf7b20
commit 7d5de35805
13 changed files with 67 additions and 225 deletions

View File

@@ -1,137 +0,0 @@
use std::fmt::{LowerHex, UpperHex};
use std::ops::{Add, AddAssign, Sub, SubAssign};
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Address(pub usize);
impl Address {
#[inline]
pub fn add(&self, value: usize) -> Self {
Self(self.0 + value)
}
#[inline]
pub fn is_zero(&self) -> bool {
self.0 == 0
}
#[inline]
pub fn sub(&self, value: usize) -> Self {
Self(self.0 - value)
}
#[inline]
pub fn as_ptr<T>(&self) -> *const T {
self.0 as *const T
}
#[inline]
pub fn as_mut_ptr<T>(&self) -> *mut T {
self.0 as *mut T
}
}
impl From<usize> for Address {
fn from(value: usize) -> Self {
Self(value)
}
}
impl From<*const u8> for Address {
fn from(value: *const u8) -> Self {
Self(value as usize)
}
}
impl From<*mut u8> for Address {
fn from(value: *mut u8) -> Self {
Self(value as usize)
}
}
impl From<Address> for usize {
fn from(value: Address) -> Self {
value.0
}
}
impl From<Address> for *const u8 {
fn from(value: Address) -> Self {
value.0 as *const u8
}
}
impl From<Address> for *mut u8 {
fn from(value: Address) -> Self {
value.0 as *mut u8
}
}
impl Add<usize> for Address {
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}
impl Add<Address> for Address {
type Output = Self;
fn add(self, rhs: Address) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign<usize> for Address {
fn add_assign(&mut self, rhs: usize) {
self.0 += rhs;
}
}
impl AddAssign<Address> for Address {
fn add_assign(&mut self, rhs: Address) {
self.0 += rhs.0;
}
}
impl Sub<usize> for Address {
type Output = Self;
fn sub(self, rhs: usize) -> Self::Output {
Self(self.0 - rhs)
}
}
impl Sub<Address> for Address {
type Output = Self;
fn sub(self, rhs: Address) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl SubAssign<usize> for Address {
fn sub_assign(&mut self, rhs: usize) {
self.0 -= rhs;
}
}
impl SubAssign<Address> for Address {
fn sub_assign(&mut self, rhs: Address) {
self.0 -= rhs.0;
}
}
impl UpperHex for Address {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:#X}", self.0)
}
}
impl LowerHex for Address {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:#x}", self.0)
}
}

View File

@@ -1,7 +1,5 @@
pub use address::Address;
pub use module::Module;
pub use process::Process;
pub mod address;
pub mod module;
pub mod process;

View File

@@ -6,8 +6,6 @@ use goblin::pe::options::ParseOptions;
use goblin::pe::section_table::SectionTable;
use goblin::pe::PE;
use super::Address;
pub struct Module<'a> {
pub name: &'a str,
pub data: &'a [u8],
@@ -28,8 +26,8 @@ impl<'a> Module<'a> {
}
#[inline]
pub fn base(&self) -> Address {
self.pe.image_base.into()
pub fn base(&self) -> usize {
self.pe.image_base
}
#[inline]
@@ -43,21 +41,21 @@ impl<'a> Module<'a> {
}
#[inline]
pub fn export_by_name(&self, name: &str) -> Option<Address> {
pub fn export_by_name(&self, name: &str) -> Option<usize> {
self.pe
.exports
.iter()
.find(|e| e.name.unwrap() == name)
.map(|e| (self.pe.image_base + e.rva).into())
.map(|e| self.pe.image_base + e.rva)
}
#[inline]
pub fn import_by_name(&self, name: &str) -> Option<Address> {
pub fn import_by_name(&self, name: &str) -> Option<usize> {
self.pe
.imports
.iter()
.find(|i| i.name.to_string() == name)
.map(|i| (self.pe.image_base + i.rva).into())
.map(|i| self.pe.image_base + i.rva)
}
#[inline]

View File

@@ -10,7 +10,7 @@ use windows::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows::Win32::System::Diagnostics::ToolHelp::*;
use windows::Win32::System::Threading::{OpenProcess, PROCESS_ALL_ACCESS};
use super::{Address, Module};
use super::Module;
#[derive(Debug)]
pub struct Process {
@@ -36,7 +36,7 @@ impl Process {
Ok(process)
}
pub fn find_pattern(&self, module_name: &str, pattern: &str) -> Option<Address> {
pub fn find_pattern(&self, module_name: &str, pattern: &str) -> Option<usize> {
let module = self.get_module_by_name(module_name)?;
let pattern_bytes = Self::pattern_to_bytes(pattern);
@@ -70,7 +70,7 @@ impl Process {
Ok(modules)
}
pub fn read_memory<T>(&self, address: Address) -> Result<T> {
pub fn read_memory<T>(&self, address: usize) -> Result<T> {
let mut buffer: T = unsafe { mem::zeroed() };
self.read_memory_raw(
@@ -82,16 +82,11 @@ impl Process {
Ok(buffer)
}
pub fn read_memory_raw(
&self,
address: Address,
buffer: *mut c_void,
size: usize,
) -> Result<()> {
pub fn read_memory_raw(&self, address: usize, buffer: *mut c_void, size: usize) -> Result<()> {
unsafe {
ReadProcessMemory(
self.handle,
address.as_ptr(),
address as *mut _,
buffer,
size,
Some(ptr::null_mut()),
@@ -100,7 +95,7 @@ impl Process {
.map_err(|e| e.into())
}
pub fn read_string(&self, address: Address) -> Result<String> {
pub fn read_string(&self, address: usize) -> Result<String> {
let mut buffer = Vec::new();
for i in 0.. {
@@ -113,7 +108,7 @@ impl Process {
Ok(String::from_utf8(buffer)?)
}
pub fn read_string_length(&self, address: Address, length: usize) -> Result<String> {
pub fn read_string_length(&self, address: usize, length: usize) -> Result<String> {
let mut buffer = vec![0; length];
self.read_memory_raw(address, buffer.as_mut_ptr() as *mut _, length)?;
@@ -127,30 +122,26 @@ impl Process {
pub fn resolve_jmp(
&self,
address: Address,
address: usize,
offset: Option<usize>,
length: Option<usize>,
) -> Result<Address> {
) -> Result<usize> {
// The displacement value can be negative.
let displacement = self.read_memory::<i32>(address.add(offset.unwrap_or(0x1)))?;
let displacement = self.read_memory::<i32>(address + offset.unwrap_or(0x1))?;
Ok(address
.add(length.unwrap_or(0x5))
.add(displacement as usize))
Ok((address + displacement as usize) + length.unwrap_or(0x5))
}
pub fn resolve_rip(
&self,
address: Address,
address: usize,
offset: Option<usize>,
length: Option<usize>,
) -> Result<Address> {
) -> Result<usize> {
// The displacement value can be negative.
let displacement = self.read_memory::<i32>(address.add(offset.unwrap_or(0x3)))?;
let displacement = self.read_memory::<i32>(address + offset.unwrap_or(0x3))?;
Ok(address
.add(length.unwrap_or(0x7))
.add(displacement as usize))
Ok((address + displacement as usize) + length.unwrap_or(0x7))
}
fn get_process_id_by_name(process_name: &str) -> Result<u32> {
@@ -193,7 +184,7 @@ impl Process {
let mut data = vec![0; entry.modBaseSize as usize];
if let Ok(_) = self.read_memory_raw(
entry.modBaseAddr.into(),
entry.modBaseAddr as _,
data.as_mut_ptr() as *mut _,
data.len(),
) {