HyperDbg/hyperdbg/hyperkd/code/driver/Driver.c

283 lines
6.9 KiB
C
Raw Permalink Normal View History

2020-04-10 06:47:10 -07:00
/**
* @file Driver.c
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
2022-10-17 13:38:56 +09:00
* @brief The project entry
2020-04-10 06:47:10 -07:00
* @details This file contains major functions and all the interactions
* with usermode codes are managed from here.
* e.g debugger commands and extension commands
* @version 0.1
* @date 2020-04-10
2022-10-17 13:38:56 +09:00
*
2020-04-10 06:47:10 -07:00
* @copyright This project is released under the GNU Public License v3.
2022-10-17 13:38:56 +09:00
*
2020-04-10 06:47:10 -07:00
*/
#include "pch.h"
2020-03-24 06:35:02 -07:00
2020-04-10 06:47:10 -07:00
/**
* @brief Main Driver Entry in the case of driver load
2022-10-17 13:38:56 +09:00
*
* @param DriverObject
* @param RegistryPath
* @return NTSTATUS
2020-04-10 06:47:10 -07:00
*/
2020-04-10 00:57:32 -07:00
NTSTATUS
DriverEntry(
PDRIVER_OBJECT DriverObject,
PUNICODE_STRING RegistryPath)
2020-03-24 06:35:02 -07:00
{
2022-03-29 10:19:36 -07:00
NTSTATUS Ntstatus = STATUS_SUCCESS;
UINT64 Index = 0;
PDEVICE_OBJECT DeviceObject = NULL;
2023-01-29 22:51:20 +09:00
UNICODE_STRING DriverName = RTL_CONSTANT_STRING(L"\\Device\\HyperDbgDebuggerDevice");
UNICODE_STRING DosDeviceName = RTL_CONSTANT_STRING(L"\\DosDevices\\HyperDbgDebuggerDevice");
2020-04-10 00:57:32 -07:00
UNREFERENCED_PARAMETER(RegistryPath);
UNREFERENCED_PARAMETER(DriverObject);
2020-04-10 06:47:10 -07:00
//
2020-04-10 00:57:32 -07:00
// Opt-in to using non-executable pool memory on Windows 8 and later.
// https://msdn.microsoft.com/en-us/library/windows/hardware/hh920402(v=vs.85).aspx
2020-04-10 06:47:10 -07:00
//
2020-04-10 00:57:32 -07:00
ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
2020-04-10 06:47:10 -07:00
//
2023-01-15 06:58:56 +09:00
// Creating the device for interaction with user-mode
2022-12-09 11:54:16 +09:00
//
2020-04-10 00:57:32 -07:00
Ntstatus = IoCreateDevice(DriverObject,
0,
&DriverName,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&DeviceObject);
if (Ntstatus == STATUS_SUCCESS)
{
for (Index = 0; Index < IRP_MJ_MAXIMUM_FUNCTION; Index++)
DriverObject->MajorFunction[Index] = DrvUnsupported;
2023-01-15 06:58:56 +09:00
//
// We cannot use logging mechanism of HyperDbg as it's not initialized yet
//
DbgPrint("Setting device major functions");
2020-04-10 00:57:32 -07:00
DriverObject->MajorFunction[IRP_MJ_CLOSE] = DrvClose;
DriverObject->MajorFunction[IRP_MJ_CREATE] = DrvCreate;
DriverObject->MajorFunction[IRP_MJ_READ] = DrvRead;
DriverObject->MajorFunction[IRP_MJ_WRITE] = DrvWrite;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DrvDispatchIoControl;
DriverObject->DriverUnload = DrvUnload;
IoCreateSymbolicLink(&DosDeviceName, &DriverName);
}
2020-04-10 06:47:10 -07:00
//
2020-04-10 00:57:32 -07:00
// Establish user-buffer access method.
2020-04-10 06:47:10 -07:00
//
2020-04-10 00:57:32 -07:00
DeviceObject->Flags |= DO_BUFFERED_IO;
2023-01-15 06:58:56 +09:00
//
// We cannot use logging mechanism of HyperDbg as it's not initialized yet
//
DbgPrint("HyperDbg's device and major functions are loaded");
2023-01-15 06:58:56 +09:00
2020-04-10 00:57:32 -07:00
ASSERT(NT_SUCCESS(Ntstatus));
return Ntstatus;
2020-03-24 06:35:02 -07:00
}
2020-04-10 06:47:10 -07:00
/**
* @brief Run in the case of driver unload to unregister the devices
2022-10-17 13:38:56 +09:00
*
* @param DriverObject
* @return VOID
2020-04-10 06:47:10 -07:00
*/
2020-04-10 00:57:32 -07:00
VOID
DrvUnload(PDRIVER_OBJECT DriverObject)
2020-03-24 06:35:02 -07:00
{
2023-01-15 06:58:56 +09:00
UNICODE_STRING DosDeviceName;
2020-03-24 06:35:02 -07:00
2023-01-29 22:51:20 +09:00
RtlInitUnicodeString(&DosDeviceName, L"\\DosDevices\\HyperDbgDebuggerDevice");
2020-04-10 00:57:32 -07:00
IoDeleteSymbolicLink(&DosDeviceName);
IoDeleteDevice(DriverObject->DeviceObject);
2020-03-24 06:35:02 -07:00
2022-12-09 11:54:16 +09:00
//
2026-05-27 20:21:21 +02:00
// Unloading Log Tracer
2020-04-10 06:47:10 -07:00
//
2026-06-04 00:55:05 +02:00
LoaderUninitLogTracer();
2020-03-24 06:35:02 -07:00
}
2020-04-10 06:47:10 -07:00
/**
* @brief IRP_MJ_CREATE Function handler
2022-10-17 13:38:56 +09:00
*
* @param DeviceObject
* @param Irp
* @return NTSTATUS
2020-04-10 06:47:10 -07:00
*/
2020-04-10 00:57:32 -07:00
NTSTATUS
DrvCreate(PDEVICE_OBJECT DeviceObject, PIRP Irp)
2020-03-24 06:35:02 -07:00
{
2024-03-01 18:11:24 +09:00
UNREFERENCED_PARAMETER(DeviceObject);
2020-04-10 06:47:10 -07:00
//
// Check for privilege
//
// Check for the correct security access.
2020-04-11 08:40:02 -07:00
// The caller must have the SeDebugPrivilege.
2020-04-10 06:47:10 -07:00
//
2020-04-02 06:31:49 -07:00
2020-04-10 00:57:32 -07:00
LUID DebugPrivilege = {SE_DEBUG_PRIVILEGE, 0};
2020-04-02 06:31:49 -07:00
2020-04-10 00:57:32 -07:00
if (!SeSinglePrivilegeCheck(DebugPrivilege, Irp->RequestorMode))
{
Irp->IoStatus.Status = STATUS_ACCESS_DENIED;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2020-04-02 06:31:49 -07:00
2020-04-10 00:57:32 -07:00
return STATUS_ACCESS_DENIED;
}
2020-04-02 06:31:49 -07:00
2020-04-21 07:56:46 -07:00
//
// Check to allow just one handle to the driver
// means that only one application can get the handle
// and new application won't allowed to create a new
// handle unless the IRP_MJ_CLOSE called.
//
if (g_HandleInUse)
{
//
// A driver got the handle before
//
2020-04-27 10:44:15 -07:00
Irp->IoStatus.Status = STATUS_SUCCESS;
2020-04-21 07:56:46 -07:00
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2020-04-27 10:44:15 -07:00
return STATUS_SUCCESS;
2020-04-21 07:56:46 -07:00
}
2020-04-10 06:47:10 -07:00
//
2026-05-27 20:21:21 +02:00
// Initialize HyperLog and Log Tracer
2020-05-12 13:12:21 -07:00
//
2026-05-27 20:21:21 +02:00
if (LoaderInitHyperLog())
{
//
// Set the variable so next CreateFile won't call log initializer again
//
g_HandleInUse = TRUE;
2020-03-24 06:35:02 -07:00
2026-05-27 20:21:21 +02:00
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
else
{
//
// There was a problem, so not loaded
//
Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_UNSUCCESSFUL;
}
2020-03-24 06:35:02 -07:00
}
2020-04-10 06:47:10 -07:00
/**
* @brief IRP_MJ_READ Function handler
2022-10-17 13:38:56 +09:00
*
* @param DeviceObject
* @param Irp
* @return NTSTATUS
2020-04-10 06:47:10 -07:00
*/
2020-04-10 00:57:32 -07:00
NTSTATUS
DrvRead(PDEVICE_OBJECT DeviceObject, PIRP Irp)
2020-03-24 06:35:02 -07:00
{
2024-03-01 18:11:24 +09:00
UNREFERENCED_PARAMETER(DeviceObject);
2023-01-15 06:58:56 +09:00
//
// Not used
//
DbgPrint("This function is not used");
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
return STATUS_SUCCESS;
2020-03-24 06:35:02 -07:00
}
2020-04-10 06:47:10 -07:00
/**
* @brief IRP_MJ_WRITE Function handler
2022-10-17 13:38:56 +09:00
*
* @param DeviceObject
* @param Irp
* @return NTSTATUS
2020-04-10 06:47:10 -07:00
*/
2020-04-10 00:57:32 -07:00
NTSTATUS
DrvWrite(PDEVICE_OBJECT DeviceObject, PIRP Irp)
2020-03-24 06:35:02 -07:00
{
2024-03-01 18:11:24 +09:00
UNREFERENCED_PARAMETER(DeviceObject);
2023-01-15 06:58:56 +09:00
//
// Not used
//
DbgPrint("This function is not used");
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
return STATUS_SUCCESS;
2020-03-24 06:35:02 -07:00
}
2020-04-10 06:47:10 -07:00
/**
* @brief IRP_MJ_CLOSE Function handler
2022-10-17 13:38:56 +09:00
*
* @param DeviceObject
* @param Irp
* @return NTSTATUS
2020-04-10 06:47:10 -07:00
*/
2020-04-10 00:57:32 -07:00
NTSTATUS
DrvClose(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
2024-03-01 18:11:24 +09:00
UNREFERENCED_PARAMETER(DeviceObject);
2020-04-21 07:56:46 -07:00
//
// If the close is called means that all of the IOCTLs
// are not in a pending state so we can safely allow
// a new handle creation for future calls to the driver
//
g_HandleInUse = FALSE;
2020-04-10 00:57:32 -07:00
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
return STATUS_SUCCESS;
2020-03-24 06:35:02 -07:00
}
2020-04-10 06:47:10 -07:00
/**
* @brief Unsupported message for all other IRP_MJ_* handlers
2022-10-17 13:38:56 +09:00
*
* @param DeviceObject
* @param Irp
* @return NTSTATUS
2020-04-10 06:47:10 -07:00
*/
2020-04-10 00:57:32 -07:00
NTSTATUS
DrvUnsupported(PDEVICE_OBJECT DeviceObject, PIRP Irp)
2020-03-24 06:35:02 -07:00
{
2024-03-01 18:11:24 +09:00
UNREFERENCED_PARAMETER(DeviceObject);
2023-01-15 06:58:56 +09:00
//
// Not supported
//
DbgPrint("This function is not supported");
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
2020-03-24 06:35:02 -07:00
2020-04-10 00:57:32 -07:00
return STATUS_SUCCESS;
2020-03-24 06:35:02 -07:00
}