neaten up imports
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use crate::Error;
|
||||
use std::ops::Deref;
|
||||
|
||||
use windows::Win32::Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE};
|
||||
use windows::Win32::System::Threading::{
|
||||
OpenProcess, PROCESS_ALL_ACCESS, PROCESS_VM_READ, PROCESS_VM_WRITE,
|
||||
use windows::Win32::{
|
||||
Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE},
|
||||
System::Threading::{OpenProcess, PROCESS_ALL_ACCESS, PROCESS_VM_READ, PROCESS_VM_WRITE},
|
||||
};
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
@@ -28,16 +28,16 @@ impl Drop for Handle {
|
||||
}
|
||||
|
||||
impl Handle {
|
||||
pub fn from_pid(pid: u32) -> Result<Self, crate::Error> {
|
||||
pub fn from_pid(pid: u32) -> Result<Self, Error> {
|
||||
let handle = unsafe { OpenProcess(PROCESS_ALL_ACCESS, false, pid) }
|
||||
.or_else(|_| unsafe { OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid) })
|
||||
.map_err(|why| {
|
||||
crate::Error::ObtainHandleError(format!(
|
||||
Error::ObtainHandleError(format!(
|
||||
"failed to open process with needed access: {why}",
|
||||
))
|
||||
})?;
|
||||
if handle == INVALID_HANDLE_VALUE {
|
||||
return Err(crate::Error::ObtainHandleError(
|
||||
return Err(Error::ObtainHandleError(
|
||||
"failed to get a valid handle".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
use super::handle::Handle;
|
||||
|
||||
use crate::{process::handle::Handle, Error};
|
||||
use std::{ffi::c_void, ptr};
|
||||
|
||||
use windows::Win32::System::Diagnostics::Debug::{ReadProcessMemory, WriteProcessMemory};
|
||||
|
||||
pub fn read<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), crate::Error> {
|
||||
pub fn read<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), Error> {
|
||||
unsafe {
|
||||
ReadProcessMemory(
|
||||
handle.0,
|
||||
@@ -14,10 +12,10 @@ pub fn read<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), cra
|
||||
None,
|
||||
)
|
||||
}
|
||||
.map_err(|why| crate::Error::AccessMemoryError(format!("failed to read memory: {why}")))
|
||||
.map_err(|why| Error::AccessMemoryError(format!("failed to read memory: {why}")))
|
||||
}
|
||||
|
||||
pub fn write<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), crate::Error> {
|
||||
pub fn write<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), Error> {
|
||||
unsafe {
|
||||
WriteProcessMemory(
|
||||
handle.0,
|
||||
@@ -27,5 +25,5 @@ pub fn write<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), cr
|
||||
None,
|
||||
)
|
||||
}
|
||||
.map_err(|why| crate::Error::AccessMemoryError(format!("failed to write memory: {why}")))
|
||||
.map_err(|why| Error::AccessMemoryError(format!("failed to write memory: {why}")))
|
||||
}
|
||||
|
||||
+11
-9
@@ -1,5 +1,7 @@
|
||||
use super::{handle::Handle, memory, module::Module, snapshot};
|
||||
|
||||
use crate::{
|
||||
process::{handle::Handle, memory, snapshot, Module},
|
||||
Error,
|
||||
};
|
||||
use derive_more::derive::Display;
|
||||
|
||||
#[derive(Display)]
|
||||
@@ -41,7 +43,7 @@ unsafe impl Sync for Process {}
|
||||
|
||||
impl Process {
|
||||
/// initialize a `Process` from a pid or a process name
|
||||
pub fn from<T: Into<Identifier>>(identifier: T) -> Result<Self, crate::Error> {
|
||||
pub fn from<T: Into<Identifier>>(identifier: T) -> Result<Self, Error> {
|
||||
let identifier = identifier.into();
|
||||
let Some(snapshot) = (match identifier {
|
||||
Identifier::Pid(pid) => snapshot::ProcessSnapshot::get_processes()?
|
||||
@@ -51,7 +53,7 @@ impl Process {
|
||||
.into_iter()
|
||||
.find(|snapshot| snapshot.name == *name),
|
||||
}) else {
|
||||
return Err(crate::Error::CreateProcessError(format!(
|
||||
return Err(Error::CreateProcessError(format!(
|
||||
"failed to find a process with identifier `{identifier}`",
|
||||
)));
|
||||
};
|
||||
@@ -66,12 +68,12 @@ impl Process {
|
||||
}
|
||||
|
||||
/// get the `Module` of a `Process` by name
|
||||
pub fn module(&self, name: &str) -> Result<Module, crate::Error> {
|
||||
pub fn module(&self, name: &str) -> Result<Module, Error> {
|
||||
let Some(snapshot) = snapshot::ModuleSnapshot::get_modules(self.id)?
|
||||
.into_iter()
|
||||
.find(|snapshot| snapshot.name == name)
|
||||
else {
|
||||
return Err(crate::Error::CreateProcessError(format!(
|
||||
return Err(Error::CreateProcessError(format!(
|
||||
"failed to find a module with identifier `{name}`",
|
||||
)));
|
||||
};
|
||||
@@ -86,7 +88,7 @@ impl Process {
|
||||
}
|
||||
|
||||
/// follow a pointer chain to the end and return an address
|
||||
pub fn resolve_pointer_chain(&self, chain: &[usize]) -> Result<usize, crate::Error> {
|
||||
pub fn resolve_pointer_chain(&self, chain: &[usize]) -> Result<usize, Error> {
|
||||
let mut chain = chain.to_vec();
|
||||
let mut address = chain.remove(0);
|
||||
while chain.len() > 1 {
|
||||
@@ -97,14 +99,14 @@ impl Process {
|
||||
}
|
||||
|
||||
/// read a given `address` of process's memory
|
||||
pub fn read_mem<T: Default>(&self, address: usize) -> Result<T, crate::Error> {
|
||||
pub fn read_mem<T: Default>(&self, address: usize) -> Result<T, Error> {
|
||||
let mut value = Default::default();
|
||||
memory::read(&self.handle, address, &mut value)?;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// write a `value` at given `address` of process's memory
|
||||
pub fn write_mem<T: Default>(&self, address: usize, mut value: T) -> Result<(), crate::Error> {
|
||||
pub fn write_mem<T: Default>(&self, address: usize, mut value: T) -> Result<(), Error> {
|
||||
memory::write(&self.handle, address, &mut value)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+7
-14
@@ -1,5 +1,6 @@
|
||||
#![allow(clippy::module_name_repetitions)]
|
||||
|
||||
use crate::Error;
|
||||
use windows::Win32::System::Diagnostics::ToolHelp::{
|
||||
CreateToolhelp32Snapshot, Module32FirstW, Module32NextW, Process32FirstW, Process32NextW,
|
||||
MODULEENTRY32W, PROCESSENTRY32W, TH32CS_SNAPMODULE, TH32CS_SNAPMODULE32, TH32CS_SNAPPROCESS,
|
||||
@@ -12,21 +13,17 @@ pub struct ProcessSnapshot {
|
||||
}
|
||||
|
||||
impl ProcessSnapshot {
|
||||
pub fn get_processes() -> Result<Vec<Self>, crate::Error> {
|
||||
pub fn get_processes() -> Result<Vec<Self>, Error> {
|
||||
let snapshot =
|
||||
unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) }.map_err(|why| {
|
||||
crate::Error::CreateSnapshotError(format!(
|
||||
"failed to create process snapshot: {why}"
|
||||
))
|
||||
Error::CreateSnapshotError(format!("failed to create process snapshot: {why}"))
|
||||
})?;
|
||||
let mut process_entry_32_w = PROCESSENTRY32W {
|
||||
dwSize: u32::try_from(size_of::<PROCESSENTRY32W>())?,
|
||||
..Default::default()
|
||||
};
|
||||
unsafe { Process32FirstW(snapshot, &mut process_entry_32_w) }.map_err(|why| {
|
||||
crate::Error::CreateSnapshotError(format!(
|
||||
"failed to get first process from snapshot: {why}"
|
||||
))
|
||||
Error::CreateSnapshotError(format!("failed to get first process from snapshot: {why}"))
|
||||
})?;
|
||||
let mut processes = Vec::new();
|
||||
loop {
|
||||
@@ -55,22 +52,18 @@ pub struct ModuleSnapshot {
|
||||
}
|
||||
|
||||
impl ModuleSnapshot {
|
||||
pub fn get_modules(pid: u32) -> Result<Vec<Self>, crate::Error> {
|
||||
pub fn get_modules(pid: u32) -> Result<Vec<Self>, Error> {
|
||||
let snapshot =
|
||||
unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid) }
|
||||
.map_err(|why| {
|
||||
crate::Error::CreateSnapshotError(format!(
|
||||
"failed to create module snapshot: {why}"
|
||||
))
|
||||
Error::CreateSnapshotError(format!("failed to create module snapshot: {why}"))
|
||||
})?;
|
||||
let mut module_entry_32_w = MODULEENTRY32W {
|
||||
dwSize: u32::try_from(size_of::<MODULEENTRY32W>())?,
|
||||
..Default::default()
|
||||
};
|
||||
unsafe { Module32FirstW(snapshot, &mut module_entry_32_w) }.map_err(|why| {
|
||||
crate::Error::CreateSnapshotError(format!(
|
||||
"failed to get first module from snapshot: {why}"
|
||||
))
|
||||
Error::CreateSnapshotError(format!("failed to get first module from snapshot: {why}"))
|
||||
})?;
|
||||
let mut modules = Vec::new();
|
||||
loop {
|
||||
|
||||
Reference in New Issue
Block a user