简体   繁体   English

如何获得甚至没有读取权限的注册表项的句柄

[英]How to get the handle of a registry key without even read permission

I'm trying to call this function to take owner for a key. 我正在尝试调用此函数以获取所有者作为密钥。

[DllImport("Advapi32.dll", CharSet = CharSet.Auto)]
public extern static int SetSecurityInfo(IntPtr hKeySrc, SE_OBJECT_TYPE lpSubKey, SECURITY_INFORMATION securityInfo, IntPtr psidOwner, IntPtr psidGroup, IntPtr pDacl, IntPtr pSacl);

However, to call this function, I first need a handle to the key (IntPtr hKeysrc). 但是,要调用此函数,我首先需要键的句柄(IntPtr hKeysrc)。 Before I use these function to get the handle. 在使用这些功能获取句柄之前。

RegistryKey key = root.OpenSubKey(keyName);

private static IntPtr GetRegistryKeyHandle(RegistryKey registryKey)
    {
        IntPtr ret = IntPtr.Zero;
        try
        {
            Type registryKeyType = typeof(RegistryKey);

            System.Reflection.FieldInfo fieldInfo =
            registryKeyType.GetField("hkey", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            SafeHandle handle = (SafeHandle) fieldInfo.GetValue(registryKey);
            ret = handle.DangerousGetHandle();
        }
        catch (Exception ex)
        {
            LogError("GetRegistryKeyHandle ERROR:" + ex.ToString());
        }
        return ret;
    }

But now the key that I'm targeting only has permission to System account! 但是现在,我定位的密钥仅具有系统帐户的权限!

When I try to open permissions in regedit for the key, I get the error You do not have permission to view the current permission settings, but you can make permission changes. 当我尝试在注册表编辑器中打开密钥的权限时,收到错误消息您没有权限查看当前的权限设置,但是可以进行权限更改。

I don't even have read permission to the key. 我什至没有对该密钥的读取权限。 Then the method I used to get handle will failed with unauthorized error. 然后,我用来获取方法的方法将因未授权错误而失败。

How can I get a handle to the key in this case? 在这种情况下,如何获得钥匙的手柄? Actually, regedit can do it. 实际上,regedit可以做到。 So I think there may an API can do it. 所以我认为可能有一个API可以做到。 But I don't know which API to call. 但是我不知道要调用哪个API。

@HarryJohnston got the right solution. @HarryJohnston得到了正确的解决方案。

  1. Enable SeTakeOwnershipPrivilege 启用S​​eTakeOwnershipPrivilege
  2. Open the key for WRITE_OWNER access 打开用于WRITE_OWNER访问的密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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