0

I'm unable to take ownership of some keys in the registry with my C# code.

I'm trying take ownership on

Software\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace{1CF1260C-4DD0-4ebb-811F-33C572699FDE}

My app is launched as administrator with the requireAdministrator flag on the app.manifest.

UAC is disabled to test without this security that might interfere.

I'm setting the token SeTakeOwnershipPrivilege properly on my process (Process Explorer confirms me that is enabled on my process).

But when I'm running my code it returns a null key at the OpenSubKey call. Here is my method :

public void takeKeyOwnership(RegistryHive root, string path)
        {
            RegistryKey RootKey = GetKey(root);
            var key = RootKey.OpenSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.TakeOwnership);

            if(key == null)
            {
                logger.Warn("[takeKeyOwnership] Unable to take ownership on key " + root + "\\" + path);
                return;
            }
            var acl = key.GetAccessControl(AccessControlSections.None);
            var localAdmins = new SecurityIdentifier(Constants.LocalAdministratorsSID);
            acl.SetOwner(localAdmins);
            key.SetAccessControl(acl);
            key.Close();
        }

My question is different from : How do I programmatically give ownership of a Registry Key to Administrators? as I've set the SeTakeOwnerShipPrivilege as specified earlier.

Itchydon
  • 2,572
  • 6
  • 19
  • 33
Jonathan Crégut
  • 193
  • 1
  • 2
  • 8
  • Possible duplicate of [How do I programmatically give ownership of a Registry Key to Administrators?](https://stackoverflow.com/questions/38448390/how-do-i-programmatically-give-ownership-of-a-registry-key-to-administrators) – Bob Swager Jul 20 '17 at 07:24
  • What is the "Target CPU" setting for your project? See more -> [Why is OpenSubKey() returning null on my Windows 7 64-bit system?](https://stackoverflow.com/questions/2464358/why-is-opensubkey-returning-null-on-my-windows-7-64-bit-system) – Rostech Jul 20 '17 at 07:24
  • My psychic powers suggest you're running [as a 32-bit process](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384232(v=vs.85).aspx) – Damien_The_Unbeliever Jul 20 '17 at 07:24
  • @BobSwager no I've seen this question and I've set the SeTakeOwnership Privilege. – Jonathan Crégut Jul 20 '17 at 07:29
  • @Rostech I'm running as a 32-bit process yes but I'm opening the registry with RegistryView.Registry32 and modifying 64bits keys on the Wow6432Node when needed. – Jonathan Crégut Jul 20 '17 at 07:31
  • @Jonathan, Please check out the answer [here](https://stackoverflow.com/a/16698274/7406606). You will need to specify the RegistryView parameter. – Rostech Jul 20 '17 at 07:56
  • @Rostech yes but if I want to manage 32bits keys too (because this key exists in the 32bit view too) ? If I can be sure Windows always use the 64bit view I'm okay with going full 64bits. – Jonathan Crégut Jul 20 '17 at 08:04
  • @Rostech I just tried on 64bit view and it doesn't work either. – Jonathan Crégut Jul 20 '17 at 08:10

0 Answers0