Sun Sep 10 2023
Rust DMA Basics: Reading and Writing Memory (with PCILeech)
Rust DMA Basics: Reading and Writing Memory (with PCILeech)
Crates you'll need:
- memprocfs (https://github.com/ufrisk/MemProcFS)
PCILeech is a library that can read and write memory over DMA, and you can drive it through the memprocfs crate.
The memprocfs wiki is here: API_Rust · ufrisk/MemProcFS Wiki · GitHub
Prerequisites
First create a new project and pull down the crate:
cargo new dma-test;
cd dma-test;
cargo add memprocfs;
Code
Connect to PCILeech's vmm.dll:
let mut args = ["-printf", "-device", "fpga"].to_vec();
let vmm = Vmm::new("path to vmm.dll", &args).unwrap();
This uses the fpga device, but you can switch it to USB3380 if that's what you have. For the other arguments, check the memprocfs wiki.
Example: finding a process and its base address
if let Ok(processes) = vmm.process_list() {
for process in processes {
if let Ok(info) = process.info() {
if info.name == "example.exe" {
return Some(process);
}
}
}
}
Getting the base address:
process.get_module_base("example.exe").unwrap();
Example: reading and writing memory
To read and write memory, use the vmm::mem_read and vmm::mem_write methods respectively.
On top of that, memprocfs also provides two generic methods, vmm::mem_read_as and mem_write_as, which let you pass a struct straight in to read and write.
