Update Tools > Permissions menu, Add support for viewing power scheme, terminal service and WMI namespace security

This commit is contained in:
dmex
2021-01-19 16:00:18 +11:00
parent b3f7c343bb
commit 50048ca8f0
6 changed files with 980 additions and 32 deletions
+48
View File
@@ -579,6 +579,15 @@ static NTSTATUS PhpOpenServiceControlManager(
return PhGetLastWin32ErrorAsNtStatus();
}
static NTSTATUS PhpOpenSecurityDummyHandle(
_Inout_ PHANDLE Handle,
_In_ ACCESS_MASK DesiredAccess,
_In_opt_ PVOID Context
)
{
return STATUS_SUCCESS;
}
VOID PhMwpOnCommand(
_In_ HWND WindowHandle,
_In_ ULONG Id
@@ -919,6 +928,42 @@ VOID PhMwpOnCommand(
);
}
break;
case ID_TOOLS_PWR_PERMISSIONS:
{
PhEditSecurity(
NULL,
L"Current Power Scheme",
L"PowerDefault",
PhpOpenSecurityDummyHandle,
NULL,
NULL
);
}
break;
case ID_TOOLS_RDP_PERMISSIONS:
{
PhEditSecurity(
NULL,
L"Terminal Server Listener",
L"RdpDefault",
PhpOpenSecurityDummyHandle,
NULL,
NULL
);
}
break;
case ID_TOOLS_WMI_PERMISSIONS:
{
PhEditSecurity(
NULL,
L"WMI Root Namespace",
L"WmiDefault",
PhpOpenSecurityDummyHandle,
NULL,
NULL
);
}
break;
case ID_USER_CONNECT:
{
PhUiConnectSession(WindowHandle, SelectedUserSessionId);
@@ -2154,7 +2199,10 @@ PPH_EMENU PhpCreateToolsMenu(
PhInsertEMenuItem(ToolsMenu, PhCreateEMenuSeparator(), ULONG_MAX);
menuItem = PhCreateEMenuItem(0, 0, L"&Permissions", NULL, NULL);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_TOOLS_PWR_PERMISSIONS, L"Current Power Scheme", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_TOOLS_SCM_PERMISSIONS, L"Service Control Manager", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_TOOLS_RDP_PERMISSIONS, L"Terminal Server Listener", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(menuItem, PhCreateEMenuItem(0, ID_TOOLS_WMI_PERMISSIONS, L"WMI Root Namespace", NULL, NULL), ULONG_MAX);
PhInsertEMenuItem(ToolsMenu, menuItem, ULONG_MAX);
return ToolsMenu;
+407 -17
View File
@@ -110,15 +110,15 @@ HRESULT PhpWmiProviderExecMethod(
if (FAILED(status))
goto CleanupExit;
wbemResourceString = SysAllocString(L"root\\CIMV2");
wbemResourceString = SysAllocString(L"Root\\CIMV2");
status = IWbemLocator_ConnectServer(
wbemLocator,
wbemResourceString,
NULL,
NULL,
NULL,
0,
0,
WBEM_FLAG_CONNECT_USE_MAX_WAIT,
NULL,
NULL,
&wbemServices
);
@@ -164,7 +164,7 @@ HRESULT PhpWmiProviderExecMethod(
if (SUCCEEDED(IWbemClassObject_Get(wbemClassObject, L"Namespace", 0, &variant, 0, 0)))
{
namespacePath = PhCreateString(variant.bstrVal);
namespacePath = PhCreateString(V_BSTR(&variant));
VariantClear(&variant);
}
@@ -172,7 +172,7 @@ HRESULT PhpWmiProviderExecMethod(
if (SUCCEEDED(IWbemClassObject_Get(wbemClassObject, L"Provider", 0, &variant, 0, 0)))
{
providerName = PhCreateString(variant.bstrVal);
providerName = PhCreateString(V_BSTR(&variant));
VariantClear(&variant);
}
@@ -180,7 +180,7 @@ HRESULT PhpWmiProviderExecMethod(
if (SUCCEEDED(IWbemClassObject_Get(wbemClassObject, L"User", 0, &variant, 0, 0)))
{
userName = PhCreateString(variant.bstrVal);
userName = PhCreateString(V_BSTR(&variant));
VariantClear(&variant);
}
@@ -188,7 +188,7 @@ HRESULT PhpWmiProviderExecMethod(
if (SUCCEEDED(IWbemClassObject_Get(wbemClassObject, L"__PATH", 0, &variant, 0, 0)))
{
instancePath = PhCreateString(variant.bstrVal);
instancePath = PhCreateString(V_BSTR(&variant));
VariantClear(&variant);
}
@@ -290,8 +290,8 @@ HRESULT PhpQueryWmiProviderFileName(
NULL,
NULL,
NULL,
0,
0,
WBEM_FLAG_CONNECT_USE_MAX_WAIT,
NULL,
NULL,
&wbemServices
);
@@ -332,9 +332,9 @@ HRESULT PhpQueryWmiProviderFileName(
if (SUCCEEDED(IWbemClassObject_Get(wbemClassObject, L"CLSID", 0, &variant, 0, 0)))
{
if (variant.bstrVal) // returns NULL for some host processes (dmex)
if (V_BSTR(&variant)) // returns NULL for some host processes (dmex)
{
clsidString = PhCreateString(variant.bstrVal);
clsidString = PhCreateString(V_BSTR(&variant));
}
VariantClear(&variant);
@@ -437,15 +437,15 @@ PPH_LIST PhpQueryWmiProviderHostProcess(
if (FAILED(status))
goto CleanupExit;
wbemResourceString = SysAllocString(L"root\\CIMV2");
wbemResourceString = SysAllocString(L"Root\\CIMV2");
status = IWbemLocator_ConnectServer(
wbemLocator,
wbemResourceString,
NULL,
NULL,
NULL,
0,
0,
WBEM_FLAG_CONNECT_USE_MAX_WAIT,
NULL,
NULL,
&wbemServices
);
@@ -493,7 +493,7 @@ PPH_LIST PhpQueryWmiProviderHostProcess(
if (SUCCEEDED(IWbemClassObject_Get(wbemClassObject, L"Namespace", 0, &variant, 0, 0)))
{
entry->NamespacePath = PhCreateString(variant.bstrVal);
entry->NamespacePath = PhCreateString(V_BSTR(&variant));
VariantClear(&variant);
}
@@ -501,7 +501,7 @@ PPH_LIST PhpQueryWmiProviderHostProcess(
if (SUCCEEDED(IWbemClassObject_Get(wbemClassObject, L"Provider", 0, &variant, 0, 0)))
{
entry->ProviderName = PhCreateString(variant.bstrVal);
entry->ProviderName = PhCreateString(V_BSTR(&variant));
VariantClear(&variant);
}
@@ -509,7 +509,7 @@ PPH_LIST PhpQueryWmiProviderHostProcess(
if (SUCCEEDED(IWbemClassObject_Get(wbemClassObject, L"User", 0, &variant, 0, 0)))
{
entry->UserName = PhCreateString(variant.bstrVal);
entry->UserName = PhCreateString(V_BSTR(&variant));
VariantClear(&variant);
}
@@ -879,3 +879,393 @@ INT_PTR CALLBACK PhpProcessWmiProvidersDlgProc(
return FALSE;
}
NTSTATUS PhGetWmiNamespaceSecurityDescriptor(
_Out_ PSECURITY_DESCRIPTOR* SecurityDescriptor
)
{
HRESULT status;
PVOID imageBaseAddress;
PVOID securityDescriptor = NULL;
PVOID securityDescriptorData = NULL;
PPH_STRING querySelectString = NULL;
BSTR wbemResourceString = NULL;
BSTR wbemLanguageString = NULL;
BSTR wbemQueryString = NULL;
BSTR wbemMethodString = NULL;
IWbemLocator* wbemLocator = NULL;
IWbemServices* wbemServices = NULL;
IWbemClassObject* wbemClassObject = NULL;
IWbemClassObject* wbemGetSDClassObject = 0;
VARIANT variantReturnValue = { VT_EMPTY };
VARIANT variantArrayValue = { VT_EMPTY };
if (!(imageBaseAddress = PhpGetWmiProviderDllBase()))
return STATUS_UNSUCCESSFUL;
status = PhGetClassObjectDllBase(
imageBaseAddress,
&CLSID_WbemLocator,
&IID_IWbemLocator,
&wbemLocator
);
if (FAILED(status))
goto CleanupExit;
wbemResourceString = SysAllocString(L"Root");
status = IWbemLocator_ConnectServer(
wbemLocator,
wbemResourceString,
NULL,
NULL,
NULL,
WBEM_FLAG_CONNECT_USE_MAX_WAIT,
NULL,
NULL,
&wbemServices
);
if (FAILED(status))
goto CleanupExit;
wbemQueryString = SysAllocString(L"__SystemSecurity");
status = IWbemServices_GetObject(
wbemServices,
wbemQueryString,
0,
NULL,
&wbemClassObject,
NULL
);
if (FAILED(status))
goto CleanupExit;
wbemMethodString = SysAllocString(L"GetSD");
status = IWbemServices_ExecMethod(
wbemServices,
wbemQueryString,
wbemMethodString,
0,
NULL,
wbemClassObject,
&wbemGetSDClassObject,
NULL
);
if (FAILED(status))
goto CleanupExit;
status = IWbemClassObject_Get(
wbemGetSDClassObject,
L"ReturnValue",
0,
&variantReturnValue,
NULL,
NULL
);
if (FAILED(status))
goto CleanupExit;
if (V_UI4(&variantReturnValue) != ERROR_SUCCESS)
{
status = HRESULT_FROM_WIN32(V_UI4(&variantReturnValue));
goto CleanupExit;
}
status = IWbemClassObject_Get(
wbemGetSDClassObject,
L"SD",
0,
&variantArrayValue,
NULL,
NULL
);
if (FAILED(status))
goto CleanupExit;
status = SafeArrayAccessData(V_ARRAY(&variantArrayValue), &securityDescriptorData);
if (FAILED(status))
goto CleanupExit;
// The Wbem security descriptor is relative but the ACUI
// dialog automatically converts the descriptor for us
// so we don't have to convert just validate. (dmex)
if (RtlValidSecurityDescriptor(securityDescriptorData))
{
securityDescriptor = PhAllocateCopy(
securityDescriptorData,
RtlLengthSecurityDescriptor(securityDescriptorData)
);
}
SafeArrayUnaccessData(V_ARRAY(&variantArrayValue));
CleanupExit:
if (wbemGetSDClassObject)
IWbemClassObject_Release(wbemGetSDClassObject);
if (wbemServices)
IWbemServices_Release(wbemServices);
if (wbemClassObject)
IWbemClassObject_Release(wbemClassObject);
if (wbemLocator)
IWbemLocator_Release(wbemLocator);
VariantClear(&variantArrayValue);
VariantClear(&variantReturnValue);
if (wbemMethodString)
SysFreeString(wbemMethodString);
if (wbemQueryString)
SysFreeString(wbemQueryString);
if (wbemLanguageString)
SysFreeString(wbemLanguageString);
if (wbemResourceString)
SysFreeString(wbemResourceString);
if (querySelectString)
PhDereferenceObject(querySelectString);
if (SUCCEEDED(status) && securityDescriptor)
{
*SecurityDescriptor = securityDescriptor;
return STATUS_SUCCESS;
}
if (securityDescriptor)
{
PhFree(securityDescriptor);
}
if (status == WBEM_E_ACCESS_DENIED)
return STATUS_ACCESS_DENIED;
if (status == WBEM_E_INVALID_PARAMETER)
return STATUS_INVALID_PARAMETER;
return STATUS_INVALID_SECURITY_DESCR;
}
NTSTATUS PhSetWmiNamespaceSecurityDescriptor(
_In_ PSECURITY_DESCRIPTOR SecurityDescriptor
)
{
HRESULT status;
PVOID imageBaseAddress;
PPH_STRING querySelectString = NULL;
BSTR wbemResourceString = NULL;
BSTR wbemLanguageString = NULL;
BSTR wbemQueryString = NULL;
BSTR wbemMethodString = NULL;
IWbemLocator* wbemLocator = NULL;
IWbemServices* wbemServices = NULL;
IWbemClassObject* wbemClassObject = NULL;
IWbemClassObject* wbemSetSDClassObject = NULL;
PVOID safeArrayData = NULL;
LPSAFEARRAY safeArray = NULL;
SAFEARRAYBOUND safeArrayBounds;
PSECURITY_DESCRIPTOR relativeSecurityDescriptor = 0;
ULONG relativeSecurityDescriptorLength = 0;
BOOLEAN freeSecurityDescriptor = FALSE;
VARIANT variantArrayValue = { VT_EMPTY };
VARIANT variantReturnValue = { VT_EMPTY };
if (!(imageBaseAddress = PhpGetWmiProviderDllBase()))
return STATUS_UNSUCCESSFUL;
status = PhGetClassObjectDllBase(
imageBaseAddress,
&CLSID_WbemLocator,
&IID_IWbemLocator,
&wbemLocator
);
if (FAILED(status))
goto CleanupExit;
wbemResourceString = SysAllocString(L"Root");
status = IWbemLocator_ConnectServer(
wbemLocator,
wbemResourceString,
NULL,
NULL,
NULL,
WBEM_FLAG_CONNECT_USE_MAX_WAIT,
NULL,
NULL,
&wbemServices
);
if (FAILED(status))
goto CleanupExit;
wbemQueryString = SysAllocString(L"__SystemSecurity");
status = IWbemServices_GetObject(
wbemServices,
wbemQueryString,
0,
NULL,
&wbemClassObject,
NULL
);
if (FAILED(status))
goto CleanupExit;
if (RtlValidRelativeSecurityDescriptor(
SecurityDescriptor,
RtlLengthSecurityDescriptor(SecurityDescriptor),
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION
))
{
relativeSecurityDescriptor = SecurityDescriptor;
relativeSecurityDescriptorLength = RtlLengthSecurityDescriptor(SecurityDescriptor);
}
else
{
NTSTATUS ntstatus;
ntstatus = RtlAbsoluteToSelfRelativeSD(
SecurityDescriptor,
NULL,
&relativeSecurityDescriptorLength
);
if (ntstatus != STATUS_BUFFER_TOO_SMALL)
{
status = HRESULT_FROM_NT(ntstatus);
goto CleanupExit;
}
relativeSecurityDescriptor = PhAllocate(relativeSecurityDescriptorLength);
ntstatus = RtlAbsoluteToSelfRelativeSD(
SecurityDescriptor,
relativeSecurityDescriptor,
&relativeSecurityDescriptorLength
);
if (!NT_SUCCESS(ntstatus))
{
PhFree(relativeSecurityDescriptor);
status = HRESULT_FROM_NT(ntstatus);
goto CleanupExit;
}
freeSecurityDescriptor = TRUE;
}
safeArrayBounds.lLbound = 0;
safeArrayBounds.cElements = relativeSecurityDescriptorLength;
if (!(safeArray = SafeArrayCreate(VT_UI1, 1, &safeArrayBounds)))
{
status = STATUS_NO_MEMORY;
goto CleanupExit;
}
status = SafeArrayAccessData(safeArray, &safeArrayData);
if (FAILED(status))
goto CleanupExit;
memcpy(
safeArrayData,
relativeSecurityDescriptor,
relativeSecurityDescriptorLength
);
status = SafeArrayUnaccessData(safeArray);
if (FAILED(status))
goto CleanupExit;
V_VT(&variantArrayValue) = VT_ARRAY | VT_UI1;
V_ARRAY(&variantArrayValue) = safeArray;
status = IWbemClassObject_Put(
wbemClassObject,
L"SD",
0,
&variantArrayValue,
CIM_EMPTY
);
if (FAILED(status))
goto CleanupExit;
wbemMethodString = SysAllocString(L"SetSD");
status = IWbemServices_ExecMethod(
wbemServices,
wbemQueryString,
wbemMethodString,
0,
NULL,
wbemClassObject,
&wbemSetSDClassObject,
NULL
);
if (FAILED(status))
goto CleanupExit;
status = IWbemClassObject_Get(
wbemSetSDClassObject,
L"ReturnValue",
0,
&variantReturnValue,
NULL,
NULL
);
if (FAILED(status))
goto CleanupExit;
if (V_UI4(&variantReturnValue) != ERROR_SUCCESS)
{
status = HRESULT_FROM_WIN32(V_UI4(&variantReturnValue));
}
CleanupExit:
if (wbemSetSDClassObject)
IWbemClassObject_Release(wbemSetSDClassObject);
if (wbemClassObject)
IWbemClassObject_Release(wbemClassObject);
if (wbemServices)
IWbemServices_Release(wbemServices);
if (wbemLocator)
IWbemLocator_Release(wbemLocator);
if (freeSecurityDescriptor && relativeSecurityDescriptor)
PhFree(relativeSecurityDescriptor);
VariantClear(&variantReturnValue);
VariantClear(&variantArrayValue);
//if (safeArray) SafeArrayDestroy(safeArray);
if (wbemMethodString)
SysFreeString(wbemMethodString);
if (wbemQueryString)
SysFreeString(wbemQueryString);
if (wbemLanguageString)
SysFreeString(wbemLanguageString);
if (wbemResourceString)
SysFreeString(wbemResourceString);
if (querySelectString)
PhDereferenceObject(querySelectString);
if (SUCCEEDED(status))
{
return STATUS_SUCCESS;
}
if (status == WBEM_E_ACCESS_DENIED)
return STATUS_ACCESS_DENIED;
if (status == WBEM_E_INVALID_PARAMETER)
return STATUS_INVALID_PARAMETER;
return STATUS_INVALID_SECURITY_DESCR;
}
+4 -2
View File
@@ -745,8 +745,10 @@
#define ID_GROUP_RESET 40299
#define ID_TOOLS_SCM_PERMISSIONS 40300
#define ID_TOOLS_RDP_PERMISSIONS 40301
#define ID_UIACCESS_REMOVE 40302
#define ID_TOOLS_LIVEDUMP 40303
#define ID_TOOLS_PWR_PERMISSIONS 40302
#define ID_TOOLS_WMI_PERMISSIONS 40303
#define ID_UIACCESS_REMOVE 40304
#define ID_TOOLS_LIVEDUMP 40305
#define IDDYNAMIC 50000
#define IDPLUGINS 55000
+30
View File
@@ -295,4 +295,34 @@ HRESULT STDMETHODCALLTYPE PhSecurityObjectTypeInfo_GetInheritSource(
_Out_ PINHERITED_FROM *InheritArray
);
// Power policy (Todo: Move to better location) (dmex)
NTSTATUS PhpGetPowerPolicySecurityDescriptor(
_Out_ PPH_STRING* StringSecurityDescriptor
);
NTSTATUS PhpSetPowerPolicySecurityDescriptor(
_In_ PPH_STRING StringSecurityDescriptor
);
// Terminal server policy (Todo: Move to better location) (dmex)
NTSTATUS PhpGetRemoteDesktopSecurityDescriptor(
_Out_ PSECURITY_DESCRIPTOR* SecurityDescriptor
);
NTSTATUS PhpSetRemoteDesktopSecurityDescriptor(
_In_ PSECURITY_DESCRIPTOR SecurityDescriptor
);
// Wbem namespace policy (forward exports from prpgwmi.c) (dmex)
NTSTATUS PhGetWmiNamespaceSecurityDescriptor(
_Out_ PSECURITY_DESCRIPTOR* SecurityDescriptor
);
NTSTATUS PhSetWmiNamespaceSecurityDescriptor(
_In_ PSECURITY_DESCRIPTOR SecurityDescriptor
);
#endif
+66 -1
View File
@@ -24,6 +24,8 @@
#include <ph.h>
#include <secedit.h>
#include <wmistr.h>
#include <wbemcli.h>
#include <wtsapi32.h>
#define ACCESS_ENTRIES(Type) static PH_ACCESS_ENTRY Ph##Type##AccessEntries[] =
#define ACCESS_ENTRY(Type, HasSynchronize) \
@@ -569,6 +571,18 @@ ACCESS_ENTRIES(WaitCompletionPacket)
{ L"Modify state", OBJECT_TYPE_CREATE, TRUE, TRUE }
};
ACCESS_ENTRIES(Wbem)
{
{ L"Enable account", WBEM_ENABLE, TRUE, TRUE },
{ L"Execute methods", WBEM_METHOD_EXECUTE, TRUE, TRUE },
{ L"Full write", WBEM_FULL_WRITE_REP, TRUE, TRUE },
{ L"Partial write", WBEM_PARTIAL_WRITE_REP, TRUE, TRUE },
{ L"Provider write", WBEM_WRITE_PROVIDER, TRUE, TRUE },
{ L"Remote enable", WBEM_REMOTE_ACCESS, TRUE, TRUE },
{ L"Get notifications", WBEM_RIGHT_SUBSCRIBE, TRUE, TRUE },
{ L"Read description", WBEM_RIGHT_PUBLISH, TRUE, TRUE }
};
ACCESS_ENTRIES(WindowStation)
{
{ L"Full control", WINSTA_ALL_ACCESS | STANDARD_RIGHTS_REQUIRED, TRUE, TRUE },
@@ -606,6 +620,25 @@ ACCESS_ENTRIES(WmiGuid)
{ L"Register provider GUIDs", TRACELOG_REGISTER_GUIDS, FALSE, TRUE, L"Register GUIDs" }
};
ACCESS_ENTRIES(Rdp)
{
{ L"Full control", WTS_SECURITY_ALL_ACCESS, TRUE, TRUE },
{ L"Query information", WTS_SECURITY_QUERY_INFORMATION, TRUE, TRUE },
{ L"Set information", WTS_SECURITY_SET_INFORMATION, TRUE, TRUE },
{ L"Reset", WTS_SECURITY_RESET, FALSE, TRUE },
{ L"Virtual channels", WTS_SECURITY_VIRTUAL_CHANNELS, FALSE, TRUE },
{ L"Remote control", WTS_SECURITY_REMOTE_CONTROL, FALSE, TRUE },
{ L"Logon", WTS_SECURITY_LOGON, FALSE, TRUE },
{ L"Logoff", WTS_SECURITY_LOGOFF, FALSE, TRUE },
{ L"Message", WTS_SECURITY_MESSAGE, FALSE, TRUE },
{ L"Connect", WTS_SECURITY_CONNECT, FALSE, TRUE },
{ L"Disconnect", WTS_SECURITY_DISCONNECT, FALSE, TRUE },
{ L"Guest access", WTS_SECURITY_GUEST_ACCESS, FALSE, TRUE },
{ L"Guest access (current)", WTS_SECURITY_CURRENT_GUEST_ACCESS, FALSE, TRUE },
{ L"User access", WTS_SECURITY_USER_ACCESS, FALSE, TRUE },
{ L"User access (current)", WTS_SECURITY_SET_INFORMATION | WTS_SECURITY_RESET | WTS_SECURITY_VIRTUAL_CHANNELS | WTS_SECURITY_LOGOFF | WTS_SECURITY_DISCONNECT, FALSE, TRUE }, // WTS_SECURITY_CURRENT_USER_ACCESS
};
static PH_SPECIFIC_TYPE PhSpecificTypes[] =
{
ACCESS_ENTRY(AlpcPort, TRUE),
@@ -653,8 +686,10 @@ static PH_SPECIFIC_TYPE PhSpecificTypes[] =
ACCESS_ENTRY(TpWorkerFactory, FALSE),
ACCESS_ENTRY(Type, FALSE),
ACCESS_ENTRY(WaitCompletionPacket, FALSE),
ACCESS_ENTRY(Wbem, FALSE),
ACCESS_ENTRY(WindowStation, FALSE),
ACCESS_ENTRY(WmiGuid, TRUE)
ACCESS_ENTRY(WmiGuid, TRUE),
ACCESS_ENTRY(Rdp, FALSE),
};
/**
@@ -701,6 +736,36 @@ BOOLEAN PhGetAccessEntries(
{
Type = L"File";
}
else if (PhEqualStringZ(Type, L"PowerDefault", TRUE))
{
Type = L"Key";
}
else if (PhEqualStringZ(Type, L"RdpDefault", TRUE))
{
Type = L"Rdp";
}
else if (PhEqualStringZ(Type, L"WmiDefault", TRUE))
{
// WBEM doesn't allow StandardAccessEntries (dmex)
for (i = 0; i < RTL_NUMBER_OF(PhSpecificTypes); i++)
{
if (PhEqualStringZ(PhSpecificTypes[i].Type, L"Wbem", TRUE))
{
specificType = &PhSpecificTypes[i];
break;
}
}
if (specificType)
{
accessEntries = PhAllocate(specificType->SizeOfAccessEntries);
memcpy(accessEntries, specificType->AccessEntries, specificType->SizeOfAccessEntries);
*AccessEntries = accessEntries;
*NumberOfAccessEntries = specificType->SizeOfAccessEntries / sizeof(PH_ACCESS_ENTRY);
return TRUE;
}
}
// Find the specific type.
for (i = 0; i < sizeof(PhSpecificTypes) / sizeof(PH_SPECIFIC_TYPE); i++)
+425 -12
View File
@@ -3,7 +3,7 @@
* object security editor
*
* Copyright (C) 2010-2016 wj32
* Copyright (C) 2017-2019 dmex
* Copyright (C) 2017-2021 dmex
*
* This file is part of Process Hacker.
*
@@ -342,11 +342,29 @@ HRESULT STDMETHODCALLTYPE PhSecurityInformation_GetObjectInformation(
ObjectInfo->dwFlags |= SI_ENABLE_EDIT_ATTRIBUTE_CONDITION | SI_MAY_WRITE; // SI_RESET | SI_READONLY
//if (Folder) ObjectInfo->dwFlags |= SI_CONTAINER;
}
if (PhEqualString2(this->ObjectType, L"TokenDefault", TRUE))
{
ObjectInfo->dwFlags &= ~(SI_EDIT_OWNER | SI_EDIT_AUDITS);
}
if (PhEqualString2(this->ObjectType, L"PowerDefault", TRUE))
{
ObjectInfo->dwFlags &= ~SI_EDIT_AUDITS;
ObjectInfo->dwFlags |= SI_NO_ACL_PROTECT | SI_NO_TREE_APPLY | SI_CONTAINER | SI_OWNER_READONLY;
}
if (PhEqualString2(this->ObjectType, L"RdpDefault", TRUE))
{
ObjectInfo->dwFlags &= ~SI_EDIT_OWNER;
ObjectInfo->dwFlags |= SI_NO_ACL_PROTECT | SI_NO_TREE_APPLY;
}
if (PhEqualString2(this->ObjectType, L"WmiDefault", TRUE))
{
ObjectInfo->dwFlags |= SI_CONTAINER | SI_OWNER_READONLY;
}
return S_OK;
}
@@ -492,20 +510,41 @@ HRESULT STDMETHODCALLTYPE PhSecurityInformation_GetInheritTypes(
_Out_ PULONG InheritTypesCount
)
{
static SI_INHERIT_TYPE inheritTypes[] =
{
0, 0, L"This folder only",
0, CONTAINER_INHERIT_ACE, L"This folder, subfolders and files",
0, INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE, L"Subfolders and files only",
};
PhSecurityInformation* this = (PhSecurityInformation*)This;
// if (Folder-Container)
*InheritTypes = inheritTypes;
*InheritTypesCount = RTL_NUMBER_OF(inheritTypes);
return S_OK;
// else
if (PhEqualString2(this->ObjectType, L"WmiDefault", TRUE))
{
static SI_INHERIT_TYPE inheritTypes[] =
{
0, 0, L"This namespace only",
0, CONTAINER_INHERIT_ACE, L"This namespace and subnamespaces",
0, INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE, L"Subnamespaces only",
};
*InheritTypes = inheritTypes;
*InheritTypesCount = RTL_NUMBER_OF(inheritTypes);
return S_OK;
}
else
{
static SI_INHERIT_TYPE inheritTypes[] =
{
0, 0, L"This folder only",
0, CONTAINER_INHERIT_ACE, L"This folder, subfolders and files",
0, INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE, L"Subfolders and files only",
};
*InheritTypes = inheritTypes;
*InheritTypesCount = RTL_NUMBER_OF(inheritTypes);
return S_OK;
}
//if (Folder-Container)
//*InheritTypes = inheritTypes;
//*InheritTypesCount = RTL_NUMBER_OF(inheritTypes);
//return S_OK;
//else
//return E_NOTIMPL;
}
@@ -1178,6 +1217,55 @@ _Callback_ NTSTATUS PhStdGetObjectSecurity(
NtClose(handle);
}
else if (PhEqualString2(this->ObjectType, L"PowerDefault", TRUE))
{
PSECURITY_DESCRIPTOR securityDescriptor;
PPH_STRING powerPolicySddl;
if (NT_SUCCESS(status = PhpGetPowerPolicySecurityDescriptor(&powerPolicySddl)))
{
if (securityDescriptor = PhGetSecurityDescriptorFromString(PhGetString(powerPolicySddl)))
{
*SecurityDescriptor = PhAllocateCopy(
securityDescriptor,
RtlLengthSecurityDescriptor(securityDescriptor)
);
PhFree(securityDescriptor);
}
else
{
status = STATUS_INVALID_SECURITY_DESCR;
}
PhDereferenceObject(powerPolicySddl);
}
}
else if (PhEqualString2(this->ObjectType, L"RdpDefault", TRUE))
{
PSECURITY_DESCRIPTOR securityDescriptor;
if (NT_SUCCESS(status = PhpGetRemoteDesktopSecurityDescriptor(&securityDescriptor)))
{
*SecurityDescriptor = PhAllocateCopy(
securityDescriptor,
RtlLengthSecurityDescriptor(securityDescriptor)
);
PhFree(securityDescriptor);
}
}
else if (PhEqualString2(this->ObjectType, L"WmiDefault", TRUE))
{
PSECURITY_DESCRIPTOR securityDescriptor;
if (NT_SUCCESS(status = PhGetWmiNamespaceSecurityDescriptor(&securityDescriptor)))
{
*SecurityDescriptor = PhAllocateCopy(
securityDescriptor,
RtlLengthSecurityDescriptor(securityDescriptor)
);
PhFree(securityDescriptor);
}
}
else
{
status = PhGetObjectSecurity(handle, SecurityInformation, SecurityDescriptor);
@@ -1298,6 +1386,59 @@ _Callback_ NTSTATUS PhStdSetObjectSecurity(
NtClose(handle);
}
else if (PhEqualString2(this->ObjectType, L"PowerDefault", TRUE))
{
PPH_STRING powerPolicySddl;
SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
UCHAR administratorsSidBuffer[FIELD_OFFSET(SID, SubAuthority) + sizeof(ULONG) * 2];
PSID administratorsSid;
// kludge the descriptor into the correct SDDL format required by powercfg (dmex)
// 1) The owner must always be the built-in domain administrator.
// 2) The group must always be NT AUTHORITY\SYSTEM.
// 3) Remove the INHERIT_REQ flag (not required but makes the sddl consistent with powercfg).
administratorsSid = (PSID)administratorsSidBuffer;
RtlInitializeSid(administratorsSid, &ntAuthority, 2);
*RtlSubAuthoritySid(administratorsSid, 0) = SECURITY_BUILTIN_DOMAIN_RID;
*RtlSubAuthoritySid(administratorsSid, 1) = DOMAIN_ALIAS_RID_ADMINS;
RtlSetOwnerSecurityDescriptor(SecurityDescriptor, administratorsSid, TRUE);
RtlSetGroupSecurityDescriptor(SecurityDescriptor, &PhSeLocalSystemSid, TRUE);
RtlSetControlSecurityDescriptor(SecurityDescriptor, SE_DACL_PROTECTED | SE_DACL_AUTO_INHERIT_REQ, SE_DACL_PROTECTED);
if (powerPolicySddl = PhGetSecurityDescriptorAsString(
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
SecurityDescriptor
))
{
status = PhpSetPowerPolicySecurityDescriptor(powerPolicySddl);
PhDereferenceObject(powerPolicySddl);
}
}
else if (PhEqualString2(this->ObjectType, L"RdpDefault", TRUE))
{
status = PhpSetRemoteDesktopSecurityDescriptor(SecurityDescriptor);
}
else if (PhEqualString2(this->ObjectType, L"WmiDefault", TRUE))
{
SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY;
UCHAR administratorsSidBuffer[FIELD_OFFSET(SID, SubAuthority) + sizeof(ULONG) * 2];
PSID administratorsSid;
// kludge the descriptor into the correct format required by wmimgmt (dmex)
// 1) The owner must always be the built-in domain administrator.
// 2) The group must always be the built-in domain administrator.
administratorsSid = (PSID)administratorsSidBuffer;
RtlInitializeSid(administratorsSid, &ntAuthority, 2);
*RtlSubAuthoritySid(administratorsSid, 0) = SECURITY_BUILTIN_DOMAIN_RID;
*RtlSubAuthoritySid(administratorsSid, 1) = DOMAIN_ALIAS_RID_ADMINS;
RtlSetOwnerSecurityDescriptor(SecurityDescriptor, administratorsSid, TRUE);
RtlSetGroupSecurityDescriptor(SecurityDescriptor, administratorsSid, TRUE);
status = PhSetWmiNamespaceSecurityDescriptor(SecurityDescriptor);
}
else
{
status = PhSetObjectSecurity(handle, SecurityInformation, SecurityDescriptor);
@@ -1417,3 +1558,275 @@ NTSTATUS PhSetSeObjectSecurity(
return STATUS_SUCCESS;
}
// Power policy security descriptors
// HACK - nightly build feature testing (dmex)
// TODO - move definitions to proper locations.
#include <powrprof.h>
ULONG (WINAPI* PowerGetActiveScheme_I)(
_In_ HKEY UserRootPowerKey,
_Out_ PGUID* ActivePolicyGuid
) = NULL;
ULONG (WINAPI* PowerSetActiveScheme_I)(
_In_ HKEY UserRootPowerKey,
_In_ PGUID SchemeGuid
) = NULL;
ULONG (WINAPI* PowerRestoreDefaultPowerSchemes_I)(
VOID
) = NULL;
// rev
ULONG (WINAPI* PowerReadSecurityDescriptor)(
_In_ POWER_DATA_ACCESSOR AccessFlags,
_In_ PGUID PowerGuid,
_Out_ PWSTR* StringSecurityDescriptor
) = NULL;
// rev
ULONG (WINAPI* PowerWriteSecurityDescriptor)(
_In_ POWER_DATA_ACCESSOR AccessFlags,
_In_ PGUID PowerGuid,
_In_ PWSTR StringSecurityDescriptor
) = NULL;
PVOID PhpInitializePowerPolicyApi(VOID)
{
static PH_INITONCE initOnce = PH_INITONCE_INIT;
static PVOID imageBaseAddress = NULL;
if (PhBeginInitOnce(&initOnce))
{
imageBaseAddress = LoadLibrary(L"powrprof.dll");
PhEndInitOnce(&initOnce);
}
return imageBaseAddress;
}
NTSTATUS PhpGetPowerPolicySecurityDescriptor(
_Out_ PPH_STRING* StringSecurityDescriptor
)
{
ULONG status;
PVOID imageBaseAddress;
PWSTR stringSecurityDescriptor;
PGUID policyGuid;
if (!(imageBaseAddress = PhpInitializePowerPolicyApi()))
return STATUS_DLL_NOT_FOUND;
if (!PowerGetActiveScheme_I)
PowerGetActiveScheme_I = PhGetDllBaseProcedureAddress(imageBaseAddress, "PowerGetActiveScheme", 0);
if (!PowerReadSecurityDescriptor)
PowerReadSecurityDescriptor = PhGetDllBaseProcedureAddress(imageBaseAddress, "PowerReadSecurityDescriptor", 0);
if (!(PowerGetActiveScheme_I && PowerReadSecurityDescriptor))
return STATUS_PROCEDURE_NOT_FOUND;
// We can use GUIDs for schemes, policies or other values but we only query the default scheme. (dmex)
status = PowerGetActiveScheme_I(
NULL,
&policyGuid
);
if (status != ERROR_SUCCESS)
return NTSTATUS_FROM_WIN32(status);
// PowerReadSecurityDescriptor/PowerWriteSecurityDescriptor both use the same SDDL
// format as registry keys and are RPC wrappers for this registry key:
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\SecurityDescriptors
status = PowerReadSecurityDescriptor(
ACCESS_DEFAULT_SECURITY_DESCRIPTOR, // ACCESS_ACTIVE_SCHEME?
policyGuid,
&stringSecurityDescriptor
);
if (status == ERROR_SUCCESS)
{
if (StringSecurityDescriptor)
{
*StringSecurityDescriptor = PhCreateString(stringSecurityDescriptor);
}
LocalFree(stringSecurityDescriptor);
}
LocalFree(policyGuid);
return NTSTATUS_FROM_WIN32(status);
}
NTSTATUS PhpSetPowerPolicySecurityDescriptor(
_In_ PPH_STRING StringSecurityDescriptor
)
{
ULONG status;
PVOID imageBaseAddress;
PGUID policyGuid;
if (!(imageBaseAddress = PhpInitializePowerPolicyApi()))
return STATUS_DLL_NOT_FOUND;
if (!PowerGetActiveScheme_I)
PowerGetActiveScheme_I = PhGetDllBaseProcedureAddress(imageBaseAddress, "PowerGetActiveScheme", 0);
if (!PowerWriteSecurityDescriptor)
PowerWriteSecurityDescriptor = PhGetDllBaseProcedureAddress(imageBaseAddress, "PowerWriteSecurityDescriptor", 0);
if (!(PowerGetActiveScheme_I && PowerWriteSecurityDescriptor))
return STATUS_PROCEDURE_NOT_FOUND;
status = PowerGetActiveScheme_I(
NULL,
&policyGuid
);
if (status != ERROR_SUCCESS)
return NTSTATUS_FROM_WIN32(status);
status = PowerWriteSecurityDescriptor(
ACCESS_DEFAULT_SECURITY_DESCRIPTOR,
policyGuid, // Todo: Pass the GUID from the original query.
PhGetString(StringSecurityDescriptor)
);
//if (status == ERROR_SUCCESS)
//{
// // refresh/reapply the current scheme.
// status = PowerSetActiveScheme_I(NULL, policyGuid);
//}
LocalFree(policyGuid);
return NTSTATUS_FROM_WIN32(status);
}
// Terminal server security descriptors
// HACK - nightly build feature testing (dmex)
// TODO - move definitions to proper locations.
#include <wtsapi32.h>
BOOL (WINAPI* WTSGetListenerSecurity_I)(
_In_ HANDLE ServerHandle,
_In_ PVOID Reserved1,
_In_ ULONG Reserved2,
_In_ PWSTR ListenerName,
_In_ SECURITY_INFORMATION SecurityInformation,
_Out_opt_ PSECURITY_DESCRIPTOR SecurityDescriptor,
_In_ ULONG Length,
_Out_ PULONG LengthNeeded
) = NULL;
BOOL (WINAPI* WTSSetListenerSecurity_I)(
_In_ HANDLE ServerHandle,
_In_ PVOID Reserved1,
_In_ ULONG Reserved2,
_In_ PWSTR ListenerName,
_In_ SECURITY_INFORMATION SecurityInformation,
_In_ PSECURITY_DESCRIPTOR SecurityDescriptor
) = NULL;
PVOID PhpInitializeRemoteDesktopServiceApi(VOID)
{
static PH_INITONCE initOnce = PH_INITONCE_INIT;
static PVOID imageBaseAddress = NULL;
if (PhBeginInitOnce(&initOnce))
{
imageBaseAddress = LoadLibrary(L"wtsapi32.dll");
PhEndInitOnce(&initOnce);
}
return imageBaseAddress;
}
NTSTATUS PhpGetRemoteDesktopSecurityDescriptor(
_Out_ PSECURITY_DESCRIPTOR* SecurityDescriptor
)
{
BOOL status;
PVOID imageBaseAddress;
ULONG securityDescriptorLength = 0;
PSECURITY_DESCRIPTOR securityDescriptor = NULL;
if (!(imageBaseAddress = PhpInitializeRemoteDesktopServiceApi()))
return STATUS_DLL_NOT_FOUND;
if (!WTSGetListenerSecurity_I)
WTSGetListenerSecurity_I = PhGetDllBaseProcedureAddress(imageBaseAddress, "WTSGetListenerSecurityW", 0);
if (!WTSGetListenerSecurity_I)
return STATUS_PROCEDURE_NOT_FOUND;
// Todo: Add support for SI_RESET using the default security descriptor:
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\DefaultSecurity
status = WTSGetListenerSecurity_I(
WTS_CURRENT_SERVER_HANDLE,
NULL,
0,
L"Rdp-Tcp", // or "Console"
DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
securityDescriptor,
0,
&securityDescriptorLength
);
if (!(!status && securityDescriptorLength))
return STATUS_INVALID_SECURITY_DESCR;
securityDescriptor = PhAllocateZero(securityDescriptorLength);
status = WTSGetListenerSecurity_I(
WTS_CURRENT_SERVER_HANDLE,
NULL,
0,
L"Rdp-Tcp", // or "Console"
DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
securityDescriptor,
securityDescriptorLength,
&securityDescriptorLength
);
if (status)
{
if (SecurityDescriptor)
{
*SecurityDescriptor = securityDescriptor;
return STATUS_SUCCESS;
}
}
return STATUS_INVALID_SECURITY_DESCR;
}
NTSTATUS PhpSetRemoteDesktopSecurityDescriptor(
_In_ PSECURITY_DESCRIPTOR SecurityDescriptor
)
{
PVOID imageBaseAddress;
if (!(imageBaseAddress = PhpInitializeRemoteDesktopServiceApi()))
return STATUS_DLL_NOT_FOUND;
if (!WTSSetListenerSecurity_I)
WTSSetListenerSecurity_I = PhGetDllBaseProcedureAddress(imageBaseAddress, "WTSSetListenerSecurityW", 0);
if (!WTSSetListenerSecurity_I)
return STATUS_PROCEDURE_NOT_FOUND;
if (WTSSetListenerSecurity_I(
WTS_CURRENT_SERVER_HANDLE,
NULL,
0,
L"RDP-Tcp",
DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
SecurityDescriptor
))
{
return STATUS_SUCCESS;
}
return STATUS_INVALID_SECURITY_DESCR;
}