HyperDbg Debugger
Loading...
Searching...
No Matches
Driver.c File Reference

The project entry. More...

#include "pch.h"

Functions

NTSTATUS DriverEntry (PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
 Main Driver Entry in the case of driver load.
 
VOID DrvUnload (PDRIVER_OBJECT DriverObject)
 Run in the case of driver unload to unregister the devices.
 
NTSTATUS DrvCreate (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 IRP_MJ_CREATE Function handler.
 
NTSTATUS DrvRead (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 IRP_MJ_READ Function handler.
 
NTSTATUS DrvWrite (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 IRP_MJ_WRITE Function handler.
 
NTSTATUS DrvClose (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 IRP_MJ_CLOSE Function handler.
 
NTSTATUS DrvUnsupported (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 Unsupported message for all other IRP_MJ_* handlers.
 

Detailed Description

The project entry.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)

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

Function Documentation

◆ DriverEntry()

NTSTATUS DriverEntry ( PDRIVER_OBJECT DriverObject,
PUNICODE_STRING RegistryPath )

Main Driver Entry in the case of driver load.

Load & Unload.

Parameters
DriverObject
RegistryPath
Returns
NTSTATUS
27{
28 NTSTATUS Ntstatus = STATUS_SUCCESS;
29 UINT64 Index = 0;
30 PDEVICE_OBJECT DeviceObject = NULL;
31 UNICODE_STRING DriverName = RTL_CONSTANT_STRING(L"\\Device\\HyperDbgDebuggerDevice");
32 UNICODE_STRING DosDeviceName = RTL_CONSTANT_STRING(L"\\DosDevices\\HyperDbgDebuggerDevice");
33
34 UNREFERENCED_PARAMETER(RegistryPath);
35 UNREFERENCED_PARAMETER(DriverObject);
36
37 //
38 // Opt-in to using non-executable pool memory on Windows 8 and later.
39 // https://msdn.microsoft.com/en-us/library/windows/hardware/hh920402(v=vs.85).aspx
40 //
41 ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
42
43 //
44 // Creating the device for interaction with user-mode
45 //
46 Ntstatus = IoCreateDevice(DriverObject,
47 0,
48 &DriverName,
50 FILE_DEVICE_SECURE_OPEN,
51 FALSE,
52 &DeviceObject);
53
54 if (Ntstatus == STATUS_SUCCESS)
55 {
56 for (Index = 0; Index < IRP_MJ_MAXIMUM_FUNCTION; Index++)
57 DriverObject->MajorFunction[Index] = DrvUnsupported;
58
59 //
60 // We cannot use logging mechanism of HyperDbg as it's not initialized yet
61 //
62 DbgPrint("Setting device major functions");
63
64 DriverObject->MajorFunction[IRP_MJ_CLOSE] = DrvClose;
65 DriverObject->MajorFunction[IRP_MJ_CREATE] = DrvCreate;
66 DriverObject->MajorFunction[IRP_MJ_READ] = DrvRead;
67 DriverObject->MajorFunction[IRP_MJ_WRITE] = DrvWrite;
68 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DrvDispatchIoControl;
69
70 DriverObject->DriverUnload = DrvUnload;
71 IoCreateSymbolicLink(&DosDeviceName, &DriverName);
72 }
73
74 //
75 // Establish user-buffer access method.
76 //
77 DeviceObject->Flags |= DO_BUFFERED_IO;
78
79 //
80 // We cannot use logging mechanism of HyperDbg as it's not initialized yet
81 //
82 DbgPrint("HyperDbg's device and major functions are loaded");
83
84 ASSERT(NT_SUCCESS(Ntstatus));
85 return Ntstatus;
86}
#define FALSE
Definition BasicTypes.h:54
unsigned __int64 UINT64
Definition BasicTypes.h:21
#define FILE_DEVICE_UNKNOWN
Definition Ioctls.h:52
NTSTATUS DrvWrite(PDEVICE_OBJECT DeviceObject, PIRP Irp)
IRP_MJ_WRITE Function handler.
Definition Driver.c:213
NTSTATUS DrvUnsupported(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Unsupported message for all other IRP_MJ_* handlers.
Definition Driver.c:263
NTSTATUS DrvRead(PDEVICE_OBJECT DeviceObject, PIRP Irp)
IRP_MJ_READ Function handler.
Definition Driver.c:189
NTSTATUS DrvClose(PDEVICE_OBJECT DeviceObject, PIRP Irp)
IRP_MJ_CLOSE Function handler.
Definition Driver.c:237
VOID DrvUnload(PDRIVER_OBJECT DriverObject)
Run in the case of driver unload to unregister the devices.
Definition Driver.c:95
NTSTATUS DrvCreate(PDEVICE_OBJECT DeviceObject, PIRP Irp)
IRP_MJ_CREATE Function handler.
Definition Driver.c:117
NTSTATUS DrvDispatchIoControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Driver IOCTL Dispatcher.
Definition Ioctl.c:23
NULL()
Definition test-case-generator.py:530
Definition casting.cpp:25

◆ DrvClose()

NTSTATUS DrvClose ( PDEVICE_OBJECT DeviceObject,
PIRP Irp )

IRP_MJ_CLOSE Function handler.

Parameters
DeviceObject
Irp
Returns
NTSTATUS
238{
239 UNREFERENCED_PARAMETER(DeviceObject);
240
241 //
242 // If the close is called means that all of the IOCTLs
243 // are not in a pending state so we can safely allow
244 // a new handle creation for future calls to the driver
245 //
247
248 Irp->IoStatus.Status = STATUS_SUCCESS;
249 Irp->IoStatus.Information = 0;
250 IoCompleteRequest(Irp, IO_NO_INCREMENT);
251
252 return STATUS_SUCCESS;
253}
BOOLEAN g_HandleInUse
Determines whether the one application gets the handle or not this is used to ensure that only one ap...
Definition Global.h:36

◆ DrvCreate()

NTSTATUS DrvCreate ( PDEVICE_OBJECT DeviceObject,
PIRP Irp )

IRP_MJ_CREATE Function handler.

IRP Major Functions.

Parameters
DeviceObject
Irp
Returns
NTSTATUS
118{
119 UNREFERENCED_PARAMETER(DeviceObject);
120
121 //
122 // Check for privilege
123 //
124 // Check for the correct security access.
125 // The caller must have the SeDebugPrivilege.
126 //
127
128 LUID DebugPrivilege = {SE_DEBUG_PRIVILEGE, 0};
129
130 if (!SeSinglePrivilegeCheck(DebugPrivilege, Irp->RequestorMode))
131 {
132 Irp->IoStatus.Status = STATUS_ACCESS_DENIED;
133 Irp->IoStatus.Information = 0;
134 IoCompleteRequest(Irp, IO_NO_INCREMENT);
135
136 return STATUS_ACCESS_DENIED;
137 }
138
139 //
140 // Check to allow just one handle to the driver
141 // means that only one application can get the handle
142 // and new application won't allowed to create a new
143 // handle unless the IRP_MJ_CLOSE called.
144 //
145 if (g_HandleInUse)
146 {
147 //
148 // A driver got the handle before
149 //
150 Irp->IoStatus.Status = STATUS_SUCCESS;
151 Irp->IoStatus.Information = 0;
152 IoCompleteRequest(Irp, IO_NO_INCREMENT);
153
154 return STATUS_SUCCESS;
155 }
156
157 //
158 // Initialize the vmm and the debugger
159 //
161 {
162 Irp->IoStatus.Status = STATUS_SUCCESS;
163 Irp->IoStatus.Information = 0;
164 IoCompleteRequest(Irp, IO_NO_INCREMENT);
165
166 return STATUS_SUCCESS;
167 }
168 else
169 {
170 //
171 // There was a problem, so not loaded
172 //
173 Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
174 Irp->IoStatus.Information = 0;
175 IoCompleteRequest(Irp, IO_NO_INCREMENT);
176
177 return STATUS_UNSUCCESSFUL;
178 }
179}
#define STATUS_UNSUCCESSFUL
Definition Windows.h:172
BOOLEAN LoaderInitVmmAndDebugger()
Initialize the VMM and Debugger.
Definition Loader.c:19

◆ DrvRead()

NTSTATUS DrvRead ( PDEVICE_OBJECT DeviceObject,
PIRP Irp )

IRP_MJ_READ Function handler.

Parameters
DeviceObject
Irp
Returns
NTSTATUS
190{
191 UNREFERENCED_PARAMETER(DeviceObject);
192
193 //
194 // Not used
195 //
196 DbgPrint("This function is not used");
197
198 Irp->IoStatus.Status = STATUS_SUCCESS;
199 Irp->IoStatus.Information = 0;
200 IoCompleteRequest(Irp, IO_NO_INCREMENT);
201
202 return STATUS_SUCCESS;
203}

◆ DrvUnload()

VOID DrvUnload ( PDRIVER_OBJECT DriverObject)

Run in the case of driver unload to unregister the devices.

Parameters
DriverObject
Returns
VOID
96{
97 UNICODE_STRING DosDeviceName;
98
99 RtlInitUnicodeString(&DosDeviceName, L"\\DosDevices\\HyperDbgDebuggerDevice");
100 IoDeleteSymbolicLink(&DosDeviceName);
101 IoDeleteDevice(DriverObject->DeviceObject);
102
103 //
104 // Unloading VMM and Debugger
105 //
107}
VOID LoaderUninitializeLogTracer()
Uninitialize the log tracer.
Definition Loader.c:129

◆ DrvUnsupported()

NTSTATUS DrvUnsupported ( PDEVICE_OBJECT DeviceObject,
PIRP Irp )

Unsupported message for all other IRP_MJ_* handlers.

Parameters
DeviceObject
Irp
Returns
NTSTATUS
264{
265 UNREFERENCED_PARAMETER(DeviceObject);
266
267 //
268 // Not supported
269 //
270 DbgPrint("This function is not supported");
271
272 Irp->IoStatus.Status = STATUS_SUCCESS;
273 Irp->IoStatus.Information = 0;
274 IoCompleteRequest(Irp, IO_NO_INCREMENT);
275
276 return STATUS_SUCCESS;
277}

◆ DrvWrite()

NTSTATUS DrvWrite ( PDEVICE_OBJECT DeviceObject,
PIRP Irp )

IRP_MJ_WRITE Function handler.

Parameters
DeviceObject
Irp
Returns
NTSTATUS
214{
215 UNREFERENCED_PARAMETER(DeviceObject);
216
217 //
218 // Not used
219 //
220 DbgPrint("This function is not used");
221
222 Irp->IoStatus.Status = STATUS_SUCCESS;
223 Irp->IoStatus.Information = 0;
224 IoCompleteRequest(Irp, IO_NO_INCREMENT);
225
226 return STATUS_SUCCESS;
227}