2023-08-17 10:45:50 +02:00
|
|
|
#include "driver.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "ioctl.h"
|
2023-08-20 16:12:04 +02:00
|
|
|
#include "callbacks.h"
|
|
|
|
|
2023-08-21 14:40:40 +02:00
|
|
|
#include "hv.h"
|
|
|
|
|
|
|
|
|
2023-08-20 16:12:04 +02:00
|
|
|
PVOID callback_registration_handle;
|
|
|
|
|
|
|
|
LONG protected_process_id;
|
|
|
|
LONG protected_process_parent_id;
|
|
|
|
KGUARDED_MUTEX mutex;
|
|
|
|
|
2023-08-20 17:04:53 +02:00
|
|
|
UNICODE_STRING DEVICE_NAME = RTL_CONSTANT_STRING( L"\\Device\\DonnaAC" );
|
|
|
|
UNICODE_STRING DEVICE_SYMBOLIC_LINK = RTL_CONSTANT_STRING( L"\\??\\DonnaAC" );
|
|
|
|
|
2023-08-20 16:12:04 +02:00
|
|
|
VOID UpdateProtectedProcessId(
|
|
|
|
_In_ LONG NewProcessId
|
|
|
|
)
|
|
|
|
{
|
|
|
|
KeAcquireGuardedMutex( &mutex );
|
|
|
|
protected_process_id = NewProcessId;
|
|
|
|
KeReleaseGuardedMutex( &mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID GetProtectedProcessId(
|
|
|
|
_Out_ PLONG ProcessId
|
|
|
|
)
|
|
|
|
{
|
|
|
|
KeAcquireGuardedMutex( &mutex );
|
|
|
|
*ProcessId = protected_process_id;
|
|
|
|
KeReleaseGuardedMutex( &mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID GetProtectedProcessParentId(
|
|
|
|
_Out_ PLONG ProcessId
|
|
|
|
)
|
|
|
|
{
|
|
|
|
KeAcquireGuardedMutex( &mutex );
|
|
|
|
*ProcessId = protected_process_parent_id;
|
|
|
|
KeReleaseGuardedMutex( &mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID UpdateProtectedProcessParentId(
|
|
|
|
_In_ LONG NewProcessId
|
|
|
|
)
|
|
|
|
{
|
|
|
|
KeAcquireGuardedMutex( &mutex );
|
|
|
|
protected_process_parent_id = NewProcessId;
|
|
|
|
KeReleaseGuardedMutex( &mutex );
|
|
|
|
}
|
2023-08-17 10:45:50 +02:00
|
|
|
|
|
|
|
VOID DriverUnload(
|
|
|
|
_In_ PDRIVER_OBJECT DriverObject
|
|
|
|
)
|
|
|
|
{
|
2023-08-21 06:08:57 +02:00
|
|
|
PsSetCreateProcessNotifyRoutine( ProcessCreateNotifyRoutine, TRUE );
|
|
|
|
ObUnRegisterCallbacks( callback_registration_handle );
|
2023-08-21 11:45:00 +02:00
|
|
|
FreeQueueObjectsAndCleanup();
|
2023-08-17 10:45:50 +02:00
|
|
|
IoDeleteSymbolicLink( &DEVICE_SYMBOLIC_LINK );
|
2023-08-19 05:36:21 +02:00
|
|
|
IoDeleteDevice( DriverObject->DeviceObject );
|
2023-08-17 10:45:50 +02:00
|
|
|
}
|
|
|
|
|
2023-08-20 18:06:21 +02:00
|
|
|
NTSTATUS InitiateDriverCallbacks()
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
OB_CALLBACK_REGISTRATION callback_registration = { 0 };
|
|
|
|
OB_OPERATION_REGISTRATION operation_registration = { 0 };
|
|
|
|
|
|
|
|
operation_registration.ObjectType = PsProcessType;
|
|
|
|
operation_registration.Operations = OB_OPERATION_HANDLE_CREATE | OB_OPERATION_HANDLE_DUPLICATE;
|
|
|
|
operation_registration.PreOperation = ObPreOpCallbackRoutine;
|
|
|
|
operation_registration.PostOperation = ObPostOpCallbackRoutine;
|
|
|
|
|
|
|
|
callback_registration.Version = OB_FLT_REGISTRATION_VERSION;
|
|
|
|
callback_registration.OperationRegistration = &operation_registration;
|
|
|
|
callback_registration.OperationRegistrationCount = 1;
|
|
|
|
callback_registration.RegistrationContext = NULL;
|
|
|
|
|
|
|
|
status = ObRegisterCallbacks(
|
|
|
|
&callback_registration,
|
|
|
|
&callback_registration_handle
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !NT_SUCCESS( status ) )
|
|
|
|
{
|
|
|
|
DEBUG_ERROR( "failed to launch obregisters with status %x", status );
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = PsSetCreateProcessNotifyRoutine(
|
|
|
|
ProcessCreateNotifyRoutine,
|
|
|
|
FALSE
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !NT_SUCCESS( status ) )
|
|
|
|
DEBUG_ERROR( "Failed to launch ps create notif routines with status %x", status );
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-08-17 10:45:50 +02:00
|
|
|
NTSTATUS DriverEntry(
|
|
|
|
_In_ PDRIVER_OBJECT DriverObject,
|
|
|
|
_In_ PUNICODE_STRING RegistryPath
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER( RegistryPath );
|
|
|
|
|
2023-08-21 11:45:00 +02:00
|
|
|
BOOLEAN flag = FALSE;
|
2023-08-17 10:45:50 +02:00
|
|
|
NTSTATUS status;
|
2023-08-20 18:06:21 +02:00
|
|
|
HANDLE handle;
|
2023-08-17 10:45:50 +02:00
|
|
|
|
|
|
|
status = IoCreateDevice(
|
|
|
|
DriverObject,
|
|
|
|
NULL,
|
|
|
|
&DEVICE_NAME,
|
|
|
|
FILE_DEVICE_UNKNOWN,
|
|
|
|
FILE_DEVICE_SECURE_OPEN,
|
|
|
|
FALSE,
|
|
|
|
&DriverObject->DeviceObject
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !NT_SUCCESS( status ) )
|
|
|
|
return STATUS_FAILED_DRIVER_ENTRY;
|
|
|
|
|
|
|
|
status = IoCreateSymbolicLink(
|
|
|
|
&DEVICE_SYMBOLIC_LINK,
|
|
|
|
&DEVICE_NAME
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !NT_SUCCESS( status ) )
|
|
|
|
{
|
2023-08-20 18:06:21 +02:00
|
|
|
DEBUG_ERROR( "failed to create symbolic link" );
|
2023-08-19 05:36:21 +02:00
|
|
|
IoDeleteDevice( DriverObject->DeviceObject );
|
2023-08-17 10:45:50 +02:00
|
|
|
return STATUS_FAILED_DRIVER_ENTRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
DriverObject->MajorFunction[ IRP_MJ_CREATE ] = DeviceCreate;
|
|
|
|
DriverObject->MajorFunction[ IRP_MJ_CLOSE ] = DeviceClose;
|
|
|
|
DriverObject->MajorFunction[ IRP_MJ_DEVICE_CONTROL ] = DeviceControl;
|
|
|
|
DriverObject->DriverUnload = DriverUnload;
|
|
|
|
|
2023-08-20 16:12:04 +02:00
|
|
|
KeInitializeGuardedMutex( &mutex );
|
|
|
|
|
|
|
|
InitCallbackReportQueue(&flag);
|
|
|
|
|
|
|
|
if ( !flag )
|
|
|
|
{
|
2023-08-20 18:06:21 +02:00
|
|
|
DEBUG_ERROR( "failed to init report queue" );
|
2023-08-20 16:12:04 +02:00
|
|
|
IoDeleteSymbolicLink( &DEVICE_SYMBOLIC_LINK );
|
|
|
|
IoDeleteDevice( DriverObject->DeviceObject );
|
|
|
|
return STATUS_FAILED_DRIVER_ENTRY;
|
|
|
|
}
|
|
|
|
|
2023-08-20 18:06:21 +02:00
|
|
|
status = PsCreateSystemThread(
|
|
|
|
&handle,
|
|
|
|
PROCESS_ALL_ACCESS,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
InitiateDriverCallbacks,
|
|
|
|
NULL
|
2023-08-20 16:12:04 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( !NT_SUCCESS( status ) )
|
|
|
|
{
|
2023-08-20 18:06:21 +02:00
|
|
|
DEBUG_ERROR( "failed to launch thread to start tings" );
|
2023-08-20 16:12:04 +02:00
|
|
|
IoDeleteSymbolicLink( &DEVICE_SYMBOLIC_LINK );
|
|
|
|
IoDeleteDevice( DriverObject->DeviceObject );
|
|
|
|
return STATUS_FAILED_DRIVER_ENTRY;
|
|
|
|
}
|
|
|
|
|
2023-08-20 18:06:21 +02:00
|
|
|
ZwClose( handle );
|
|
|
|
|
2023-08-19 17:12:25 +02:00
|
|
|
DEBUG_LOG( "DonnaAC Driver Entry Complete. type: %lx", DriverObject->DeviceObject->DeviceType );
|
2023-08-17 10:45:50 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|