Skip to content

Commit e4550e0

Browse files
committed
Add PhAdjustPrivilege
1 parent 1b036dc commit e4550e0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

phlib/include/phnative.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,15 @@ PhSetTokenPrivilege2(
579579
_In_ ULONG Attributes
580580
);
581581

582+
PHLIBAPI
583+
NTSTATUS
584+
NTAPI
585+
PhAdjustPrivilege(
586+
_In_opt_ PWSTR PrivilegeName,
587+
_In_opt_ LONG Privilege,
588+
_In_ BOOLEAN Enable
589+
);
590+
582591
PHLIBAPI
583592
NTSTATUS
584593
NTAPI

phlib/native.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,74 @@ BOOLEAN PhSetTokenPrivilege2(
27392739
return PhSetTokenPrivilege(TokenHandle, NULL, &privilegeLuid, Attributes);
27402740
}
27412741

2742+
NTSTATUS PhAdjustPrivilege(
2743+
_In_opt_ PWSTR PrivilegeName,
2744+
_In_opt_ LONG Privilege,
2745+
_In_ BOOLEAN Enable
2746+
)
2747+
{
2748+
NTSTATUS status;
2749+
HANDLE tokenHandle;
2750+
TOKEN_PRIVILEGES privileges;
2751+
2752+
status = NtOpenProcessToken(
2753+
NtCurrentProcess(),
2754+
TOKEN_ADJUST_PRIVILEGES,
2755+
&tokenHandle
2756+
);
2757+
2758+
if (!NT_SUCCESS(status))
2759+
return status;
2760+
2761+
privileges.PrivilegeCount = 1;
2762+
privileges.Privileges[0].Attributes = Enable ? SE_PRIVILEGE_ENABLED : 0;
2763+
2764+
if (Privilege)
2765+
{
2766+
LUID privilegeLuid;
2767+
2768+
privilegeLuid = RtlConvertLongToLuid(Privilege);
2769+
2770+
privileges.Privileges[0].Luid = privilegeLuid;
2771+
}
2772+
else if (PrivilegeName)
2773+
{
2774+
PH_STRINGREF privilegeName;
2775+
2776+
PhInitializeStringRef(&privilegeName, PrivilegeName);
2777+
2778+
if (!PhLookupPrivilegeValue(
2779+
&privilegeName,
2780+
&privileges.Privileges[0].Luid
2781+
))
2782+
{
2783+
NtClose(tokenHandle);
2784+
return STATUS_UNSUCCESSFUL;
2785+
}
2786+
}
2787+
else
2788+
{
2789+
NtClose(tokenHandle);
2790+
return STATUS_INVALID_PARAMETER_1;
2791+
}
2792+
2793+
status = NtAdjustPrivilegesToken(
2794+
tokenHandle,
2795+
FALSE,
2796+
&privileges,
2797+
0,
2798+
NULL,
2799+
NULL
2800+
);
2801+
2802+
NtClose(tokenHandle);
2803+
2804+
if (status == STATUS_NOT_ALL_ASSIGNED)
2805+
return STATUS_PRIVILEGE_NOT_HELD;
2806+
2807+
return status;
2808+
}
2809+
27422810
/**
27432811
* Modifies a token group.
27442812
*

0 commit comments

Comments
 (0)