Is there any way to detect from the driver that this process is protected ?
Disclaimer: This following procedure depends on undocmented data structure which is extracted from PDB symbol of combase.dll
file. As usual this may not work in future Windows OS version.
This is the sample C code to detect protected process. The program accepts a valid process ID in its first argument. This code need to be linked with ntdll.lib
library. In user mode, this code may need to be run as administrator for some processes.
#include <Windows.h>
#include <winternl.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct _PROCESS_EXTENDED_BASIC_INFORMATION {
size_t Size;
PROCESS_BASIC_INFORMATION BasicInfo;
union {
unsigned int Flags;
struct {
unsigned int IsProtectedProcess : 1;
unsigned int IsWow64Process : 1;
unsigned int IsProcessDeleting : 1;
unsigned int IsCrossSessionCreate : 1;
unsigned int IsFrozen : 1;
unsigned int IsBackground : 1;
unsigned int IsStronglyNamed : 1;
unsigned int IsSecureProcess : 1;
unsigned int IsSubsystemProcess : 1;
unsigned int SpareBits : 23;
};
};
} PROCESS_EXTENDED_BASIC_INFORMATION, *PPROCESS_EXTENDED_BASIC_INFORMATION; /* size: 0x0040 */
int main(int argc, char *argv[])
{
if (argc < 2)
return;
void *hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, atoi(argv[1]));
if (hProcess != NULL)
{
unsigned int ret;
PROCESS_EXTENDED_BASIC_INFORMATION pInfo;
memset(&pInfo, 0, sizeof pInfo);
NTSTATUS Status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pInfo, sizeof pInfo, &ret);
if (Status == 0)
printf("Protected: %d\n", pInfo.IsProtectedProcess);
CloseHandle(hProcess);
}
else
printf("OpenProcess error: %d\n", GetLastError());
}
To add or change process mitigation policy flags, programs use SetProcessMitigationPolicy()
function which uses NtSetInformationProcess()
function (retrieved from KernelBase.dll
). Here is a sample C code which enables dynamic code policy.
#include <Windows.h>
#include <winternl.h>
typedef struct _PROCESS_MITIGATION_POLICY_INFORMATION {
PROCESS_MITIGATION_POLICY Policy;
union {
PROCESS_MITIGATION_ASLR_POLICY ASLRPolicy;
PROCESS_MITIGATION_STRICT_HANDLE_CHECK_POLICY StrictHandleCheckPolicy;
PROCESS_MITIGATION_SYSTEM_CALL_DISABLE_POLICY SystemCallDisablePolicy;
PROCESS_MITIGATION_EXTENSION_POINT_DISABLE_POLICY ExtensionPointDisablePolicy;
PROCESS_MITIGATION_DYNAMIC_CODE_POLICY DynamicCodePolicy;
PROCESS_MITIGATION_CONTROL_FLOW_GUARD_POLICY ControlFlowGuardPolicy;
PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY SignaturePolicy;
PROCESS_MITIGATION_FONT_DISABLE_POLICY FontDisablePolicy;
PROCESS_MITIGATION_IMAGE_LOAD_POLICY ImageLoadPolicy;
PROCESS_MITIGATION_SYSTEM_CALL_FILTER_POLICY SystemCallFilterPolicy;
PROCESS_MITIGATION_PAYLOAD_RESTRICTION_POLICY PayloadRestrictionPolicy;
PROCESS_MITIGATION_CHILD_PROCESS_POLICY ChildProcessPolicy;
PROCESS_MITIGATION_SIDE_CHANNEL_ISOLATION_POLICY SideChannelIsolationPolicy;
};
} PROCESS_MITIGATION_POLICY_INFORMATION, *PPROCESS_MITIGATION_POLICY_INFORMATION; /* size: 0x0008 */
int main(void)
{
PROCESS_MITIGATION_POLICY_INFORMATION PolicyInfo = { 0 };
PolicyInfo.Policy = ProcessDynamicCodePolicy;
PolicyInfo.DynamicCodePolicy.ProhibitDynamicCode = TRUE;
NTSTATUS Status = NtSetInformationProcess(
GetCurrentProcess(),
52, /* ProcessMitigationPolicy */
&PolicyInfo,
sizeof PolicyInfo);
}
Any one of the policy can be enabled with this method. Use Zw
alternatives of Nt
function to use in kernel mode. All of these undocumented structures can also be found in ProcessHacker/phnt repository.
ProcessDynamicCodePolicy
by themselves usingSetProcessMitigationPolicy
. For the second type of processes I wish to override this bit from the driver. do you know ifSetProcessMitigationPolicy
is also available from kernel space ?