- Added rustfmt.toml

- Added `kernel-print` example code
- Updated the `rename` Makefile script
This commit is contained in:
not-matthias
2020-08-21 23:02:34 +02:00
parent 01e613d967
commit e95b1e51c6
8 changed files with 59 additions and 32 deletions
+2
View File
@@ -10,6 +10,8 @@ path = "src/lib.rs"
crate-type = ["cdylib"]
[dependencies]
kernel-print = "0.1.0"
kernel-alloc = "0.1.0"
obfstr = "0.1.1"
[dependencies.winapi]
+1
View File
@@ -11,6 +11,7 @@ script = [
]
[tasks.rename]
dependencies = ["build-driver"]
ignore_errors = true
script = [
"cd %TARGET_PATH%",
+7 -14
View File
@@ -1,12 +1,12 @@
use failure::format_err;
use failure::Error;
use failure::{format_err, Error};
use std::{
env::var,
path::{Path, PathBuf},
};
use winreg::{enums::*, RegKey};
/// Returns the path to the `Windows Kits` directory. It's by default at `C:\Program Files (x86)\Windows Kits\10`.
/// Returns the path to the `Windows Kits` directory. It's by default at
/// `C:\Program Files (x86)\Windows Kits\10`.
fn get_windows_kits_dir() -> Result<PathBuf, Error> {
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let key = r"SOFTWARE\Microsoft\Windows Kits\Installed Roots";
@@ -15,7 +15,8 @@ fn get_windows_kits_dir() -> Result<PathBuf, Error> {
Ok(dir.into())
}
/// Returns the path to the kernel mode libraries. The path may look like this: `C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\km`.
/// Returns the path to the kernel mode libraries. The path may look like this:
/// `C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\km`.
fn get_km_dir(windows_kits_dir: &PathBuf) -> Result<PathBuf, Error> {
let readdir = Path::new(windows_kits_dir).join("lib").read_dir()?;
@@ -49,21 +50,13 @@ fn internal_link_search() {
};
let lib_dir = km_dir.join(arch);
println!(
"cargo:rustc-link-search=native={}",
lib_dir.to_str().unwrap()
);
println!("cargo:rustc-link-search=native={}", lib_dir.to_str().unwrap());
}
fn extra_link_search() {}
fn main() {
if var(format!(
"CARGO_FEATURE_{}",
"extra_link_search".to_uppercase()
))
.is_ok()
{
if var(format!("CARGO_FEATURE_{}", "extra_link_search".to_uppercase())).is_ok() {
extra_link_search()
} else {
internal_link_search()
+21
View File
@@ -0,0 +1,21 @@
edition = "2018"
brace_style = "PreferSameLine"
color = "Always"
fn_args_layout = "Compressed"
fn_single_line = true
format_code_in_doc_comments = true
format_macro_matchers = true
format_macro_bodies = true
format_strings = true
inline_attribute_width = 80
max_width = 120
merge_imports = true
normalize_doc_attributes = true
reorder_impl_items = true
reorder_imports = true
reorder_modules = true
use_field_init_shorthand = true
use_try_shorthand = true
where_single_line = true
wrap_comments = true
+1 -2
View File
@@ -1,5 +1,4 @@
use winapi::km::wdm::PEPROCESS;
use winapi::shared::ntdef::NTSTATUS;
use winapi::{km::wdm::PEPROCESS, shared::ntdef::NTSTATUS};
pub type PVOID = *mut core::ffi::c_void;
pub type HANDLE = PVOID;
+25 -9
View File
@@ -1,8 +1,9 @@
#![no_std]
#![feature(alloc_error_handler)]
use crate::include::MmIsAddressValid;
use crate::process::Process;
use crate::string::create_unicode_string;
extern crate alloc;
use crate::{include::MmIsAddressValid, process::Process, string::create_unicode_string};
use core::panic::PanicInfo;
pub mod include;
@@ -10,15 +11,21 @@ pub mod log;
pub mod process;
pub mod string;
/// Explanation can be found here: https://github.com/Trantect/win_driver_example/issues/4
#[used]
/// When using the alloc crate it seems like it does some unwinding. Adding this
/// export satisfies the compiler but may introduce undefined behaviour when a
/// panic occurs.
#[no_mangle]
static _fltused: i32 = 0;
pub extern "system" fn __CxxFrameHandler3(_: *mut u8, _: *mut u8, _: *mut u8, _: *mut u8) -> i32 { unimplemented!() }
#[global_allocator]
static GLOBAL: kernel_alloc::KernelAlloc = kernel_alloc::KernelAlloc;
/// Explanation can be found here: https://github.com/Trantect/win_driver_example/issues/4
#[export_name = "_fltused"]
static _FLTUSED: i32 = 0;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
fn panic(_info: &PanicInfo) -> ! { loop {} }
#[no_mangle]
pub extern "system" fn driver_entry() -> u32 {
@@ -37,5 +44,14 @@ pub extern "system" fn driver_entry() -> u32 {
let process = Process::by_id(4 as _);
log!("Process found: %i", process.is_some() as u64);
// Logger
//
kernel_print::kernel_println!("BEFORE");
kernel_print::kernel_dbg!(2 + 2);
kernel_print::kernel_println!("{} + {} = {}", 2, 2, 2 + 2);
kernel_print::kernel_print!("{} + {} = {}\n", 2, 2, 2 + 2);
kernel_print::kernel_println!("AFTER");
0 /* STATUS_SUCCESS */
}
+1 -2
View File
@@ -1,6 +1,5 @@
use crate::include::{ObfDereferenceObject, PsLookupProcessByProcessId};
use winapi::km::wdm::PEPROCESS;
use winapi::shared::ntdef::NT_SUCCESS;
use winapi::{km::wdm::PEPROCESS, shared::ntdef::NT_SUCCESS};
pub struct Process {
pub process: PEPROCESS,
+1 -5
View File
@@ -3,11 +3,7 @@ use winapi::shared::ntdef::UNICODE_STRING;
pub fn create_unicode_string(s: &[u16]) -> UNICODE_STRING {
let len = s.len();
let n = if len > 0 && s[len - 1] == 0 {
len - 1
} else {
len
};
let n = if len > 0 && s[len - 1] == 0 { len - 1 } else { len };
UNICODE_STRING {
Length: (n * 2) as u16,