2
2
#![ allow( non_snake_case) ]
3
3
4
4
extern crate alloc;
5
+
5
6
use windows_kernel_alloc;
6
7
use windows_kernel_alloc:: kernel_alloc:: POOL_TAG ;
7
8
8
- pub mod shared_def;
9
-
10
9
use core:: panic:: PanicInfo ;
10
+ use core:: ptr;
11
11
use core:: ptr:: null_mut;
12
12
use windows_kernel_macros:: { InitializeObjectAttributes , NT_SUCCESS , PAGED_CODE } ;
13
13
use windows_kernel_string:: UNICODE_STRING ;
@@ -21,6 +21,7 @@ use windows_kernel_sys::base::{
21
21
PCHAR , PFLT_CALLBACK_DATA , PFLT_FILTER , PFLT_PORT , PSECURITY_DESCRIPTOR , PULONG , PVOID ,
22
22
STATUS_SUCCESS , ULONG , USHORT ,
23
23
} ;
24
+ use windows_kernel_sys:: c_int;
24
25
use windows_kernel_sys:: fltmgr:: {
25
26
strcpy, DbgPrint , FltBuildDefaultSecurityDescriptor , FltCloseClientPort ,
26
27
FltCloseCommunicationPort , FltCreateCommunicationPort , FltFreeSecurityDescriptor ,
@@ -42,10 +43,10 @@ const G_FILTER_REGISTRATION: FLT_REGISTRATION = FLT_REGISTRATION {
42
43
ContextRegistration : null_mut ( ) ,
43
44
OperationRegistration : G_CALLBACKS . as_ptr ( ) ,
44
45
FilterUnloadCallback : Some ( InstanceFilterUnloadCallback ) ,
45
- InstanceSetupCallback : Some ( InstanceSetupCallback ) ,
46
- InstanceQueryTeardownCallback : Some ( InstanceQueryTeardownCallback ) ,
47
- InstanceTeardownStartCallback : Some ( InstanceTeardownStartCallback ) ,
48
- InstanceTeardownCompleteCallback : Some ( InstanceTeardownCompleteCallback ) ,
46
+ InstanceSetupCallback : None , // Some(InstanceSetupCallback),
47
+ InstanceQueryTeardownCallback : None , // Some(InstanceQueryTeardownCallback),
48
+ InstanceTeardownStartCallback : None , // Some(InstanceTeardownStartCallback),
49
+ InstanceTeardownCompleteCallback : None , // Some(InstanceTeardownCompleteCallback),
49
50
GenerateFileNameCallback : None ,
50
51
NormalizeNameComponentCallback : None ,
51
52
NormalizeContextCleanupCallback : None ,
@@ -74,9 +75,9 @@ unsafe extern "C" fn InstanceTeardownCompleteCallback(
74
75
///
75
76
const G_CALLBACKS : & [ FLT_OPERATION_REGISTRATION ] = {
76
77
& [
77
- FLT_OPERATION_REGISTRATION :: new ( )
78
- . set_major_function ( FLT_OPERATION_REGISTRATION :: IRP_MJ_CREATE )
79
- . set_preop ( Some ( PreOperationCreate ) ) ,
78
+ // FLT_OPERATION_REGISTRATION::new()
79
+ // .set_major_function(FLT_OPERATION_REGISTRATION::IRP_MJ_CREATE)
80
+ // .set_preop(Some(PreOperationCreate)),
80
81
FLT_OPERATION_REGISTRATION :: new ( )
81
82
. set_major_function ( FLT_OPERATION_REGISTRATION :: IRP_MJ_OPERATION_END ) ,
82
83
]
@@ -93,21 +94,19 @@ unsafe extern "C" fn PreOperationCreate(
93
94
let k = & ( * ( * ( * Data ) . Iopb ) . TargetFileObject ) . FileName ;
94
95
95
96
unsafe {
96
- DbgPrint ( "%wZ\n " . as_ptr ( ) as _ , k) ;
97
+ DbgPrint ( "%wZ\n \0 " . as_ptr ( ) as _ , k) ;
97
98
}
98
99
99
100
FLT_PREOP_SUCCESS_NO_CALLBACK
100
101
}
101
102
102
- ///
103
103
/// This is called before a filter is unloaded.
104
104
/// If NULL is specified for this routine, then the filter can never be unloaded.
105
- ///
106
105
extern "C" fn InstanceFilterUnloadCallback ( _Flags : FLT_FILTER_UNLOAD_FLAGS ) -> NTSTATUS {
107
106
PAGED_CODE ! ( ) ;
108
107
109
108
unsafe {
110
- DbgPrint ( "Unloading rust minifilter\0 \n " . as_ptr ( ) as _ ) ;
109
+ DbgPrint ( "Unloading rust minifilter\n \0 " . as_ptr ( ) as _ ) ;
111
110
FltCloseCommunicationPort ( PORT ) ;
112
111
113
112
FltUnregisterFilter ( G_MINIFILTER_HANDLE ) ;
@@ -156,34 +155,49 @@ pub extern "system" fn DriverEntry(
156
155
) -> NTSTATUS {
157
156
let mut sd: PSECURITY_DESCRIPTOR = null_mut ( ) ;
158
157
let mut oa: OBJECT_ATTRIBUTES = unsafe { core:: mem:: zeroed ( ) } ;
159
- let mut name: UNICODE_STRING = UNICODE_STRING :: create ( "\\ mf" ) ;
158
+ let mut name = "\\ mf" ;
160
159
161
160
unsafe {
162
- DbgPrint ( "Hello from Rust!\0 " . as_ptr ( ) as _ ) ;
161
+ DbgPrint ( "Hello from Rust!\n \ 0 " . as_ptr ( ) as _ ) ;
163
162
}
164
163
164
+ // driver.DriverUnload = Some(driver_exit);
165
+
165
166
//
166
167
// register minifilter driver
167
168
//
168
169
let mut status: NTSTATUS =
169
170
unsafe { FltRegisterFilter ( driver, & G_FILTER_REGISTRATION , & mut G_MINIFILTER_HANDLE ) } ;
170
171
172
+ unsafe {
173
+ DbgPrint ( "1 Here\n \0 " . as_ptr ( ) as _ ) ;
174
+ }
175
+
171
176
if !NT_SUCCESS ! ( status) {
172
177
return status;
173
178
}
174
179
180
+ unsafe {
181
+ DbgPrint ( "2 Here\n \0 " . as_ptr ( ) as _ ) ;
182
+ }
183
+
175
184
status = unsafe { FltBuildDefaultSecurityDescriptor ( & mut sd, FLT_PORT_ALL_ACCESS ) } ;
176
185
186
+ let name = UNICODE_STRING :: create ( name) ;
187
+
177
188
if NT_SUCCESS ! ( status) {
178
189
unsafe {
179
190
InitializeObjectAttributes (
180
191
& mut oa,
181
- & mut name,
182
- OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE ,
192
+ & mut name. as_base_unicode ( ) ,
193
+ OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE ,
183
194
null_mut ( ) ,
184
195
sd,
185
196
) ;
186
197
}
198
+ unsafe {
199
+ DbgPrint ( "3 Here\n \0 " . as_ptr ( ) as _ ) ;
200
+ }
187
201
188
202
status = unsafe {
189
203
FltCreateCommunicationPort (
@@ -202,17 +216,31 @@ pub extern "system" fn DriverEntry(
202
216
FltFreeSecurityDescriptor ( sd) ;
203
217
}
204
218
219
+ unsafe {
220
+ DbgPrint ( "4 Here\n \0 " . as_ptr ( ) as _ ) ;
221
+ }
222
+
205
223
if NT_SUCCESS ! ( status) {
224
+ unsafe {
225
+ DbgPrint ( "5 Here\n \0 " . as_ptr ( ) as _ ) ;
226
+ }
206
227
// driver.DriverUnload = Some(driver_exit);
207
228
208
229
// start minifilter driver
209
230
status = unsafe { FltStartFiltering ( G_MINIFILTER_HANDLE ) } ;
210
231
211
232
if !NT_SUCCESS ! ( status) {
233
+ unsafe {
234
+ DbgPrint ( "6 Here\0 \n " . as_ptr ( ) as _ ) ;
235
+ }
212
236
unsafe {
213
237
FltUnregisterFilter ( G_MINIFILTER_HANDLE ) ;
214
238
}
215
239
}
240
+ } else {
241
+ unsafe {
242
+ FltCloseCommunicationPort ( PORT ) ;
243
+ }
216
244
}
217
245
}
218
246
@@ -227,7 +255,7 @@ unsafe extern "C" fn MiniConnect(
227
255
ConnectionPortCookie : * mut PVOID ,
228
256
) -> NTSTATUS {
229
257
CLIENT_PORT = ClientPort ;
230
- DbgPrint ( "Rust connect fromm application\n \0 " . as_ptr ( ) as _ ) ;
258
+ DbgPrint ( "Rust connect from application\n \0 " . as_ptr ( ) as _ ) ;
231
259
232
260
STATUS_SUCCESS
233
261
}
@@ -245,20 +273,13 @@ unsafe extern "C" fn MiniSendRec(
245
273
OutputBufferLength : ULONG ,
246
274
ReturnOutputBufferLength : PULONG ,
247
275
) -> NTSTATUS {
248
- // let mut msg: PCHAR = "Rust from kernel".as_mut_ptr() as *mut i8;
249
- unsafe {
250
- DbgPrint (
251
- "Rust message from application: %s\n \0 " . as_ptr ( ) as _ ,
252
- InputBuffer as PCHAR ,
253
- ) ;
254
- }
276
+ let mut msg: PCHAR = "Rust from kernel\n \0 " . as_bytes ( ) . as_ptr ( ) as * mut i8 ;
277
+ DbgPrint (
278
+ "Rust message from application: %s" . as_ptr ( ) as _ ,
279
+ InputBuffer as * mut i8 ,
280
+ ) ;
255
281
256
- unsafe {
257
- strcpy (
258
- OutputBuffer as PCHAR ,
259
- "Rust from kernel" . as_ptr ( ) as * mut i8 ,
260
- ) ;
261
- }
282
+ strcpy ( OutputBuffer as PCHAR , msg) ;
262
283
263
284
STATUS_SUCCESS
264
285
}
0 commit comments