From ad3cebfccb1b6c824e445ead8e53e5e01bcd3f16 Mon Sep 17 00:00:00 2001 From: dmex Date: Sat, 20 Jan 2018 00:59:42 +1100 Subject: [PATCH] Improve PR #224 --- ProcessHacker/include/procprv.h | 4 --- ProcessHacker/modprv.c | 20 +++++++++---- ProcessHacker/procprv.c | 51 +++++++++------------------------ 3 files changed, 28 insertions(+), 47 deletions(-) diff --git a/ProcessHacker/include/procprv.h b/ProcessHacker/include/procprv.h index 72ebd3ea0..7ee4e9f37 100644 --- a/ProcessHacker/include/procprv.h +++ b/ProcessHacker/include/procprv.h @@ -412,8 +412,4 @@ PhReferenceProcessItemForRecord( ); // end_phapppub -BOOLEAN PhProcessIsCFGuardEnabled( - _In_ HANDLE ProcessHandle -); - #endif diff --git a/ProcessHacker/modprv.c b/ProcessHacker/modprv.c index 17a9c3179..9a7589f41 100644 --- a/ProcessHacker/modprv.c +++ b/ProcessHacker/modprv.c @@ -84,6 +84,7 @@ PPH_MODULE_PROVIDER PhCreateModuleProvider( PhEmGetObjectSize(EmModuleProviderType, sizeof(PH_MODULE_PROVIDER)), PhModuleProviderType ); + memset(moduleProvider, 0, sizeof(PH_MODULE_PROVIDER)); moduleProvider->ModuleHashtable = PhCreateHashtable( sizeof(PPH_MODULE_ITEM), @@ -122,9 +123,21 @@ PPH_MODULE_PROVIDER PhCreateModuleProvider( moduleProvider->RunStatus = status; } - if (moduleProvider->ProcessHandle) + if (WindowsVersion >= WINDOWS_8 && moduleProvider->ProcessHandle) + { moduleProvider->PackageFullName = PhGetProcessPackageFullName(moduleProvider->ProcessHandle); + if (WindowsVersion >= WINDOWS_8_1) + { + BOOLEAN cfguardEnabled; + + if (NT_SUCCESS(PhGetProcessIsCFGuardEnabled(moduleProvider->ProcessHandle, &cfguardEnabled))) + { + moduleProvider->ControlFlowGuardEnabled = cfguardEnabled; + } + } + } + RtlInitializeSListHead(&moduleProvider->QueryListHead); PhEmCallObjectOperation(EmModuleProviderType, moduleProvider, EmObjectCreate); @@ -354,7 +367,6 @@ VOID PhModuleProviderUpdate( PPH_MODULE_PROVIDER moduleProvider = (PPH_MODULE_PROVIDER)Object; PPH_LIST modules; ULONG i; - BOOLEAN cfGuardEnabled = FALSE; // If we didn't get a handle when we created the provider, // abort (unless this is the System process - in that case @@ -445,8 +457,6 @@ VOID PhModuleProviderUpdate( } } - cfGuardEnabled = PhProcessIsCFGuardEnabled(moduleProvider->ProcessHandle); - // Look for new modules. for (i = 0; i < modules->Count; i++) { @@ -531,7 +541,7 @@ VOID PhModuleProviderUpdate( } // remove CF Guard flag if CFG mitigation is not enabled for the process - if (!cfGuardEnabled) + if (!moduleProvider->ControlFlowGuardEnabled) moduleItem->ImageDllCharacteristics &= ~IMAGE_DLLCHARACTERISTICS_GUARD_CF; if (NT_SUCCESS(PhQueryFullAttributesFileWin32(moduleItem->FileName->Buffer, &networkOpenInfo))) diff --git a/ProcessHacker/procprv.c b/ProcessHacker/procprv.c index 4ab5bcb16..ce3e38e02 100644 --- a/ProcessHacker/procprv.c +++ b/ProcessHacker/procprv.c @@ -1449,34 +1449,35 @@ VOID PhpFillProcessItem( { PS_PROTECTION protection; - if (NT_SUCCESS(NtQueryInformationProcess( - ProcessItem->QueryHandle, - ProcessProtectionInformation, - &protection, - sizeof(PS_PROTECTION), - NULL - ))) + if (NT_SUCCESS(PhGetProcessProtection(ProcessItem->QueryHandle, &protection))) { ProcessItem->Protection.Level = protection.Level; } } else { - // "emulate" PS_PROTECTION info for older OSes + // HACK: 'emulate' the PS_PROTECTION info for older OSes. if (ProcessItem->IsProtectedProcess) ProcessItem->Protection.Type = PsProtectedTypeProtected; } } else { - // we weren't able to get protection info - // lets signalize that with special value + // Signalize that we weren't able to get protection info with a special value. + // Note: We use this value to determine if we should show protection information. ProcessItem->Protection.Level = UCHAR_MAX; } // Control Flow Guard - if (ProcessItem->QueryHandle) - ProcessItem->IsControlFlowGuardEnabled = PhProcessIsCFGuardEnabled(ProcessItem->QueryHandle); + if (WindowsVersion >= WINDOWS_8_1 && ProcessItem->QueryHandle) + { + BOOLEAN cfguardEnabled; + + if (NT_SUCCESS(PhGetProcessIsCFGuardEnabled(ProcessItem->QueryHandle, &cfguardEnabled))) + { + ProcessItem->IsControlFlowGuardEnabled = cfguardEnabled; + } + } // On Windows 8.1 and above, processes without threads are reflected processes // which will not terminate if we have a handle open. @@ -2989,29 +2990,3 @@ PPH_PROCESS_ITEM PhReferenceProcessItemForRecord( return processItem; } -BOOLEAN PhProcessIsCFGuardEnabled( - _In_ HANDLE ProcessHandle -) -{ - BOOLEAN cfgEnabled = FALSE; - - if (WindowsVersion >= WINDOWS_8_1) - { - PROCESS_MITIGATION_POLICY_INFORMATION policyInfo; - - policyInfo.Policy = ProcessControlFlowGuardPolicy; - - if (NT_SUCCESS(NtQueryInformationProcess( - ProcessHandle, - ProcessMitigationPolicy, - &policyInfo, - sizeof(PROCESS_MITIGATION_POLICY_INFORMATION), - NULL - ))) - { - cfgEnabled = (policyInfo.ControlFlowGuardPolicy.EnableControlFlowGuard != 0); - } - } - - return cfgEnabled; -}