Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit 1b8ecd9

Browse files
authored
Merge pull request #4 from sn99/master
refactor and user application
2 parents 54a14ac + 4bef72b commit 1b8ecd9

File tree

23 files changed

+179
-78
lines changed

23 files changed

+179
-78
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
/target
2-
/Cargo.lock
3-
4-
DriverCertificate.cer
1+
Cargo.lock
2+
/target/

Cargo.toml

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
[package]
2-
name = "driver"
3-
version = "0.1.0"
4-
edition = "2021"
5-
build = "build.rs"
6-
7-
[lib]
8-
path = "src/lib.rs"
9-
crate-type = ["cdylib"]
10-
11-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12-
13-
[profile.dev]
14-
panic = "abort"
15-
16-
[profile.release]
17-
panic = "abort"
18-
19-
[build-dependencies]
20-
thiserror = "1.0"
21-
winreg = "0.11.0"
22-
23-
[dependencies]
24-
windows-kernel-sys = { path = "windows-kernel-sys" }
25-
windows-kernel-macros = { path = "windows-kernel-macros" }
26-
windows-kernel-string = {path = "windows-kernel-string"}
27-
windows-kernel-alloc = {path = "windows-kernel-alloc"}
1+
[workspace]
2+
members = [
3+
"windows-kernel-alloc",
4+
"windows-kernel-macros",
5+
"windows-kernel-string",
6+
"windows-kernel-sys",
7+
"windows-rust-minifilter",
8+
]
9+
exclude = [
10+
"windows-rust-application"
11+
]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ You can set up a VM for testing by following [DEBUG](DEBUG.md).
2121

2222
## Building
2323

24+
From inside [windows-rust-minifilter](windows-rust-minifilter), run:
25+
2426
`cargo make --profile production all`
2527

2628
**Note: You might need to run `cargo clean` before rebuilding again.**

windows-kernel-alloc/src/kernel_alloc.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
//it must be defined in lib.rs
2-
//#![feature(alloc_error_handler)]
1+
// It must be defined in lib.rs
2+
// #![feature(alloc_error_handler)]
33
#[allow(unused_imports)]
44
use alloc::alloc::handle_alloc_error;
55
use core::alloc::{GlobalAlloc, Layout};
66
use windows_kernel_sys::base::{SIZE_T, ULONG64};
77
use windows_kernel_sys::ntoskrnl::{ExAllocatePool2, ExFreePool};
88

9-
109
pub const POOL_TAG: u32 = u32::from_ne_bytes(*b"TSUR");
1110
pub const POOL_FLAG_PAGED: ULONG64 = 0x0000000000000100;
1211

windows-kernel-alloc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//source idea: https://os.phil-opp.com/minimal-rust-kernel/
1+
// source idea: https://os.phil-opp.com/minimal-rust-kernel/
22
#![no_std]
33
#![feature(lang_items)]
44
#![feature(alloc_error_handler)]

windows-kernel-macros/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ pub unsafe fn InitializeObjectAttributes(
3434
s: windows_kernel_sys::base::PVOID,
3535
) {
3636
use core::mem::size_of;
37-
(*p).Length = size_of::<windows_kernel_sys::base::OBJECT_ATTRIBUTES>() as windows_kernel_sys::base::ULONG;
37+
(*p).Length =
38+
size_of::<windows_kernel_sys::base::OBJECT_ATTRIBUTES>() as windows_kernel_sys::base::ULONG;
3839
(*p).RootDirectory = r;
3940
(*p).Attributes = a;
4041
(*p).ObjectName = n;
4142
(*p).SecurityDescriptor = s;
4243
(*p).SecurityQualityOfService = NULL;
43-
}
44+
}

windows-kernel-string/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#![no_std]
22
#![allow(non_camel_case_types)]
33
#![allow(non_snake_case)]
4+
#![allow(clippy::upper_case_acronyms)]
45

56
extern crate alloc;
67

78
use alloc::string::String;
89
use core::fmt::{Display, Formatter};
9-
use windows_kernel_sys::base::{BOOLEAN, LONG, NTSTATUS, TRUE, ULONG, ULONGLONG};
1010
use windows_kernel_sys::base;
11+
use windows_kernel_sys::base::{BOOLEAN, LONG, NTSTATUS, TRUE, ULONG, ULONGLONG};
1112

1213
type PCSZ = *const u8;
1314
type PCWSTR = *const u16;
@@ -31,9 +32,11 @@ impl<'a> From<&'a [u8]> for ANSI_STRING {
3132
let mut str = ANSI_STRING::default();
3233

3334
let mut buffer = buffer.to_vec();
34-
if *buffer.last().expect("bad unwrap on From<&'a [u8]> for ANSI_STRING") != 0 {
35-
//let mut buffer = buffer.to_vec();
36-
buffer.push(0);
35+
36+
if let Some(last_byte) = buffer.last() {
37+
if *last_byte != 0 {
38+
buffer.push(0);
39+
}
3740
}
3841

3942
unsafe {
@@ -55,7 +58,7 @@ impl Default for ANSI_STRING {
5558
fn default() -> Self {
5659
Self {
5760
Length: 0,
58-
MaximumLength: 0 as u16,
61+
MaximumLength: 0_u16,
5962
Buffer: core::ptr::null(),
6063
}
6164
}
@@ -127,8 +130,11 @@ impl<'a> From<&'a [u16]> for UNICODE_STRING {
127130
let mut str = UNICODE_STRING::default();
128131

129132
let mut buffer = buffer.to_vec();
130-
if *buffer.last().expect("bad unwrap on From<&'a [u16]> for UNICODE_STRING") == 0 {
131-
buffer.push(0);
133+
134+
if let Some(last_byte) = buffer.last(){
135+
if *last_byte == 0{
136+
buffer.push(0);
137+
}
132138
}
133139

134140
unsafe {
@@ -158,7 +164,7 @@ impl Default for UNICODE_STRING {
158164
fn default() -> Self {
159165
Self {
160166
Length: 0,
161-
MaximumLength: 0 as u16,
167+
MaximumLength: 0_u16,
162168
ptr: core::ptr::null(),
163169
}
164170
}

windows-kernel-sys/src/base.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![allow(non_upper_case_globals)]
22
#![allow(non_camel_case_types)]
33
#![allow(non_snake_case)]
4+
#![allow(clippy::useless_transmute)]
5+
#![allow(clippy::too_many_arguments)]
6+
#![allow(clippy::unnecessary_cast)]
47

58
pub use cty::*;
69

windows-kernel-sys/src/fltmgr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![allow(non_upper_case_globals)]
22
#![allow(non_camel_case_types)]
33
#![allow(non_snake_case)]
4+
#![allow(clippy::useless_transmute)]
5+
#![allow(clippy::too_many_arguments)]
6+
#![allow(clippy::unnecessary_cast)]
47

58
use crate::base::*;
69

windows-kernel-sys/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#![no_std]
2+
#![allow(clippy::useless_transmute)]
3+
#![allow(clippy::too_many_arguments)]
4+
#![allow(clippy::unnecessary_cast)]
25

36
pub mod base;
47
pub mod fltmgr;

windows-kernel-sys/src/ntoskrnl.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![allow(non_upper_case_globals)]
22
#![allow(non_camel_case_types)]
33
#![allow(non_snake_case)]
4+
#![allow(clippy::useless_transmute)]
5+
#![allow(clippy::too_many_arguments)]
6+
#![allow(clippy::unnecessary_cast)]
47

58
use crate::base::*;
69

windows-kernel-sys/src/wrapper.h

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,50 @@
1414
#include <suppress.h>
1515
#include <wdm.h>
1616

17-
typedef union _KGDTENTRY64
18-
{
19-
struct
20-
{
17+
typedef union _KGDTENTRY64 {
18+
struct {
2119
unsigned short LimitLow;
2220
unsigned short BaseLow;
23-
union
24-
{
25-
struct
26-
{
21+
union {
22+
struct {
2723
unsigned char BaseMiddle;
2824
unsigned char Flags1;
2925
unsigned char Flags2;
3026
unsigned char BaseHigh;
3127
} Bytes;
32-
struct
33-
{
34-
unsigned long BaseMiddle : 8;
35-
unsigned long Type : 5;
36-
unsigned long Dpl : 2;
37-
unsigned long Present : 1;
38-
unsigned long LimitHigh : 4;
39-
unsigned long System : 1;
40-
unsigned long LongMode : 1;
41-
unsigned long DefaultBig : 1;
42-
unsigned long Granularity : 1;
43-
unsigned long BaseHigh : 8;
28+
struct {
29+
unsigned long BaseMiddle: 8;
30+
unsigned long Type: 5;
31+
unsigned long Dpl: 2;
32+
unsigned long Present: 1;
33+
unsigned long LimitHigh: 4;
34+
unsigned long System: 1;
35+
unsigned long LongMode: 1;
36+
unsigned long DefaultBig: 1;
37+
unsigned long Granularity: 1;
38+
unsigned long BaseHigh: 8;
4439
} Bits;
4540
};
4641
unsigned long BaseUpper;
4742
unsigned long MustBeZero;
4843
};
49-
unsigned __int64 Alignment;
44+
unsigned __int64
45+
Alignment;
5046
} KGDTENTRY64, *PKGDTENTRY64;
5147

52-
typedef union _KIDTENTRY64
53-
{
54-
struct
55-
{
48+
typedef union _KIDTENTRY64 {
49+
struct {
5650
unsigned short OffsetLow;
5751
unsigned short Selector;
58-
unsigned short IstIndex : 3;
59-
unsigned short Reserved0 : 5;
60-
unsigned short Type : 5;
61-
unsigned short Dpl : 2;
62-
unsigned short Present : 1;
52+
unsigned short IstIndex: 3;
53+
unsigned short Reserved0: 5;
54+
unsigned short Type: 5;
55+
unsigned short Dpl: 2;
56+
unsigned short Present: 1;
6357
unsigned short OffsetMiddle;
6458
unsigned long OffsetHigh;
6559
unsigned long Reserved1;
6660
};
67-
unsigned __int64 Alignment;
61+
unsigned __int64
62+
Alignment;
6863
} KIDTENTRY64, *PKIDTENTRY64;

windows-rust-application/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cargo.lock
2+
target

windows-rust-application/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "rust"
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+
10+
[dependencies.windows-sys]
11+
version = "0.45.0"
12+
features = [
13+
"Win32_Storage_InstallableFileSystems",
14+
"Win32_Foundation",
15+
"Win32_Security",
16+
"Win32_Storage_FileSystem",
17+
"Win32_System_Threading",
18+
"Win32_System_ProcessStatus",
19+
"Win32_System_Diagnostics_Debug",
20+
]
21+
22+
[profile.release]
23+
lto = true
24+
codegen-units = 1
25+
opt-level = 3

windows-rust-application/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rust windows Application
2+
3+
To be run with [Rust Minifilter POC](https://github.com/SubconsciousCompute/poc-windows-rust-filter)

windows-rust-application/src/main.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::ffi::c_void;
2+
use std::ptr::null;
3+
4+
use windows_sys::w;
5+
use windows_sys::Win32::Foundation::HANDLE;
6+
use windows_sys::Win32::Storage::InstallableFileSystems::{
7+
FilterConnectCommunicationPort, FilterSendMessage,
8+
};
9+
10+
static mut PORT: HANDLE = -1;
11+
12+
fn main() {
13+
println!("Press ctrl-z to ctrl-c to exit...");
14+
15+
let mut byterec = 0;
16+
let buffer = "Hello from Rust user application\n\0".as_bytes().as_ptr() as *const c_void;
17+
let bufferlen = 50;
18+
let rbuffer_size = 256;
19+
let mut rbuffer: Vec<u8> = vec![0; rbuffer_size];
20+
let recbuffer: *mut c_void = rbuffer.as_mut_ptr() as *mut c_void;
21+
22+
unsafe {
23+
if PORT == -1
24+
&& FilterConnectCommunicationPort(w!("\\mf"), 0, null(), 0, null(), &mut PORT) != 0
25+
{
26+
panic!("port connection failed");
27+
}
28+
}
29+
30+
unsafe {
31+
if FilterSendMessage(PORT, buffer, bufferlen as u32, recbuffer, 50, &mut byterec) != 0 {
32+
println!("failed to get message");
33+
} else {
34+
let pchar = recbuffer as *mut i8;
35+
let string = std::ffi::CStr::from_ptr(pchar)
36+
.to_str()
37+
.expect("Not a valid String");
38+
print!("{}", string);
39+
}
40+
}
41+
42+
loop {}
43+
}
File renamed without changes.

windows-rust-minifilter/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target
2+
/Cargo.lock
3+
4+
DriverCertificate.cer

windows-rust-minifilter/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "driver"
3+
version = "0.1.0"
4+
edition = "2021"
5+
build = "build.rs"
6+
7+
[lib]
8+
path = "src/lib.rs"
9+
crate-type = ["cdylib"]
10+
11+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12+
13+
[profile.dev]
14+
panic = "abort"
15+
16+
[profile.release]
17+
panic = "abort"
18+
19+
[build-dependencies]
20+
thiserror = "1.0"
21+
winreg = "0.11.0"
22+
23+
[dependencies]
24+
windows-kernel-sys = { path = "../windows-kernel-sys" }
25+
windows-kernel-macros = { path = "../windows-kernel-macros" }
26+
windows-kernel-string = {path = "../windows-kernel-string"}
27+
windows-kernel-alloc = {path = "../windows-kernel-alloc"}

0 commit comments

Comments
 (0)