Move platform specific configs in config.rs

This commit is contained in:
Albert24GG 2024-03-05 13:40:07 +02:00
parent 10a908ab6f
commit 96da055d1a
5 changed files with 72 additions and 57 deletions

View File

@ -39,3 +39,36 @@ pub struct Signature {
pub pattern: String, pub pattern: String,
pub operations: Vec<Operation>, pub operations: Vec<Operation>,
} }
#[derive(Debug)]
pub struct SchemaSystemConfig {
pub module_name: &'static str,
pub pattern: &'static str,
pub type_scope_size_offset: usize,
pub type_scope_data_offset: usize,
pub declared_classes_offset: usize,
}
#[cfg(target_os = "windows")]
pub const SCHEMA_CONF: SchemaSystemConfig = SchemaSystemConfig {
module_name: "schemasystem.dll",
pattern: "48 8D 0D ? ? ? ? E9 ? ? ? ? CC CC CC CC 48 8D 0D ? ? ? ? E9 ? ? ? ? CC CC CC CC 48 83 EC 28",
type_scope_size_offset: 0x190,
type_scope_data_offset: 0x198,
declared_classes_offset: 0x5B8,
};
#[cfg(target_os = "linux")]
pub const SCHEMA_CONF: SchemaSystemConfig = SchemaSystemConfig {
module_name: "libschemasystem.so",
pattern: "48 8D 05 ? ? ? ? c3 ? ? ? 00 00 00 00 00 48 8d 05 ? ? ? ? c3 ? ? ? 00 00 00 00 00 48 ? ? ? c3",
type_scope_size_offset: 0x1f8,
type_scope_data_offset: 0x200,
declared_classes_offset: 0x620,
};
#[cfg(target_os = "windows")]
pub const PROC_NAME: &str = "cs2.exe";
#[cfg(target_os = "linux")]
pub const PROC_NAME: &str = "cs2";

View File

@ -93,7 +93,7 @@ fn main() -> Result<()> {
// Create the output directory if it doesn't exist. // Create the output directory if it doesn't exist.
fs::create_dir_all(&output)?; fs::create_dir_all(&output)?;
let mut process = Process::new("cs2.exe")?; let mut process = Process::new(config::PROC_NAME)?;
let now = Instant::now(); let now = Instant::now();

View File

@ -56,8 +56,8 @@ impl Process {
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn new(process_name: &str) -> Result<Self> { pub fn new(name: &str) -> Result<Self> {
let id = Self::get_process_id_by_name(process_name.strip_suffix(".exe").unwrap())?; let id = Self::get_process_id_by_name(name)?;
let mut process = Self { let mut process = Self {
id, id,
modules: HashMap::new(), modules: HashMap::new(),
@ -298,29 +298,6 @@ impl Process {
Ok(()) Ok(())
} }
#[cfg(target_os = "linux")]
fn read_elf_file(path: &PathBuf) -> Result<Vec<u8>> {
let mut file = File::open(path)?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
Ok(data)
}
fn get_transformed_module_name(path: PathBuf) -> Option<String> {
if let Ok(module_path) = path.into_os_string().into_string() {
if let Some(module_name) = module_path.split('/').last() {
if module_name.starts_with("lib") && module_name.ends_with(".so") {
return Some(format!(
"{}.dll",
module_name.strip_prefix("lib")?.strip_suffix(".so")?
));
}
}
}
None
}
#[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.id as i32)?;
@ -328,35 +305,37 @@ impl Process {
let mut modules_info: HashMap<String, ((u64, u64), PathBuf)> = HashMap::new(); let mut modules_info: HashMap<String, ((u64, u64), PathBuf)> = HashMap::new();
for mmap in process.maps()? { for mmap in process.maps()? {
let mmap_path = match mmap.pathname { let module_path = match mmap.pathname {
process::MMapPath::Path(path) => path, process::MMapPath::Path(path) => path,
_ => continue, _ => continue,
}; };
let module_name = match Process::get_transformed_module_name(mmap_path.clone()) { let get_module_name = |path: &PathBuf| -> Option<String> {
Some(new_path) => new_path, path.file_name()
None => continue, .and_then(|name| name.to_str())
.filter(|name| name.starts_with("lib") && name.ends_with(".so"))
.map(|name| name.to_string())
}; };
if module_name != "client.dll" if let Some(module_name) = get_module_name(&module_path) {
&& module_name != "engine2.dll" let module_entry = modules_info
&& module_name != "inputsystem.dll" .entry(module_name)
&& module_name != "matchmaking.dll" .or_insert_with(|| (mmap.address, module_path));
&& module_name != "schemasystem.dll" module_entry.0 = (
{ std::cmp::min(mmap.address.0, module_entry.0 .0),
continue; std::cmp::max(mmap.address.1, module_entry.0 .1),
);
} }
let module_entry = modules_info
.entry(module_name)
.or_insert_with(|| (mmap.address, mmap_path));
module_entry.0 = (
std::cmp::min(mmap.address.0, module_entry.0 .0),
std::cmp::max(mmap.address.1, module_entry.0 .1),
);
} }
for (module_name, (address_space, path)) in modules_info.into_iter() { for (module_name, (address_space, path)) in modules_info.into_iter() {
let (start, end) = address_space; let (start, end) = address_space;
// let mut data = vec![0; (end - start + 1) as usize]; let read_elf_file = |path: &PathBuf| -> Result<Vec<u8>> {
if let Ok(data) = Process::read_elf_file(&path) { let mut file = File::open(path)?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
Ok(data)
};
if let Ok(data) = read_elf_file(&path) {
self.modules.insert( self.modules.insert(
module_name, module_name,
ModuleEntry { ModuleEntry {

View File

@ -6,6 +6,8 @@ use super::SchemaSystemTypeScope;
use crate::os::Process; use crate::os::Process;
use crate::config::SCHEMA_CONF;
pub struct SchemaSystem<'a> { pub struct SchemaSystem<'a> {
process: &'a Process, process: &'a Process,
address: usize, address: usize,
@ -14,11 +16,7 @@ pub struct SchemaSystem<'a> {
impl<'a> SchemaSystem<'a> { impl<'a> SchemaSystem<'a> {
pub fn new(process: &'a Process) -> Result<Self> { pub fn new(process: &'a Process) -> Result<Self> {
let mut address = process let mut address = process
.find_pattern( .find_pattern(SCHEMA_CONF.module_name, SCHEMA_CONF.pattern)
"schemasystem.dll",
"48 8D 05 ? ? ? ? c3 ? ? ? 00 00 00 00 00 48 8d 05 ? ? ? ? c3 ? ? ? 00 00 00 00 00 48 ? ? ? c3"
// "48 8D 0D ? ? ? ? E9 ? ? ? ? CC CC CC CC 48 8D 0D ? ? ? ? E9 ? ? ? ? CC CC CC CC 48 83 EC 28"
)
.expect("unable to find schema system pattern"); .expect("unable to find schema system pattern");
address = process.resolve_rip(address, None, None)?; address = process.resolve_rip(address, None, None)?;
@ -27,15 +25,17 @@ impl<'a> SchemaSystem<'a> {
} }
pub fn type_scopes(&self) -> Result<Vec<SchemaSystemTypeScope>> { pub fn type_scopes(&self) -> Result<Vec<SchemaSystemTypeScope>> {
// let size = self.process.read_memory::<u32>(self.address + 0x190)?; let size = self
let size = self.process.read_memory::<u32>(self.address + 0x1f8)?; .process
.read_memory::<u32>(self.address + SCHEMA_CONF.type_scope_size_offset)?;
if size == 0 { if size == 0 {
bail!("no type scopes found"); bail!("no type scopes found");
} }
// let data = self.process.read_memory::<usize>(self.address + 0x198)?; let data = self
let data = self.process.read_memory::<usize>(self.address + 0x200)?; .process
.read_memory::<usize>(self.address + SCHEMA_CONF.type_scope_data_offset)?;
let mut addresses = vec![0; size as usize]; let mut addresses = vec![0; size as usize];

View File

@ -4,6 +4,8 @@ use super::{SchemaClassInfo, SchemaTypeDeclaredClass, UtlTsHash};
use crate::os::Process; use crate::os::Process;
use crate::config::SCHEMA_CONF;
pub struct SchemaSystemTypeScope<'a> { pub struct SchemaSystemTypeScope<'a> {
process: &'a Process, process: &'a Process,
address: usize, address: usize,
@ -17,8 +19,9 @@ impl<'a> SchemaSystemTypeScope<'a> {
pub fn classes(&self) -> Result<Vec<SchemaClassInfo>> { pub fn classes(&self) -> Result<Vec<SchemaClassInfo>> {
let declared_classes = self let declared_classes = self
.process .process
.read_memory::<UtlTsHash<*mut SchemaTypeDeclaredClass>>(self.address + 0x620)?; .read_memory::<UtlTsHash<*mut SchemaTypeDeclaredClass>>(
// .read_memory::<UtlTsHash<*mut SchemaTypeDeclaredClass>>(self.address + 0x5B8)?; self.address + SCHEMA_CONF.declared_classes_offset,
)?;
let classes: Vec<SchemaClassInfo> = declared_classes let classes: Vec<SchemaClassInfo> = declared_classes
.elements(self.process)? .elements(self.process)?