简体   繁体   中英

How to recover the effective rights of a registry key? c++

I would like to retrieve the effective rights of a given registry key as a parameter. I test with an existing registry key under Windows. To do that I use the methods CreateFile, GetSecurityInfo and GetEffectiveRightsFromAclA. I wanted to know if this is the correct method, because I have an error for the CreateFile method that returns INVALID_HANDLE_VALUE. Moreover for the method GetEffectiveRightsFromAclA, I do not understand which parameter I must put in TRUSTEE_A?

LPCWSTR lpwRegistryKey = L"HKEY_CLASSES_ROOT\\.acc\\OpenWithProgids\\WMP11.AssocFile.ADTS";
HANDLE handleKey;
handleKey = CreateFile(lpwRegistryKey, GENERIC_READ, FILE_SHARED_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);

if(handleKey == INVALID_HANDLE_VALUE)
{
   //qDebug()<<"error";
}

//Here is an user SID
PSID pUserSid;
QString sSid("S-4-5-12");
BOOL resultSidConvert = ConvertStringSidToSidA(sSid.toStdString().c_str(), &pUserSid);
//Here success
if(resultSidConvert != 0)
{
    PACL pDacl;
    PSECURITY_DESCRIPTOR pSecurityDescriptor;

    DWORD dwResultSecurity = GetSecurityInfo(handleKey, SE_REGISTRY_KEY, DACL_SECURITY_INFORMATION, nullptr, &pUserSid, &pDacl, nullptr, &pSecurityDescritptor);
    if(dwResultSecurity == ERROR_SUCCESS)
    {
       ACCESS_MASK pAccessMask;
       TRUSTEE_A pTrustee; //How should I initialize pTrustee?

       DWORD dwEffectiveRights = (GetEffectiveRightsFromAclA(pDacl, &pTrustee, &pAccessMask);
       if(dwEffectiveRights == ERROR_SUCCESS)
       {
           if((pAccessMask & DELETE) == DELETE)
           {
               qDebug()<<"Delete";
           }
           if((pAccessMask & GENERIC_READ) == GENERIC_READ)
           {
              qDebug()<<"READ";
           }
         //etc ..........
       }
    }
}

Don't use CreateFile. Use RegOpenKeyEx instead. First, you must run the application as admin.

Then, you can open the key successfully. After that, take a look at RegGetKeySecurity function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM