forked from winsiderss/systeminformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.c
More file actions
502 lines (426 loc) · 13.6 KB
/
Copy pathverify.c
File metadata and controls
502 lines (426 loc) · 13.6 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
/*
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
*
* This file is part of System Informer.
*
* Authors:
*
* wj32 2016
* jxy-s 2020-2022
*
*/
#include <kph.h>
#include <dyndata.h>
#include <trace.h>
KPH_PROTECTED_DATA_SECTION_PUSH();
static BYTE KphpTrustedPublicKey[] =
{
0x45, 0x43, 0x53, 0x31, 0x20, 0x00, 0x00, 0x00,
0x7C, 0x32, 0xAB, 0xA6, 0x40, 0x79, 0x9C, 0x00,
0x67, 0xE2, 0x54, 0x7E, 0x0E, 0x4F, 0x55, 0x4A,
0xE1, 0x78, 0xC2, 0x62, 0x9A, 0x96, 0x81, 0x63,
0xCD, 0x4E, 0x3A, 0x28, 0x52, 0xB2, 0x4D, 0x28,
0x6A, 0x96, 0xE5, 0x7D, 0x1B, 0xCB, 0x9B, 0x27,
0xC8, 0xFD, 0x53, 0xDC, 0x00, 0x0D, 0x96, 0x62,
0xA2, 0x55, 0x38, 0x71, 0xF0, 0x0F, 0xCC, 0x8F,
0x84, 0xF4, 0x2B, 0x60, 0x38, 0xA6, 0xE7, 0x37,
};
KPH_PROTECTED_DATA_SECTION_POP();
PAGED_FILE();
static UNICODE_STRING KphpSigExtension = RTL_CONSTANT_STRING(L".sig");
static BCRYPT_ALG_HANDLE KphpBCryptSigningProvider = NULL;
static BCRYPT_KEY_HANDLE KphpTrustedPublicKeyHandle = NULL;
/**
* \brief Initializes verification infrastructure.
*
* \return Successful or errant status.
*/
_IRQL_requires_max_(PASSIVE_LEVEL)
_Must_inspect_result_
NTSTATUS KphInitializeVerify(
VOID
)
{
NTSTATUS status;
PAGED_PASSIVE();
status = BCryptOpenAlgorithmProvider(&KphpBCryptSigningProvider,
BCRYPT_ECDSA_P256_ALGORITHM,
NULL,
0);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_ERROR,
VERIFY,
"BCryptOpenAlgorithmProvider failed: %!STATUS!",
status);
KphpBCryptSigningProvider = NULL;
return status;
}
status = BCryptImportKeyPair(KphpBCryptSigningProvider,
NULL,
BCRYPT_ECCPUBLIC_BLOB,
&KphpTrustedPublicKeyHandle,
KphpTrustedPublicKey,
sizeof(KphpTrustedPublicKey),
0);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_ERROR,
VERIFY,
"BCryptImportKeyPair failed: %!STATUS!",
status);
KphpTrustedPublicKeyHandle = NULL;
return status;
}
return status;
}
/**
* \brief Cleans up verification infrastructure.
*/
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID KphCleanupVerify(
VOID
)
{
PAGED_PASSIVE();
if (KphpTrustedPublicKeyHandle)
{
BCryptDestroyKey(KphpTrustedPublicKeyHandle);
KphpTrustedPublicKeyHandle = NULL;
}
if (KphpBCryptSigningProvider)
{
BCryptCloseAlgorithmProvider(KphpBCryptSigningProvider, 0);
KphpBCryptSigningProvider = NULL;
}
}
/**
* \brief Verifies a buffer matches the provided signature.
*
* \param[in] Buffer The buffer to verify.
* \param[in] BufferLength The length of the buffer.
* \param[in] Signature The signature to check.
* \param[in] SignatureLength The length of the signature.
*
* \return Successful or errant status.
*/
_IRQL_requires_max_(PASSIVE_LEVEL)
_Must_inspect_result_
NTSTATUS KphVerifyBuffer(
_In_ PBYTE Buffer,
_In_ ULONG BufferLength,
_In_ PBYTE Signature,
_In_ ULONG SignatureLength
)
{
NTSTATUS status;
KPH_HASH hash;
PAGED_PASSIVE();
NT_ASSERT(KphpTrustedPublicKeyHandle);
status = KphHashBuffer(Buffer, BufferLength, CALG_SHA_256, &hash);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"KphHashBuffer failed: %!STATUS!",
status);
goto Exit;
}
status = BCryptVerifySignature(KphpTrustedPublicKeyHandle,
NULL,
hash.Buffer,
hash.Size,
Signature,
SignatureLength,
0);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"BCryptVerifySignature failed: %!STATUS!",
status);
goto Exit;
}
status = STATUS_SUCCESS;
Exit:
return status;
}
/**
* \brief Verifies that a file matches the provided signature.
*
* \param[in] FileName File name of file to verify.
*
* \return Successful or errant status.
*/
_IRQL_requires_max_(PASSIVE_LEVEL)
_Must_inspect_result_
NTSTATUS KphVerifyFile(
_In_ PUNICODE_STRING FileName
)
{
NTSTATUS status;
KPH_HASH hash;
UNICODE_STRING signatureFileName;
OBJECT_ATTRIBUTES objectAttributes;
HANDLE signatureFileHandle;
IO_STATUS_BLOCK ioStatusBlock;
BYTE signature[MINCRYPT_MAX_HASH_LEN];
PAGED_PASSIVE();
NT_ASSERT(KphpTrustedPublicKeyHandle);
signatureFileHandle = NULL;
RtlZeroMemory(&signatureFileName, sizeof(signatureFileName));
if ((FileName->Length <= KphpSigExtension.Length) ||
(FileName->Buffer[0] != L'\\'))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"File name is invalid \"%wZ\"",
FileName);
status = STATUS_OBJECT_NAME_INVALID;
goto Exit;
}
status = RtlDuplicateUnicodeString(0, FileName, &signatureFileName);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"RtlDuplicateUnicodeString failed: %!STATUS!",
status);
RtlZeroMemory(&signatureFileName, sizeof(signatureFileName));
goto Exit;
}
//
// Replace the file extension with ".sig". Our verification requires that
// the signature file be placed alongside the file we're verifying.
//
if (signatureFileName.Length < KphpSigExtension.Length)
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"Signature file name length invalid \"%wZ\"",
&signatureFileName);
status = STATUS_OBJECT_NAME_INVALID;
goto Exit;
}
signatureFileName.Length -= KphpSigExtension.Length;
status = RtlAppendUnicodeStringToString(&signatureFileName,
&KphpSigExtension);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"RtlAppendUnicodeStringToString failed: %!STATUS!",
status);
goto Exit;
}
InitializeObjectAttributes(&objectAttributes,
&signatureFileName,
OBJ_KERNEL_HANDLE,
NULL,
NULL);
status = IoCreateFileEx(&signatureFileHandle,
FILE_READ_ACCESS,
&objectAttributes,
&ioStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN,
FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0,
CreateFileTypeNone,
NULL,
IO_IGNORE_SHARE_ACCESS_CHECK,
NULL);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"Failed to open signature file \"%wZ\": %!STATUS!",
&signatureFileName,
status);
signatureFileHandle = NULL;
goto Exit;
}
status = ZwReadFile(signatureFileHandle,
NULL,
NULL,
NULL,
&ioStatusBlock,
signature,
ARRAYSIZE(signature),
NULL,
NULL);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"Failed to read signature file \"%wZ\": %!STATUS!",
&signatureFileName,
status);
goto Exit;
}
NT_ASSERT(ioStatusBlock.Information <= ARRAYSIZE(signature));
status = KphHashFileByName(FileName, CALG_SHA_256, &hash);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"KphHashFileByName failed: %!STATUS!",
status);
goto Exit;
}
status = BCryptVerifySignature(KphpTrustedPublicKeyHandle,
NULL,
hash.Buffer,
hash.Size,
signature,
(ULONG)ioStatusBlock.Information,
0);
if (!NT_SUCCESS(status))
{
KphTracePrint(TRACE_LEVEL_VERBOSE,
VERIFY,
"BCryptVerifySignature failed: %!STATUS!",
status);
goto Exit;
}
status = STATUS_SUCCESS;
Exit:
RtlFreeUnicodeString(&signatureFileName);
if (signatureFileHandle)
{
ObCloseHandle(signatureFileHandle, KernelMode);
}
return status;
}
/**
* \brief Performs a domination check between a calling process and a target process.
*
* \details A process dominates the other when the protected level of the
* process exceeds the other. This domination check is not ideal, it is overly
* strict and lacks enough information from the kernel to fully understand the
* protected process state.
*
* \param[in] Client - Client information.
* \param[in] Process - Calling process.
* \param[in] ProcessTarget - Target process to check that the calling process
* dominates.
* \param[in] AccessMode - Access mode of the request, if KernelMode the
* domination check is bypassed.
*
* \return Appropriate status:
* STATUS_SUCCESS - The calling process dominates the target.
* STATUS_ACCESS_DENIED - The calling process does not dominate the target.
*/
_IRQL_requires_max_(APC_LEVEL)
_Must_inspect_result_
NTSTATUS KphDominationCheck(
_In_ PEPROCESS Process,
_In_ PEPROCESS ProcessTarget,
_In_ KPROCESSOR_MODE AccessMode
)
{
PS_PROTECTION processProtection;
PS_PROTECTION targetProtection;
PAGED_CODE();
if (AccessMode == KernelMode)
{
//
// Give the kernel what it wants...
//
return STATUS_SUCCESS;
}
//
// Until Microsoft gives us more insight into protected process domination
// we'll do a very strict check here:
//
if (NT_SUCCESS(KphGetProcessProtection(Process, &processProtection)) &&
NT_SUCCESS(KphGetProcessProtection(ProcessTarget, &targetProtection)) &&
(targetProtection.Type != PsProtectedTypeNone) &&
(targetProtection.Type >= processProtection.Type))
{
//
// Calling process protection does not dominate the other, deny access.
// We could do our own domination check/mapping here with the signing
// level, but it won't be great and Microsoft might change it, so we'll
// do this strict check until Microsoft exports:
// PsTestProtectedProcessIncompatibility
// RtlProtectedAccess/RtlTestProtectedAccess
//
return STATUS_ACCESS_DENIED;
}
//
// Either the protected process check is not exported or the verified
// process dominates the target. Our domination check succeeded.
//
return STATUS_SUCCESS;
}
/**
* \brief Retrieves the process state mask from a process.
*
* \param[in] Process The process to get the state from.
*
* \return State mask describing what state the process is in.
*/
_IRQL_requires_max_(APC_LEVEL)
KPH_PROCESS_STATE KphGetProcessState(
_In_ PKPH_PROCESS_CONTEXT Process
)
{
KPH_PROCESS_STATE processState;
PAGED_CODE();
if (KphKdPresent())
{
//
// There is an active kernel debugger. This ultimately permits low
// state callers into the driver. But still check for verification.
// We still want to exercise the code below, regardless.
//
processState = ~KPH_PROCESS_VERIFIED_PROCESS;
}
else
{
processState = 0;
}
if (Process->SecurelyCreated)
{
processState |= KPH_PROCESS_SECURELY_CREATED;
}
if (Process->VerifiedProcess)
{
processState |= KPH_PROCESS_VERIFIED_PROCESS;
}
if (Process->Protected)
{
processState |= KPH_PROCESS_PROTECTED_PROCESS;
}
if (Process->NumberOfUntrustedImageLoads == 0)
{
processState |= KPH_PROCESS_NO_UNTRUSTED_IMAGES;
}
if (!PsIsProcessBeingDebugged(Process->EProcess))
{
processState |= KPH_PROCESS_NOT_BEING_DEBUGGED;
}
if (!Process->FileObject)
{
return processState;
}
processState |= KPH_PROCESS_HAS_FILE_OBJECT;
if (!IoGetTransactionParameterBlock(Process->FileObject))
{
processState |= KPH_PROCESS_NO_FILE_TRANSACTION;
}
if (!Process->FileObject->SectionObjectPointer)
{
return processState;
}
processState |= KPH_PROCESS_HAS_SECTION_OBJECT_POINTERS;
if (!MmDoesFileHaveUserWritableReferences(Process->FileObject->SectionObjectPointer))
{
processState |= KPH_PROCESS_NO_USER_WRITABLE_REFERENCES;
}
return processState;
}