Advertisement
matt_mods

useful functions

Mar 9th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         function force-mkdir($path) {
  2.     if (!(Test-Path $path)) {
  3.         New-Item -ItemType Directory -Force -Path $path
  4.     }
  5. }
  6.  
  7. function Takeown-Registry($key) {
  8.     # TODO does not work for all root keys yet
  9.     switch ($key.split('\')[0]) {
  10.         "HKEY_CLASSES_ROOT" {
  11.             $reg = [Microsoft.Win32.Registry]::ClassesRoot
  12.             $key = $key.substring(18)
  13.         }
  14.         "HKEY_CURRENT_USER" {
  15.             $reg = [Microsoft.Win32.Registry]::CurrentUser
  16.             $key = $key.substring(18)
  17.         }
  18.         "HKEY_LOCAL_MACHINE" {
  19.             $reg = [Microsoft.Win32.Registry]::LocalMachine
  20.             $key = $key.substring(19)
  21.         }
  22.     }
  23.  
  24.     # get administraor group
  25.     $admins = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
  26.     $admins = $admins.Translate([System.Security.Principal.NTAccount])
  27.  
  28.     # set owner
  29.     $key = $reg.OpenSubKey($key, "ReadWriteSubTree", "TakeOwnership")
  30.     $acl = $key.GetAccessControl()
  31.     $acl.SetOwner($admins)
  32.     $key.SetAccessControl($acl)
  33.  
  34.     # set FullControl
  35.     $acl = $key.GetAccessControl()
  36.     $rule = New-Object System.Security.AccessControl.RegistryAccessRule($admins, "FullControl", "Allow")
  37.     $acl.SetAccessRule($rule)
  38.     $key.SetAccessControl($acl)
  39. }
  40.  
  41. function Takeown-File($path) {
  42.     takeown.exe /A /F $path
  43.     $acl = Get-Acl $path
  44.  
  45.     # get administraor group
  46.     $admins = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
  47.     $admins = $admins.Translate([System.Security.Principal.NTAccount])
  48.  
  49.     # add NT Authority\SYSTEM
  50.     $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($admins, "FullControl", "None", "None", "Allow")
  51.     $acl.AddAccessRule($rule)
  52.  
  53.     Set-Acl -Path $path -AclObject $acl
  54. }
  55.  
  56. function Takeown-Folder($path) {
  57.     Takeown-File $path
  58.     foreach ($item in Get-ChildItem $path) {
  59.         if (Test-Path $item -PathType Container) {
  60.             Takeown-Folder $item.FullName
  61.         } else {
  62.             Takeown-File $item.FullName
  63.         }
  64.     }
  65. }
  66.  
  67. function Elevate-Privileges {
  68.     param($Privilege)
  69.     $Definition = @"
  70.    using System;
  71.    using System.Runtime.InteropServices;
  72.    public class AdjPriv {
  73.        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  74.            internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr rele);
  75.        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  76.            internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
  77.        [DllImport("advapi32.dll", SetLastError = true)]
  78.            internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
  79.        [StructLayout(LayoutKind.Sequential, Pack = 1)]
  80.            internal struct TokPriv1Luid {
  81.                public int Count;
  82.                public long Luid;
  83.                public int Attr;
  84.            }
  85.        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
  86.        internal const int TOKEN_QUERY = 0x00000008;
  87.        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  88.        public static bool EnablePrivilege(long processHandle, string privilege) {
  89.            bool retVal;
  90.            TokPriv1Luid tp;
  91.            IntPtr hproc = new IntPtr(processHandle);
  92.            IntPtr htok = IntPtr.Zero;
  93.            retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
  94.            tp.Count = 1;
  95.            tp.Luid = 0;
  96.            tp.Attr = SE_PRIVILEGE_ENABLED;
  97.            retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
  98.            retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
  99.            return retVal;
  100.        }
  101.    }
  102. "@
  103.     $ProcessHandle = (Get-Process -id $pid).Handle
  104.     $type = Add-Type $definition -PassThru
  105.     $type[0]::EnablePrivilege($processHandle, $Privilege)
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement