mirror-ac/driver/ioctl.c

412 lines
10 KiB
C
Raw Normal View History

2023-08-17 10:45:50 +02:00
#include "ioctl.h"
2023-08-19 04:52:57 +02:00
#include "modules.h"
2023-08-20 16:12:04 +02:00
#include "driver.h"
2023-08-20 17:04:53 +02:00
#include "callbacks.h"
2023-08-26 14:07:06 +02:00
#include "pool.h"
2023-08-22 19:32:25 +02:00
#include "integrity.h"
2023-08-30 13:15:57 +02:00
#include "thread.h"
2023-09-02 10:54:04 +02:00
#include "queue.h"
2023-08-21 14:40:40 +02:00
#include "hv.h"
2023-10-08 06:24:54 +02:00
STATIC
NTSTATUS
DispatchApcOperation(
_In_ PAPC_OPERATION_ID Operation);
2023-10-06 13:08:30 +02:00
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, DispatchApcOperation)
#pragma alloc_text(PAGE, DeviceControl)
#pragma alloc_text(PAGE, DeviceClose)
#pragma alloc_text(PAGE, DeviceCreate)
#endif
2023-10-11 18:05:29 +02:00
#define IOCCTL_RUN_NMI_CALLBACKS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20001, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_VALIDATE_DRIVER_OBJECTS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20002, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_NOTIFY_DRIVER_ON_PROCESS_LAUNCH CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20004, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_HANDLE_REPORTS_IN_CALLBACK_QUEUE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20005, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_PERFORM_VIRTUALIZATION_CHECK CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20006, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_ENUMERATE_HANDLE_TABLES CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20007, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_RETRIEVE_MODULE_EXECUTABLE_REGIONS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20008, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_REQUEST_TOTAL_MODULE_SIZE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20009, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_NOTIFY_DRIVER_ON_PROCESS_TERMINATION CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20010, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_SCAN_FOR_UNLINKED_PROCESS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20011, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_VALIDATE_KPRCB_CURRENT_THREAD CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20012, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_PERFORM_INTEGRITY_CHECK CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20013, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_DETECT_ATTACHED_THREADS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20014, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_VALIDATE_PROCESS_LOADED_MODULE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20015, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_REQUEST_HARDWARE_INFORMATION CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20016, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_INITIATE_APC_OPERATION CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20017, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_CHECK_FOR_EPT_HOOK CTL_CODE(FILE_DEVICE_UNKNOWN, 0x20018, METHOD_BUFFERED, FILE_ANY_ACCESS)
2023-09-28 18:10:01 +02:00
#define APC_OPERATION_STACKWALK 0x1
STATIC
2023-10-05 08:27:17 +02:00
NTSTATUS
2023-10-08 06:24:54 +02:00
DispatchApcOperation(
_In_ PAPC_OPERATION_ID Operation
)
2023-09-28 18:10:01 +02:00
{
2023-10-09 20:19:51 +02:00
PAGED_CODE();
2023-10-03 17:23:01 +02:00
NTSTATUS status;
2023-09-28 18:10:01 +02:00
2023-10-05 08:27:17 +02:00
switch (Operation->operation_id)
2023-09-28 18:10:01 +02:00
{
case APC_OPERATION_STACKWALK:
2023-10-05 08:27:17 +02:00
DEBUG_LOG("Initiating APC stackwalk operation with operation id %i", Operation->operation_id);
2023-09-28 18:10:01 +02:00
status = ValidateThreadsViaKernelApc();
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("ValidateThreadsViaKernelApc failed with status %x", status);
2023-09-28 18:10:01 +02:00
return status;
default:
2023-10-05 08:27:17 +02:00
DEBUG_ERROR("Invalid operation ID passed");
2023-09-28 18:10:01 +02:00
return STATUS_INVALID_PARAMETER;
}
return status;
}
2023-09-27 15:10:12 +02:00
2023-10-10 19:49:17 +02:00
//_Dispatch_type_(IRP_MJ_SYSTEM_CONTROL)
2023-10-05 08:27:17 +02:00
NTSTATUS
2023-09-27 06:22:14 +02:00
DeviceControl(
2023-08-17 10:45:50 +02:00
_In_ PDRIVER_OBJECT DriverObject,
2023-10-07 17:37:47 +02:00
_Inout_ PIRP Irp
2023-08-17 10:45:50 +02:00
)
{
2023-10-05 08:27:17 +02:00
UNREFERENCED_PARAMETER(DriverObject);
2023-10-09 20:19:51 +02:00
PAGED_CODE();
2023-08-19 04:52:57 +02:00
NTSTATUS status = STATUS_SUCCESS;
2023-10-05 08:27:17 +02:00
PIO_STACK_LOCATION stack_location = IoGetCurrentIrpStackLocation(Irp);
2023-08-19 04:52:57 +02:00
HANDLE handle;
2023-10-09 18:27:04 +02:00
PKTHREAD thread = NULL;
2023-08-24 17:10:40 +02:00
BOOLEAN security_flag = FALSE;
2023-10-11 18:05:29 +02:00
DEBUG_LOG("IOCTL Code: %lx", stack_location->Parameters.DeviceIoControl.IoControlCode);
2023-08-24 17:10:40 +02:00
/*
2023-10-09 18:27:04 +02:00
* LMAO
2023-08-24 17:10:40 +02:00
*/
2023-10-10 19:49:17 +02:00
//ReadProcessInitialisedConfigFlag(&security_flag);
2023-08-24 17:10:40 +02:00
2023-10-10 19:49:17 +02:00
//if (security_flag == FALSE &&
// stack_location->Parameters.DeviceIoControl.IoControlCode != IOCTL_NOTIFY_DRIVER_ON_PROCESS_LAUNCH)
//{
// status = STATUS_ACCESS_DENIED;
// goto end;
//}
2023-08-19 04:52:57 +02:00
2023-10-05 08:27:17 +02:00
switch (stack_location->Parameters.DeviceIoControl.IoControlCode)
2023-08-19 04:52:57 +02:00
{
case IOCCTL_RUN_NMI_CALLBACKS:
2023-10-05 08:27:17 +02:00
status = HandleNmiIOCTL(Irp);
2023-08-19 04:52:57 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("RunNmiCallbacks failed with status %lx", status);
2023-08-19 04:52:57 +02:00
break;
case IOCTL_VALIDATE_DRIVER_OBJECTS:
/*
* The reason this function is run in a new thread and not the thread
* issuing the IOCTL is because ZwOpenDirectoryObject issues a
* user mode handle if called on the user mode thread calling DeviceIoControl.
* This is a problem because when we pass said handle to ObReferenceObjectByHandle
* it will issue a bug check under windows driver verifier.
*/
2023-08-20 07:46:02 +02:00
2023-08-19 04:52:57 +02:00
status = PsCreateSystemThread(
&handle,
PROCESS_ALL_ACCESS,
NULL,
NULL,
NULL,
HandleValidateDriversIOCTL,
Irp
);
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
2023-08-19 08:06:51 +02:00
{
2023-10-05 08:27:17 +02:00
DEBUG_ERROR("Failed to start thread to validate system drivers");
2023-08-19 08:06:51 +02:00
goto end;
}
/*
* Thread objects are a type of dispatcher object, meaning when they are freed
* its set to the signal state and any waiters will be signalled. This allows
* us to wait til our threads terminated and the IRP buffer has been either filled
* or left empty and then from there we can complete the IRP and return.
*/
status = ObReferenceObjectByHandle(
handle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
&thread,
NULL
);
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
2023-08-19 08:06:51 +02:00
{
2023-10-05 08:27:17 +02:00
DEBUG_ERROR("ObReferenceObjectbyhandle failed with status %lx", status);
ZwClose(handle);
2023-08-19 08:06:51 +02:00
goto end;
}
2023-10-05 08:27:17 +02:00
KeWaitForSingleObject(thread, Executive, KernelMode, FALSE, NULL);
2023-08-21 11:45:00 +02:00
2023-10-05 08:27:17 +02:00
ZwClose(handle);
ObDereferenceObject(thread);
2023-08-19 08:06:51 +02:00
2023-08-19 06:37:53 +02:00
break;
2023-08-19 04:52:57 +02:00
2023-08-20 16:12:04 +02:00
case IOCTL_NOTIFY_DRIVER_ON_PROCESS_LAUNCH:;
2023-10-09 18:27:04 +02:00
status = ProcLoadInitialiseProcessConfig(Irp);
2023-08-24 15:12:49 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
2023-08-24 15:12:49 +02:00
{
2023-10-05 08:27:17 +02:00
DEBUG_ERROR("Failed to initialise driver config on proc launch with status %x", status);
2023-08-24 15:12:49 +02:00
goto end;
}
2023-10-09 18:27:04 +02:00
status = ProcLoadEnableObCallbacks();
2023-08-24 15:12:49 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("InitiateDriverCallbacks failed with status %x", status);
2023-08-20 16:12:04 +02:00
break;
2023-08-20 17:04:53 +02:00
case IOCTL_HANDLE_REPORTS_IN_CALLBACK_QUEUE:
2023-09-27 06:22:14 +02:00
status = QueryActiveApcContextsForCompletion();
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("QueryActiveApcContextsForCompletion filed with status %x", status);
2023-09-27 06:22:14 +02:00
2023-09-02 10:54:04 +02:00
status = HandlePeriodicGlobalReportQueueQuery(Irp);
2023-08-20 17:04:53 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("Failed to handle period callback report queue");
2023-08-20 17:04:53 +02:00
break;
2023-08-20 16:12:04 +02:00
2023-08-21 17:48:34 +02:00
case IOCTL_PERFORM_VIRTUALIZATION_CHECK:
2023-10-05 08:27:17 +02:00
status = PerformVirtualizationDetection(Irp);
2023-08-21 17:48:34 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("PerformVirtualizationDetection failed with status %x", status);
2023-08-21 17:48:34 +02:00
break;
2023-08-22 10:51:52 +02:00
case IOCTL_ENUMERATE_HANDLE_TABLES:
2023-10-05 08:27:17 +02:00
2023-08-22 10:51:52 +02:00
/* can maybe implement this better so we can extract a status value */
2023-10-10 19:49:17 +02:00
EnumerateProcessListWithCallbackRoutine(
2023-09-13 12:06:25 +02:00
EnumerateProcessHandles,
NULL
2023-08-22 10:51:52 +02:00
);
break;
2023-08-22 19:32:25 +02:00
case IOCTL_RETRIEVE_MODULE_EXECUTABLE_REGIONS:
2023-08-31 18:42:38 +02:00
status = PsCreateSystemThread(
&handle,
PROCESS_ALL_ACCESS,
NULL,
NULL,
NULL,
RetrieveInMemoryModuleExecutableSections,
Irp
);
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
2023-08-31 18:42:38 +02:00
{
2023-10-05 08:27:17 +02:00
DEBUG_ERROR("Failed to start system thread to get executable regions");
2023-08-31 18:42:38 +02:00
goto end;
}
status = ObReferenceObjectByHandle(
handle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
&thread,
NULL
);
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
2023-08-31 18:42:38 +02:00
{
2023-10-05 08:27:17 +02:00
DEBUG_ERROR("ObReferenceObjectbyhandle failed with status %lx", status);
ZwClose(handle);
2023-08-31 18:42:38 +02:00
goto end;
}
2023-10-05 08:27:17 +02:00
KeWaitForSingleObject(thread, Executive, KernelMode, FALSE, NULL);;
2023-08-31 18:42:38 +02:00
2023-10-05 08:27:17 +02:00
ZwClose(handle);
ObDereferenceObject(thread);
2023-08-31 18:42:38 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("Failed to retrieve executable regions");
2023-08-22 19:32:25 +02:00
break;
2023-08-23 14:14:20 +02:00
case IOCTL_REQUEST_TOTAL_MODULE_SIZE:
2023-10-05 08:27:17 +02:00
status = GetDriverImageSize(Irp);
2023-08-23 14:14:20 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("Failed to retrieve driver image size");
2023-08-23 14:14:20 +02:00
break;
2023-08-24 17:10:40 +02:00
case IOCTL_NOTIFY_DRIVER_ON_PROCESS_TERMINATION:
2023-08-24 15:12:49 +02:00
2023-10-09 18:27:04 +02:00
ProcCloseClearProcessConfiguration();
ProcCloseDisableObCallbacks();
2023-08-24 15:12:49 +02:00
break;
2023-08-28 17:00:52 +02:00
case IOCTL_SCAN_FOR_UNLINKED_PROCESS:
2023-10-05 08:27:17 +02:00
status = FindUnlinkedProcesses(Irp);
2023-08-28 17:00:52 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("FindUNlinekdProcesses failed with status %x", status);
2023-08-28 17:00:52 +02:00
break;
2023-08-30 13:15:57 +02:00
case IOCTL_VALIDATE_KPRCB_CURRENT_THREAD:
2023-10-05 08:27:17 +02:00
ValidateKPCRBThreads(Irp);
2023-08-30 13:15:57 +02:00
break;
2023-09-01 13:46:31 +02:00
case IOCTL_PERFORM_INTEGRITY_CHECK:
status = VerifyInMemoryImageVsDiskImage();
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("VerifyInMemoryImageVsDisk failed with status %x", status);
2023-09-01 13:46:31 +02:00
break;
2023-09-02 15:47:15 +02:00
case IOCTL_DETECT_ATTACHED_THREADS:
DetectThreadsAttachedToProtectedProcess();
break;
2023-09-05 11:16:32 +02:00
case IOCTL_VALIDATE_PROCESS_LOADED_MODULE:
2023-10-05 08:27:17 +02:00
status = ValidateProcessLoadedModule(Irp);
2023-09-05 11:16:32 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("ValidateProcessLoadedModule failed with status %x", status);
2023-09-05 11:16:32 +02:00
break;
2023-09-07 19:49:36 +02:00
case IOCTL_REQUEST_HARDWARE_INFORMATION:;
PSYSTEM_INFORMATION system_information = NULL;
2023-10-05 08:27:17 +02:00
GetDriverConfigSystemInformation(&system_information);
2023-09-07 19:49:36 +02:00
2023-10-05 08:27:17 +02:00
if (system_information == NULL)
2023-09-07 19:49:36 +02:00
{
2023-10-05 08:27:17 +02:00
DEBUG_ERROR("GetDriverConfigSystemInformation failed");
2023-09-07 19:49:36 +02:00
goto end;
}
2023-10-05 08:27:17 +02:00
Irp->IoStatus.Information = sizeof(SYSTEM_INFORMATION);
2023-09-07 19:49:36 +02:00
RtlCopyMemory(
Irp->AssociatedIrp.SystemBuffer,
system_information,
2023-10-05 08:27:17 +02:00
sizeof(SYSTEM_INFORMATION)
2023-09-07 19:49:36 +02:00
);
break;
2023-09-28 18:10:01 +02:00
case IOCTL_INITIATE_APC_OPERATION:;
PAPC_OPERATION_ID operation = (PAPC_OPERATION_ID)Irp->AssociatedIrp.SystemBuffer;
2023-10-05 08:27:17 +02:00
status = DispatchApcOperation(operation);
2023-09-28 18:10:01 +02:00
2023-10-05 08:27:17 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("DispatchApcOperation failed with status %x", status);
2023-09-28 18:10:01 +02:00
break;
2023-10-06 09:02:10 +02:00
case IOCTL_CHECK_FOR_EPT_HOOK:
2023-10-06 10:30:14 +02:00
status = DetectEptHooksInKeyFunctions();
2023-10-06 09:02:10 +02:00
if (!NT_SUCCESS(status))
DEBUG_ERROR("DetectEpthooksInKeyFunctions failed with status %x", status);
break;
2023-09-28 18:10:01 +02:00
2023-08-19 04:52:57 +02:00
default:
2023-10-11 18:05:29 +02:00
DEBUG_ERROR("Invalid IOCTL passed to driver: %lx", stack_location->Parameters.DeviceIoControl.IoControlCode);
2023-10-03 17:23:01 +02:00
status = STATUS_INVALID_PARAMETER;
2023-08-19 04:52:57 +02:00
break;
}
2023-08-19 08:06:51 +02:00
end:
2023-08-19 04:52:57 +02:00
Irp->IoStatus.Status = status;
2023-10-05 08:27:17 +02:00
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2023-08-19 04:52:57 +02:00
return status;
2023-08-17 10:45:50 +02:00
}
2023-10-10 15:52:42 +02:00
_Dispatch_type_(IRP_MJ_CLOSE)
2023-10-05 08:27:17 +02:00
NTSTATUS
2023-09-27 06:22:14 +02:00
DeviceClose(
2023-08-17 10:45:50 +02:00
_In_ PDEVICE_OBJECT DeviceObject,
2023-10-07 17:37:47 +02:00
_Inout_ PIRP Irp
2023-08-17 10:45:50 +02:00
)
{
2023-10-10 15:52:42 +02:00
PAGED_CODE();
2023-10-07 17:37:47 +02:00
UNREFERENCED_PARAMETER(DeviceObject);
2023-10-05 08:27:17 +02:00
DEBUG_LOG("Handle closed to DonnaAC");
2023-08-30 15:23:04 +02:00
2023-09-18 19:12:22 +02:00
/*
* For now its fine, but this will need to be moved to our process load callbacks
* since right now anyone can open a handle to our driver and then close it lol
*/
2023-09-29 05:56:44 +02:00
/* we also lose reports here, so sohuld pass em into the irp before freeing */
2023-09-02 10:54:04 +02:00
FreeGlobalReportQueueObjects();
2023-10-09 18:27:04 +02:00
ProcCloseClearProcessConfiguration();
ProcCloseDisableObCallbacks();
2023-08-30 15:23:04 +02:00
2023-10-05 08:27:17 +02:00
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2023-08-17 10:45:50 +02:00
return Irp->IoStatus.Status;
}
2023-10-10 15:52:42 +02:00
_Dispatch_type_(IRP_MJ_CREATE)
2023-10-05 08:27:17 +02:00
NTSTATUS
2023-09-27 06:22:14 +02:00
DeviceCreate(
2023-08-17 10:45:50 +02:00
_In_ PDEVICE_OBJECT DeviceObject,
2023-10-07 17:37:47 +02:00
_Inout_ PIRP Irp
2023-08-17 10:45:50 +02:00
)
{
2023-10-09 20:19:51 +02:00
PAGED_CODE();
2023-10-05 08:27:17 +02:00
DEBUG_LOG("Handle opened to DonnaAC");
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2023-08-17 10:45:50 +02:00
return Irp->IoStatus.Status;
}