Skip to main content

Crate procodile

Crate procodile 

Source
Expand description

§procodile

Cross-platform (Linux and Windows) primitives for reading and modifying the memory of other processes: everything a tool like Cheat Engine is built from.

  • Processes: list, find by name, open, suspend/resume, query pointer width.
  • Memory: safe typed reads and writes, regions with protection and kind, protect, allocate and free (Windows).
  • Modules: loaded images with base and size.
  • Scanning: first/next value scans (exact, ranges, changed, increased, unknown initial value, …), array-of-bytes patterns, and pointer scans with saveable pointer maps.
  • Structs: Rust-syntax struct definitions, read from memory as a tree of named fields.
  • Freezing: hold values in place from a background thread.

For an interactive tool built on this crate, see procodile-cli.

cargo add procodile

§Permissions

  • Linux: opening another process’s memory (/proc/<pid>/mem) needs ptrace permission. Either run as root, grant CAP_SYS_PTRACE, or set /proc/sys/kernel/yama/ptrace_scope to 0. The default (1) only allows your own child processes, unless the target opts in with prctl(PR_SET_PTRACER, ...).
  • Windows: protected processes need an elevated (Administrator) prompt.

Error::is_permission_denied tells these failures apart from others.

§Processes and memory

use procodile::{Access, Address, Process};

let game = Process::by_name("game.exe")?.next().expect("not running");
let handle = game.open(Access::ReadWrite)?;

let health_at = Address::new(0x7ff6_1234_5678);
let health: i32 = handle.read(health_at)?;
handle.write(health_at, &(health + 100))?;

// Raw bytes; `read_bytes` reports short reads instead of failing.
let mut buf = [0u8; 64];
let n = handle.read_bytes(health_at, &mut buf)?;

ProcessHandle::read works for any Pod type: integers, floats, arrays of them, and your own #[repr(C)] structs if you implement the (unsafe) trait for them. Handles are Send + Sync, so one can be shared between threads behind an Arc.

§Regions and modules

for region in handle.regions()? {
    if region.protection.readable() && region.protection.writable() {
        println!("{:?}..{:?} {} {:?}", region.base, region.end(), region.protection, region.kind);
    }
}

let exe = handle.module("game.exe")?.expect("module");
println!("game.exe is at {:?} ({:#x} bytes)", exe.base, exe.size);

§Value scanning

A first scan finds every matching value; next scans narrow the results by comparing against what each address held last time.

use procodile::scan::{Compare, Scan, ScanOptions, Value, ValueType};

let options = ScanOptions::new(ValueType::I32);
let mut scan = Scan::first(&handle, options, Compare::Exact(Value::I32(100)), None)?;

// ...the value changes in the game...
scan.next(&handle, Compare::Decreased, None)?;

for m in scan.results().take(10) {
    println!("{:?} was {}", m.address, m.value);
}

Pass a scan::Progress to watch progress or cancel from another thread. Use Compare::Unknown for an unknown initial value: the first scan then keeps a snapshot of memory, and relative next scans (Changed, Increased, …) narrow it down.

§Byte patterns

use procodile::scan::{Pattern, PatternOptions, find_pattern};

let pattern: Pattern = "48 8b 05 ?? ?? ?? ?? 48 85 c0".parse().unwrap();
let options = PatternOptions {
    executable_only: true,
    ..Default::default()
};
let hits = find_pattern(&handle, &pattern, &options, None)?;

§Pointer scanning

Heap addresses change every run. A pointer scan finds paths from module statics to a value, such as [[game.exe+0x5e068]+0x18]+0x0, which keep working after a restart.

use procodile::Address;
use procodile::scan::{PointerMap, PointerScanOptions};

let map = PointerMap::build(&handle, &Default::default(), None)?;
let paths = map.scan(value_address, &PointerScanOptions::default());

// Save the map, and after a restart, keep only paths that still lead to the value.
map.write_to(std::fs::File::create("run1.pmap")?)?;
let still_valid = procodile::scan::filter_pointers(&handle, &paths, new_address)?;

A scan::PointerMap can be scanned, resolve paths and filter them offline, so maps saved from several runs can be intersected later. That is the most reliable way to find paths that survive restarts.

§Structs

Describe data with Rust struct syntax and read it as named fields. Layout follows #[repr(C)] rules for the target’s pointer size; #[offset(N)] pins a field.

use procodile::Address;
use procodile::structs::{Node, Structs};

let mut structs = Structs::new();
structs
    .define(
        "struct Stats { health: i32, armor: i32 }
         struct Player {
             name: *const c_char,
             stats: *const Stats,
             #[offset(0x40)]
             pos: [f32; 3],
         }",
    )
    .unwrap();

let width = handle.pointer_width()?;
let player = structs.read(&handle, "Player", Address::new(0x1000), width, 1)?;
if let Node::Struct { fields, .. } = player {
    for field in fields {
        println!("{} @ {:#x}: {:?}", field.name, field.offset, field.value);
    }
}

structs::dissect guesses the fields of memory you have no definition for yet.

§Freezing

use std::sync::Arc;
use procodile::Address;
use procodile::freeze::Freezer;

let freezer = Freezer::new(Arc::new(handle));
let id = freezer.freeze(Address::new(0x1000), 999i32.to_ne_bytes().to_vec());
// ...
freezer.unfreeze(id);

§Injection

Load a shared library into another process. Via the OS loader the module shows up in the target’s module list; manual mapping (the optional inject feature) keeps it out and also covers static/musl targets, stripped binaries and ptrace-blocked environments. x86-64 only.

use procodile::{ProcessHandle, LoadMethod};

let handle = ProcessHandle::open_current()?;
// Via the OS loader: dlopen through ptrace on Linux, LoadLibraryW on Windows.
let _module = handle.load_library("payload.so", LoadMethod::Loader)?;

Run an exported entry point in the target with execute_remote, or without waiting for it with execute_remote_detached. With the inject feature you can also map an image straight from memory with map_image, inject a file with inject_file, pass LoadMethod::ManualMap so the library never joins the OS module list, and enumerate manually-mapped modules with mapped_images.

§Platform support

LinuxWindows
Processes, memory, regions, modules, scansyesyes
Suspend / resumeSIGSTOP / SIGCONTNtSuspendProcess
Protect, allocate, freeno (Error::Unsupported)yes

Like the standard library, the crate is split into platform-independent modules over a private sys backend per OS. Things that only exist on one platform are extension traits in os, such as raw file descriptor and handle access, or the Windows working set.

§License

MIT OR Apache-2.0.

Re-exports§

pub use address::Address;
pub use error::Error;
pub use error::Result;
pub use inject::LoadMethod;
pub use memory::Pod;
pub use memory::Protection;
pub use memory::Region;
pub use memory::RegionKind;
pub use module::Module;
pub use process::Access;
pub use process::Process;
pub use process::ProcessHandle;

Modules§

address
error
freeze
Holding values in place by rewriting them on an interval.
inject
Library injection: loading a shared library (.dll / .so) into another process.
memory
module
os
Platform-specific extensions, in the style of std::os.
process
scan
Cheat Engine-style value scanning: a first scan over all matching memory, then any number of next scans that narrow the candidates by comparing against their previous values.
structs
Structure definitions: describing how data is laid out in another process, so it can be read as named fields instead of raw bytes.