Use separate structs for windows and linux

This commit is contained in:
Albert24GG 2024-03-17 00:09:13 +02:00
parent c56c319f7f
commit e50d781c0d
2 changed files with 29 additions and 13 deletions

View File

@ -10,6 +10,7 @@ use goblin::elf::{sym, Elf, SectionHeader};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
use std::path::PathBuf; use std::path::PathBuf;
/// Represents the data associated with a specific module on Linux.
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[derive(Debug)] #[derive(Debug)]
pub struct ModuleEntry { pub struct ModuleEntry {
@ -17,22 +18,30 @@ pub struct ModuleEntry {
pub start_addr: usize, pub start_addr: usize,
pub data: Vec<u8>, pub data: Vec<u8>,
} }
/// Represents a module loaded in a Windows process. /// Represents a module loaded in a Windows process.
#[cfg(target_os = "windows")]
pub struct Module<'a> { pub struct Module<'a> {
/// The name of the module. /// The name of the module.
pub name: &'a str, pub name: &'a str,
/// A reference to a slice of bytes containing the module data. /// A reference to a slice of bytes containing the module data.
#[cfg(target_os = "windows")]
pub data: &'a [u8], pub data: &'a [u8],
#[cfg(target_os = "linux")]
pub module_info: &'a ModuleEntry,
#[cfg(target_os = "windows")]
/// The PE file format representation of the module. /// The PE file format representation of the module.
pub pe: PE<'a>, pub pe: PE<'a>,
}
#[cfg(target_os = "linux")] /// Represents a module loaded in a Linux process.
#[cfg(target_os = "linux")]
pub struct Module<'a> {
/// The name of the module.
pub name: &'a str,
/// A reference to a slice of bytes containing the module info.
pub module_info: &'a ModuleEntry,
/// The Elf file format representation of the module.
pub elf: Elf<'a>, pub elf: Elf<'a>,
} }

View File

@ -30,20 +30,27 @@ use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
/// Represents a Windows process. /// Represents a Windows process.
#[cfg(target_os = "windows")]
#[derive(Debug)] #[derive(Debug)]
pub struct Process { pub struct Process {
/// ID of the process. /// ID of the process.
id: u32, id: u32,
#[cfg(target_os = "windows")]
/// Handle to the process. /// Handle to the process.
handle: HANDLE, handle: HANDLE,
/// A HashMap containing the name of each module and its corresponding raw data. /// A HashMap containing the name of each module and its corresponding raw data.
#[cfg(target_os = "windows")]
modules: HashMap<String, Vec<u8>>, modules: HashMap<String, Vec<u8>>,
}
#[cfg(target_os = "linux")] /// Represents a Linux process.
#[cfg(target_os = "linux")]
#[derive(Debug)]
pub struct Process {
/// PID of the process.
pid: u32,
/// A HashMap containing the name of each module and its corresponding data.
modules: HashMap<String, ModuleEntry>, modules: HashMap<String, ModuleEntry>,
} }
@ -65,9 +72,9 @@ impl Process {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn new(name: &str) -> Result<Self> { pub fn new(name: &str) -> Result<Self> {
let id = Self::get_process_id_by_name(name)?; let pid = Self::get_process_pid_by_name(name)?;
let mut process = Self { let mut process = Self {
id, pid,
modules: HashMap::new(), modules: HashMap::new(),
}; };
process.parse_loaded_modules()?; process.parse_loaded_modules()?;
@ -160,7 +167,7 @@ impl Process {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn read_memory_raw(&self, address: usize, buffer: *mut c_void, size: usize) -> Result<()> { pub fn read_memory_raw(&self, address: usize, buffer: *mut c_void, size: usize) -> Result<()> {
let proc_mem_path = format!("/proc/{}/mem", self.id); let proc_mem_path = format!("/proc/{}/mem", self.pid);
let mut mem_file = File::open(proc_mem_path)?; let mut mem_file = File::open(proc_mem_path)?;
// Go to the start address // Go to the start address
@ -257,7 +264,7 @@ impl Process {
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn get_process_id_by_name(process_name: &str) -> Result<u32> { fn get_process_pid_by_name(process_name: &str) -> Result<u32> {
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
for process_iter in all_processes()? { for process_iter in all_processes()? {
@ -308,7 +315,7 @@ impl Process {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn parse_loaded_modules(&mut self) -> Result<()> { fn parse_loaded_modules(&mut self) -> Result<()> {
let process = process::Process::new(self.id as i32)?; let process = process::Process::new(self.pid as i32)?;
let mut modules_info: HashMap<String, ((u64, u64), PathBuf)> = HashMap::new(); let mut modules_info: HashMap<String, ((u64, u64), PathBuf)> = HashMap::new();