Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d795ec982
|
||
|
|
bd90e08a87
|
||
|
|
9eb953f84c
|
||
|
|
93bb675333
|
||
|
|
99db8bcd72
|
||
|
|
4477c81b81
|
||
|
|
a9a1c4870c
|
||
|
|
432ec8adea
|
||
|
|
f2da8b5328
|
||
|
|
bd51e3e2e2
|
+8
-8
@@ -1,20 +1,20 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "haxor"
|
name = "haxor"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
description = "memory hacking library"
|
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository = "https://codeberg.org/futile/haxor"
|
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
|
description = "memory hacking library"
|
||||||
|
repository = "https://github.com/elituf/haxor"
|
||||||
keywords = ["memory", "game", "hacking", "process"]
|
keywords = ["memory", "game", "hacking", "process"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
derive_more = { version = "1.0.0", features = ["display"] }
|
derive_more = { version = "2", features = ["display"] }
|
||||||
log = "0.4.22"
|
log = "0.4"
|
||||||
thiserror = "2.0.9"
|
thiserror = "2"
|
||||||
|
|
||||||
[dependencies.windows]
|
[dependencies.windows]
|
||||||
version = "0.58.0"
|
version = "0.62"
|
||||||
features = [
|
features = [
|
||||||
"Win32_Foundation",
|
"Win32_Foundation",
|
||||||
"Win32_System_Threading",
|
"Win32_System_Threading",
|
||||||
|
|||||||
@@ -4,5 +4,6 @@
|
|||||||
mod error;
|
mod error;
|
||||||
/// types and methods to ease the r/w of a process's memory
|
/// types and methods to ease the r/w of a process's memory
|
||||||
pub mod process;
|
pub mod process;
|
||||||
|
mod sys;
|
||||||
|
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
use super::{handle::Handle, memory, module::Module, snapshot};
|
use crate::{
|
||||||
|
Error,
|
||||||
|
sys::{handle::Handle, memory, snapshot},
|
||||||
|
};
|
||||||
use derive_more::derive::Display;
|
use derive_more::derive::Display;
|
||||||
|
|
||||||
#[derive(Display)]
|
#[derive(Display)]
|
||||||
/// an identifier for searching for a process
|
/// an identifier for searching for a process
|
||||||
pub enum Identifier {
|
pub enum Identifier {
|
||||||
/// the process id
|
/// process id to search for
|
||||||
Pid(u32),
|
Pid(u32),
|
||||||
/// the process name
|
/// process name to search for
|
||||||
Name(String),
|
Name(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,20 +43,19 @@ unsafe impl Sync for Process {}
|
|||||||
|
|
||||||
impl Process {
|
impl Process {
|
||||||
/// initialize a `Process` from a pid or a process name
|
/// 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 identifier = identifier.into();
|
||||||
let Some(snapshot) = (match identifier {
|
let snapshot = snapshot::ProcessSnapshot::get_processes()?
|
||||||
Identifier::Pid(pid) => snapshot::ProcessSnapshot::get_processes()?
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|snapshot| snapshot.id == pid),
|
.find(|snapshot| match identifier {
|
||||||
Identifier::Name(ref name) => snapshot::ProcessSnapshot::get_processes()?
|
Identifier::Pid(pid) => snapshot.id == pid,
|
||||||
.into_iter()
|
Identifier::Name(ref name) => snapshot.name == *name,
|
||||||
.find(|snapshot| snapshot.name == *name),
|
})
|
||||||
}) else {
|
.ok_or_else(|| {
|
||||||
return Err(crate::Error::CreateProcessError(format!(
|
Error::CreateProcessError(format!(
|
||||||
"failed to find a process with identifier `{identifier}`",
|
"failed to find a process with identifier `{identifier}`",
|
||||||
)));
|
))
|
||||||
};
|
})?;
|
||||||
let mut process = Self {
|
let mut process = Self {
|
||||||
name: snapshot.name,
|
name: snapshot.name,
|
||||||
id: snapshot.id,
|
id: snapshot.id,
|
||||||
@@ -65,13 +66,13 @@ impl Process {
|
|||||||
Ok(process)
|
Ok(process)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// get the `Module` of a `Process` by name
|
/// get a `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)?
|
let Some(snapshot) = snapshot::ModuleSnapshot::get_modules(self.id)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|snapshot| snapshot.name == name)
|
.find(|snapshot| snapshot.name == name)
|
||||||
else {
|
else {
|
||||||
return Err(crate::Error::CreateProcessError(format!(
|
return Err(Error::CreateProcessError(format!(
|
||||||
"failed to find a module with identifier `{name}`",
|
"failed to find a module with identifier `{name}`",
|
||||||
)));
|
)));
|
||||||
};
|
};
|
||||||
@@ -86,7 +87,7 @@ impl Process {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// follow a pointer chain to the end and return an address
|
/// 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 chain = chain.to_vec();
|
||||||
let mut address = chain.remove(0);
|
let mut address = chain.remove(0);
|
||||||
while chain.len() > 1 {
|
while chain.len() > 1 {
|
||||||
@@ -97,15 +98,30 @@ impl Process {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// read a given `address` of process's memory
|
/// 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();
|
let mut value = Default::default();
|
||||||
memory::read(&self.handle, address, &mut value)?;
|
memory::read(&self.handle, address, &mut value)?;
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// write a `value` at given `address` of process's memory
|
/// 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)?;
|
memory::write(&self.handle, address, &mut value)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
/// a module running within a process
|
||||||
|
pub struct Module {
|
||||||
|
/// the parent process id (th32ProcessID)
|
||||||
|
pub process_id: u32,
|
||||||
|
/// the module name (szModule)
|
||||||
|
pub name: String,
|
||||||
|
/// the module executable path (szExePath)
|
||||||
|
pub path: String,
|
||||||
|
/// the module base address (modBaseAddr)
|
||||||
|
pub base_address: usize,
|
||||||
|
/// the module base size (modBaseSize)
|
||||||
|
pub base_size: usize,
|
||||||
|
}
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
|
||||||
pub struct Handle(pub HANDLE);
|
|
||||||
|
|
||||||
unsafe impl Send for Handle {}
|
|
||||||
unsafe impl Sync for Handle {}
|
|
||||||
|
|
||||||
impl Deref for Handle {
|
|
||||||
type Target = HANDLE;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for Handle {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
unsafe {
|
|
||||||
if let Err(why) = CloseHandle(**self) {
|
|
||||||
log::error!("failed to close handle: {why}");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Handle {
|
|
||||||
pub fn from_pid(pid: u32) -> Result<Self, crate::Error> {
|
|
||||||
let handle = unsafe {
|
|
||||||
OpenProcess(PROCESS_ALL_ACCESS, false, pid)
|
|
||||||
.or_else(|_| OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid))
|
|
||||||
.map_err(|why| {
|
|
||||||
crate::Error::ObtainHandleError(format!(
|
|
||||||
"failed to open process with needed access: {why}",
|
|
||||||
))
|
|
||||||
})?
|
|
||||||
};
|
|
||||||
if handle == INVALID_HANDLE_VALUE {
|
|
||||||
return Err(crate::Error::ObtainHandleError(
|
|
||||||
"failed to get a valid handle".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
Ok(Self(handle))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#![allow(clippy::module_inception)]
|
|
||||||
|
|
||||||
mod handle;
|
|
||||||
mod memory;
|
|
||||||
mod module;
|
|
||||||
mod process;
|
|
||||||
mod snapshot;
|
|
||||||
|
|
||||||
pub use module::Module;
|
|
||||||
pub use process::{Identifier, Process};
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#[derive(Debug, Default)]
|
|
||||||
/// a module running within a process
|
|
||||||
pub struct Module {
|
|
||||||
/// the parent process id (th32ProcessID)
|
|
||||||
pub process_id: u32,
|
|
||||||
/// the module name (szModule)
|
|
||||||
pub name: String,
|
|
||||||
/// the module executable path (szExePath)
|
|
||||||
pub path: String,
|
|
||||||
/// the module base address (modBaseAddr)
|
|
||||||
pub base_address: usize,
|
|
||||||
/// the module base size (modBaseSize)
|
|
||||||
pub base_size: usize,
|
|
||||||
}
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
#![allow(clippy::module_name_repetitions)]
|
|
||||||
|
|
||||||
use windows::Win32::System::Diagnostics::ToolHelp::{
|
|
||||||
CreateToolhelp32Snapshot, Module32FirstW, Module32NextW, Process32FirstW, Process32NextW,
|
|
||||||
MODULEENTRY32W, PROCESSENTRY32W, TH32CS_SNAPMODULE, TH32CS_SNAPMODULE32, TH32CS_SNAPPROCESS,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ProcessSnapshot {
|
|
||||||
pub id: u32,
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ProcessSnapshot {
|
|
||||||
pub fn get_processes() -> Result<Vec<Self>, crate::Error> {
|
|
||||||
let snapshot = unsafe {
|
|
||||||
match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) {
|
|
||||||
Ok(snapshot) => snapshot,
|
|
||||||
Err(why) => {
|
|
||||||
return Err(crate::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 {
|
|
||||||
match Process32FirstW(snapshot, &mut process_entry_32_w) {
|
|
||||||
Ok(()) => {}
|
|
||||||
Err(why) => {
|
|
||||||
return Err(crate::Error::CreateSnapshotError(format!(
|
|
||||||
"failed to get first process from snapshot: {why}"
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
let mut processes = Vec::new();
|
|
||||||
loop {
|
|
||||||
let name = String::from_utf16_lossy(&process_entry_32_w.szExeFile)
|
|
||||||
.trim_end_matches('\0')
|
|
||||||
.to_string();
|
|
||||||
let process = Self {
|
|
||||||
id: process_entry_32_w.th32ProcessID,
|
|
||||||
name,
|
|
||||||
};
|
|
||||||
processes.push(process);
|
|
||||||
unsafe {
|
|
||||||
if Process32NextW(snapshot, &mut process_entry_32_w).is_err() {
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(processes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ModuleSnapshot {
|
|
||||||
pub name: String,
|
|
||||||
pub path: String,
|
|
||||||
pub base_address: usize,
|
|
||||||
pub base_size: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ModuleSnapshot {
|
|
||||||
pub fn get_modules(pid: u32) -> Result<Vec<Self>, crate::Error> {
|
|
||||||
let snapshot = unsafe {
|
|
||||||
match CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid) {
|
|
||||||
Ok(snapshot) => snapshot,
|
|
||||||
Err(why) => {
|
|
||||||
return Err(crate::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 {
|
|
||||||
match Module32FirstW(snapshot, &mut module_entry_32_w) {
|
|
||||||
Ok(()) => {}
|
|
||||||
Err(why) => {
|
|
||||||
return Err(crate::Error::CreateSnapshotError(format!(
|
|
||||||
"failed to get first module from snapshot: {why}"
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut modules = Vec::new();
|
|
||||||
loop {
|
|
||||||
let name = String::from_utf16_lossy(&module_entry_32_w.szModule)
|
|
||||||
.trim_end_matches('\0')
|
|
||||||
.to_string();
|
|
||||||
let path = String::from_utf16_lossy(&module_entry_32_w.szExePath)
|
|
||||||
.trim_end_matches('\0')
|
|
||||||
.to_string();
|
|
||||||
let module = Self {
|
|
||||||
name,
|
|
||||||
path,
|
|
||||||
base_address: module_entry_32_w.modBaseAddr as usize,
|
|
||||||
base_size: module_entry_32_w.modBaseSize as usize,
|
|
||||||
};
|
|
||||||
modules.push(module);
|
|
||||||
unsafe {
|
|
||||||
if Module32NextW(snapshot, &mut module_entry_32_w).is_err() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(modules)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod handle;
|
||||||
|
pub mod memory;
|
||||||
|
pub mod snapshot;
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
use crate::Error;
|
||||||
|
use std::ops::Deref;
|
||||||
|
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)]
|
||||||
|
pub struct Handle(pub HANDLE);
|
||||||
|
|
||||||
|
unsafe impl Send for Handle {}
|
||||||
|
unsafe impl Sync for Handle {}
|
||||||
|
|
||||||
|
impl Deref for Handle {
|
||||||
|
type Target = HANDLE;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Handle {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if let Err(why) = unsafe { CloseHandle(**self) } {
|
||||||
|
log::error!("failed to close handle: {why}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Handle {
|
||||||
|
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| {
|
||||||
|
Error::ObtainHandleError(format!(
|
||||||
|
"failed to open process with needed access: {why}",
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
if handle == INVALID_HANDLE_VALUE {
|
||||||
|
return Err(Error::ObtainHandleError(
|
||||||
|
"failed to get a valid handle".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(Self(handle))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,39 +1,29 @@
|
|||||||
use super::handle::Handle;
|
use crate::{sys::handle::Handle, Error};
|
||||||
|
|
||||||
use std::{ffi::c_void, ptr};
|
use std::{ffi::c_void, ptr};
|
||||||
|
|
||||||
use windows::Win32::System::Diagnostics::Debug::{ReadProcessMemory, WriteProcessMemory};
|
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 {
|
unsafe {
|
||||||
match ReadProcessMemory(
|
ReadProcessMemory(
|
||||||
handle.0,
|
handle.0,
|
||||||
address as *const c_void,
|
address as *const c_void,
|
||||||
(ptr::from_mut::<T>(value)).cast::<c_void>(),
|
(ptr::from_mut::<T>(value)).cast::<c_void>(),
|
||||||
size_of::<T>(),
|
size_of::<T>(),
|
||||||
None,
|
None,
|
||||||
) {
|
)
|
||||||
Ok(()) => Ok(()),
|
|
||||||
Err(why) => Err(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 {
|
unsafe {
|
||||||
match WriteProcessMemory(
|
WriteProcessMemory(
|
||||||
handle.0,
|
handle.0,
|
||||||
address as *const c_void,
|
address as *const c_void,
|
||||||
(ptr::from_mut::<T>(value)).cast::<c_void>(),
|
(ptr::from_mut::<T>(value)).cast::<c_void>(),
|
||||||
size_of::<T>(),
|
size_of::<T>(),
|
||||||
None,
|
None,
|
||||||
) {
|
)
|
||||||
Ok(()) => Ok(()),
|
|
||||||
Err(why) => Err(crate::Error::AccessMemoryError(format!(
|
|
||||||
"failed to write memory: {why}"
|
|
||||||
))),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
.map_err(|why| Error::AccessMemoryError(format!("failed to write memory: {why}")))
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
use crate::Error;
|
||||||
|
use windows::Win32::System::Diagnostics::ToolHelp::{
|
||||||
|
CreateToolhelp32Snapshot, MODULEENTRY32W, Module32FirstW, Module32NextW, PROCESSENTRY32W,
|
||||||
|
Process32FirstW, Process32NextW, TH32CS_SNAPMODULE, TH32CS_SNAPMODULE32, TH32CS_SNAPPROCESS,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ProcessSnapshot {
|
||||||
|
pub id: u32,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProcessSnapshot {
|
||||||
|
pub fn get_processes() -> Result<Vec<Self>, Error> {
|
||||||
|
let snapshot =
|
||||||
|
unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) }.map_err(|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, &raw mut process_entry_32_w) }.map_err(|why| {
|
||||||
|
Error::CreateSnapshotError(format!("failed to get first process from snapshot: {why}"))
|
||||||
|
})?;
|
||||||
|
let mut processes = Vec::new();
|
||||||
|
loop {
|
||||||
|
let name = String::from_utf16_lossy(&process_entry_32_w.szExeFile)
|
||||||
|
.trim_end_matches('\0')
|
||||||
|
.to_string();
|
||||||
|
let process = Self {
|
||||||
|
id: process_entry_32_w.th32ProcessID,
|
||||||
|
name,
|
||||||
|
};
|
||||||
|
processes.push(process);
|
||||||
|
if unsafe { Process32NextW(snapshot, &raw mut process_entry_32_w) }.is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(processes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ModuleSnapshot {
|
||||||
|
pub name: String,
|
||||||
|
pub path: String,
|
||||||
|
pub base_address: usize,
|
||||||
|
pub base_size: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ModuleSnapshot {
|
||||||
|
pub fn get_modules(pid: u32) -> Result<Vec<Self>, Error> {
|
||||||
|
let snapshot =
|
||||||
|
unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid) }
|
||||||
|
.map_err(|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, &raw mut module_entry_32_w) }.map_err(|why| {
|
||||||
|
Error::CreateSnapshotError(format!("failed to get first module from snapshot: {why}"))
|
||||||
|
})?;
|
||||||
|
let mut modules = Vec::new();
|
||||||
|
loop {
|
||||||
|
let name = String::from_utf16_lossy(&module_entry_32_w.szModule)
|
||||||
|
.trim_end_matches('\0')
|
||||||
|
.to_string();
|
||||||
|
let path = String::from_utf16_lossy(&module_entry_32_w.szExePath)
|
||||||
|
.trim_end_matches('\0')
|
||||||
|
.to_string();
|
||||||
|
let module = Self {
|
||||||
|
name,
|
||||||
|
path,
|
||||||
|
base_address: module_entry_32_w.modBaseAddr as usize,
|
||||||
|
base_size: module_entry_32_w.modBaseSize as usize,
|
||||||
|
};
|
||||||
|
modules.push(module);
|
||||||
|
if unsafe { Module32NextW(snapshot, &raw mut module_entry_32_w).is_err() } {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(modules)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user