Windows Hardware Documentation

Syscall Filter WDF KM Samples

This section provides sample code for implementing a syscall filter using the Windows Driver Framework (WDF) in Kernel Mode (KM).

Overview

A syscall filter is a kernel-mode driver that intercepts system calls made by user-mode applications. This allows for advanced scenarios such as security monitoring, application control, and performance analysis. Implementing such filters requires a deep understanding of the Windows kernel and WDF.

Sample Files

The following sample demonstrates the core concepts of building a syscall filter with WDF KM:

SyscallFilterWdfKm

Key Concepts Demonstrated

Implementation Details

The primary mechanism for filtering syscalls in this sample involves:

Example: Intercepting NtCreateFile

To intercept the NtCreateFile system call, the driver would typically:

  1. Locate the address of the NtCreateFile kernel function.
  2. Determine its corresponding entry in the SSDT.
  3. Save the original address from the SSDT.
  4. Replace the SSDT entry with the address of a custom handler, MyNtCreateFileHandler.

The MyNtCreateFileHandler function would then have access to the arguments passed to NtCreateFile, such as the file name and desired access rights.


// Simplified example of a syscall handler
NTSTATUS MyNtCreateFileHandler(
    OUT PHANDLE FileHandle,
    IN ACCESS_MASK DesiredAccess,
    IN POBJECT_ATTRIBUTES ObjectAttributes,
    OUT PIO_STATUS_BLOCK IoStatusBlock,
    IN ULONG ShareAccess,
    IN ULONG CreateDisposition,
    IN ULONG CreateOptions,
    IN ULONG EaLength,
    IN PVOID EaBuffer,
    IN ULONG FileAttributes,
    IN ULONG SecurityFlags
)
{
    // Log the syscall event
    DbgPrint("SyscallFilter: NtCreateFile called by PID %d\n", PsGetCurrentProcessId());

    // Call the original NtCreateFile function
    // ... (requires saving the original function pointer)
    NTSTATUS status = OriginalNtCreateFile(
        FileHandle,
        DesiredAccess,
        ObjectAttributes,
        IoStatusBlock,
        ShareAccess,
        CreateDisposition,
        CreateOptions,
        EaLength,
        EaBuffer,
        FileAttributes,
        SecurityFlags
    );

    // Process the return status if needed
    return status;
}
        

Prerequisites

Building and Testing

To build and test this sample:

  1. Open the solution in Visual Studio.
  2. Configure the build for a kernel-mode driver (e.g., WinDbg target).
  3. Build the driver.
  4. Install and load the driver on a test system. Use tools like WinDbg for debugging.
  5. Monitor driver output and system behavior.

Important Notes