Jump to content

Need A Script !!

I just spent a day assigning registry rights, one at a time, in order to delete some keys that were preventing the USB from initializing on my brother's pc. Of course i just wanted to reinstall and start again but didnt really want to deal with redownloading all his stuff. I got it working in the end, but got me thinking that someone out there has made an easier and much less tedious way of assigning ownership of a bunch of regkeys to the admin, and then deleting them while also enumerating all the subkeys. It was fucking torture, i would get to the last key in the section, give myself ownership, and then 30 more subkeys appear inside it !!!!

 

Has anyone found a way to make this easier ?

Home PC:

CPU: i7 4790s ~ Motherboard: Asus B85M-E ~ RAM: 32GB Ballistix Sport DDR3 1666 ~ GPU: Sapphire R9 390 Nitro ~ Case: Corsair Carbide Spec-03 ~ Storage: Kingston Predator 240GB   PCIE M.2 Boot, 2TB HDD, 3x 480GB SATA SSD's in RAID 0 ~ PSU:    Corsair CX600
Display(s): Asus PB287Q , Generic Samsung 1080p 22" ~ Cooling: Arctic T3 Air Cooler, All case fans replaced with Noctua NF-B9 Redux's ~ Keyboard: Logitech G810 Orion ~ Mouse: Cheap Microsoft Wired (i like it) ~ Sound: Radial Pro USB DAC into 250w Powered Speakers ~ Operating System: Windows 10 Enterprise x64
 

Work PC:

CPU: Intel Xeon E3 1275 v3 ~ Motherboard: Asrock E3C226D2I ~ RAM: 16GB DDR3 ~ GPU: GTX 460 ~ Case: Silverstone SG05 ~ Storage: 512GB SATA SSD ~ Displays: 3x1080p 24" mix and matched Dell monitors plus a 10" 1080p lilliput monitor above ~ Operating System: Windows 10 Enterprise x64

Link to comment
Share on other sites

Link to post
Share on other sites

Found this on Goolge: https://www.remkoweijnen.nl/blog/2012/01/16/take-ownership-of-a-registry-key-in-powershell/

 

You would need to change "DesktopBackground\Shell\Display" to your keys I guess, or maybe the whole $Key line would need tweaking to your needs.

$definition = @"
using System;
using System.Runtime.InteropServices;
 
namespace Win32Api
{
 
public class NtDll
{
[DllImport("ntdll.dll", EntryPoint="RtlAdjustPrivilege")]
public static extern int RtlAdjustPrivilege(ulong Privilege, bool Enable, bool CurrentThread, ref bool Enabled);
}
}
"@
 
Add-Type -TypeDefinition $definition -PassThru
 
$bEnabled = $false
 
# Enable SeTakeOwnershipPrivilege
$res = [Win32Api.NtDll]::RtlAdjustPrivilege(9, $true, $false, [ref]$bEnabled)
 
$key = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey("DesktopBackground\Shell\Display", [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::takeownership)
$acl = $key.GetAccessControl()
$acl.SetOwner([System.Security.Principal.NTAccount]"Administrators")
$key.SetAccessControl($acl)

Save the below as a powershell script (extension .PS1)

Right click the file and select run with powershell

 

You might get errors and need to run this to allow custom scripts:

Set-ExecutionPolicy ByPass

Current RIG:

Motherboard:    ROG Crosshair VIII Impact
CPU:            AMD Ryzen 9 3900X @4.4GHz
RAM:            32GB DDR4 G.Skill Trident Z
M.2:            1TB Western Digital Black
GPU:            16GB Asrock 7800XT OC
Case:           Fractal Design Define Mini C
Cooler:         Corsair H115i Platinum
PSU:            Corsair CS850M

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, mehtaman said:

Found this on Goolge: https://www.remkoweijnen.nl/blog/2012/01/16/take-ownership-of-a-registry-key-in-powershell/

 

You would need to change "DesktopBackground\Shell\Display" to your keys I guess, or maybe the whole $Key line would need tweaking to your needs.


$definition = @"
using System;
using System.Runtime.InteropServices;
 
namespace Win32Api
{
 
public class NtDll
{
[DllImport("ntdll.dll", EntryPoint="RtlAdjustPrivilege")]
public static extern int RtlAdjustPrivilege(ulong Privilege, bool Enable, bool CurrentThread, ref bool Enabled);
}
}
"@
 
Add-Type -TypeDefinition $definition -PassThru
 
$bEnabled = $false
 
# Enable SeTakeOwnershipPrivilege
$res = [Win32Api.NtDll]::RtlAdjustPrivilege(9, $true, $false, [ref]$bEnabled)
 
$key = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey("DesktopBackground\Shell\Display", [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::takeownership)
$acl = $key.GetAccessControl()
$acl.SetOwner([System.Security.Principal.NTAccount]"Administrators")
$key.SetAccessControl($acl)

Save the below as a powershell script (extension .PS1)

Right click the file and select run with powershell

 

You might get errors and need to run this to allow custom scripts:

Set-ExecutionPolicy ByPass

thanks for the reply, we tried this one, but it didnt work because it doesn't recursively grant rights, this script will try to unlock the "top" key, then realise it needs to unlock the subkeys first, but then doesnt go back to unlock the root key, so it does try, but ultimately fails, and still leaves me keys to manually take ownership of and delete

Home PC:

CPU: i7 4790s ~ Motherboard: Asus B85M-E ~ RAM: 32GB Ballistix Sport DDR3 1666 ~ GPU: Sapphire R9 390 Nitro ~ Case: Corsair Carbide Spec-03 ~ Storage: Kingston Predator 240GB   PCIE M.2 Boot, 2TB HDD, 3x 480GB SATA SSD's in RAID 0 ~ PSU:    Corsair CX600
Display(s): Asus PB287Q , Generic Samsung 1080p 22" ~ Cooling: Arctic T3 Air Cooler, All case fans replaced with Noctua NF-B9 Redux's ~ Keyboard: Logitech G810 Orion ~ Mouse: Cheap Microsoft Wired (i like it) ~ Sound: Radial Pro USB DAC into 250w Powered Speakers ~ Operating System: Windows 10 Enterprise x64
 

Work PC:

CPU: Intel Xeon E3 1275 v3 ~ Motherboard: Asrock E3C226D2I ~ RAM: 16GB DDR3 ~ GPU: GTX 460 ~ Case: Silverstone SG05 ~ Storage: 512GB SATA SSD ~ Displays: 3x1080p 24" mix and matched Dell monitors plus a 10" 1080p lilliput monitor above ~ Operating System: Windows 10 Enterprise x64

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×