xs4eyes

Get-SpecterStatus

Jan 5th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $pclist = Get-ADComputer -filter 'Enabled -eq $true' | select -ExpandProperty name
  2. foreach($pc in $pclist)
  3. {
  4.   if(Test-Connection -ComputerName $pc -BufferSize 16 -Count 1 -Quiet){
  5.  
  6.  
  7.     Invoke-Command -ComputerName $pc {
  8.  
  9.       process {
  10.  
  11.         $NtQSIDefinition = @'
  12.    [DllImport("ntdll.dll")]
  13.    public static extern int NtQuerySystemInformation(uint systemInformationClass, IntPtr systemInformation, uint systemInformationLength, IntPtr returnLength);
  14. '@
  15.  
  16.         $ntdll = Add-Type -MemberDefinition $NtQSIDefinition -Name 'ntdll' -Namespace 'Win32' -PassThru
  17.  
  18.  
  19.         [System.IntPtr]$systemInformationPtr = [System.Runtime.InteropServices.Marshal]::AllocHGlobal(4)
  20.         [System.IntPtr]$returnLengthPtr = [System.Runtime.InteropServices.Marshal]::AllocHGlobal(4)
  21.  
  22.         $object = New-Object -TypeName PSObject
  23.  
  24.         try {
  25.  
  26.           #
  27.           # Query branch target injection information.
  28.           #
  29.  
  30.           Write-Host "Speculation control settings for CVE-2017-5715 [branch target injection]" -ForegroundColor Cyan
  31.           Write-Host
  32.  
  33.           $btiHardwarePresent = $false
  34.           $btiWindowsSupportPresent = $false
  35.           $btiWindowsSupportEnabled = $false
  36.           $btiDisabledBySystemPolicy = $false
  37.           $btiDisabledByNoHardwareSupport = $false
  38.  
  39.           [System.UInt32]$systemInformationClass = 201
  40.           [System.UInt32]$systemInformationLength = 4
  41.  
  42.           $retval = $ntdll::NtQuerySystemInformation($systemInformationClass, $systemInformationPtr, $systemInformationLength, $returnLengthPtr)
  43.  
  44.           if ($retval -eq 0xc0000003) {
  45.             # fallthrough
  46.           }
  47.           elseif ($retval -ne 0) {
  48.             throw (("Querying branch target injection information failed with error {0:X8}" -f $retval))
  49.           }
  50.           else {
  51.  
  52.             [System.UInt32]$scfBpbEnabled = 0x01
  53.             [System.UInt32]$scfBpbDisabledSystemPolicy = 0x02
  54.             [System.UInt32]$scfBpbDisabledNoHardwareSupport = 0x04
  55.             [System.UInt32]$scfHwReg1Enumerated = 0x08
  56.             [System.UInt32]$scfHwReg2Enumerated = 0x10
  57.             [System.UInt32]$scfHwMode1Present = 0x20
  58.             [System.UInt32]$scfHwMode2Present = 0x40
  59.             [System.UInt32]$scfSmepPresent = 0x80
  60.  
  61.             [System.UInt32]$flags = [System.UInt32][System.Runtime.InteropServices.Marshal]::ReadInt32($systemInformationPtr)
  62.  
  63.             $btiHardwarePresent = ((($flags -band $scfHwReg1Enumerated) -ne 0) -or (($flags -band $scfHwReg2Enumerated)))
  64.             $btiWindowsSupportPresent = $true
  65.             $btiWindowsSupportEnabled = (($flags -band $scfBpbEnabled) -ne 0)
  66.  
  67.             if ($btiWindowsSupportEnabled -eq $false) {
  68.                 $btiDisabledBySystemPolicy = (($flags -band $scfBpbDisabledSystemPolicy) -ne 0)
  69.                 $btiDisabledByNoHardwareSupport = (($flags -band $scfBpbDisabledNoHardwareSupport) -ne 0)
  70.             }
  71.  
  72.             if ($PSBoundParameters['Verbose']) {
  73.                 Write-Host "BpbEnabled                   :" (($flags -band $scfBpbEnabled) -ne 0)
  74.                 Write-Host "BpbDisabledSystemPolicy      :" (($flags -band $scfBpbDisabledSystemPolicy) -ne 0)
  75.                 Write-Host "BpbDisabledNoHardwareSupport :" (($flags -band $scfBpbDisabledNoHardwareSupport) -ne 0)
  76.                 Write-Host "HwReg1Enumerated             :" (($flags -band $scfHwReg1Enumerated) -ne 0)
  77.                 Write-Host "HwReg2Enumerated             :" (($flags -band $scfHwReg2Enumerated) -ne 0)
  78.                 Write-Host "HwMode1Present               :" (($flags -band $scfHwMode1Present) -ne 0)
  79.                 Write-Host "HwMode2Present               :" (($flags -band $scfHwMode2Present) -ne 0)
  80.                 Write-Host "SmepPresent                  :" (($flags -band $scfSmepPresent) -ne 0)
  81.             }
  82.           }
  83.  
  84.           Write-Host "Hardware support for branch target injection mitigation is present:"($btiHardwarePresent) -ForegroundColor $(If ($btiHardwarePresent) { [System.ConsoleColor]::Green } Else { [System.ConsoleColor]::Red })
  85.           Write-Host "Windows OS support for branch target injection mitigation is present:"($btiWindowsSupportPresent) -ForegroundColor $(If ($btiWindowsSupportPresent) { [System.ConsoleColor]::Green } Else { [System.ConsoleColor]::Red })
  86.           Write-Host "Windows OS support for branch target injection mitigation is enabled:"($btiWindowsSupportEnabled) -ForegroundColor $(If ($btiWindowsSupportEnabled) { [System.ConsoleColor]::Green } Else { [System.ConsoleColor]::Red })
  87.  
  88.           if ($btiWindowsSupportPresent -eq $true -and $btiWindowsSupportEnabled -eq $false) {
  89.             Write-Host -ForegroundColor Red "Windows OS support for branch target injection mitigation is disabled by system policy:"($btiDisabledBySystemPolicy)
  90.             Write-Host -ForegroundColor Red "Windows OS support for branch target injection mitigation is disabled by absence of hardware support:"($btiDisabledByNoHardwareSupport)
  91.           }
  92.  
  93.           $object | Add-Member -MemberType NoteProperty -Name BTIHardwarePresent -Value $btiHardwarePresent
  94.           $object | Add-Member -MemberType NoteProperty -Name BTIWindowsSupportPresent -Value $btiWindowsSupportPresent
  95.           $object | Add-Member -MemberType NoteProperty -Name BTIWindowsSupportEnabled -Value $btiWindowsSupportEnabled
  96.           $object | Add-Member -MemberType NoteProperty -Name BTIDisabledBySystemPolicy -Value $btiDisabledBySystemPolicy
  97.           $object | Add-Member -MemberType NoteProperty -Name BTIDisabledByNoHardwareSupport -Value $btiDisabledByNoHardwareSupport
  98.  
  99.           #
  100.           # Query kernel VA shadow information.
  101.           #
  102.  
  103.           Write-Host
  104.           Write-Host "Speculation control settings for CVE-2017-5754 [rogue data cache load]" -ForegroundColor Cyan
  105.           Write-Host    
  106.  
  107.           $kvaShadowRequired = $true
  108.           $kvaShadowPresent = $false
  109.           $kvaShadowEnabled = $false
  110.           $kvaShadowPcidEnabled = $false
  111.  
  112.           $cpu = Get-WmiObject Win32_Processor
  113.  
  114.           if ($cpu.Manufacturer -eq "AuthenticAMD") {
  115.             $kvaShadowRequired = $false
  116.           }
  117.           elseif ($cpu.Manufacturer -eq "GenuineIntel") {
  118.             $regex = [regex]'Family (\d+) Model (\d+) Stepping (\d+)'
  119.             $result = $regex.Match($cpu.Description)
  120.  
  121.             if ($result.Success) {
  122.                 $family = [System.UInt32]$result.Groups[1].Value
  123.                 $model = [System.UInt32]$result.Groups[2].Value
  124.                 $stepping = [System.UInt32]$result.Groups[3].Value
  125.  
  126.                 if (($family -eq 0x6) -and
  127.                     (($model -eq 0x1c) -or
  128.                      ($model -eq 0x26) -or
  129.                      ($model -eq 0x27) -or
  130.                      ($model -eq 0x36) -or
  131.                      ($model -eq 0x35))) {
  132.  
  133.                     $kvaShadowRequired = $false
  134.                 }
  135.             }
  136.           }
  137.           else {
  138.             throw ("Unsupported processor manufacturer: {0}" -f $cpu.Manufacturer)
  139.           }
  140.  
  141.           [System.UInt32]$systemInformationClass = 196
  142.           [System.UInt32]$systemInformationLength = 4
  143.  
  144.           $retval = $ntdll::NtQuerySystemInformation($systemInformationClass, $systemInformationPtr, $systemInformationLength, $returnLengthPtr)
  145.  
  146.           if ($retval -eq 0xc0000003) {
  147.           }
  148.           elseif ($retval -ne 0) {
  149.             throw (("Querying kernel VA shadow information failed with error {0:X8}" -f $retval))
  150.           }
  151.           else {
  152.  
  153.             [System.UInt32]$kvaShadowEnabledFlag = 0x01
  154.             [System.UInt32]$kvaShadowUserGlobalFlag = 0x02
  155.             [System.UInt32]$kvaShadowPcidFlag = 0x04
  156.             [System.UInt32]$kvaShadowInvpcidFlag = 0x08
  157.  
  158.             [System.UInt32]$flags = [System.UInt32][System.Runtime.InteropServices.Marshal]::ReadInt32($systemInformationPtr)
  159.  
  160.             $kvaShadowPresent = $true
  161.             $kvaShadowEnabled = (($flags -band $kvaShadowEnabledFlag) -ne 0)
  162.             $kvaShadowPcidEnabled = ((($flags -band $kvaShadowPcidFlag) -ne 0) -and (($flags -band $kvaShadowInvpcidFlag) -ne 0))
  163.  
  164.             if ($PSBoundParameters['Verbose']) {
  165.                 Write-Host "KvaShadowEnabled             :" (($flags -band $kvaShadowEnabledFlag) -ne 0)
  166.                 Write-Host "KvaShadowUserGlobal          :" (($flags -band $kvaShadowUserGlobalFlag) -ne 0)
  167.                 Write-Host "KvaShadowPcid                :" (($flags -band $kvaShadowPcidFlag) -ne 0)
  168.                 Write-Host "KvaShadowInvpcid             :" (($flags -band $kvaShadowInvpcidFlag) -ne 0)
  169.             }
  170.           }
  171.  
  172.           Write-Host "Hardware requires kernel VA shadowing:"$kvaShadowRequired
  173.  
  174.           if ($kvaShadowRequired) {
  175.  
  176.             Write-Host "Windows OS support for kernel VA shadow is present:"$kvaShadowPresent -ForegroundColor $(If ($kvaShadowPresent) { [System.ConsoleColor]::Green } Else { [System.ConsoleColor]::Red })
  177.             Write-Host "Windows OS support for kernel VA shadow is enabled:"$kvaShadowEnabled -ForegroundColor $(If ($kvaShadowEnabled) { [System.ConsoleColor]::Green } Else { [System.ConsoleColor]::Red })
  178.  
  179.             if ($kvaShadowEnabled) {
  180.                 Write-Host "Windows OS support for PCID optimization is enabled:"$kvaShadowPcidEnabled -ForegroundColor $(If ($kvaShadowPcidEnabled) { [System.ConsoleColor]::Green } Else { [System.ConsoleColor]::Red })
  181.             }
  182.           }
  183.  
  184.  
  185.           $object | Add-Member -MemberType NoteProperty -Name KVAShadowRequired -Value $kvaShadowRequired
  186.           $object | Add-Member -MemberType NoteProperty -Name KVAShadowWindowsSupportPresent -Value $kvaShadowPresent
  187.           $object | Add-Member -MemberType NoteProperty -Name KVAShadowWindowsSupportEnabled -Value $kvaShadowEnabled
  188.           $object | Add-Member -MemberType NoteProperty -Name KVAShadowPcidEnabled -Value $kvaShadowPcidEnabled
  189.  
  190.           return $object
  191.  
  192.         }
  193.         finally
  194.         {
  195.           if ($systemInformationPtr -ne [System.IntPtr]::Zero) {
  196.             [System.Runtime.InteropServices.Marshal]::FreeHGlobal($systemInformationPtr)
  197.           }
  198.  
  199.           if ($returnLengthPtr -ne [System.IntPtr]::Zero) {
  200.             [System.Runtime.InteropServices.Marshal]::FreeHGlobal($returnLengthPtr)
  201.           }
  202.         }    
  203.       }
  204.     }
  205.   }
  206.   else{write-host "$pc is offline"}
  207.  }
Advertisement
Add Comment
Please, Sign In to add comment