Merge branch 'main' into linux

This commit is contained in:
Albert24GG
2024-03-01 20:32:24 +02:00
26 changed files with 238 additions and 1050 deletions

View File

@@ -1,41 +1,25 @@
use super::{generate_files, Entries, Entry};
use crate::builder::FileBuilderEnum;
use crate::util::{Address, Process};
use std::ffi::c_char;
use std::mem::offset_of;
use anyhow::Result;
use simplelog::{debug, info};
use std::ffi::c_char;
use std::mem::offset_of;
use super::{generate_files, Entries, Entry};
use crate::builder::FileBuilderEnum;
use crate::os::Process;
/// Represents a node in a linked list of interfaces.
#[derive(Debug)]
#[repr(C)]
struct InterfaceNode {
/// Used to instantiate an instance of the interface.
pub create_fn: *const (),
/// Pointer to the name of the interface.
pub name: *const c_char,
/// Pointer to the next entry in the linked list.
pub next: *mut InterfaceNode,
}
impl InterfaceNode {
/// Returns the instance of the interface.
///
/// # Arguments
///
/// * `&self` - A reference to the `InterfaceNode` struct.
/// * `process` - A reference to the `Process` struct.
///
/// # Returns
///
/// * `Result<Address>` - A `Result` containing the instance of the interface if successful, or an error if the memory read fails.
fn instance(&self, process: &Process) -> Result<Address> {
fn instance(&self, process: &Process) -> Result<usize> {
process
.read_memory::<usize>(
(self as *const _ as usize + offset_of!(InterfaceNode, create_fn)).into(),
@@ -43,18 +27,6 @@ impl InterfaceNode {
.map(|ptr| ptr.into())
}
/// Returns the name of the interface with the version number appended.
///
/// E.g. "Source2Client002".
///
/// # Arguments
///
/// * `&self` - A reference to the `InterfaceNode` struct.
/// * `process` - A reference to the `Process` struct.
///
/// # Returns
///
/// * `Result<String>` - A `Result` containing the name of the interface if successful, or an error if the memory read fails.
fn name(&self, process: &Process) -> Result<String> {
let name_ptr = process.read_memory::<usize>(
(self as *const _ as usize + offset_of!(InterfaceNode, name)).into(),
@@ -63,16 +35,6 @@ impl InterfaceNode {
process.read_string(name_ptr.into())
}
/// Returns a pointer to the next `InterfaceNode` in the linked list.
///
/// # Arguments
///
/// * `&self` - A reference to the `InterfaceNode` struct.
/// * `process` - A reference to the `Process` struct.
///
/// # Returns
///
/// * `Result<*mut InterfaceNode>` - A `Result` containing a pointer to the next `InterfaceNode` if successful, or an error if the memory read fails.
fn next(&self, process: &Process) -> Result<*mut InterfaceNode> {
process.read_memory::<*mut InterfaceNode>(
(self as *const _ as usize + offset_of!(InterfaceNode, next)).into(),
@@ -80,18 +42,6 @@ impl InterfaceNode {
}
}
/// Dumps all interfaces and writes the results to a file.
///
/// # Arguments
///
/// * `process` - A reference to the `Process` struct.
/// * `builders` - A mutable reference to a vector of `FileBuilderEnum`.
/// * `file_path` - A string slice representing the path to the file to write the results to.
/// * `indent` - The number of spaces to use for indentation in the output file.
///
/// # Returns
///
/// * `Result<()>` - A `Result` indicating the outcome of the operation.
pub fn dump_interfaces(
process: &Process,
builders: &mut Vec<FileBuilderEnum>,
@@ -100,13 +50,12 @@ pub fn dump_interfaces(
) -> Result<()> {
let mut entries = Entries::new();
// Iterate over all modules in the process, excluding crashhandler64.dll.
for module in process
.modules()?
.iter()
.filter(|m| m.name != "crashhandler64.dll")
{
if let Some(create_interface_export) = module.get_export_by_name("CreateInterface") {
if let Some(create_interface_export) = module.export_by_name("CreateInterface") {
info!("Dumping interfaces in <blue>{}</>...", module.name);
let create_interface_address;
@@ -127,7 +76,6 @@ pub fn dump_interfaces(
let mut node = process.read_memory::<*mut InterfaceNode>(create_interface_address)?;
// Iterate over each node in the linked list.
while !node.is_null() {
let instance = unsafe { (*node).instance(process) }?;
let name = unsafe { (*node).name(process) }?;
@@ -151,7 +99,6 @@ pub fn dump_interfaces(
indent: Some(indent),
});
// Get the next node in the linked list.
node = unsafe { (*node).next(process) }?;
}
}

View File

@@ -1,9 +1,3 @@
use crate::builder::{FileBuilder, FileBuilderEnum};
use anyhow::Result;
use chrono::Utc;
pub use interfaces::dump_interfaces;
pub use offsets::dump_offsets;
pub use schemas::dump_schemas;
@@ -12,51 +6,32 @@ use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;
use anyhow::Result;
use chrono::Utc;
use crate::builder::{FileBuilder, FileBuilderEnum};
pub mod interfaces;
pub mod offsets;
pub mod schemas;
/// Represents an entry in the generated file.
#[derive(Debug, PartialEq)]
pub struct Entry {
/// The name of the entry.
pub name: String,
/// The value of the entry.
pub value: usize,
/// An optional comment associated with the entry.
pub comment: Option<String>,
/// An optional indentation level for the entry.
pub indent: Option<usize>,
}
/// A container for entries, which consists of data and an optional comment.
#[derive(Default)]
pub struct EntriesContainer {
/// The data associated with the container.
pub data: Vec<Entry>,
/// An optional comment associated with the container.
pub comment: Option<String>,
}
/// A type alias for a `BTreeMap` that maps `String` keys to `EntriesContainer` values.
pub type Entries = BTreeMap<String, EntriesContainer>;
/// Generates a file using the given `builder`, `entries`, `file_path`, and `file_name`.
///
/// # Arguments
///
/// * `builder` - A mutable reference to the `FileBuilderEnum`.
/// * `entries` - A reference to the `Entries` struct.
/// * `file_path` - A string slice representing the path to the file.
/// * `file_name` - A string slice representing the name of the file.
///
/// # Returns
///
/// * `Result<()>` - A `Result` indicating the outcome of the operation.
pub fn generate_file(
builder: &mut FileBuilderEnum,
entries: &Entries,
@@ -96,18 +71,6 @@ pub fn generate_file(
Ok(())
}
/// Generate files using the given `builders`, `entries`, `file_path`, and `file_name`.
///
/// # Arguments
///
/// * `builders` - A mutable slice of `FileBuilderEnum` objects.
/// * `entries` - A reference to the `Entries` struct.
/// * `file_path` - A string slice representing the path to the file.
/// * `file_name` - A string slice representing the name of the file.
///
/// # Returns
///
/// * `Result<()>` - A `Result` indicating the outcome of the operation.
pub fn generate_files(
builders: &mut [FileBuilderEnum],
entries: &Entries,
@@ -119,16 +82,6 @@ pub fn generate_files(
.try_for_each(|builder| generate_file(builder, entries, file_path, file_name))
}
/// Writes the banner to the given file based on the file extension.
///
/// # Arguments
///
/// * `file` - A mutable reference to the file to write the banner to.
/// * `file_extension` - A string slice representing the file extension of the file.
///
/// # Returns
///
/// * `Result<()>` - A `Result` indicating the outcome of the operation.
fn write_banner_to_file(file: &mut File, file_extension: &str) -> Result<()> {
const REPO_URL: &str = "https://github.com/a2x/cs2-dumper";
@@ -137,12 +90,12 @@ fn write_banner_to_file(file: &mut File, file_extension: &str) -> Result<()> {
let banner = match file_extension {
"json" => None,
"py" => Some(format!(
"'''\nCreated using {}\n{}\n'''\n\n",
"'''\nGenerated using {}\n{}\n'''\n\n",
REPO_URL, time_now
)),
"yaml" => None,
_ => Some(format!(
"/*\n * Created using {}\n * {}\n */\n\n",
"/*\n * Generated using {}\n * {}\n */\n\n",
REPO_URL, time_now
)),
};

View File

@@ -1,28 +1,16 @@
use super::{generate_files, Entries, Entry};
use crate::builder::FileBuilderEnum;
use crate::config::Config;
use crate::config::Operation::*;
use crate::util::Process;
use std::fs::File;
use anyhow::Result;
use simplelog::{debug, error, info};
use std::fs::File;
use super::{generate_files, Entries, Entry};
use crate::builder::FileBuilderEnum;
use crate::config::Config;
use crate::config::Operation::*;
use crate::os::Process;
/// Dumps all offsets specified in the `config.json` file and writes the results to a file.
///
/// # Arguments
///
/// * `process` - A reference to the `Process` struct.
/// * `builders` - A mutable reference to a vector of `FileBuilderEnum`.
/// * `file_path` - A string slice representing the path to the file to write the results to.
/// * `indent` - The number of spaces to use for indentation in the output file.
///
/// # Returns
///
/// * `Result<()>` - A `Result` indicating the outcome of the operation.
pub fn dump_offsets(
process: &Process,
builders: &mut Vec<FileBuilderEnum>,
@@ -64,11 +52,7 @@ pub fn dump_offsets(
let size = size.unwrap_or(8);
for _ in 0..times {
process.read_memory_raw(
address,
&mut address.0 as *mut _ as *mut _,
size,
)?;
process.read_memory_raw(address, &mut address as *mut _ as *mut _, size)?;
}
}
Jmp { offset, length } => {
@@ -81,7 +65,7 @@ pub fn dump_offsets(
let mut result: usize = 0;
process.read_memory_raw(
address.add(start.try_into().unwrap()),
address + start,
&mut result as *mut _ as *mut _,
end - start,
)?;
@@ -98,17 +82,17 @@ pub fn dump_offsets(
signature.name, address
);
(signature.name, address.0)
(signature.name, address)
} else {
debug!(
"Found <bright-yellow>{}</> @ <bright-magenta>{:#X}</> (<blue>{}</> + <bright-blue>{:#X}</>)",
signature.name,
address,
signature.module,
address.sub(module.base().0)
address - module.base()
);
(signature.name, address.sub(module.base().0).0)
(signature.name, address - module.base())
};
if name == "dwBuildNumber" {
@@ -165,8 +149,6 @@ mod tests {
fn setup() -> Result<Process> {
let mut process = Process::new("cs2.exe")?;
process.initialize()?;
Ok(process)
}

View File

@@ -1,25 +1,13 @@
use super::{generate_files, Entries, Entry};
use crate::builder::FileBuilderEnum;
use crate::sdk::SchemaSystem;
use crate::util::Process;
use anyhow::Result;
use simplelog::{debug, info};
/// Dumps all schema system classes and writes the results to a file.
///
/// # Arguments
///
/// * `process` - A reference to the `Process` struct.
/// * `builders` - A mutable reference to a vector of `FileBuilderEnum`.
/// * `file_path` - A string slice representing the path to the file to write the results to.
/// * `indent` - The number of spaces to use for indentation in the output file.
///
/// # Returns
///
/// * `Result<()>` - A `Result` indicating the outcome of the operation.
use super::{generate_files, Entries, Entry};
use crate::builder::FileBuilderEnum;
use crate::os::Process;
use crate::sdk::SchemaSystem;
pub fn dump_schemas(
process: &Process,
builders: &mut Vec<FileBuilderEnum>,