|
| 1 | +/* |
| 2 | + DLL Unhooking Method 2 |
| 3 | + @5mukx |
| 4 | + Resource covered from ired.team |
| 5 | + */ |
| 6 | + |
| 7 | +use std::io::{self, ErrorKind, Result}; |
| 8 | +use std::ptr::null_mut; |
| 9 | + |
| 10 | +use winapi::um::fileapi::{CreateFileW, OPEN_EXISTING}; |
| 11 | +use winapi::um::handleapi::CloseHandle; |
| 12 | +use winapi::um::libloaderapi::GetModuleHandleA; |
| 13 | +use winapi::um::memoryapi::{CreateFileMappingW, MapViewOfFile, UnmapViewOfFile, FILE_MAP_READ}; |
| 14 | +use winapi::um::processthreadsapi::GetCurrentProcess; |
| 15 | +use winapi::um::psapi::{GetModuleInformation, MODULEINFO}; |
| 16 | +use winapi::um::winnt::{FILE_SHARE_READ, GENERIC_READ, PAGE_READONLY, SEC_IMAGE}; |
| 17 | + |
| 18 | +fn main() -> Result<()> { |
| 19 | + unsafe { |
| 20 | + let process = GetCurrentProcess(); |
| 21 | + let ntdll_module = GetModuleHandleA("ntdll.dll\0".as_ptr() as *const i8); |
| 22 | + |
| 23 | + if ntdll_module.is_null() { |
| 24 | + eprintln!("[-] Failed to get ntdll.dll handle"); |
| 25 | + return Err(io::Error::new(ErrorKind::Other, "Failed to get ntdll handle")); |
| 26 | + } |
| 27 | + |
| 28 | + println!("[+] Process Info: {:?}", process); |
| 29 | + |
| 30 | + let mut mi = MODULEINFO { |
| 31 | + lpBaseOfDll: null_mut(), |
| 32 | + SizeOfImage: 0, |
| 33 | + EntryPoint: null_mut(), |
| 34 | + }; |
| 35 | + |
| 36 | + let success = GetModuleInformation( |
| 37 | + process, |
| 38 | + ntdll_module, |
| 39 | + &mut mi, |
| 40 | + std::mem::size_of::<MODULEINFO>() as u32, |
| 41 | + ); |
| 42 | + |
| 43 | + if success == 0 { |
| 44 | + return Err(io::Error::new(ErrorKind::Other, "Failed to get module information")); |
| 45 | + } |
| 46 | + |
| 47 | + let ntdll_path = "C:\\windows\\system32\\ntdll.dll"; |
| 48 | + let ntdll_path_utf16: Vec<u16> = ntdll_path.encode_utf16().chain(Some(0)).collect(); |
| 49 | + let ntdll_file = CreateFileW( |
| 50 | + ntdll_path_utf16.as_ptr(), |
| 51 | + GENERIC_READ, |
| 52 | + FILE_SHARE_READ, |
| 53 | + null_mut(), |
| 54 | + OPEN_EXISTING, |
| 55 | + 0, |
| 56 | + null_mut(), |
| 57 | + ); |
| 58 | + |
| 59 | + if ntdll_file.is_null() { |
| 60 | + return Err(io::Error::last_os_error()); |
| 61 | + } |
| 62 | + |
| 63 | + println!("ntdll_file Handle: {:?}", ntdll_file); |
| 64 | + |
| 65 | + let ntdll_mapping = CreateFileMappingW( |
| 66 | + ntdll_file, |
| 67 | + null_mut(), |
| 68 | + PAGE_READONLY | SEC_IMAGE, |
| 69 | + 0, |
| 70 | + 0, |
| 71 | + null_mut(), |
| 72 | + ); |
| 73 | + |
| 74 | + if ntdll_mapping.is_null() { |
| 75 | + CloseHandle(ntdll_file); |
| 76 | + return Err(io::Error::last_os_error()); |
| 77 | + } |
| 78 | + |
| 79 | + println!("CreateFileMappingW: {:?}", ntdll_mapping); |
| 80 | + |
| 81 | + let ntdll_mapping_addr = MapViewOfFile( |
| 82 | + ntdll_mapping, |
| 83 | + FILE_MAP_READ, |
| 84 | + 0, |
| 85 | + 0, |
| 86 | + 0, |
| 87 | + ); |
| 88 | + |
| 89 | + if ntdll_mapping_addr.is_null() { |
| 90 | + CloseHandle(ntdll_mapping); |
| 91 | + CloseHandle(ntdll_file); |
| 92 | + return Err(io::Error::last_os_error()); |
| 93 | + } |
| 94 | + |
| 95 | + println!("ntdll mapping addr: {:?}", ntdll_mapping_addr); |
| 96 | + |
| 97 | + UnmapViewOfFile(ntdll_mapping_addr); |
| 98 | + CloseHandle(ntdll_mapping); |
| 99 | + CloseHandle(ntdll_file); |
| 100 | + } |
| 101 | + |
| 102 | + Ok(()) |
| 103 | +} |
| 104 | + |
0 commit comments