0

我在 Windows 和 C++ 上我想恢复给定特权的所有 SID。为了恢复 SID,我使用了以下方法:LsaOpenPolicy、LsaEnumerateAccountsWithUserRight 和 ConvertSidToStringSidA。问题来自返回错误的 ConvertSidToStringSidA 方法:Invalid 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";
    }

4

1 回答 1

3

LsaEnumerateAccountsWithUserRight填写指向 a 的指针LSA_ENUMERATION_INFORMATION因此您需要更改此内容:

LSA_ENUMERATION_INFORMATION pEnumInfo;

对此:

LSA_ENUMERATION_INFORMATION *pEnumInfo;

要访问返回的第一个 SID,请更改:

PSID pSid=pEnumInfo.Sid;

对此:

PSID pSid=pEnumInfo->Sid;

然后它工作。

LsaFreeMemory当你完成它们并清理它们时,不要忘记释放返回的结构LsaClose

于 2019-07-10T16:39:13.400 回答