Skip to content

Commit

Permalink
fixed issue with write memory functions on rust
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbo committed Jun 16, 2024
1 parent ad993b8 commit 3a53387
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 4 additions & 4 deletions bindings/rust/libmem/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub fn read_memory_ex<T>(process: &Process, source: Address) -> Option<T> {
/// let value_to_write: u32 = 1337;
/// write_memory(0xdeadbeef, &value_to_write);
/// ```
pub unsafe fn write_memory<T>(dest: Address, value: &T) {
let size = mem::size_of::<T>();
pub unsafe fn write_memory<T: ?Sized>(dest: Address, value: &T) {
let size = mem::size_of_val(value);
unsafe {
// This function can't actually fail, no need for extra checking.
// If it fails, the program will crash anyways.
Expand All @@ -62,9 +62,9 @@ pub unsafe fn write_memory<T>(dest: Address, value: &T) {
/// let value_to_write: u32 = 1337;
/// write_memory_ex(&process, 0xdeadbeef, &value_to_write);
/// ```
pub fn write_memory_ex<T>(process: &Process, dest: Address, value: &T) -> Option<()> {
pub fn write_memory_ex<T: ?Sized>(process: &Process, dest: Address, value: &T) -> Option<()> {
let raw_process: lm_process_t = process.to_owned().into();
let size = mem::size_of::<T>();
let size = mem::size_of_val(value);
let result = unsafe {
libmem_sys::LM_WriteMemoryEx(
&raw_process as *const lm_process_t,
Expand Down
11 changes: 10 additions & 1 deletion bindings/rust/tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ fn main() {
println!("[*] Read memory from number: {}", read_number);
unsafe { write_memory::<i32>(number_addr, &1337) };
println!("[*] Wrote new memory on number: {}", number);
let buf: Vec<u8> = vec![0x64, 0, 0, 0];
unsafe { write_memory(number_addr, buf.as_slice()) };
println!("[*] Wrote buffer to number: {}", number);
unsafe { set_memory(number_addr, 0, std::mem::size_of_val(&number)) };
println!("[*] Zeroed number memory: {}", number);

Expand Down Expand Up @@ -226,7 +229,13 @@ fn main() {
write_memory_ex(&target_process, target_alloc, &1337).unwrap();
println!("[*] Wrote number to the target process memory");

set_memory_ex(&target_process, target_alloc + 4, 0xFF, 4).unwrap();
write_memory_ex(
&target_process,
target_alloc + 4,
&[0x39u8, 0x05, 0xFF, 0xFF],
);

set_memory_ex(&target_process, target_alloc + 6, 0x0, 2).unwrap();
println!("[*] Set bytes on the target process memory");

let (written_number, set_number) =
Expand Down

0 comments on commit 3a53387

Please sign in to comment.