-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrv.cpp
More file actions
427 lines (335 loc) · 10.4 KB
/
drv.cpp
File metadata and controls
427 lines (335 loc) · 10.4 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <ntifs.h>
#include <windef.h>
UNICODE_STRING name, link;
typedef struct _SYSTEM_BIGPOOL_ENTRY
{
union {
PVOID VirtualAddress;
ULONG_PTR NonPaged : 1;
};
ULONG_PTR SizeInBytes;
union {
UCHAR Tag[4];
ULONG TagUlong;
};
} SYSTEM_BIGPOOL_ENTRY, * PSYSTEM_BIGPOOL_ENTRY;
typedef struct _SYSTEM_BIGPOOL_INFORMATION {
ULONG Count;
SYSTEM_BIGPOOL_ENTRY AllocatedInfo[ANYSIZE_ARRAY];
} SYSTEM_BIGPOOL_INFORMATION, * PSYSTEM_BIGPOOL_INFORMATION;
typedef enum _SYSTEM_INFORMATION_CLASS {
SystemBigPoolInformation = 0x42
} SYSTEM_INFORMATION_CLASS;
extern "C" NTSTATUS NTAPI IoCreateDriver(PUNICODE_STRING DriverName, PDRIVER_INITIALIZE InitializationFunction);
extern "C" PVOID NTAPI PsGetProcessSectionBaseAddress(PEPROCESS Process);
extern "C" NTSTATUS NTAPI ZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS systemInformationClass, PVOID systemInformation, ULONG systemInformationLength, PULONG returnLength);
#define code_rw CTL_CODE(FILE_DEVICE_UNKNOWN, 0x71, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
#define code_ba CTL_CODE(FILE_DEVICE_UNKNOWN, 0x72, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
#define code_get_guarded_region CTL_CODE(FILE_DEVICE_UNKNOWN, 0x73, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
#define code_security 0x85b3e12
#define win_1803 17134
#define win_1809 17763
#define win_1903 18362
#define win_1909 18363
#define win_2004 19041
#define win_20H2 19569
#define win_21H1 20180
#define PAGE_OFFSET_SIZE 12
static const UINT64 PMASK = (~0xfull << 8) & 0xfffffffffull;
typedef struct _rw {
INT32 security;
INT32 process_id;
ULONGLONG address;
ULONGLONG buffer;
ULONGLONG size;
BOOLEAN write;
} rw, * prw;
typedef struct _ba {
INT32 security;
INT32 process_id;
ULONGLONG* address;
} ba, * pba;
typedef struct _ga {
INT32 security;
ULONGLONG* address;
} ga, * pga;
NTSTATUS read(PVOID target_address, PVOID buffer, SIZE_T size, SIZE_T* bytes_read) {
MM_COPY_ADDRESS to_read = { 0 };
to_read.PhysicalAddress.QuadPart = (LONGLONG)target_address;
return MmCopyMemory(buffer, to_read, size, MM_COPY_MEMORY_PHYSICAL, bytes_read);
}
NTSTATUS write(PVOID target_address, PVOID buffer, SIZE_T size, SIZE_T* bytes_read)
{
if (!target_address)
return STATUS_UNSUCCESSFUL;
PHYSICAL_ADDRESS AddrToWrite = { 0 };
AddrToWrite.QuadPart = LONGLONG(target_address);
PVOID pmapped_mem = MmMapIoSpaceEx(AddrToWrite, size, PAGE_READWRITE);
if (!pmapped_mem)
return STATUS_UNSUCCESSFUL;
memcpy(pmapped_mem, buffer, size);
*bytes_read = size;
MmUnmapIoSpace(pmapped_mem, size);
return STATUS_SUCCESS;
}
INT32 get_winver() {
RTL_OSVERSIONINFOW ver = { 0 };
RtlGetVersion(&ver);
switch (ver.dwBuildNumber)
{
case win_1803:
return 0x0278;
break;
case win_1809:
return 0x0278;
break;
case win_1903:
return 0x0280;
break;
case win_1909:
return 0x0280;
break;
case win_2004:
return 0x0388;
break;
case win_20H2:
return 0x0388;
break;
case win_21H1:
return 0x0388;
break;
default:
return 0x0388;
}
}
UINT64 get_process_cr3(const PEPROCESS pProcess) {
PUCHAR process = (PUCHAR)pProcess;
ULONG_PTR process_dirbase = *(PULONG_PTR)(process + 0x28);
if (process_dirbase == 0)
{
INT32 UserDirOffset = get_winver();
ULONG_PTR process_userdirbase = *(PULONG_PTR)(process + UserDirOffset);
return process_userdirbase;
}
return process_dirbase;
}
UINT64 translate_linear(UINT64 directoryTableBase, UINT64 virtualAddress) {
directoryTableBase &= ~0xf;
UINT64 pageOffset = virtualAddress & ~(~0ul << PAGE_OFFSET_SIZE);
UINT64 pte = ((virtualAddress >> 12) & (0x1ffll));
UINT64 pt = ((virtualAddress >> 21) & (0x1ffll));
UINT64 pd = ((virtualAddress >> 30) & (0x1ffll));
UINT64 pdp = ((virtualAddress >> 39) & (0x1ffll));
SIZE_T readsize = 0;
UINT64 pdpe = 0;
read(PVOID(directoryTableBase + 8 * pdp), &pdpe, sizeof(pdpe), &readsize);
if (~pdpe & 1)
return 0;
UINT64 pde = 0;
read(PVOID((pdpe & PMASK) + 8 * pd), &pde, sizeof(pde), &readsize);
if (~pde & 1)
return 0;
/* 1GB large page, use pde's 12-34 bits */
if (pde & 0x80)
return (pde & (~0ull << 42 >> 12)) + (virtualAddress & ~(~0ull << 30));
UINT64 pteAddr = 0;
read(PVOID((pde & PMASK) + 8 * pt), &pteAddr, sizeof(pteAddr), &readsize);
if (~pteAddr & 1)
return 0;
/* 2MB large page */
if (pteAddr & 0x80)
return (pteAddr & PMASK) + (virtualAddress & ~(~0ull << 21));
virtualAddress = 0;
read(PVOID((pteAddr & PMASK) + 8 * pte), &virtualAddress, sizeof(virtualAddress), &readsize);
virtualAddress &= PMASK;
if (!virtualAddress)
return 0;
return virtualAddress + pageOffset;
}
ULONG64 find_min(INT32 g, SIZE_T f) {
INT32 h = (INT32)f;
ULONG64 result = 0;
result = (((g) < (h)) ? (g) : (h));
return result;
}
NTSTATUS frw(prw x) {
if (x->security != code_security)
return STATUS_UNSUCCESSFUL;
if (!x->process_id)
return STATUS_UNSUCCESSFUL;
PEPROCESS process = NULL;
PsLookupProcessByProcessId((HANDLE)x->process_id, &process);
if (!process)
return STATUS_UNSUCCESSFUL;
ULONGLONG process_base = get_process_cr3(process);
ObDereferenceObject(process);
SIZE_T this_offset = NULL;
SIZE_T total_size = x->size;
INT64 physical_address = translate_linear(process_base, (ULONG64)x->address + this_offset);
if (!physical_address)
return STATUS_UNSUCCESSFUL;
ULONG64 final_size = find_min(PAGE_SIZE - (physical_address & 0xFFF), total_size);
SIZE_T bytes_trough = NULL;
if (x->write) {
write(PVOID(physical_address), (PVOID)((ULONG64)x->buffer + this_offset), final_size, &bytes_trough);
}
else {
read(PVOID(physical_address), (PVOID)((ULONG64)x->buffer + this_offset), final_size, &bytes_trough);
}
return STATUS_SUCCESS;
}
NTSTATUS fba(pba x) {
if (x->security != code_security)
return STATUS_UNSUCCESSFUL;
if (!x->process_id)
return STATUS_UNSUCCESSFUL;
PEPROCESS process = NULL;
PsLookupProcessByProcessId((HANDLE)x->process_id, &process);
if (!process)
return STATUS_UNSUCCESSFUL;
ULONGLONG image_base = (ULONGLONG)PsGetProcessSectionBaseAddress(process);
if (!image_base)
return STATUS_UNSUCCESSFUL;
RtlCopyMemory(x->address, &image_base, sizeof(image_base));
ObDereferenceObject(process);
return STATUS_SUCCESS;
}
NTSTATUS fget_guarded_region(pga x) {
if (x->security != code_security)
return STATUS_UNSUCCESSFUL;
ULONG infoLen = 0;
NTSTATUS status = ZwQuerySystemInformation(SystemBigPoolInformation, &infoLen, 0, &infoLen);
PSYSTEM_BIGPOOL_INFORMATION pPoolInfo = 0;
while (status == STATUS_INFO_LENGTH_MISMATCH)
{
if (pPoolInfo)
ExFreePool(pPoolInfo);
pPoolInfo = (PSYSTEM_BIGPOOL_INFORMATION)ExAllocatePool(NonPagedPool, infoLen);
status = ZwQuerySystemInformation(SystemBigPoolInformation, pPoolInfo, infoLen, &infoLen);
}
if (pPoolInfo)
{
for (unsigned int i = 0; i < pPoolInfo->Count; i++)
{
SYSTEM_BIGPOOL_ENTRY* Entry = &pPoolInfo->AllocatedInfo[i];
PVOID VirtualAddress;
VirtualAddress = (PVOID)((uintptr_t)Entry->VirtualAddress & ~1ull);
SIZE_T SizeInBytes = Entry->SizeInBytes;
BOOLEAN NonPaged = Entry->NonPaged;
if (NonPaged && SizeInBytes == 0x200000)
{
if (Entry->TagUlong == 'TnoC') {
RtlCopyMemory((void*)x->address, &VirtualAddress, sizeof(VirtualAddress));
//virtualaddress ist die guarded region address poggers
/*uintptr_t uwuboy = *(uintptr_t*)((PBYTE)VirtualAddress + 0x50);
RtlCopyMemory((void*)args->OutAddress, &uwuboy, sizeof(uwuboy));*/
return STATUS_SUCCESS;
}
}
}
ExFreePool(pPoolInfo);
}
return STATUS_SUCCESS;
}
NTSTATUS io_controller(PDEVICE_OBJECT device_obj, PIRP irp) {
UNREFERENCED_PARAMETER(device_obj);
NTSTATUS status = { };
ULONG bytes = { };
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(irp);
ULONG code = stack->Parameters.DeviceIoControl.IoControlCode;
ULONG size = stack->Parameters.DeviceIoControl.InputBufferLength;
if (code == code_rw) {
if (size == sizeof(_rw)) {
prw req = (prw)(irp->AssociatedIrp.SystemBuffer);
status = frw(req);
bytes = sizeof(_rw);
}
else
{
status = STATUS_INFO_LENGTH_MISMATCH;
bytes = 0;
}
}
else if (code == code_ba) {
if (size == sizeof(_ba)) {
pba req = (pba)(irp->AssociatedIrp.SystemBuffer);
status = fba(req);
bytes = sizeof(_ba);
}
else
{
status = STATUS_INFO_LENGTH_MISMATCH;
bytes = 0;
}
}
else if (code == code_get_guarded_region) {
if (size == sizeof(_ga)) {
pga req = (pga)(irp->AssociatedIrp.SystemBuffer);
status = fget_guarded_region(req);
bytes = sizeof(_ga);
}
else
{
status = STATUS_INFO_LENGTH_MISMATCH;
bytes = 0;
}
}
irp->IoStatus.Status = status;
irp->IoStatus.Information = bytes;
IoCompleteRequest(irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS unsupported_dispatch(PDEVICE_OBJECT device_obj, PIRP irp) {
UNREFERENCED_PARAMETER(device_obj);
irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
IoCompleteRequest(irp, IO_NO_INCREMENT);
return irp->IoStatus.Status;
}
NTSTATUS dispatch_handler(PDEVICE_OBJECT device_obj, PIRP irp) {
UNREFERENCED_PARAMETER(device_obj);
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(irp);
switch (stack->MajorFunction) {
case IRP_MJ_CREATE:
break;
case IRP_MJ_CLOSE:
break;
default:
break;
}
IoCompleteRequest(irp, IO_NO_INCREMENT);
return irp->IoStatus.Status;
}
void unload_drv(PDRIVER_OBJECT drv_obj) {
NTSTATUS status = { };
status = IoDeleteSymbolicLink(&link);
if (!NT_SUCCESS(status))
return;
IoDeleteDevice(drv_obj->DeviceObject);
}
NTSTATUS initialize_driver(PDRIVER_OBJECT drv_obj, PUNICODE_STRING path) {
UNREFERENCED_PARAMETER(path);
NTSTATUS status = { };
PDEVICE_OBJECT device_obj = { };
RtlInitUnicodeString(&name, L"\\Device\\NtGdiDdDDIGetSetSwapChainMetadata");
RtlInitUnicodeString(&link, L"\\DosDevices\\NtGdiDdDDIGetSetSwapChainMetadata");
status = IoCreateDevice(drv_obj, 0, &name, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &device_obj);
if (!NT_SUCCESS(status))
return status;
status = IoCreateSymbolicLink(&link, &name);
if (!NT_SUCCESS(status))
return status;
for (int i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
drv_obj->MajorFunction[i] = &unsupported_dispatch;
device_obj->Flags |= DO_BUFFERED_IO;
drv_obj->MajorFunction[IRP_MJ_CREATE] = &dispatch_handler;
drv_obj->MajorFunction[IRP_MJ_CLOSE] = &dispatch_handler;
drv_obj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = &io_controller;
drv_obj->DriverUnload = &unload_drv;
device_obj->Flags &= ~DO_DEVICE_INITIALIZING;
return status;
}
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath) {
UNREFERENCED_PARAMETER(DriverObject);
UNREFERENCED_PARAMETER(RegistryPath);
return IoCreateDriver(NULL, &initialize_driver);
}