Add PhGetProcessIsStronglyNamed, PhGetProcessIsFullTrustPackage and update SID wrappers

This commit is contained in:
dmex
2023-03-15 01:40:42 +11:00
parent 6fc745fc18
commit 0b0af4714f
2 changed files with 134 additions and 18 deletions
+116 -2
View File
@@ -676,7 +676,7 @@ PhDoesTokenSecurityAttributeExist(
PHLIBAPI
BOOLEAN
NTAPI
PhIsTokenFullTrustPackage(
PhGetTokenIsFullTrustPackage(
_In_ HANDLE TokenHandle
);
@@ -704,6 +704,21 @@ PhGetTokenPackageFullName(
_In_ HANDLE TokenHandle
);
PHLIBAPI
NTSTATUS
NTAPI
PhGetProcessIsStronglyNamed(
_In_ HANDLE ProcessHandle,
_Out_ PBOOLEAN IsStronglyNamed
);
PHLIBAPI
BOOLEAN
NTAPI
PhGetProcessIsFullTrustPackage(
_In_ HANDLE ProcessHandle
);
PHLIBAPI
PPH_STRING
NTAPI
@@ -714,8 +729,9 @@ PhGetProcessPackageFullName(
// rev from RtlInitializeSid (dmex)
FORCEINLINE
BOOLEAN
NTAPI
PhInitializeSid(
_In_ PSID Sid,
_Out_ PSID Sid,
_In_ PSID_IDENTIFIER_AUTHORITY IdentifierAuthority,
_In_ UCHAR SubAuthorityCount
)
@@ -723,6 +739,12 @@ PhInitializeSid(
((PISID)Sid)->Revision = SID_REVISION;
((PISID)Sid)->IdentifierAuthority = *IdentifierAuthority;
((PISID)Sid)->SubAuthorityCount = SubAuthorityCount;
for (UCHAR i = 0; i < SubAuthorityCount; i++)
{
((PISID)Sid)->SubAuthority[i] = 0;
}
return TRUE;
}
@@ -738,9 +760,21 @@ PhLengthSid(
return UFIELD_OFFSET(SID, SubAuthority[((PISID)Sid)->SubAuthorityCount]);
}
// rev from RtlLengthRequiredSid (dmex)
FORCEINLINE
ULONG
NTAPI
PhLengthRequiredSid(
_In_ ULONG SubAuthorityCount
)
{
return UFIELD_OFFSET(SID, SubAuthority[SubAuthorityCount]);
}
// rev from RtlEqualSid (dmex)
FORCEINLINE
BOOLEAN
NTAPI
PhEqualSid(
_In_ PSID Sid1,
_In_ PSID Sid2
@@ -749,9 +783,30 @@ PhEqualSid(
return (BOOLEAN)RtlEqualMemory(Sid1, Sid2, PhLengthSid(Sid1));
}
// rev from RtlValidSid (dmex)
FORCEINLINE
BOOLEAN
NTAPI
PhValidSid(
_In_ PSID Sid
)
{
if (
((PISID)Sid) &&
((PISID)Sid)->Revision == SID_REVISION &&
((PISID)Sid)->SubAuthorityCount <= SID_MAX_SUB_AUTHORITIES
)
{
return TRUE;
}
return FALSE;
}
// rev from RtlSubAuthoritySid (dmex)
FORCEINLINE
PULONG
NTAPI
PhSubAuthoritySid(
_In_ PSID Sid,
_In_ ULONG SubAuthority
@@ -760,6 +815,65 @@ PhSubAuthoritySid(
return &((PISID)Sid)->SubAuthority[SubAuthority];
}
// rev from RtlSubAuthorityCountSid (dmex)
FORCEINLINE
PUCHAR
NTAPI
PhSubAuthorityCountSid(
_In_ PSID Sid
)
{
return &((PISID)Sid)->SubAuthorityCount;
}
// rev from RtlIdentifierAuthoritySid (dmex)
FORCEINLINE
PSID_IDENTIFIER_AUTHORITY
NTAPI
PhIdentifierAuthoritySid(
_In_ PSID Sid
)
{
return &((PISID)Sid)->IdentifierAuthority;
}
FORCEINLINE
BOOLEAN
NTAPI
PhEqualIdentifierAuthoritySid(
_In_ PSID_IDENTIFIER_AUTHORITY IdentifierAuthoritySid1,
_In_ PSID_IDENTIFIER_AUTHORITY IdentifierAuthoritySid2
)
{
return (BOOLEAN)RtlEqualMemory(IdentifierAuthoritySid1, IdentifierAuthoritySid2, sizeof(SID_IDENTIFIER_AUTHORITY));
}
// rev from RtlFreeSid (dmex)
FORCEINLINE
BOOLEAN
NTAPI
PhFreeSid(
_In_ _Post_invalid_ PSID Sid
)
{
return !!RtlFreeHeap(RtlProcessHeap(), 0, Sid);
}
// rev from RtlFreeUnicodeString (dmex)
FORCEINLINE
VOID
NTAPI
PhFreeUnicodeString(
_Inout_ _At_(UnicodeString->Buffer, _Frees_ptr_opt_ _Post_invalid_) PUNICODE_STRING UnicodeString
)
{
#ifdef PHNT_INLINE_FREE_UNICODE_STRING
RtlFreeUnicodeString(UnicodeString);
#else
RtlFreeHeap(RtlProcessHeap(), 0, UnicodeString->Buffer);
#endif
}
PHLIBAPI
NTSTATUS
NTAPI
+18 -16
View File
@@ -3071,7 +3071,7 @@ PTOKEN_SECURITY_ATTRIBUTE_V1 PhFindTokenSecurityAttributeName(
return NULL;
}
BOOLEAN PhIsTokenFullTrustPackage(
BOOLEAN PhGetTokenIsFullTrustPackage(
_In_ HANDLE TokenHandle
)
{
@@ -3110,7 +3110,7 @@ NTSTATUS PhGetProcessIsStronglyNamed(
return status;
}
BOOLEAN PhIsProcessFullTrustPackage(
BOOLEAN PhGetProcessIsFullTrustPackage(
_In_ HANDLE ProcessHandle
)
{
@@ -3184,9 +3184,9 @@ ULONG64 PhGetTokenSecurityAttributeValueUlong64(
if (NT_SUCCESS(PhGetTokenSecurityAttribute(TokenHandle, Name, &info)))
{
PTOKEN_SECURITY_ATTRIBUTE_V1 attribute = PhFindTokenSecurityAttributeName(info, Name);
PTOKEN_SECURITY_ATTRIBUTE_V1 attribute = info->Attribute.pAttributeV1;
if (attribute && attribute->ValueType == TOKEN_SECURITY_ATTRIBUTE_TYPE_UINT64 && ValueIndex < attribute->ValueCount)
if (attribute->ValueType == TOKEN_SECURITY_ATTRIBUTE_TYPE_UINT64 && ValueIndex < attribute->ValueCount)
{
value = attribute->Values.pUint64[ValueIndex];
}
@@ -3208,9 +3208,9 @@ PPH_STRING PhGetTokenSecurityAttributeValueString(
if (NT_SUCCESS(PhGetTokenSecurityAttribute(TokenHandle, Name, &info)))
{
PTOKEN_SECURITY_ATTRIBUTE_V1 attribute = PhFindTokenSecurityAttributeName(info, Name);
PTOKEN_SECURITY_ATTRIBUTE_V1 attribute = info->Attribute.pAttributeV1;
if (attribute && attribute->ValueType == TOKEN_SECURITY_ATTRIBUTE_TYPE_STRING && ValueIndex < attribute->ValueCount)
if (attribute->ValueType == TOKEN_SECURITY_ATTRIBUTE_TYPE_STRING && ValueIndex < attribute->ValueCount)
{
value = PhCreateStringFromUnicodeString(&attribute->Values.pString[ValueIndex]);
}
@@ -3233,9 +3233,9 @@ PPH_STRING PhGetTokenPackageApplicationUserModelId(
if (NT_SUCCESS(PhGetTokenSecurityAttribute(TokenHandle, &attributeName, &info)))
{
PTOKEN_SECURITY_ATTRIBUTE_V1 attribute = PhFindTokenSecurityAttributeName(info, &attributeName);
PTOKEN_SECURITY_ATTRIBUTE_V1 attribute = info->Attribute.pAttributeV1;
if (attribute && attribute->ValueType == TOKEN_SECURITY_ATTRIBUTE_TYPE_STRING && attribute->ValueCount >= 3)
if (attribute->ValueType == TOKEN_SECURITY_ATTRIBUTE_TYPE_STRING && attribute->ValueCount >= 3)
{
PPH_STRING relativeIdName;
PPH_STRING packageFamilyName;
@@ -3269,9 +3269,9 @@ PPH_STRING PhGetTokenPackageFullName(
if (NT_SUCCESS(PhGetTokenSecurityAttribute(TokenHandle, &attributeName, &info)))
{
PTOKEN_SECURITY_ATTRIBUTE_V1 attribute = PhFindTokenSecurityAttributeName(info, &attributeName);
PTOKEN_SECURITY_ATTRIBUTE_V1 attribute = info->Attribute.pAttributeV1;
if (attribute && attribute->ValueType == TOKEN_SECURITY_ATTRIBUTE_TYPE_STRING)
if (attribute->ValueType == TOKEN_SECURITY_ATTRIBUTE_TYPE_STRING)
{
packageFullName = PhCreateStringFromUnicodeString(&attribute->Values.pString[0]);
}
@@ -3610,11 +3610,11 @@ NTSTATUS PhGetTokenIntegrityLevelRID(
if (!NT_SUCCESS(status))
return status;
subAuthoritiesCount = *RtlSubAuthorityCountSid(mandatoryLabel->Label.Sid);
subAuthoritiesCount = *PhSubAuthorityCountSid(mandatoryLabel->Label.Sid);
if (subAuthoritiesCount > 0)
{
subAuthority = *RtlSubAuthoritySid(mandatoryLabel->Label.Sid, subAuthoritiesCount - 1);
subAuthority = *PhSubAuthoritySid(mandatoryLabel->Label.Sid, subAuthoritiesCount - 1);
}
else
{
@@ -3748,13 +3748,15 @@ NTSTATUS PhGetTokenProcessTrustLevelRID(
if (!trustLevel->TrustLevelSid)
return STATUS_UNSUCCESSFUL;
subAuthoritiesCount = *RtlSubAuthorityCountSid(trustLevel->TrustLevelSid);
//RtlIdentifierAuthoritySid(TokenPageContext->Capabilities->Groups[i].Sid) == (BYTE[])SECURITY_PROCESS_TRUST_AUTHORITY
if (!PhEqualIdentifierAuthoritySid(PhIdentifierAuthoritySid(trustLevel->TrustLevelSid), &(SID_IDENTIFIER_AUTHORITY)SECURITY_PROCESS_TRUST_AUTHORITY))
return STATUS_INVALID_SUB_AUTHORITY;
subAuthoritiesCount = *PhSubAuthorityCountSid(trustLevel->TrustLevelSid);
if (subAuthoritiesCount == SECURITY_PROCESS_TRUST_AUTHORITY_RID_COUNT)
{
protectionType = *RtlSubAuthoritySid(trustLevel->TrustLevelSid, 0);
protectionLevel = *RtlSubAuthoritySid(trustLevel->TrustLevelSid, 1);
protectionType = *PhSubAuthoritySid(trustLevel->TrustLevelSid, 0);
protectionLevel = *PhSubAuthoritySid(trustLevel->TrustLevelSid, 1);
}
else
{