Exemplo n.º 1
0
        public void TakeOwnership(string key)
        {
            try
            {
                TokenManipulator.AddPrivilege("SeRestorePrivilege");
                TokenManipulator.AddPrivilege("SeBackupPrivilege");
                TokenManipulator.AddPrivilege("SeTakeOwnershipPrivilege");

                SecurityIdentifier sid     = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
                NTAccount          account = sid.Translate(typeof(NTAccount)) as NTAccount;
                using (var HKLM = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
                    using (var Registry = HKLM.OpenSubKey(key, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.TakeOwnership))
                    {
                        var rs = Registry.GetAccessControl(AccessControlSections.All);
                        rs.SetOwner(account);
                        Registry.SetAccessControl(rs);
                    }
            }
            finally
            {
                TokenManipulator.RemovePrivilege("SeRestorePrivilege");
                TokenManipulator.RemovePrivilege("SeBackupPrivilege");
                TokenManipulator.RemovePrivilege("SeTakeOwnershipPrivilege");
            }
        }
Exemplo n.º 2
0
        public void RemoveProtection(string key)
        {
            try
            {
                TokenManipulator.AddPrivilege("SeRestorePrivilege");
                TokenManipulator.AddPrivilege("SeBackupPrivilege");
                TokenManipulator.AddPrivilege("SeTakeOwnershipPrivilege");

                using (var HKLM = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
                    using (var Registry = HKLM.OpenSubKey(key, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions))
                    {
                        var rs = Registry.GetAccessControl(AccessControlSections.All);
                        rs.SetAccessRuleProtection(true, true);
                        rs.AddAccessRule(new RegistryAccessRule("Users", RegistryRights.FullControl, AccessControlType.Allow));
                        Registry.SetAccessControl(rs);
                    }
            }
            finally
            {
                TokenManipulator.RemovePrivilege("SeRestorePrivilege");
                TokenManipulator.RemovePrivilege("SeBackupPrivilege");
                TokenManipulator.RemovePrivilege("SeTakeOwnershipPrivilege");
            }
        }