Advertisement
Guest User

Detect OpenSSL 3.x.x

a guest
Oct 31st, 2022
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #requires -RunAsAdministrator
  2.  
  3. # Useful if deploying script with Intune; exits on first detection
  4. $EXIT_WITH_ERROR = $true
  5. # Intune seems to ignore the exit code, only responds to Write-Error
  6. $EXIT_WITH_ERROR_CODE = 1
  7. # Look for version 3.0.0 through 3.0.6; includes build numbers (fake examples: 3.0.0.5 and 3.1.0 r3)
  8. $VERSION_3XX_REGEX = "3\.0\.[0-6](?:\.[0-9])*"
  9. $COMMON_DIRECTORIES = @(
  10.     "C:\Program Files",
  11.     "C:\Program Files (x86)",
  12.     "C:\ProgramData"
  13. )
  14. $USER_DIRECTORIES = (Get-ChildItem -Path "C:\Users" -Directory).FullName
  15.  
  16. # No reliable way of determing which directory a package is installed
  17. Get-Package -Name *openssl* -ErrorAction SilentlyContinue | ForEach-Object {
  18.     # Version might be empty, so the name may contain it
  19.     if ($_.Name -match $VERSION_3XX_REGEX -or $_.Version -match $VERSION_3XX_REGEX) {
  20.         if ($_.Metadata["Publisher"]) {
  21.             $msg = "Installed program: $($_.Name) - $($_.Metadata["Publisher"])"
  22.         }
  23.         else {
  24.             $msg = "Installed program: $($_.Name)"
  25.         }
  26.  
  27.         if ($EXIT_WITH_ERROR) {
  28.             Write-Error -Message $msg
  29.             exit $EXIT_WITH_ERROR_CODE
  30.         }
  31.         else {
  32.             Write-Warning -Message $msg
  33.         }
  34.     }
  35. }
  36.  
  37. if ($env:PATH + $env:OPENSSL) {
  38.     ($env:PATH + $env:OPENSSL).Split(";") | ForEach-Object {
  39.         if ($_.ToLower().Contains("openssl")) {
  40.             Get-ChildItem -Path $_ -Recurse -Filter "openssl.exe" | ForEach-Object {
  41.                 # Unsure if all version properties are populated; guessing FileVersionRaw is always present
  42.                 # Could compare the Version object instead of using RegEx
  43.                 if ((Get-Item -Path $_.FullName).VersionInfo.FileVersionRaw.ToString() -match $VERSION_3XX_REGEX) {
  44.                     if ($EXIT_WITH_ERROR) {
  45.                         Write-Error -Message "Environment variable: $($_.FullName)"
  46.                         exit $EXIT_WITH_ERROR_CODE
  47.                     }
  48.                     else {
  49.                         Write-Warning -Message "Environment variable: $($_.FullName)"
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.     }
  55. }
  56.  
  57. # Some directories even a local admin can't access; errors will be suppressed
  58. $COMMON_DIRECTORIES | ForEach-Object {
  59.     Get-ChildItem -Path $_ -Recurse -Filter "openssl.exe" -ErrorAction SilentlyContinue | ForEach-Object {
  60.         if ((Get-Item -Path $_.FullName -ErrorAction SilentlyContinue).VersionInfo.FileVersionRaw.ToString() -match $VERSION_3XX_REGEX) {
  61.             if ($EXIT_WITH_ERROR) {
  62.                 Write-Error -Message "Common folder: $($_.FullName)"
  63.                 exit $EXIT_WITH_ERROR_CODE
  64.             }
  65.             else {
  66.                 Write-Warning -Message "Common folder: $($_.FullName)"
  67.             }
  68.         }
  69.     }
  70. }
  71.  
  72. $USER_DIRECTORIES | ForEach-Object {
  73.     Get-ChildItem -Path $_ -Recurse -Filter "openssl.exe" -ErrorAction SilentlyContinue | ForEach-Object {
  74.         if ((Get-Item -Path $_.FullName -ErrorAction SilentlyContinue).VersionInfo.FileVersionRaw.ToString() -match $VERSION_3XX_REGEX) {
  75.             if ($EXIT_WITH_ERROR) {
  76.                 Write-Error -Message "User profile: $($_.FullName)"
  77.                 exit $EXIT_WITH_ERROR_CODE
  78.             }
  79.             else {
  80.                 Write-Warning -Message "User profile: $($_.FullName)"
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement