试图在Windows中更改互斥对象的所有者

时间:2015-07-31 14:57:25

标签: c++ windows object ownership

我正在尝试更改互斥锁对象的所有权,但似乎此代码无效,尝试将“Everyone”设置为所有者,我收到ERROR_INVALID_OWNER错误代码,是否有任何想法?

#include <Aclapi.h>

BOOLEAN SetEveryone(HANDLE handle)
{
    SID_IDENTIFIER_AUTHORITY Auth = SECURITY_WORLD_SID_AUTHORITY;
    PSID pSID = NULL;

    if (AllocateAndInitializeSid(
        &Auth, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pSID))
    {
        if (SetSecurityInfo(
            handle,
            SE_KERNEL_OBJECT,
            OWNER_SECURITY_INFORMATION,
            pSID,
            NULL,
            NULL,
            NULL) == ERROR_SUCCESS)
        {
            printf("success\n");
        }

        FreeSid(pSID);
    }

    return TRUE;
}

int main()
{
    HANDLE Token;
    LUID luid;
    TOKEN_PRIVILEGES privs;

    OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &Token);
    LookupPrivilegeValue(NULL, SE_TAKE_OWNERSHIP_NAME, &luid);

    privs.PrivilegeCount = 1;
    privs.Privileges[0].Luid = luid;
    privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    if (AdjustTokenPrivileges(
        Token,
        FALSE,
        &privs,
        sizeof(TOKEN_PRIVILEGES),
        NULL,
        NULL))
    {
        HANDLE mutex = CreateMutex(NULL, FALSE, TEXT("test1"));
        SetEveryone(mutex);
    }

    CloseHandle(Token);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

通常,您只能为自己或访问令牌中的组设置所有权并设置SE_GROUP_OWNER标记。

但是,您可以通过启用备份和还原权限来覆盖此规则。