(size: usize)
| 1 | use crate::utils::{load_library, get_proc_address}; |
| 2 | pub unsafe fn alloc_mem(size: usize) -> Result<*mut u8, String> { |
| 3 | use obfstr::{obfstr, obfbytes}; |
| 4 | use core::ffi::c_void; |
| 5 | |
| 6 | type VirtualProtectFn = unsafe extern "system" fn(lp_address: *mut c_void, dw_size: usize, fl_new_protect: u32, lpfl_old_protect: *mut u32) -> i32; |
| 7 | |
| 8 | let kernel32 = load_library(obfbytes!(b"kernel32.dll\0").as_slice())?; |
| 9 | let virtual_protect: VirtualProtectFn = core::mem::transmute(get_proc_address(kernel32, obfbytes!(b"VirtualProtect\0").as_slice())?); |
| 10 | |
| 11 | let mmap = match memmap2::MmapMut::map_anon(size) { |
| 12 | Ok(m) => m, |
| 13 | Err(_) => return Err(obfstr!("mmap anonymous failed").to_string()), |
| 14 | }; |
| 15 | let boxed = Box::new(mmap); |
| 16 | let leaked: &'static mut memmap2::MmapMut = Box::leak(boxed); |
| 17 | let p = leaked.as_mut_ptr(); |
| 18 | let mut old_protect = 0u32; |
| 19 | let ok = virtual_protect(p as *mut _, size, 0x40, &mut old_protect); // PAGE_EXECUTE_READWRITE |
| 20 | if ok == 0 { |
| 21 | return Err(obfstr!("VirtualProtect failed").to_string()); |
| 22 | } |
| 23 | Ok(p as *mut u8) |
| 24 | } |
nothing calls this directly
no test coverage detected