-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathssdt.c
More file actions
547 lines (459 loc) · 15.7 KB
/
ssdt.c
File metadata and controls
547 lines (459 loc) · 15.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <ntifs.h>
#include "Private.h"
#include "Utils.h"
#include "main.h"
#include <Ntstrsafe.h>
#ifndef AMD64
extern PSYSTEM_SERVICE_DESCRIPTOR_TABLE KeServiceDescriptorTable;
#endif
BOOLEAN WriteKernelMemory(PVOID pDestination, PVOID pSourceAddress, SIZE_T SizeOfCopy);
ULONG GetNativeFunctionIndex(const char *lpFunctionName);
NTSTATUS BBInitDynamicData(IN OUT PDYNAMIC_DATA pData);
NTSTATUS CreateMyDbgkDebugObjectType(void);
NTSTATUS RestoreDbgkDebugObjectType(void);
#pragma alloc_text(PAGE, GetNativeFunctionIndex)
#pragma alloc_text(PAGE, GetKernelBase)
#pragma alloc_text(PAGE, GetSSDTBase)
#pragma alloc_text(PAGE, GetSSDTEntry)
#pragma alloc_text(PAGE, CreateMyDbgkDebugObjectType)
#pragma alloc_text(PAGE, RestoreDbgkDebugObjectType)
#pragma alloc_text(INIT, BBInitDynamicData)
DYNAMIC_DATA dynData = {0};
PVOID g_KernelBase = NULL;
ULONG g_KernelSize = 0;
PSYSTEM_SERVICE_DESCRIPTOR_TABLE g_SSDT = NULL;
ULONG GetNativeFunctionIndex(const char *lpFunctionName)
{
HANDLE hSection, hFile;
PIMAGE_DOS_HEADER dosHeader;
PIMAGE_NT_HEADERS32 ntHeader;
IMAGE_EXPORT_DIRECTORY* pExportTable;
ULONG* arrayOfFunctionAddresses;
ULONG* arrayOfFunctionNames;
USHORT* arrayOfFunctionOrdinals;
ULONG x;
PUCHAR functionAddress = NULL;
char* functionName = NULL;
PVOID BaseAddress = NULL;
SIZE_T Size = 0;
OBJECT_ATTRIBUTES oa;
IO_STATUS_BLOCK iosb;
NTSTATUS status;
ULONG uIndex = 0;
UNICODE_STRING pDllName;
#ifdef AMD64
RtlInitUnicodeString(&pDllName, L"\\SystemRoot\\SysWOW64\\ntdll.dll");
#else
RtlInitUnicodeString(&pDllName, L"\\SystemRoot\\System32\\ntdll.dll");
#endif
InitializeObjectAttributes(&oa, &pDllName, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
status = ZwOpenFile(&hFile, FILE_GENERIC_READ | SYNCHRONIZE, &oa, &iosb, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
if (NT_SUCCESS(status))
{
oa.ObjectName = 0;
status = ZwCreateSection(&hSection, SECTION_ALL_ACCESS, &oa, 0, PAGE_READONLY, 0x01000000, hFile);
if (NT_SUCCESS(status))
{
BaseAddress = NULL;
status = ZwMapViewOfSection(hSection, NtCurrentProcess(), &BaseAddress, 0, 0, 0, &Size, ViewShare, MEM_TOP_DOWN, PAGE_READONLY);
if (NT_SUCCESS(status))
{
dosHeader = (PIMAGE_DOS_HEADER)BaseAddress;
ntHeader = (PIMAGE_NT_HEADERS32)((PUCHAR)BaseAddress + dosHeader->e_lfanew);
pExportTable = (PIMAGE_EXPORT_DIRECTORY)((PUCHAR)BaseAddress + ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
arrayOfFunctionAddresses = (ULONG*)((PUCHAR)BaseAddress + pExportTable->AddressOfFunctions);
arrayOfFunctionNames = (ULONG*)((PUCHAR)BaseAddress + pExportTable->AddressOfNames);
arrayOfFunctionOrdinals = (USHORT*)((PUCHAR)BaseAddress + pExportTable->AddressOfNameOrdinals);
for (x = 0; x < pExportTable->NumberOfFunctions; x++)
{
functionName = (char*)((unsigned char*)BaseAddress + arrayOfFunctionNames[x]);
functionAddress = ((unsigned char*)BaseAddress + arrayOfFunctionAddresses[arrayOfFunctionOrdinals[x]]);
if (!_stricmp(functionName, lpFunctionName))
{
uIndex = *(USHORT *)(functionAddress + 1);
break;
}
}
ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
}
ZwClose(hSection);
}
ZwClose(hFile);
}
return uIndex;
}
/// <summary>
/// Get ntoskrnl base address
/// </summary>
/// <param name="pSize">Size of module</param>
/// <returns>Found address, NULL if not found</returns>
PVOID GetKernelBase(OUT PULONG pSize)
{
NTSTATUS status = STATUS_SUCCESS;
ULONG bytes = 0;
PRTL_PROCESS_MODULES pMods = NULL;
PVOID checkPtr = NULL;
UNICODE_STRING routineName;
ULONG i;
// Already found
if (g_KernelBase != NULL)
{
if (pSize)
*pSize = g_KernelSize;
return g_KernelBase;
}
RtlUnicodeStringInit(&routineName, L"NtOpenFile");
checkPtr = MmGetSystemRoutineAddress(&routineName);
if (checkPtr == NULL)
return NULL;
// Protect from UserMode AV
status = ZwQuerySystemInformation(SystemModuleInformation, 0, bytes, &bytes);
if (bytes == 0)
{
DPRINT("BlackBone: %s: Invalid SystemModuleInformation size\n", __FUNCTION__);
return NULL;
}
pMods = (PRTL_PROCESS_MODULES)ExAllocatePoolWithTag(NonPagedPool, bytes, BB_POOL_TAG);
RtlZeroMemory(pMods, bytes);
status = ZwQuerySystemInformation(SystemModuleInformation, pMods, bytes, &bytes);
if (NT_SUCCESS(status))
{
PRTL_PROCESS_MODULE_INFORMATION pMod = pMods->Modules;
for (i = 0; i < pMods->NumberOfModules; i++)
{
// System routine is inside module
if (checkPtr >= pMod[i].ImageBase &&
checkPtr < (PVOID)((PUCHAR)pMod[i].ImageBase + pMod[i].ImageSize))
{
g_KernelBase = pMod[i].ImageBase;
g_KernelSize = pMod[i].ImageSize;
if (pSize)
*pSize = g_KernelSize;
break;
}
}
}
if (pMods)
ExFreePoolWithTag(pMods, BB_POOL_TAG);
return g_KernelBase;
}
/// <summary>
/// Search for pattern
/// </summary>
/// <param name="pattern">Pattern to search for</param>
/// <param name="wildcard">Used wildcard</param>
/// <param name="len">Pattern length</param>
/// <param name="base">Base address for searching</param>
/// <param name="size">Address range to search in</param>
/// <param name="ppFound">Found location</param>
/// <returns>Status code</returns>
NTSTATUS BBSearchPattern(IN PCUCHAR pattern, IN UCHAR wildcard, IN ULONG_PTR len, IN const VOID* base, IN ULONG_PTR size, OUT PVOID* ppFound)
{
ULONG_PTR i, j;
if (ppFound == NULL || pattern == NULL || base == NULL)
return STATUS_INVALID_PARAMETER;
for (i = 0; i < size - len; i++)
{
BOOLEAN found = TRUE;
for (j = 0; j < len; j++)
{
if (pattern[j] != wildcard && pattern[j] != ((PCUCHAR)base)[i + j])
{
found = FALSE;
break;
}
}
if (found != FALSE)
{
*ppFound = (PUCHAR)base + i;
return STATUS_SUCCESS;
}
}
return STATUS_NOT_FOUND;
}
PSYSTEM_SERVICE_DESCRIPTOR_TABLE GetSSDTBase()
{
#ifdef AMD64
PIMAGE_NT_HEADERS pHdr;
PIMAGE_SECTION_HEADER pFirstSec;
PIMAGE_SECTION_HEADER pSec;
PUCHAR ntosBase;
ntosBase = GetKernelBase(NULL);
// Already found
if (g_SSDT != NULL)
return g_SSDT;
if (!ntosBase)
return NULL;
pHdr = RtlImageNtHeader(ntosBase);
pFirstSec = (PIMAGE_SECTION_HEADER)(pHdr + 1);
for (pSec = pFirstSec; pSec < pFirstSec + pHdr->FileHeader.NumberOfSections; pSec++)
{
// Non-paged, non-discardable, readable sections
// Probably still not fool-proof enough...
if (pSec->Characteristics & IMAGE_SCN_MEM_NOT_PAGED &&
pSec->Characteristics & IMAGE_SCN_MEM_EXECUTE &&
!(pSec->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) &&
(*(PULONG)pSec->Name != 'TINI') &&
(*(PULONG)pSec->Name != 'EGAP'))
{
PVOID pFound = NULL;
// KiSystemServiceRepeat pattern
UCHAR pattern[] = "\x4c\x8d\x15\xcc\xcc\xcc\xcc\x4c\x8d\x1d\xcc\xcc\xcc\xcc\xf7";
NTSTATUS status = BBSearchPattern(pattern, 0xCC, sizeof(pattern) - 1, ntosBase + pSec->VirtualAddress, pSec->Misc.VirtualSize, &pFound);
if (NT_SUCCESS(status))
{
g_SSDT = (PSYSTEM_SERVICE_DESCRIPTOR_TABLE)((PUCHAR)pFound + *(PULONG)((PUCHAR)pFound + 3) + 7);
//DPRINT( "BlackBone: %s: KeSystemServiceDescriptorTable = 0x%p\n", __FUNCTION__, g_SSDT );
return g_SSDT;
}
}
}
return NULL;
#else
return KeServiceDescriptorTable;
#endif
}
/// <summary>
/// Gets the SSDT entry address by index.
/// </summary>
/// <param name="index">Service index</param>
/// <returns>Found service address, NULL if not found</returns>
PVOID GetSSDTEntry(IN ULONG index)
{
PSYSTEM_SERVICE_DESCRIPTOR_TABLE pSSDT = GetSSDTBase();
if (!pSSDT)
return NULL;
// Index range check
if (index > pSSDT->NumberOfServices)
return NULL;
#ifdef AMD64
return (PUCHAR)pSSDT->ServiceTableBase + (((PLONG)pSSDT->ServiceTableBase)[index] >> 4);
#else
return (PVOID)pSSDT->ServiceTableBase[index];
#endif
}
NTSTATUS BBInitDynamicData(IN OUT PDYNAMIC_DATA pData)
{
NTSTATUS status = STATUS_SUCCESS;
RTL_OSVERSIONINFOEXW verInfo = { 0 };
PVOID fnExGetPreviousMode = NULL;
PVOID pFoundPattern = NULL;
UCHAR PreviousModePattern[] = "\x00\x00\xC3";
if (pData == NULL)
return STATUS_INVALID_ADDRESS;
RtlZeroMemory(pData, sizeof(DYNAMIC_DATA));
verInfo.dwOSVersionInfoSize = sizeof(verInfo);
status = RtlGetVersion((PRTL_OSVERSIONINFOW)&verInfo);
if (status == STATUS_SUCCESS)
{
ULONG ver_short = (verInfo.dwMajorVersion << 8) | (verInfo.dwMinorVersion << 4) | verInfo.wServicePackMajor;
DPRINT(
"OS version %d.%d.%d.%d\n",
verInfo.dwMajorVersion,
verInfo.dwMinorVersion,
verInfo.dwBuildNumber,
verInfo.wServicePackMajor
);
switch (ver_short)
{
case WINVER_VISTA:
#ifdef AMD64
pData->Protection = 0x36C; // Bitfield, bit index - 0xB
#else
pData->Protection = 0x43C; // Bitfield, bit index - 0xB
#endif
// Windows 7
// Windows 7 SP1
case WINVER_7:
case WINVER_7_SP1:
#ifdef AMD64
pData->Protection = 0x43C; // Bitfield, bit index - 0xB
#else
pData->Protection = 0x26C; // Bitfield, bit index - 0xB
#endif
break;
// Windows 8
case WINVER_8:
#ifdef AMD64
pData->Protection = 0x648;
#else
pData->Protection = 0x2D4;
#endif
break;
// Windows 8.1
case WINVER_81:
pData->Protection = 0x67A;
break;
// Windows 10, build 10586
case WINVER_10:
pData->Protection = 0x6B2;
break;
default:
break;
}
pData->OsVer = (WinVer)ver_short;
pData->NtReadIndex = GetNativeFunctionIndex("NtReadVirtualMemory");
pData->NtWriteIndex = GetNativeFunctionIndex("NtWriteVirtualMemory");
pData->NtProtectIndex = GetNativeFunctionIndex("NtProtectVirtualMemory");
pData->NtQueryIndex = GetNativeFunctionIndex("NtQueryVirtualMemory");
pData->NtCreateThdExIndex = GetNativeFunctionIndex("NtCreateThreadEx");
pData->NtDebugActiveProcessIndex = GetNativeFunctionIndex("NtDebugActiveProcess");
fnExGetPreviousMode = ExGetPreviousMode;
if (NT_SUCCESS(BBSearchPattern(PreviousModePattern, 0xCC, sizeof(PreviousModePattern) - 1, fnExGetPreviousMode, 32, &pFoundPattern)))
{
pData->PrevMode = *(DWORD *)((PUCHAR)pFoundPattern - 2);
}
DPRINT(
"Dynamic search status: SSDT - %s\n",
GetSSDTBase() != NULL ? "SUCCESS" : "FAIL"
);
return status;
}
return status;
}
//Pass TP
PVOID MyDbgkDebugObjectType = NULL;
NTSTATUS CreateMyDbgkDebugObjectType(void)
{
NTSTATUS status;
UNICODE_STRING ObjectTypeName;
union
{
OBJECT_TYPE_INITIALIZER_VISTA vista;
OBJECT_TYPE_INITIALIZER_WIN7 win7;
OBJECT_TYPE_INITIALIZER_WIN8 win8;
}ObjectTypeInitializer;
union
{
OBJECT_TYPE_VISTA *vista;
OBJECT_TYPE_WIN7 *win7;
OBJECT_TYPE_WIN8 *win8;
}DbgkDebugObjectType;
PVOID fnNtDebugActiveProcess = NULL;
PVOID pFoundPattern = NULL;
#ifdef AMD64
UCHAR DebugObjectTypePattern[] = "\xE9\x2A\x2A\x2A\x2A\x4C\x8B\x05";
#else
UCHAR DebugObjectTypePattern[] = "\x50\xFF\x2A\x2A\xFF\x35\x2A\x2A\x2A\x2A\x6A\x02";
#endif
if (MyDbgkDebugObjectType)
{
goto end;
}
//Search Pattern...
if (!dynData.DbgkDebugObjectType)
{
int offset;
if (!dynData.NtDebugActiveProcessIndex)
return STATUS_NOT_FOUND;
fnNtDebugActiveProcess = GetSSDTEntry(dynData.NtDebugActiveProcessIndex);
if (!fnNtDebugActiveProcess)
return STATUS_NOT_FOUND;
if (!NT_SUCCESS(BBSearchPattern(DebugObjectTypePattern, 0x2A, sizeof(DebugObjectTypePattern) - 1, fnNtDebugActiveProcess, 0x200, &pFoundPattern)))
return STATUS_NOT_FOUND;
//有符号
#ifdef AMD64
offset = *(int *)((PUCHAR)pFoundPattern + 8);
dynData.pDbgkDebugObjectType = (PVOID)((PUCHAR)pFoundPattern + 12 + offset);
#else
dynData.pDbgkDebugObjectType = *(PVOID *)((PUCHAR)pFoundPattern + 6);
#endif
dynData.DbgkDebugObjectType = *(PVOID *)dynData.pDbgkDebugObjectType;
}
RtlInitUnicodeString(&ObjectTypeName, L"MyDebugObject");
if (dynData.OsVer >= WINVER_VISTA && dynData.OsVer < WINVER_7)
{
DbgkDebugObjectType.vista = dynData.DbgkDebugObjectType;
if (RtlCompareUnicodeString(&DbgkDebugObjectType.vista->Name, &ObjectTypeName, FALSE) == 0)
{
KdPrint(("已经替换为MyDebugObject.\n"));
return STATUS_ALREADY_WIN32;
}
RtlCopyMemory(&ObjectTypeInitializer.vista, &DbgkDebugObjectType.vista->TypeInfo, sizeof(ObjectTypeInitializer.vista));
if (DbgkDebugObjectType.vista->TypeInfo.ValidAccessMask == 0)
{
ObjectTypeInitializer.vista.GenericMapping.GenericRead = 0x00020001;
ObjectTypeInitializer.vista.GenericMapping.GenericWrite = 0x00020002;
ObjectTypeInitializer.vista.GenericMapping.GenericExecute = 0x00120000;
ObjectTypeInitializer.vista.GenericMapping.GenericAll = 0x001f000f;
ObjectTypeInitializer.vista.ValidAccessMask = 0x001f000f;
}
status = ObCreateObjectType(&ObjectTypeName, &ObjectTypeInitializer.vista, (PSECURITY_DESCRIPTOR)NULL, &MyDbgkDebugObjectType);
if (!NT_SUCCESS(status))
{
KdPrint(("ObCreateObjectType status = %X\n", status));
return status;
}
}
else if (dynData.OsVer >= WINVER_7 && dynData.OsVer < WINVER_8)
{
DbgkDebugObjectType.win7 = dynData.DbgkDebugObjectType;
if (RtlCompareUnicodeString(&DbgkDebugObjectType.win7->Name, &ObjectTypeName, FALSE) == 0)
{
KdPrint(("已经替换为MyDebugObject.\n"));
return STATUS_ALREADY_WIN32;
}
RtlCopyMemory(&ObjectTypeInitializer.win7, &DbgkDebugObjectType.win7->TypeInfo, sizeof(ObjectTypeInitializer.win7));
if (DbgkDebugObjectType.win7->TypeInfo.ValidAccessMask == 0)
{
ObjectTypeInitializer.win7.GenericMapping.GenericRead = 0x00020001;
ObjectTypeInitializer.win7.GenericMapping.GenericWrite = 0x00020002;
ObjectTypeInitializer.win7.GenericMapping.GenericExecute = 0x00120000;
ObjectTypeInitializer.win7.GenericMapping.GenericAll = 0x001f000f;
ObjectTypeInitializer.win7.ValidAccessMask = 0x001f000f;
}
status = ObCreateObjectType(&ObjectTypeName, &ObjectTypeInitializer.win7, (PSECURITY_DESCRIPTOR)NULL, &MyDbgkDebugObjectType);
if (!NT_SUCCESS(status))
{
KdPrint(("ObCreateObjectType status = %X\n", status));
return status;
}
}
else if (dynData.OsVer >= WINVER_8 && dynData.OsVer <= WINVER_10)
{
DbgkDebugObjectType.win8 = dynData.DbgkDebugObjectType;
if (RtlCompareUnicodeString(&DbgkDebugObjectType.win8->Name, &ObjectTypeName, FALSE) == 0)
{
KdPrint(("已经替换为MyDebugObject.\n"));
return STATUS_ALREADY_WIN32;
}
RtlCopyMemory(&ObjectTypeInitializer.win8, &DbgkDebugObjectType.win8->TypeInfo, sizeof(ObjectTypeInitializer.win8));
if (DbgkDebugObjectType.win8->TypeInfo.ValidAccessMask == 0)
{
ObjectTypeInitializer.win8.GenericMapping.GenericRead = 0x00020001;
ObjectTypeInitializer.win8.GenericMapping.GenericWrite = 0x00020002;
ObjectTypeInitializer.win8.GenericMapping.GenericExecute = 0x00120000;
ObjectTypeInitializer.win8.GenericMapping.GenericAll = 0x001f000f;
ObjectTypeInitializer.win8.ValidAccessMask = 0x001f000f;
}
status = ObCreateObjectType(&ObjectTypeName, &ObjectTypeInitializer.win8, (PSECURITY_DESCRIPTOR)NULL, &MyDbgkDebugObjectType);
if (!NT_SUCCESS(status))
{
KdPrint(("ObCreateObjectType status = %X\n", status));
return status;
}
}
else
{
KdPrint(("Unsupport OS version!\n"));
return STATUS_UNSUCCESSFUL;
}
end:
if(!WriteKernelMemory(dynData.pDbgkDebugObjectType, &MyDbgkDebugObjectType, sizeof(PVOID)))
{
KdPrint(("WriteKernelMemory failed\n"));
return STATUS_UNSUCCESSFUL;
}
return STATUS_SUCCESS;
}
NTSTATUS RestoreDbgkDebugObjectType(void)
{
if (!MyDbgkDebugObjectType)
return STATUS_UNSUCCESSFUL;
if (!WriteKernelMemory(dynData.pDbgkDebugObjectType, &dynData.DbgkDebugObjectType, sizeof(PVOID)))
{
KdPrint(("WriteKernelMemory failed\n"));
return STATUS_UNSUCCESSFUL;
}
return STATUS_SUCCESS;
}