Advertisement
RulerOf

God-mode WinDirStat

Aug 14th, 2017
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Set-TokenPrivilege
  2. {
  3.   param(    ## The privilege to adjust. This set is taken from
  4.       ## http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx
  5.       [ValidateSet(
  6.           "SeAssignPrimaryTokenPrivilege", "SeAuditPrivilege", "SeBackupPrivilege",
  7.           "SeChangeNotifyPrivilege", "SeCreateGlobalPrivilege", "SeCreatePagefilePrivilege",
  8.           "SeCreatePermanentPrivilege", "SeCreateSymbolicLinkPrivilege", "SeCreateTokenPrivilege",
  9.           "SeDebugPrivilege", "SeEnableDelegationPrivilege", "SeImpersonatePrivilege", "SeIncreaseBasePriorityPrivilege",
  10.           "SeIncreaseQuotaPrivilege", "SeIncreaseWorkingSetPrivilege", "SeLoadDriverPrivilege",
  11.           "SeLockMemoryPrivilege", "SeMachineAccountPrivilege", "SeManageVolumePrivilege",
  12.           "SeProfileSingleProcessPrivilege", "SeRelabelPrivilege", "SeRemoteShutdownPrivilege",
  13.           "SeRestorePrivilege", "SeSecurityPrivilege", "SeShutdownPrivilege", "SeSyncAgentPrivilege",
  14.           "SeSystemEnvironmentPrivilege", "SeSystemProfilePrivilege", "SeSystemtimePrivilege",
  15.           "SeTakeOwnershipPrivilege", "SeTcbPrivilege", "SeTimeZonePrivilege", "SeTrustedCredManAccessPrivilege",
  16.           "SeUndockPrivilege", "SeUnsolicitedInputPrivilege")]
  17.       $Privilege,
  18.       ## The process on which to adjust the privilege. Defaults to the current process.
  19.       $ProcessId = $pid,
  20.       ## Switch to disable the privilege, rather than enable it.
  21.       [Switch] $Disable
  22.   )
  23.   ## Taken from P/Invoke.NET with minor adjustments.
  24. $definition = @
  25. using System;
  26. using System.Runtime.InteropServices;
  27. public class AdjPriv
  28. {
  29.   [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  30.   internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
  31.   ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
  32.   [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  33.   internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
  34.   [DllImport("advapi32.dll", SetLastError = true)]
  35.   internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
  36.   [StructLayout(LayoutKind.Sequential, Pack = 1)]
  37.   internal struct TokPriv1Luid
  38.   {
  39.     public int Count;
  40.     public long Luid;
  41.     public int Attr;
  42.   }
  43.   internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
  44.   internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
  45.   internal const int TOKEN_QUERY = 0x00000008;
  46.   internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  47.   public static bool EnablePrivilege(long processHandle, string privilege, bool disable)
  48.   {
  49.     bool retVal;
  50.     TokPriv1Luid tp;
  51.     IntPtr hproc = new IntPtr(processHandle);
  52.     IntPtr htok = IntPtr.Zero;
  53.     retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
  54.     tp.Count = 1;
  55.     tp.Luid = 0;
  56.     if(disable)
  57.     {
  58.       tp.Attr = SE_PRIVILEGE_DISABLED;
  59.     }
  60.     else
  61.     {
  62.       tp.Attr = SE_PRIVILEGE_ENABLED;
  63.     }
  64.     retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
  65.     retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
  66.     return retVal;
  67.   }
  68. }
  69. @
  70.   $processHandle = (Get-Process -id $ProcessId).Handle
  71.   $type = Add-Type $definition -PassThru
  72.   $type[0]::EnablePrivilege($processHandle, $Privilege, $Disable)
  73. }
  74.  
  75. Get-Process -Name windirstat | ForEach-Object { Set-TokenPrivilege -Privilege SeBackupPrivilege -ProcessId $_.Id; Set-TokenPrivilege -Privilege SeRestorePrivilege -ProcessId $_.Id }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement