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

The project entry (RM) 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 (RM)

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.2
Date
2023-01-29

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
26{
27 NTSTATUS Ntstatus = STATUS_SUCCESS;
28 UINT64 Index = 0;
29 PDEVICE_OBJECT DeviceObject = NULL;
30 UNICODE_STRING DriverName = RTL_CONSTANT_STRING(L"\\Device\\HyperDbgReversingMachineDevice");
31 UNICODE_STRING DosDeviceName = RTL_CONSTANT_STRING(L"\\DosDevices\\HyperDbgReversingMachineDevice");
32
33 UNREFERENCED_PARAMETER(RegistryPath);
34 UNREFERENCED_PARAMETER(DriverObject);
35
36 //
37 // Opt-in to using non-executable pool memory on Windows 8 and later.
38 // https://msdn.microsoft.com/en-us/library/windows/hardware/hh920402(v=vs.85).aspx
39 //
40 ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
41
42 //
43 // Creating the device for interaction with user-mode
44 //
45 Ntstatus = IoCreateDevice(DriverObject,
46 0,
47 &DriverName,
49 FILE_DEVICE_SECURE_OPEN,
50 FALSE,
51 &DeviceObject);
52
53 if (Ntstatus == STATUS_SUCCESS)
54 {
55 for (Index = 0; Index < IRP_MJ_MAXIMUM_FUNCTION; Index++)
56 DriverObject->MajorFunction[Index] = DrvUnsupported;
57
58 //
59 // We cannot use logging mechanism of HyperDbg as it's not initialized yet
60 //
61 DbgPrint("Setting device major functions");
62
63 DriverObject->MajorFunction[IRP_MJ_CLOSE] = DrvClose;
64 DriverObject->MajorFunction[IRP_MJ_CREATE] = DrvCreate;
65 DriverObject->MajorFunction[IRP_MJ_READ] = DrvRead;
66 DriverObject->MajorFunction[IRP_MJ_WRITE] = DrvWrite;
67 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DrvDispatchIoControl;
68
69 DriverObject->DriverUnload = DrvUnload;
70 IoCreateSymbolicLink(&DosDeviceName, &DriverName);
71 }
72
73 //
74 // Establish user-buffer access method.
75 //
76 if (DeviceObject != NULL)
77 {
78 DeviceObject->Flags |= DO_BUFFERED_IO;
79 }
80
81 //
82 // We cannot use logging mechanism of HyperDbg as it's not initialized yet
83 //
84 DbgPrint("HyperDbg's device and major functions are loaded");
85
86 ASSERT(NT_SUCCESS(Ntstatus));
87 return Ntstatus;
88}
#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
240{
241 UNREFERENCED_PARAMETER(DeviceObject);
242
243 //
244 // If the close is called means that all of the IOCTLs
245 // are not in a pending state so we can safely allow
246 // a new handle creation for future calls to the driver
247 //
249
250 Irp->IoStatus.Status = STATUS_SUCCESS;
251 Irp->IoStatus.Information = 0;
252 IoCompleteRequest(Irp, IO_NO_INCREMENT);
253
254 return STATUS_SUCCESS;
255}
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
120{
121 UNREFERENCED_PARAMETER(DeviceObject);
122
123 //
124 // Check for privilege
125 //
126 // Check for the correct security access.
127 // The caller must have the SeDebugPrivilege.
128 //
129
130 LUID DebugPrivilege = {SE_DEBUG_PRIVILEGE, 0};
131
132 if (!SeSinglePrivilegeCheck(DebugPrivilege, Irp->RequestorMode))
133 {
134 Irp->IoStatus.Status = STATUS_ACCESS_DENIED;
135 Irp->IoStatus.Information = 0;
136 IoCompleteRequest(Irp, IO_NO_INCREMENT);
137
138 return STATUS_ACCESS_DENIED;
139 }
140
141 //
142 // Check to allow just one handle to the driver
143 // means that only one application can get the handle
144 // and new application won't allowed to create a new
145 // handle unless the IRP_MJ_CLOSE called.
146 //
147 if (g_HandleInUse)
148 {
149 //
150 // A driver got the handle before
151 //
152 Irp->IoStatus.Status = STATUS_SUCCESS;
153 Irp->IoStatus.Information = 0;
154 IoCompleteRequest(Irp, IO_NO_INCREMENT);
155
156 return STATUS_SUCCESS;
157 }
158
159 //
160 // Initialize the vmm and the reversing machine
161 //
163 {
164 Irp->IoStatus.Status = STATUS_SUCCESS;
165 Irp->IoStatus.Information = 0;
166 IoCompleteRequest(Irp, IO_NO_INCREMENT);
167
168 return STATUS_SUCCESS;
169 }
170 else
171 {
172 //
173 // There was a problem, so not loaded
174 //
175 Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
176 Irp->IoStatus.Information = 0;
177 IoCompleteRequest(Irp, IO_NO_INCREMENT);
178
179 return STATUS_UNSUCCESSFUL;
180 }
181}
#define STATUS_UNSUCCESSFUL
Definition Windows.h:172
BOOLEAN LoaderInitVmmAndReversingMachine()
Initialize the VMM and Reversing Machine.
Definition Loader.c:19

◆ DrvRead()

NTSTATUS DrvRead ( PDEVICE_OBJECT DeviceObject,
PIRP Irp )

IRP_MJ_READ Function handler.

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

◆ DrvUnload()

VOID DrvUnload ( PDRIVER_OBJECT DriverObject)

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

Parameters
DriverObject
Returns
VOID
98{
99 UNICODE_STRING DosDeviceName;
100
101 RtlInitUnicodeString(&DosDeviceName, L"\\DosDevices\\HyperDbgReversingMachineDevice");
102 IoDeleteSymbolicLink(&DosDeviceName);
103 IoDeleteDevice(DriverObject->DeviceObject);
104
105 //
106 // Unloading VMM and Debugger
107 //
109}
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
266{
267 UNREFERENCED_PARAMETER(DeviceObject);
268
269 //
270 // Not supported
271 //
272 DbgPrint("This function is not supported");
273
274 Irp->IoStatus.Status = STATUS_SUCCESS;
275 Irp->IoStatus.Information = 0;
276 IoCompleteRequest(Irp, IO_NO_INCREMENT);
277
278 return STATUS_SUCCESS;
279}

◆ DrvWrite()

NTSTATUS DrvWrite ( PDEVICE_OBJECT DeviceObject,
PIRP Irp )

IRP_MJ_WRITE Function handler.

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