如何枚举已分配指定用户权限的所有 SID?C++

How to enumerate all SIDs to which a specified user privilege has been assigned? c++

本文关键字:权限 用户 C++ SID 何枚举 枚举 分配      更新时间:2023-10-16

我在Windows和C++我想恢复给定权限的所有 SID。为了恢复 SID,我使用了以下方法:LsaOpenPolicy, LsaEnumerateAccountsWithUserRight 和 ConvertSidToStringSidA.问题来自返回错误 ConvertSidToStringSidA 方法:无效的 SID。这是我使用的代码:

    LSA_HANDLE lsaPolicyHandle;
    LSA_OBJECT_ATTRIBUTES lsaObjectAttributes;
    ZeroMemory(&lsaObjectAttributes, sizeof (lsaObjectAttributes));
    NTSTATUS ntStatus;
    ntStatus=LsaOpenPolicy(nullptr,&lsaObjectAttributes, POLICY_ALL_ACCESS, &lsaPolicyHandle);
    //Here ntstatus == ERROR_SUCCESS
    if(ntStatus != ERROR_SUCCESS)
    {
        qDebug()<<"error";
    }
    LSA_UNICODE_STRING lsaUSerRight;
    DWORD64 dwLen=0;
    LPCWSTR pcwStr = L"SeServiceLogonRight";
    dwLen = wcslen(pcwStr);
    lsaUSerRight.Buffer = const_cast<wchar_t*>(pcwStr);
    lsaUSerRight.Length = static_cast<unsigned short>(dwLen) * sizeof(WCHAR);
    lsaUSerRight.MaximumLength= static_cast<unsigned short>(dwLen+1) *sizeof(WCHAR);
    LSA_ENUMERATION_INFORMATION pEnumInfo;
    ULONG ulCount;
    ntStatus=LsaEnumerateAccountsWithUserRight(lsaPolicyHandle,
                                               &lsaUSerRight,
                                               reinterpret_cast<PVOID*>(&pEnumInfo),
                                               &ulCount);
    //Here ntstatus == ERROR_SUCCESS
    if(ntStatus != ERROR_SUCCESS)
    {
       qDebug()<<"error";
    }
    //here pEnumInfo has an adress 0x45FF34c et ulCount = 2
    LPSTR lpStringSid;
    PSID pSid=pEnumInfo.Sid;
   //Here invalid SID  
    BOOL bResultConvert=ConvertSidToStringSidA(pSid, &lpStringSid);
    if(bResultConvert==0)
    {
        qDebug()<<"error";
    }

LsaEnumerateAccountsWithUserRight填充指向LSA_ENUMERATION_INFORMATION指针,因此您需要更改以下内容:

LSA_ENUMERATION_INFORMATION pEnumInfo;

对此:

LSA_ENUMERATION_INFORMATION *pEnumInfo;

要访问返回的第一个 SID,请更改以下内容:

PSID pSid=pEnumInfo.Sid;

对此:

PSID pSid=pEnumInfo->Sid;

然后它起作用了。

完成后

,不要忘记释放随LsaFreeMemory返回的结构,并用LsaClose清理。