Ledger Nano X - The secure hardware wallet
SHARE
TWEET

reg_takeownership

aveyo Sep 27th, 2018 (edited) 1,074 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @title reg_own snippet usage - 2020.04.14 final
  2. :: AveYo: csc-less via reflection; support any hive; recursively add or delete rights and try to preserve owner; still 12 lines
  3. @echo off
  4. echo Usage:
  5. echo call :reg_own "key" all[""=key-only] user[""=Administrators] owner[""=Preserve] access[""=Allow] perm[""=FullControl]
  6. echo.
  7.  
  8. :: Define TI sid (TrustedInstaller)
  9. for /f "tokens=2 delims=:" %%s in ('sc.exe showsid TrustedInstaller ^|findstr "S-1-"') do set TI=%%s& call set TI=%%TI: =%%
  10. :: Define USER sid before asking for elevation since it gets replaced for limited accounts
  11. if not defined USER for /f "tokens=2" %%s in ('whoami /user /fo list') do set "USER=%%s" >nul
  12. :: Ask for elevation passing USER and any batch arguments
  13. set _=set "USER=%USER%" ^&^& call "%~f0" %*
  14. reg query HKU\S-1-5-19>nul 2>nul||(powershell -nop -c start cmd -args '/d/x/q/r',$env:_ -verb runas & exit)
  15.  
  16. :: Setup a test key
  17. reg delete HKCU\TEMP\REG_OWN /f >nul 2>nul & reg add HKCU\TEMP\REG_OWN\DEL\ME /f >nul 2>nul
  18. :: Define color macros [windows10]: %<% = <ESC> and %>% = <ESC>[m
  19. for /f "tokens=3 delims=." %%b in ('ver') do if %%b gtr 14393 for /f %%s in ('echo prompt $E^|cmd') do set "<=%%s"&set ">=%%s[m"
  20.  
  21. echo %<%[7m Allow FullControl from Administrators (default) - just this key %>%`
  22. call :reg_own "HKEY_CURRENT_USER\TEMP\REG_OWN"
  23.  
  24. echo %<%[7m Allow FullControl from Everyone - recursive, preserve inheritance %>%`
  25. call :reg_own "HKEY_CURRENT_USER\TEMP\REG_OWN" preserve S-1-1-0
  26.  
  27. echo %<%[7m Allow READ from Users - recursive, disable inheritance %>%`
  28. call :reg_own "HKCU\TEMP\REG_OWN" all S-1-5-32-545 "" Allow "ReadPermissions, ReadKey"
  29.  
  30. echo %<%[7m Deny changes from %%USER%% and set owner to TrustedInstaller - just this key %>%`
  31. call :reg_own "HKCU\TEMP\REG_OWN" "" %USER% %TI% Deny "SetValue, Delete"
  32.  
  33. echo %<%[7m Allow FullControl from %%USER%% and set owner to SYSTEM - all subkeys %>%`
  34. call :reg_own "HKCU\TEMP\REG_OWN" all %USER% S-1-5-18 Allow FullControl
  35.  
  36. echo %<%[7m Remove non-inherited rules from Everyone - recursive, remove %>%`
  37. call :reg_own "HKCU\TEMP\REG_OWN" none S-1-1-0
  38.  
  39. echo %<%[106;30m Done! %>%
  40. cmd /k
  41.  
  42. ::::::::::::::::::::::::::::::::::::::::::::::::
  43. :: Snippet to copy-paste in batch (cmd) scripts:
  44. ::::::::::::::::::::::::::::::::::::::::::::::::
  45.  
  46. :reg_own #key [optional] all user owner access permission  :  call :reg_own "HKCU\My" "" S-1-5-32-545 S-1-5-18 Allow FullControl
  47. powershell -nop -c $A='%~1','%~2','%~3','%~4','%~5','%~6';iex(([io.file]::ReadAllText('%~f0')-split':Own1\:.*')[1])&exit/b:Own1:
  48. $D1=[IO.IODescriptionAttribute].Module.GetType('System.Diagnostics.Process').GetMethods(42) |? {$_.Name -eq 'SetPrivilege'}
  49. 'SeSecurityPrivilege','SeTakeOwnershipPrivilege','SeBackupPrivilege','SeRestorePrivilege' |% {$D1.Invoke($null, @("$_",2))}
  50. $path=$A[0]; $rk=$path-split'\\',2; $HK=gi -lit Registry::$($rk[0]) -fo; $s=$A[1]; $sps=[Security.Principal.SecurityIdentifier]
  51. $u=($A[2],'S-1-5-32-544')[!$A[2]];$o=($A[3],$u)[!$A[3]];$w=$u,$o |% {new-object $sps($_)}; $old=!$A[3];$own=!$old; $y=$s-eq'all'
  52. $rar=new-object Security.AccessControl.RegistryAccessRule( $w[0], ($A[5],'FullControl')[!$A[5]], 1, 0, ($A[4],'Allow')[!$A[4]] )
  53. $x=$s-eq'none'; function Own1($k){$t=$HK.OpenSubKey($k,2,'TakeOwnership'); 0,4 |% {try{$o=$t.GetAccessControl($_)}catch{$old=0}}
  54. if($old){$own=1;$w[1]=$o.GetOwner($sps)};$o.SetOwner($w[0]);$t.SetAccessControl($o); $c=$HK.OpenSubKey($k,2,'ChangePermissions')
  55. $p=$c.GetAccessControl(2);if($y){$p.SetAccessRuleProtection(1,1)};$p.ResetAccessRule($rar);if($x){$p.RemoveAccessRuleAll($rar)}
  56. $c.SetAccessControl($p);if($own){$o.SetOwner($w[1]);$t.SetAccessControl($o)};if($s){$subkeys=$HK.OpenSubKey($k).GetSubKeyNames()
  57. foreach($n in $subkeys){Own1 "$k\$n"}}}; Own1 $rk[1]; get-acl Registry::$path|fl # :Own1: lean and mean .bat snippet by AveYo
  58.  
  59. ####################################################################
  60. # Snippet to copy-paste in ps1/hybrid scripts or powershell console:
  61. # hybrid cmd+powershell code block example: pastebin.com/fZ361Yw2
  62. # simpler to just copy it at the top of the script before calling it
  63. ####################################################################
  64.  
  65. function reg_own([string[]]$A){ #key [opt],all,usr,own,acc,perm  :  "HKCU\My","","S-1-5-32-545","S-1-5-18","Allow","FullControl"
  66. $D1=[IO.IODescriptionAttribute].Module.GetType('System.Diagnostics.Process').GetMethods(42) |? {$_.Name -eq 'SetPrivilege'}
  67. 'SeSecurityPrivilege','SeTakeOwnershipPrivilege','SeBackupPrivilege','SeRestorePrivilege' |% {$D1.Invoke($null, @("$_",2))}
  68. $path=$A[0]; $rk=$path-split'\\',2; $HK=gi -lit Registry::$($rk[0]) -fo; $s=$A[1]; $sps=[Security.Principal.SecurityIdentifier]
  69. $u=($A[2],'S-1-5-32-544')[!$A[2]];$o=($A[3],$u)[!$A[3]];$w=$u,$o |% {new-object $sps($_)}; $old=!$A[3];$own=!$old; $y=$s-eq'all'
  70. $rar=new-object Security.AccessControl.RegistryAccessRule( $w[0], ($A[5],'FullControl')[!$A[5]], 1, 0, ($A[4],'Allow')[!$A[4]] )
  71. $x=$s-eq'none'; function Own1($k){$t=$HK.OpenSubKey($k,2,'TakeOwnership'); 0,4 |% {try{$o=$t.GetAccessControl($_)}catch{$old=0}}
  72. if($old){$own=1;$w[1]=$o.GetOwner($sps)};$o.SetOwner($w[0]);$t.SetAccessControl($o); $c=$HK.OpenSubKey($k,2,'ChangePermissions')
  73. $p=$c.GetAccessControl(2);if($y){$p.SetAccessRuleProtection(1,1)};$p.ResetAccessRule($rar);if($x){$p.RemoveAccessRuleAll($rar)}
  74. $c.SetAccessControl($p);if($own){$o.SetOwner($w[1]);$t.SetAccessControl($o)};if($s){$subkeys=$HK.OpenSubKey($k).GetSubKeyNames()
  75. foreach($n in $subkeys){Own1 "$k\$n"}}}; Own1 $rk[1]; get-acl Registry::$path|fl } # lean and mean powershell snippet by AveYo
  76.  
  77. #-_-:
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top