leviathan-driver 0.1.0

Windows kernel-mode EDR/XDR driver framework in Rust - callbacks, filters, detection, forensics
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Kernel Memory Management
//!
//! Provides safe wrappers for kernel memory operations:
//! - Pool allocations (NonPaged, Paged)
//! - Memory Descriptor Lists (MDL)
//! - Secure memory access
//! - User/Kernel buffer handling
//!
//! # Memory Types
//! - **NonPagedPool**: Always in physical memory, accessible at any IRQL
//! - **PagedPool**: Can be paged out, only accessible at IRQL < DISPATCH_LEVEL
//! - **NonPagedPoolNx**: Non-executable nonpaged pool (security best practice)
//!
//! # Security Considerations
//! - Always validate user-mode buffer pointers
//! - Use MDLs for safe user buffer access
//! - Never trust user-provided sizes without validation

use core::ptr;
use wdk::println;
use wdk_sys::{
    ntddk::{
        ExAllocatePool2, ExFreePoolWithTag,
        IoAllocateMdl, IoFreeMdl, MmProbeAndLockPages, MmUnlockPages,
        MmGetSystemAddressForMdlSafe, MmBuildMdlForNonPagedPool,
        ProbeForRead, ProbeForWrite,
    },
    POOL_FLAG_NON_PAGED, POOL_FLAG_PAGED, POOL_FLAG_NON_PAGED_EXECUTE,
    PMDL, PVOID, ULONG, LOCK_OPERATION, MM_PAGE_PRIORITY,
};

/// Pool tag for our allocations (must be 4 chars)
/// 'LVTN' = Leviathan
pub const POOL_TAG: u32 = u32::from_le_bytes(*b"LVTN");

/// Memory pool types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PoolType {
    /// Non-paged pool - always resident, any IRQL
    NonPaged,
    /// Paged pool - can page out, IRQL < DISPATCH_LEVEL only
    Paged,
    /// Non-paged, non-executable - security best practice
    NonPagedNx,
}

impl PoolType {
    fn to_flags(self) -> u64 {
        match self {
            PoolType::NonPaged => POOL_FLAG_NON_PAGED as u64,
            PoolType::Paged => POOL_FLAG_PAGED as u64,
            PoolType::NonPagedNx => (POOL_FLAG_NON_PAGED | POOL_FLAG_NON_PAGED_EXECUTE) as u64,
        }
    }
}

/// Safe wrapper for pool allocations
pub struct PoolAllocation {
    ptr: PVOID,
    size: usize,
    pool_type: PoolType,
}

impl PoolAllocation {
    /// Allocate memory from the specified pool
    ///
    /// # Safety
    /// - NonPaged/NonPagedNx: Any IRQL
    /// - Paged: IRQL < DISPATCH_LEVEL
    pub unsafe fn new(size: usize, pool_type: PoolType) -> Option<Self> {
        if size == 0 {
            return None;
        }

        let ptr = unsafe {
            ExAllocatePool2(pool_type.to_flags(), size as u64, POOL_TAG)
        };

        if ptr.is_null() {
            println!("[Leviathan] Pool allocation failed: size={}", size);
            return None;
        }

        // Zero the memory for security
        unsafe {
            ptr::write_bytes(ptr as *mut u8, 0, size);
        }

        Some(Self { ptr, size, pool_type })
    }

    /// Get raw pointer to the allocation
    pub fn as_ptr(&self) -> PVOID {
        self.ptr
    }

    /// Get mutable raw pointer
    pub fn as_mut_ptr(&mut self) -> PVOID {
        self.ptr
    }

    /// Get the size of the allocation
    pub fn size(&self) -> usize {
        self.size
    }

    /// Get as typed pointer
    ///
    /// # Safety
    /// Caller must ensure T is valid for this memory
    pub unsafe fn as_typed<T>(&self) -> *const T {
        self.ptr as *const T
    }

    /// Get as mutable typed pointer
    ///
    /// # Safety
    /// Caller must ensure T is valid for this memory
    pub unsafe fn as_typed_mut<T>(&mut self) -> *mut T {
        self.ptr as *mut T
    }

    /// Get as byte slice
    ///
    /// # Safety
    /// Must be at appropriate IRQL for pool type
    pub unsafe fn as_slice(&self) -> &[u8] {
        unsafe { core::slice::from_raw_parts(self.ptr as *const u8, self.size) }
    }

    /// Get as mutable byte slice
    ///
    /// # Safety
    /// Must be at appropriate IRQL for pool type
    pub unsafe fn as_slice_mut(&mut self) -> &mut [u8] {
        unsafe { core::slice::from_raw_parts_mut(self.ptr as *mut u8, self.size) }
    }
}

impl Drop for PoolAllocation {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            // Zero memory before freeing (security)
            unsafe {
                ptr::write_bytes(self.ptr as *mut u8, 0, self.size);
                ExFreePoolWithTag(self.ptr, POOL_TAG);
            }
        }
    }
}

/// Memory Descriptor List (MDL) wrapper
///
/// MDLs describe physical pages backing a virtual buffer,
/// allowing safe access to user-mode memory from kernel mode.
pub struct Mdl {
    mdl: PMDL,
    locked: bool,
    system_address: PVOID,
}

impl Mdl {
    /// Create an MDL for a buffer
    ///
    /// # Parameters
    /// - `virtual_address`: Start of the buffer
    /// - `length`: Size of the buffer
    /// - `secondary_buffer`: TRUE if this is a secondary buffer
    /// - `charge_quota`: TRUE to charge against process quota
    ///
    /// # Safety
    /// Must be at IRQL <= DISPATCH_LEVEL
    pub unsafe fn new(
        virtual_address: PVOID,
        length: usize,
        secondary_buffer: bool,
        charge_quota: bool,
    ) -> Option<Self> {
        let mdl = unsafe {
            IoAllocateMdl(
                virtual_address,
                length as u32,
                secondary_buffer as u8,
                charge_quota as u8,
                ptr::null_mut(),
            )
        };

        if mdl.is_null() {
            return None;
        }

        Some(Self {
            mdl,
            locked: false,
            system_address: ptr::null_mut(),
        })
    }

    /// Lock the pages described by the MDL
    ///
    /// This ensures the pages won't be paged out and gets their physical addresses.
    ///
    /// # Parameters
    /// - `access_mode`: KernelMode or UserMode
    /// - `operation`: IoReadAccess, IoWriteAccess, or IoModifyAccess
    ///
    /// # Safety
    /// - Must be at IRQL <= APC_LEVEL
    /// - For user buffers, must be in the context of the owning process
    pub unsafe fn lock_pages(&mut self, operation: LOCK_OPERATION) -> Result<(), ()> {
        if self.locked {
            return Ok(());
        }

        // Use SEH in production to catch access violations
        unsafe {
            MmProbeAndLockPages(
                self.mdl,
                wdk_sys::MODE::KernelMode as i8,
                operation,
            );
        }

        self.locked = true;
        Ok(())
    }

    /// Get a system-space virtual address for the MDL
    ///
    /// This maps the physical pages to a kernel virtual address.
    ///
    /// # Safety
    /// Pages must be locked first
    pub unsafe fn get_system_address(&mut self, priority: MM_PAGE_PRIORITY) -> Option<PVOID> {
        if !self.locked {
            return None;
        }

        if self.system_address.is_null() {
            self.system_address = unsafe {
                MmGetSystemAddressForMdlSafe(self.mdl, priority as u32)
            };
        }

        if self.system_address.is_null() {
            None
        } else {
            Some(self.system_address)
        }
    }

    /// Build MDL for non-paged pool memory
    ///
    /// Use this for buffers allocated from NonPagedPool.
    /// No need to lock pages - they're already locked.
    ///
    /// # Safety
    /// Buffer must be from NonPagedPool
    pub unsafe fn build_for_nonpaged(&mut self) {
        unsafe { MmBuildMdlForNonPagedPool(self.mdl) };
        self.locked = true;
    }

    /// Get the raw MDL pointer
    pub fn as_raw(&self) -> PMDL {
        self.mdl
    }
}

impl Drop for Mdl {
    fn drop(&mut self) {
        if self.locked && !self.mdl.is_null() {
            unsafe { MmUnlockPages(self.mdl) };
        }
        if !self.mdl.is_null() {
            unsafe { IoFreeMdl(self.mdl) };
        }
    }
}

/// Safely probe and validate a user-mode buffer
///
/// # Safety
/// Must be called in the context of the process that owns the buffer
pub unsafe fn probe_user_buffer(
    buffer: PVOID,
    length: usize,
    alignment: u32,
    for_write: bool,
) -> Result<(), ()> {
    if buffer.is_null() || length == 0 {
        return Err(());
    }

    // Wrap in SEH in production
    if for_write {
        unsafe { ProbeForWrite(buffer, length as u64, alignment) };
    } else {
        unsafe { ProbeForRead(buffer, length as u64, alignment) };
    }

    Ok(())
}

/// Copy data from user buffer to kernel buffer safely
///
/// # Safety
/// - Must be in user process context
/// - User buffer must be valid and accessible
pub unsafe fn copy_from_user(
    kernel_buffer: &mut [u8],
    user_buffer: PVOID,
    length: usize,
) -> Result<usize, ()> {
    if user_buffer.is_null() || length == 0 {
        return Err(());
    }

    let copy_len = core::cmp::min(length, kernel_buffer.len());

    // In production: Use SEH and proper MDL mapping
    unsafe {
        ProbeForRead(user_buffer, copy_len as u64, 1);
        ptr::copy_nonoverlapping(
            user_buffer as *const u8,
            kernel_buffer.as_mut_ptr(),
            copy_len,
        );
    }

    Ok(copy_len)
}

/// Copy data from kernel buffer to user buffer safely
///
/// # Safety
/// - Must be in user process context
/// - User buffer must be valid and writable
pub unsafe fn copy_to_user(
    user_buffer: PVOID,
    kernel_buffer: &[u8],
    length: usize,
) -> Result<usize, ()> {
    if user_buffer.is_null() || length == 0 {
        return Err(());
    }

    let copy_len = core::cmp::min(length, kernel_buffer.len());

    // In production: Use SEH and proper MDL mapping
    unsafe {
        ProbeForWrite(user_buffer, copy_len as u64, 1);
        ptr::copy_nonoverlapping(
            kernel_buffer.as_ptr(),
            user_buffer as *mut u8,
            copy_len,
        );
    }

    Ok(copy_len)
}

/// Secure memory zeroing that won't be optimized away
pub fn secure_zero(buffer: &mut [u8]) {
    // volatile write to prevent optimization
    for byte in buffer.iter_mut() {
        unsafe {
            ptr::write_volatile(byte, 0);
        }
    }
    // Memory barrier
    core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}

/// Lookaside list for frequent same-size allocations
///
/// More efficient than individual pool allocations for
/// fixed-size objects.
pub struct LookasideList {
    // Would contain NPAGED_LOOKASIDE_LIST or PAGED_LOOKASIDE_LIST
    // Simplified for this example
    entry_size: usize,
    pool_type: PoolType,
}

impl LookasideList {
    /// Create a new lookaside list
    ///
    /// # Safety
    /// Must be called at PASSIVE_LEVEL
    pub unsafe fn new(entry_size: usize, pool_type: PoolType) -> Self {
        // In production: Call ExInitializeNPagedLookasideList or
        // ExInitializePagedLookasideList
        Self { entry_size, pool_type }
    }

    /// Allocate an entry from the lookaside list
    ///
    /// # Safety
    /// Depends on pool type
    pub unsafe fn allocate(&self) -> Option<PVOID> {
        // In production: ExAllocateFromNPagedLookasideList
        PoolAllocation::new(self.entry_size, self.pool_type)
            .map(|alloc| {
                let ptr = alloc.as_ptr();
                core::mem::forget(alloc); // Don't free
                ptr
            })
    }

    /// Free an entry back to the lookaside list
    ///
    /// # Safety
    /// Entry must have been allocated from this list
    pub unsafe fn free(&self, entry: PVOID) {
        // In production: ExFreeToNPagedLookasideList
        if !entry.is_null() {
            unsafe { ExFreePoolWithTag(entry, POOL_TAG) };
        }
    }
}