This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.rs
287 lines (242 loc) · 8.5 KB
/
lib.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
#![no_std]
#![allow(non_snake_case)]
extern crate alloc;
use windows_kernel_alloc;
use windows_kernel_alloc::kernel_alloc::POOL_TAG;
use core::panic::PanicInfo;
use core::ptr;
use core::ptr::null_mut;
use windows_kernel_macros::{InitializeObjectAttributes, NT_SUCCESS, PAGED_CODE};
use windows_kernel_string::UNICODE_STRING;
use windows_kernel_sys::base::_FLT_PREOP_CALLBACK_STATUS::FLT_PREOP_SUCCESS_NO_CALLBACK;
use windows_kernel_sys::base::{
DRIVER_OBJECT, FLT_FILESYSTEM_TYPE, FLT_FILTER_UNLOAD_FLAGS, FLT_INSTANCE_QUERY_TEARDOWN_FLAGS,
FLT_INSTANCE_SETUP_FLAGS, FLT_INSTANCE_TEARDOWN_FLAGS, FLT_OPERATION_REGISTRATION,
FLT_PORT_ALL_ACCESS, FLT_PREOP_CALLBACK_STATUS, FLT_REGISTRATION, FLT_REGISTRATION_VERSION,
NTSTATUS, OBJECT_ATTRIBUTES, OBJ_CASE_INSENSITIVE, OBJ_KERNEL_HANDLE, PCFLT_RELATED_OBJECTS,
PCHAR, PFLT_CALLBACK_DATA, PFLT_FILTER, PFLT_PORT, PSECURITY_DESCRIPTOR, PULONG, PVOID,
STATUS_SUCCESS, ULONG, USHORT,
};
use windows_kernel_sys::c_int;
use windows_kernel_sys::fltmgr::{
strcpy, DbgPrint, FltBuildDefaultSecurityDescriptor, FltCloseClientPort,
FltCloseCommunicationPort, FltCreateCommunicationPort, FltFreeSecurityDescriptor,
FltRegisterFilter, FltStartFiltering, FltUnregisterFilter,
};
static mut PORT: PFLT_PORT = null_mut();
static mut CLIENT_PORT: PFLT_PORT = null_mut();
/// The minifilter handle that results from a call to FltRegisterFilter
/// NOTE: This handle must be passed to FltUnregisterFilter during minifilter unloading
static mut G_MINIFILTER_HANDLE: PFLT_FILTER = null_mut();
/// The FLT_REGISTRATION structure provides information about a file system minifilter to the filter manager.
const G_FILTER_REGISTRATION: FLT_REGISTRATION = FLT_REGISTRATION {
Size: core::mem::size_of::<FLT_REGISTRATION>() as USHORT, // Size
Version: FLT_REGISTRATION_VERSION as USHORT,
Flags: 0,
ContextRegistration: null_mut(),
OperationRegistration: G_CALLBACKS.as_ptr(),
FilterUnloadCallback: Some(InstanceFilterUnloadCallback),
InstanceSetupCallback: None, //Some(InstanceSetupCallback),
InstanceQueryTeardownCallback: None, // Some(InstanceQueryTeardownCallback),
InstanceTeardownStartCallback: None, //Some(InstanceTeardownStartCallback),
InstanceTeardownCompleteCallback: None, //Some(InstanceTeardownCompleteCallback),
GenerateFileNameCallback: None,
NormalizeNameComponentCallback: None,
NormalizeContextCleanupCallback: None,
TransactionNotificationCallback: None,
NormalizeNameComponentExCallback: None,
SectionNotificationCallback: None,
};
#[link_section = "PAGE"]
unsafe extern "C" fn InstanceTeardownStartCallback(
_flt_objects: PCFLT_RELATED_OBJECTS,
_flags: FLT_INSTANCE_TEARDOWN_FLAGS,
) {
}
#[link_section = "PAGE"]
unsafe extern "C" fn InstanceTeardownCompleteCallback(
_flt_objects: PCFLT_RELATED_OBJECTS,
_flags: FLT_INSTANCE_TEARDOWN_FLAGS,
) {
}
///
/// Constant FLT_REGISTRATION structure for our filter.
/// This initializes the callback routines our filter wants to register for.
///
const G_CALLBACKS: &[FLT_OPERATION_REGISTRATION] = {
&[
// FLT_OPERATION_REGISTRATION::new()
// .set_major_function(FLT_OPERATION_REGISTRATION::IRP_MJ_CREATE)
// .set_preop(Some(PreOperationCreate)),
FLT_OPERATION_REGISTRATION::new()
.set_major_function(FLT_OPERATION_REGISTRATION::IRP_MJ_OPERATION_END),
]
};
///
/// Pre-create callback to get file info during creation or opening
///
unsafe extern "C" fn PreOperationCreate(
Data: PFLT_CALLBACK_DATA,
_FltObjects: PCFLT_RELATED_OBJECTS,
_CompletionContext: *mut PVOID,
) -> FLT_PREOP_CALLBACK_STATUS {
let k = &(*(*(*Data).Iopb).TargetFileObject).FileName;
unsafe {
DbgPrint("%wZ\n\0".as_ptr() as _, k);
}
FLT_PREOP_SUCCESS_NO_CALLBACK
}
/// This is called before a filter is unloaded.
/// If NULL is specified for this routine, then the filter can never be unloaded.
extern "C" fn InstanceFilterUnloadCallback(_Flags: FLT_FILTER_UNLOAD_FLAGS) -> NTSTATUS {
PAGED_CODE!();
unsafe {
DbgPrint("Unloading rust minifilter\n\0".as_ptr() as _);
FltCloseCommunicationPort(PORT);
FltUnregisterFilter(G_MINIFILTER_HANDLE);
}
STATUS_SUCCESS
}
///
/// This is called to see if a filter would like to attach an instance to the given volume.
///
#[link_section = "PAGE"]
unsafe extern "C" fn InstanceSetupCallback(
_flt_objects: PCFLT_RELATED_OBJECTS,
_flags: FLT_INSTANCE_SETUP_FLAGS,
_volume_device_type: ULONG,
_volume_filesystem_type: FLT_FILESYSTEM_TYPE,
) -> NTSTATUS {
PAGED_CODE!();
STATUS_SUCCESS
}
///
/// This is called to see if the filter wants to detach from the given volume.
///
#[link_section = "PAGE"]
extern "C" fn InstanceQueryTeardownCallback(
_flt_objects: PCFLT_RELATED_OBJECTS,
_flags: FLT_INSTANCE_QUERY_TEARDOWN_FLAGS,
) -> NTSTATUS {
PAGED_CODE!();
unsafe {
FltUnregisterFilter(G_MINIFILTER_HANDLE);
}
STATUS_SUCCESS
}
#[link_section = "INIT"]
#[no_mangle]
pub extern "system" fn DriverEntry(
driver: &mut DRIVER_OBJECT,
_registry_path: *const UNICODE_STRING,
) -> NTSTATUS {
let mut sd: PSECURITY_DESCRIPTOR = null_mut();
let mut oa: OBJECT_ATTRIBUTES = unsafe { core::mem::zeroed() };
let mut name = "\\mf";
unsafe {
DbgPrint("Hello from Rust kernel space!\n\0".as_ptr() as _);
}
// driver.DriverUnload = Some(driver_exit);
//
// register minifilter driver
//
let mut status: NTSTATUS =
unsafe { FltRegisterFilter(driver, &G_FILTER_REGISTRATION, &mut G_MINIFILTER_HANDLE) };
if !NT_SUCCESS!(status) {
return status;
}
unsafe {
DbgPrint("Filter Registration Successful!\n\0".as_ptr() as _);
}
status = unsafe { FltBuildDefaultSecurityDescriptor(&mut sd, FLT_PORT_ALL_ACCESS) };
let name = UNICODE_STRING::create(name);
if NT_SUCCESS!(status) {
unsafe {
InitializeObjectAttributes(
&mut oa,
&mut name.as_base_unicode(),
OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE,
null_mut(),
sd,
);
}
unsafe {
DbgPrint("Filter Security Descriptor successful!\n\0".as_ptr() as _);
}
status = unsafe {
FltCreateCommunicationPort(
G_MINIFILTER_HANDLE,
&mut PORT,
&mut oa,
null_mut(),
Some(MiniConnect),
Some(MiniDisconnect),
Some(MiniSendRec),
1,
)
};
unsafe {
FltFreeSecurityDescriptor(sd);
}
if NT_SUCCESS!(status) {
unsafe {
DbgPrint("Filter communication Port Successful!\n\0".as_ptr() as _);
}
// driver.DriverUnload = Some(driver_exit);
// start minifilter driver
status = unsafe { FltStartFiltering(G_MINIFILTER_HANDLE) };
if !NT_SUCCESS!(status) {
unsafe {
DbgPrint("Filter filtering Successful!\0\n".as_ptr() as _);
}
unsafe {
FltUnregisterFilter(G_MINIFILTER_HANDLE);
}
}
} else {
unsafe {
FltCloseCommunicationPort(PORT);
}
}
}
status
}
unsafe extern "C" fn MiniConnect(
ClientPort: PFLT_PORT,
ServerPortCookie: PVOID,
ConnectionContext: PVOID,
SizeOfContext: ULONG,
ConnectionPortCookie: *mut PVOID,
) -> NTSTATUS {
CLIENT_PORT = ClientPort;
DbgPrint("Rust connect from application!\n\0".as_ptr() as _);
STATUS_SUCCESS
}
unsafe extern "C" fn MiniDisconnect(ConnectionCookie: PVOID) {
DbgPrint("Rust disconnect form application!\n\0".as_ptr() as _);
FltCloseClientPort(G_MINIFILTER_HANDLE, &mut CLIENT_PORT);
}
unsafe extern "C" fn MiniSendRec(
PortCookie: PVOID,
InputBuffer: PVOID,
InputBufferLength: ULONG,
OutputBuffer: PVOID,
OutputBufferLength: ULONG,
ReturnOutputBufferLength: PULONG,
) -> NTSTATUS {
let mut msg: PCHAR = "Hello Rust from kernel\n\0".as_bytes().as_ptr() as *mut i8;
DbgPrint(
"Rust message from application: %s".as_ptr() as _,
InputBuffer as *mut i8,
);
strcpy(OutputBuffer as PCHAR, msg);
STATUS_SUCCESS
}
/*
unsafe extern "C" fn driver_exit(_driver: *mut DRIVER_OBJECT) {
FltUnregisterFilter(G_MINIFILTER_HANDLE);
unsafe {
DbgPrint("\nBye bye from Rust!\0".as_ptr() as _);
}
}
*/