-
Notifications
You must be signed in to change notification settings - Fork 4
/
zero.cpp
361 lines (265 loc) · 8.69 KB
/
zero.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <ntddk.h>
#include <ntintsafe.h>
#ifdef _M_IA64
EXTERN_C
#endif
NTKERNELAPI
int
SystemPrng(
OUT PUCHAR RandomBuffer,
UINT_PTR RandomBufferLength
);
EXTERN_C
NTKERNELAPI
ULONG
RtlRandom(IN OUT PULONG Seed);
EXTERN_C
NTKERNELAPI
ULONG
RtlRandomEx(IN OUT PULONG Seed);
PDEVICE_OBJECT ZeroDev;
PDEVICE_OBJECT RandomDev;
LARGE_INTEGER DriverLoadSystemTime;
#if !defined(NTDDI_WINXP) || NTDDI_VERSION < NTDDI_WINXP
PVOID
MmGetSystemAddressForMdlPrettySafe(PMDL Mdl)
{
CSHORT MdlMappingCanFail;
PVOID MappedSystemVa;
if (Mdl == NULL)
return NULL;
if (Mdl->MdlFlags & (MDL_MAPPED_TO_SYSTEM_VA | MDL_SOURCE_IS_NONPAGED_POOL))
return Mdl->MappedSystemVa;
MdlMappingCanFail = Mdl->MdlFlags & MDL_MAPPING_CAN_FAIL;
Mdl->MdlFlags |= MDL_MAPPING_CAN_FAIL;
MappedSystemVa = MmMapLockedPages(Mdl, KernelMode);
if (MdlMappingCanFail == 0)
Mdl->MdlFlags &= ~MDL_MAPPING_CAN_FAIL;
return MappedSystemVa;
}
#ifdef MmGetSystemAddressForMdlSafe
#undef MmGetSystemAddressForMdlSafe
#endif
#define MmGetSystemAddressForMdlSafe(Mdl, Priority) MmGetSystemAddressForMdlPrettySafe((Mdl))
#ifdef RtlRandomEx
#undef RtlRandomEx
#endif
#define RtlRandomEx RtlRandom
#endif
#if !defined(NTDDI_VISTA) || NTDDI_VERSION < NTDDI_VISTA
void
FillRandom(PUCHAR buffer, SIZE_T size)
{
PUCHAR ptr = buffer;
for (;
ptr <= buffer + size - sizeof(USHORT);
ptr += sizeof(USHORT))
{
ULONG rnd = RtlRandomEx(&DriverLoadSystemTime.LowPart);
*(PUSHORT)ptr = HIWORD(rnd) ^ LOWORD(rnd);
}
for (;
ptr < buffer + size;
ptr++)
{
ULONG rnd = RtlRandomEx(&DriverLoadSystemTime.LowPart);
*ptr = (UCHAR)(HIWORD(rnd) ^ LOWORD(rnd));
}
}
#define SystemPrng(buffer, size) FillRandom(buffer, size)
#endif
///////
EXTERN_C
NTSTATUS
DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);
NTSTATUS
ZeroDispatchCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS
ZeroDispatchRead(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS
ZeroDispatchWrite(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS
ZeroDispatchQueryInformation(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS
ZeroDispatchSetInformation(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
VOID
ZeroUnload(IN PDRIVER_OBJECT DriverObject);
#pragma code_seg("INIT")
NTSTATUS
DriverEntry(IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath)
{
NTSTATUS status;
UNICODE_STRING device_name;
UNICODE_STRING link_name;
UNREFERENCED_PARAMETER(RegistryPath);
KeQuerySystemTime(&DriverLoadSystemTime);
RtlInitUnicodeString(&device_name, L"\\Device\\Zero");
status = IoCreateDevice(DriverObject, 0, &device_name, FILE_DEVICE_NULL, 0,
FALSE, &ZeroDev);
if (!NT_SUCCESS(status))
ZeroDev = NULL;
else
ZeroDev->Flags |= DO_DIRECT_IO;
RtlInitUnicodeString(&link_name, L"\\DosDevices\\Zero");
IoCreateUnprotectedSymbolicLink(&link_name, &device_name);
RtlInitUnicodeString(&device_name, L"\\Device\\Random");
status = IoCreateDevice(DriverObject, 0, &device_name, FILE_DEVICE_NULL, 0,
FALSE, &RandomDev);
if (!NT_SUCCESS(status))
RandomDev = NULL;
else
RandomDev->Flags |= DO_DIRECT_IO;
if ((ZeroDev == NULL) && (RandomDev == NULL))
return status;
RtlInitUnicodeString(&link_name, L"\\DosDevices\\Random");
IoCreateUnprotectedSymbolicLink(&link_name, &device_name);
DriverObject->MajorFunction[IRP_MJ_CREATE] = ZeroDispatchCreateClose;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = ZeroDispatchCreateClose;
DriverObject->MajorFunction[IRP_MJ_READ] = ZeroDispatchRead;
DriverObject->MajorFunction[IRP_MJ_WRITE] = ZeroDispatchWrite;
DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] =
ZeroDispatchQueryInformation;
DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] =
ZeroDispatchSetInformation;
DriverObject->DriverUnload = ZeroUnload;
return STATUS_SUCCESS;
}
#pragma code_seg("PAGE")
VOID
ZeroUnload(IN PDRIVER_OBJECT DriverObject)
{
PDEVICE_OBJECT device_object;
UNICODE_STRING link_name;
PAGED_CODE();
RtlInitUnicodeString(&link_name, L"\\DosDevices\\Zero");
IoDeleteSymbolicLink(&link_name);
RtlInitUnicodeString(&link_name, L"\\DosDevices\\Random");
IoDeleteSymbolicLink(&link_name);
while ((device_object = DriverObject->DeviceObject) != NULL)
IoDeleteDevice(device_object);
}
#pragma code_seg()
NTSTATUS
ZeroDispatchCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
UNREFERENCED_PARAMETER(DeviceObject);
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS
ZeroDispatchRead(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
PIO_STACK_LOCATION io_stack = IoGetCurrentIrpStackLocation(Irp);
if (io_stack->Parameters.Read.Length == 0 ||
Irp->MdlAddress == NULL)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_DISK_INCREMENT);
return STATUS_SUCCESS;
}
PVOID system_buffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, LowPagePriority);
if (system_buffer == NULL)
{
Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_INSUFFICIENT_RESOURCES;
}
if (DeviceObject == RandomDev)
{
SystemPrng((PUCHAR)system_buffer, io_stack->Parameters.Read.Length);
}
else // if (DeviceObject == ZeroDev)
{
RtlZeroMemory(system_buffer, io_stack->Parameters.Read.Length);
}
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = io_stack->Parameters.Read.Length;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS
ZeroDispatchWrite(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
UNREFERENCED_PARAMETER(DeviceObject);
PIO_STACK_LOCATION io_stack = IoGetCurrentIrpStackLocation(Irp);
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = io_stack->Parameters.Write.Length;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS
ZeroDispatchQueryInformation(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
UNREFERENCED_PARAMETER(DeviceObject);
PIO_STACK_LOCATION io_stack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status;
switch (io_stack->Parameters.QueryFile.FileInformationClass)
{
case FileStandardInformation:
{
PFILE_STANDARD_INFORMATION file_standard_information =
(PFILE_STANDARD_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
if (io_stack->Parameters.QueryFile.Length <
sizeof(FILE_STANDARD_INFORMATION))
{
status = STATUS_INVALID_PARAMETER;
break;
}
file_standard_information->AllocationSize.QuadPart = 0;
file_standard_information->EndOfFile.QuadPart = 0;
file_standard_information->NumberOfLinks = 0;
file_standard_information->DeletePending = FALSE;
file_standard_information->Directory = FALSE;
Irp->IoStatus.Information = sizeof(FILE_STANDARD_INFORMATION);
status = STATUS_SUCCESS;
break;
}
case FilePositionInformation:
{
PFILE_POSITION_INFORMATION file_position_information =
(PFILE_POSITION_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
if (io_stack->Parameters.QueryFile.Length <
sizeof(FILE_POSITION_INFORMATION))
{
status = STATUS_INVALID_PARAMETER;
break;
}
file_position_information->CurrentByteOffset.QuadPart = 0;
Irp->IoStatus.Information = sizeof(FILE_POSITION_INFORMATION);
status = STATUS_SUCCESS;
break;
}
default:
status = STATUS_INVALID_DEVICE_REQUEST;
}
if (!NT_SUCCESS(status))
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS
ZeroDispatchSetInformation(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
UNREFERENCED_PARAMETER(DeviceObject);
PIO_STACK_LOCATION io_stack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status;
switch (io_stack->Parameters.SetFile.FileInformationClass)
{
case FileEndOfFileInformation:
case FileAllocationInformation:
status = STATUS_SUCCESS;
break;
default:
status = STATUS_INVALID_DEVICE_REQUEST;
}
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}