Advertisement
Guest User

Set-LHSTokenPrivilege.psm1

a guest
Aug 14th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Set-LHSTokenPrivilege
  2. {
  3. <#
  4. .SYNOPSIS
  5.     Enables or disables privileges in a specified access token.
  6.  
  7. .DESCRIPTION
  8.     Enables or disables privileges in a specified access token.
  9.  
  10. .PARAMETER Privilege
  11.     The privilege to adjust. This set is taken from
  12.     http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx
  13.  
  14. .PARAMETER $ProcessId
  15.     The process on which to adjust the privilege. Defaults to the current process.
  16.  
  17. .PARAMETER Disable
  18.     Switch to disable the privilege, rather than enable it.
  19.  
  20. .EXAMPLE
  21.     Set-LHSTokenPrivilege -Privilege SeRestorePrivilege
  22.  
  23.     To set the 'Restore Privilege' for the current Powershell Process.
  24.  
  25. .EXAMPLE
  26.     Set-LHSTokenPrivilege -Privilege SeRestorePrivilege -Disable
  27.  
  28.     To disable 'Restore Privilege' for the current Powershell Process.
  29.  
  30. .EXAMPLE
  31.     Set-LHSTokenPrivilege -Privilege SeShutdownPrivilege -ProcessId 4711
  32.    
  33.     To set the 'Shutdown Privilege' for the Process with Process ID 4711
  34.  
  35. .INPUTS
  36.     None to the pipeline
  37.  
  38. .OUTPUTS
  39.     System.Boolean, True if the privilege could be enabled
  40.  
  41. .NOTES
  42.     to check privileges use whoami
  43.     PS:\> whoami /priv
  44.  
  45.     PRIVILEGES INFORMATION
  46.     ----------------------
  47.  
  48.     Privilege Name                Description                          State
  49.     ============================= ==================================== ========
  50.     SeShutdownPrivilege           Shut down the system                 Disabled
  51.     SeChangeNotifyPrivilege       Bypass traverse checking             Enabled
  52.     SeUndockPrivilege             Remove computer from docking station Disabled
  53.     SeIncreaseWorkingSetPrivilege Increase a process working set       Disabled
  54.  
  55.  
  56.     AUTHOR: Pasquale Lantella
  57.     LASTEDIT:
  58.     KEYWORDS: Token Privilege
  59.  
  60. .LINK
  61.     http://www.leeholmes.com/blog/2010/09/24/adjusting-token-privileges-in-powershell/
  62.  
  63.     The privilege to adjust. This set is taken from
  64.     http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx
  65.  
  66.     pinvoke AdjustTokenPrivileges (advapi32)
  67.     http://www.pinvoke.net/default.aspx/advapi32.AdjustTokenPrivileges
  68.  
  69. #Requires -Version 2.0
  70. #>
  71.    
  72. [cmdletbinding(  
  73.     ConfirmImpact = 'low',
  74.     SupportsShouldProcess = $false
  75. )]  
  76.  
  77. [OutputType('System.Boolean')]
  78.  
  79. Param(
  80.  
  81.     [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$False,HelpMessage='An Token Privilege.')]
  82.     [ValidateSet(
  83.         "SeAssignPrimaryTokenPrivilege", "SeAuditPrivilege", "SeBackupPrivilege",
  84.         "SeChangeNotifyPrivilege", "SeCreateGlobalPrivilege", "SeCreatePagefilePrivilege",
  85.         "SeCreatePermanentPrivilege", "SeCreateSymbolicLinkPrivilege", "SeCreateTokenPrivilege",
  86.         "SeDebugPrivilege", "SeEnableDelegationPrivilege", "SeImpersonatePrivilege", "SeIncreaseBasePriorityPrivilege",
  87.         "SeIncreaseQuotaPrivilege", "SeIncreaseWorkingSetPrivilege", "SeLoadDriverPrivilege",
  88.         "SeLockMemoryPrivilege", "SeMachineAccountPrivilege", "SeManageVolumePrivilege",
  89.         "SeProfileSingleProcessPrivilege", "SeRelabelPrivilege", "SeRemoteShutdownPrivilege",
  90.         "SeRestorePrivilege", "SeSecurityPrivilege", "SeShutdownPrivilege", "SeSyncAgentPrivilege",
  91.         "SeSystemEnvironmentPrivilege", "SeSystemProfilePrivilege", "SeSystemtimePrivilege",
  92.         "SeTakeOwnershipPrivilege", "SeTcbPrivilege", "SeTimeZonePrivilege", "SeTrustedCredManAccessPrivilege",
  93.         "SeUndockPrivilege", "SeUnsolicitedInputPrivilege")]
  94.     [String]$Privilege,
  95.  
  96.     [Parameter(Position=1)]
  97.     $ProcessId = $pid,
  98.  
  99.     [Switch]$Disable
  100.    )
  101.  
  102. BEGIN {
  103.  
  104.     Set-StrictMode -Version Latest
  105.     ${CmdletName} = $Pscmdlet.MyInvocation.MyCommand.Name
  106.  
  107. ## Taken from P/Invoke.NET with minor adjustments.
  108.  
  109. $definition = @'
  110. using System;
  111. using System.Runtime.InteropServices;
  112.  
  113. public class AdjPriv
  114. {
  115.  [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  116.  internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
  117.  
  118.  [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  119.  internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
  120.  
  121.  [DllImport("advapi32.dll", SetLastError = true)]
  122.  internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
  123.  
  124.  [StructLayout(LayoutKind.Sequential, Pack = 1)]
  125.  internal struct TokPriv1Luid
  126.  {
  127.   public int Count;
  128.   public long Luid;
  129.   public int Attr;
  130.  }
  131.  
  132.  internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
  133.  internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
  134.  internal const int TOKEN_QUERY = 0x00000008;
  135.  internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  136.  
  137.  public static bool EnablePrivilege(long processHandle, string privilege, bool disable)
  138.  {
  139.   bool retVal;
  140.   TokPriv1Luid tp;
  141.   IntPtr hproc = new IntPtr(processHandle);
  142.   IntPtr htok = IntPtr.Zero;
  143.   retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
  144.   tp.Count = 1;
  145.   tp.Luid = 0;
  146.   if(disable)
  147.   {
  148.    tp.Attr = SE_PRIVILEGE_DISABLED;
  149.   }
  150.   else
  151.   {
  152.    tp.Attr = SE_PRIVILEGE_ENABLED;
  153.   }
  154.   retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
  155.   retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
  156.   return retVal;
  157.  }
  158. }
  159. '@
  160.  
  161.  
  162.  
  163. } # end BEGIN
  164.  
  165. PROCESS {
  166.  
  167.     $processHandle = (Get-Process -id $ProcessId).Handle
  168.    
  169.     $type = Add-Type $definition -PassThru
  170.     $type[0]::EnablePrivilege($processHandle, $Privilege, $Disable)
  171.  
  172. } # end PROCESS
  173.  
  174. END { Write-Verbose "Function ${CmdletName} finished." }
  175.  
  176. } # end Function Set-LHSTokenPrivilege
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement