使用 SetTokenInformation returns 创建提升的令牌错误 87

Create elevated token with SetTokenInformation returns error 87

我正在尝试使用 SetTokenInformation 创建提升的令牌,但它失败并不断返回错误代码 87。

这是我的代码:

#include <Windows.h>

int main()
{
    HANDLE currentProcessToken, newTok;
    OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &currentProcessToken);
    DuplicateTokenEx(currentProcessToken, TOKEN_ALL_ACCESS, nullptr, SecurityImpersonation, TokenPrimary, &newTok);
    CloseHandle(currentProcessToken);
    TOKEN_ELEVATION elev = { 1 };
    BOOL setTokenInfo = SetTokenInformation(newTok, TokenElevation, &elev, sizeof(TOKEN_ELEVATION));
    DWORD error = GetLastError(); // is 87 which is "the parameter is incorrect"
    return 0;
}

TokenElevation 是 class 仅对 GetTokenInformation 函数有效的信息。你可以查询 TokenIsElevated 但你不能设置它。 NtSetInformationToken return STATUS_INVALID_INFO_CLASS 在这种情况下。 SetTokenInformation 将此错误转换为 ERROR_INVALID_PARAMETER。原始 NTSTATUS 错误代码,您可以通过调用 RtlGetLastNtStatus() 获得。无论如何你不能 "elevate" 已经存在的令牌。这是设计使然