-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathKernelMode.cpp
More file actions
485 lines (413 loc) · 11.7 KB
/
KernelMode.cpp
File metadata and controls
485 lines (413 loc) · 11.7 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#define SizeAlign(Size) ((Size + 0xFFF) & 0xFFFFFFFFFFFFF000)
//
// structs
//
#pragma pack(push, 1)
typedef struct _BUFFER_STATS
{
volatile LONG64 LostLogCount;
volatile LONG64 ReadLogCount;
volatile LONG64 WrittenLogCount;
volatile LONG64 OverwrittenCount;
volatile LONG64 TooBigErrorCount;
volatile LONG64 TryZeroWriteCount;
volatile LONG64 MissingDataCount;
volatile LONG64 WrapedCount;
} BUFFER_STATS, * PBUFFER_STATS;
typedef struct _BUFFER_HEADER
{
volatile LONG WriteOffset;
volatile LONG ReadOffset;
SIZE_T BufferSize;
BUFFER_STATS Stats;
} BUFFER_HEADER, * PBUFFER_HEADER;
typedef struct _SHARED_MEMORY
{
BUFFER_HEADER Header;
UCHAR Data[1];
} SHARED_MEMORY, * PSHARED_MEMORY;
typedef struct _RING_BUFFER_CONTEXT
{
PSHARED_MEMORY SharedMemoryKernelVA;
PSHARED_MEMORY SharedMemoryUserVA;
SIZE_T TotalSize;
} RING_BUFFER_CONTEXT, * PRING_BUFFER_CONTEXT;
typedef struct _RING_BUFFER_CONTEXTS
{
SIZE_T ProcessorCount;
RING_BUFFER_CONTEXT ProcessorBuffer[32];
} RING_BUFFER_CONTEXTS, * PRING_BUFFER_CONTEXTS;
typedef struct _LOG_ENTRY
{
UINT64 Timestamp;
UINT64 Index;
UINT32 Processor;
UINT32 LogLevel;
UINT32 Length;
UINT32 Remarks;
CHAR Data[1];
} LogEntry, * PLOG_ENTRY;
typedef struct _SHARED_MEMORY_CONTEXT
{
PVOID KernelVirtualAddress;
PVOID UserVirtualAddress;
SIZE_T Size;
PMDL Mdl;
HANDLE ProcessId;
} SHARED_MEMORY_CONTEXT, * PSHARED_MEMORY_CONTEXT;
#pragma pack(pop)
//
// variables
//
extern PRING_BUFFER_CONTEXTS g_LoggerMemorys;
extern volatile LONG64 g_GlobalIndexNumber;
//
// functions
//
extern NTSTATUS LogMessage(
_In_ PCHAR Format,
...
);
extern NTSTATUS SendData(
_In_ UINT32 Type,
_In_ UINT32 Remarks,
_In_ PUCHAR Buffer,
_In_ SIZE_T Len
);
extern NTSTATUS CreateSharedMemory(
_In_ HANDLE ProcessId,
_Out_ PSHARED_MEMORY_CONTEXT Context
);
extern NTSTATUS InitializeRingBufferEx(
_Out_ PRING_BUFFER_CONTEXTS* Context,
_In_ SIZE_T BufferSize,
_Out_ PRING_BUFFER_CONTEXTS* UserVa
);
// CreateSharedMemory
// ProcessId: The process id
// Context: The shared memory context will be stored here
NTSTATUS
CreateSharedMemory(
_In_ HANDLE ProcessId,
_Out_ PSHARED_MEMORY_CONTEXT Context
)
{
NTSTATUS status;
PHYSICAL_ADDRESS lowAddress, highAddress;
lowAddress.QuadPart = 0;
highAddress.QuadPart = MAXLONGLONG;
// Allocate memory from NonPaged pool
Context->KernelVirtualAddress = MmAllocateContiguousMemorySpecifyCache(
Context->Size,
lowAddress,
highAddress,
lowAddress,
MmCached);
if (!Context->KernelVirtualAddress)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
// Create and build MDL
Context->Mdl = IoAllocateMdl(
Context->KernelVirtualAddress,
(ULONG)Context->Size,
FALSE,
FALSE,
NULL);
if (!Context->Mdl)
{
MmFreeContiguousMemorySpecifyCache(
Context->KernelVirtualAddress,
Context->Size,
MmCached);
Context->KernelVirtualAddress = NULL;
return STATUS_INSUFFICIENT_RESOURCES;
}
MmBuildMdlForNonPagedPool(Context->Mdl);
// Map the memory for user mode access
__try
{
Context->UserVirtualAddress = MmMapLockedPagesSpecifyCache(
Context->Mdl,
UserMode,
MmCached,
NULL,
FALSE,
NormalPagePriority);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
IoFreeMdl(Context->Mdl);
Context->Mdl = NULL;
MmFreeContiguousMemorySpecifyCache(
Context->KernelVirtualAddress,
Context->Size,
MmCached);
Context->KernelVirtualAddress = NULL;
return STATUS_UNSUCCESSFUL;
}
Context->ProcessId = ProcessId;
return STATUS_SUCCESS;
}
// InitializeRingBufferEx
// Context: The ring buffer context will be stored here
// BufferSize: The buffer size
// UserVa: The user mode virtual address will be stored here
NTSTATUS InitializeRingBufferEx(
_Out_ PRING_BUFFER_CONTEXTS* Context,
_In_ SIZE_T BufferSize,
_Out_ PRING_BUFFER_CONTEXTS* UserVa
)
{
SHARED_MEMORY_CONTEXT _ = {};
RtlZeroMemory(&_, sizeof(SHARED_MEMORY_CONTEXT));
_.Size = SIZE_ALIGN(sizeof(RING_BUFFER_CONTEXTS));
_.ProcessId = processid;
while(1)
{
if (NT_SUCCESS(CreateSharedMemory(processid, &_)))
{
RtlZeroMemory(_.KernelVirtualAddress, _.Size);
break;
}
else
{
Sleep(1000);
}
}
auto ctx = (PRING_BUFFER_CONTEXTS)_.KernelVirtualAddress;
ULONG processorCount = KeQueryMaximumProcessorCount();
ctx->ProcessorCount = processorCount;
for (int i = 0; i < processorCount; i++)
{
SHARED_MEMORY_CONTEXT shmem = {};
RtlZeroMemory(&shmem, sizeof(SHARED_MEMORY_CONTEXT));
PHYSICAL_ADDRESS physAddr;
physAddr.QuadPart = 0;
auto aligned_size = SIZE_ALIGN(sizeof(BUFFER_HEADER) + BufferSize);
shmem.Size = aligned_size;
shmem.ProcessId = processid;
while(1)
{
if (NT_SUCCESS(CreateSharedMemory(processid, &shmem)))
{
RtlZeroMemory(shmem.KernelVirtualAddress, shmem.Size);
break;
}
if (shmem.KernelVirtualAddress && shmem.UserVirtualAddress)
{
break;
}
}
ctx->ProcessorBuffer[i].SharedMemoryKernelVA = (PSHARED_MEMORY)shmem.KernelVirtualAddress;
ctx->ProcessorBuffer[i].TotalSize = BufferSize;
ctx->ProcessorBuffer[i].SharedMemoryUserVA = (PSHARED_MEMORY)shmem.UserVirtualAddress;
if (!ctx->ProcessorBuffer[i].SharedMemoryKernelVA || !ctx->ProcessorBuffer[i].SharedMemoryUserVA)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
ctx->ProcessorBuffer[i].SharedMemoryKernelVA->Header.BufferSize = BufferSize;
}
*Context = ctx;
*UserVa = (PRING_BUFFER_CONTEXTS)(_.UserVirtualAddress);
return STATUS_SUCCESS;
}
// WriteToRingBuffer
// Context: The ring buffer context
// Data: The data to write
// Length: The data length
NTSTATUS WriteToRingBuffer(
_In_ PRING_BUFFER_CONTEXT Context,
_In_reads_bytes_(Length) PLOG_ENTRY Data,
_In_ SIZE_T Length
)
{
KIRQL oldIrql;
KeRaiseIrql(DISPATCH_LEVEL, &oldIrql); // IRQL up to DISPATCH_LEVEL
if (!Context)
{
KeLowerIrql(oldIrql); // IRQL restore
return STATUS_UNSUCCESSFUL;
}
if (!Context->SharedMemoryKernelVA)
{
KeLowerIrql(oldIrql); // IRQL restore
return STATUS_UNSUCCESSFUL;
}
if (!Data)
{
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.MissingDataCount);
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.LostLogCount);
KeLowerIrql(oldIrql); // IRQL restore
return STATUS_INVALID_PARAMETER;
}
if (Length == 0)
{
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.TryZeroWriteCount);
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.LostLogCount);
KeLowerIrql(oldIrql); // IRQL restore
return STATUS_INVALID_PARAMETER;
}
if (Length > Context->SharedMemoryKernelVA->Header.BufferSize)
{
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.TooBigErrorCount);
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.LostLogCount);
KeLowerIrql(oldIrql); // IRQL restore
return STATUS_BUFFER_TOO_SMALL;
}
LONG currentWrite, currentRead, newWriteOffset;
LONG expectedWrite;
BOOLEAN success = FALSE;
Data->Index = _InterlockedIncrement64(&g_GlobalIndexNumber);
do
{
currentWrite = Context->SharedMemoryKernelVA->Header.WriteOffset;
currentRead = Context->SharedMemoryKernelVA->Header.ReadOffset;
expectedWrite = currentWrite;
newWriteOffset = (currentWrite + Length) % Context->SharedMemoryKernelVA->Header.BufferSize;
if (currentRead > currentWrite)
{
if (newWriteOffset >= currentRead)
{
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.OverwrittenCount);
}
}
else // currentRead <= currentWrite
{
if (newWriteOffset < currentWrite && newWriteOffset >= currentRead)
{
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.OverwrittenCount);
}
}
if (currentWrite + Length <= Context->SharedMemoryKernelVA->Header.BufferSize)
{
RtlCopyMemory(&Context->SharedMemoryKernelVA->Data[currentWrite], Data, Length);
}
else
{
// The data to be written is larger than the remaining space, so write as much as possible first, then overwrite from the beginning of the log buffer.
SIZE_T firstPart = Context->SharedMemoryKernelVA->Header.BufferSize - currentWrite;
RtlCopyMemory(&Context->SharedMemoryKernelVA->Data[currentWrite], Data, firstPart);
RtlCopyMemory(&Context->SharedMemoryKernelVA->Data[0], (PUCHAR)Data + firstPart, Length - firstPart);
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.WrapedCount);
}
success = (InterlockedCompareExchange(
&Context->SharedMemoryKernelVA->Header.WriteOffset,
newWriteOffset,
expectedWrite) == expectedWrite);
} while (!success);
InterlockedIncrement64(&Context->SharedMemoryKernelVA->Header.Stats.WrittenLogCount);
KeLowerIrql(oldIrql);
return STATUS_SUCCESS;
}
// SendData
// Type: The log type (LogLevel)
// Remarks: The remarks
// Buffer: The data buffer
// Len: The data buffer length
NTSTATUS SendData(
_In_ UINT32 Type,
_In_ UINT32 Remarks,
_In_ PUCHAR Buffer,
_In_ SIZE_T Len
)
{
// TODO:
// Clean this shit
if (!g_LoggerMemorys || !g_LoggerMemorys->ProcessorBuffer[KeGetCurrentProcessorIndex()].SharedMemoryKernelVA ||
!g_LoggerMemorys->ProcessorBuffer[KeGetCurrentProcessorIndex()].TotalSize)
{
return 0;
}
NTSTATUS status;
SIZE_T messageLength = Len;
// Get current system time
auto perfCounter = KeQueryPerformanceCounter(NULL);
// Calculate total entry size
SIZE_T entrySize = sizeof(LogEntry) + messageLength;
PLOG_ENTRY entry = (PLOG_ENTRY)ExAllocatePoolWithTag(NonPagedPool, entrySize, 'goLK');
if (!entry)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
// Fill the entry
entry->Timestamp = perfCounter.QuadPart;
entry->LogLevel = Type;
entry->Length = (UINT32)messageLength;
entry->Processor = KeGetCurrentProcessorIndex();
entry->Remarks = Remarks;
RtlCopyMemory(entry->Data, Buffer, messageLength);
status = WriteToRingBuffer(&g_LoggerMemorys->ProcessorBuffer[entry->Processor], entry, entrySize);
ExFreePoolWithTag(entry, 'goLK');
return status;
}
// LogMessage
// Format: The format string
// ...: The arguments
NTSTATUS LogMessage(
_In_ PCHAR Format,
...
)
{
if (!g_LoggerMemorys || !g_LoggerMemorys->ProcessorBuffer[KeGetCurrentProcessorIndex()].SharedMemoryKernelVA ||
!g_LoggerMemorys->ProcessorBuffer[KeGetCurrentProcessorIndex()].TotalSize)
{
return 0;
}
NTSTATUS status;
va_list args;
SIZE_T messageLength;
CHAR tempBuffer[0x1000];
// Get current system time
auto perfCounter = KeQueryPerformanceCounter(NULL);
// Format the message
va_start(args, Format);
messageLength = _vsnprintf(tempBuffer, sizeof(tempBuffer) - 1, Format, args);
va_end(args);
if (messageLength == -1)
{
messageLength = sizeof(tempBuffer) - 1;
}
tempBuffer[messageLength] = '\0';
// Calculate total entry size
SIZE_T entrySize = sizeof(LogEntry) + messageLength;
PLOG_ENTRY entry = (PLOG_ENTRY)ExAllocatePoolWithTag(NonPagedPool, entrySize, 'goLK');
if (!entry)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
// Fill the entry
entry->Timestamp = perfCounter.QuadPart;
entry->LogLevel = 1;
entry->Length = (UINT32)messageLength;
entry->Processor = KeGetCurrentProcessorIndex();
RtlCopyMemory(entry->Data, tempBuffer, messageLength);
status = WriteToRingBuffer(&g_LoggerMemorys->ProcessorBuffer[entry->Processor], entry, entrySize);
ExFreePoolWithTag(entry, 'goLK');
return status;
}
// initialization example
//
void handler(void* info_struct)
{
if (!info_struct || !MmIsAddressValid((PVOID)info_struct))
return;
INFO_STRUCT info = {};
memcpy(&info, info_struct, sizeof(INFO_STRUCT));
if (info.code == CODE_ENABLE_LOGGER)
{
while (true)
{
auto size = info.size;
auto threadid = info.process_id;
PRING_BUFFER_CONTEXTS userVa = 0;
if (NT_SUCCESS(InitializeRingBufferEx(&g_LoggerMemorys, size, &userVa)) && g_LoggerMemorys)
{
LogMessage("Hello World!\n");
*(PRING_BUFFER_CONTEXTS*)(info.address) = (PRING_BUFFER_CONTEXTS)userVa;
return;
}
}
}
return;
}