128 lines
5.8 KiB
PowerShell
128 lines
5.8 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Intune Proactive Remediation - Detection Script
|
|
Erkennt, ob Brave Browser (System- oder Benutzerinstallation) TATSAECHLICH vorhanden ist.
|
|
.DESCRIPTION
|
|
Ein Uninstall-Registry-Eintrag zaehlt nur noch als Treffer, wenn die zugehoerige
|
|
brave.exe / setup.exe auf der Platte existiert. Verwaiste Eintraege (z. B. nach
|
|
einer Deinstallation ueber Brave selbst, waehrend der MSI-Wrapper-Eintrag stehen
|
|
blieb, oder nach SYSTEM-Deinstallation einer Per-User-Installation) werden
|
|
ignoriert und nur in der Ausgabe gemeldet.
|
|
Die Ausgabe nennt immer die Fundstelle -> sichtbar in Intune unter
|
|
"Ausgabe der Erkennung vor der Wartung".
|
|
.NOTES
|
|
Exit 0 = konform (Brave NICHT gefunden) -> keine Remediation
|
|
Exit 1 = nicht konform (Brave gefunden) -> Remediation wird ausgefuehrt
|
|
Intune: Ausfuehrung als System, "Skript in 64-Bit-PowerShell ausfuehren" = Ja
|
|
(falls nicht gesetzt, startet sich das Skript selbst in 64-Bit neu).
|
|
#>
|
|
|
|
# In 32-Bit-PowerShell (Intune-Default) wird HKLM\SOFTWARE auf WOW6432Node umgeleitet
|
|
# -> 64-Bit-Uninstall-Key waere unsichtbar. Daher in 64-Bit-Host neu starten.
|
|
if ($env:PROCESSOR_ARCHITEW6432 -eq 'AMD64' -and $PSCommandPath) {
|
|
& "$env:WINDIR\sysnative\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File $PSCommandPath
|
|
exit $LASTEXITCODE
|
|
}
|
|
|
|
$ErrorActionPreference = 'SilentlyContinue'
|
|
$uninstallSub = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
|
|
$findings = New-Object System.Collections.Generic.List[string]
|
|
$orphans = New-Object System.Collections.Generic.List[string]
|
|
|
|
function Get-ExePathFromString([string]$s) {
|
|
if ([string]::IsNullOrWhiteSpace($s)) { return $null }
|
|
$s = [Environment]::ExpandEnvironmentVariables($s.Trim())
|
|
if ($s -match '^"([^"]+)"') { return $Matches[1] } # "C:\...\setup.exe" --uninstall
|
|
if ($s -match '^(.+?\.exe)') { return $Matches[1] } # C:\...\brave.exe,0
|
|
return $null
|
|
}
|
|
|
|
# Liefert den Pfad, der die Installation belegt, oder $null (= verwaister Eintrag)
|
|
function Get-BraveEntryProof($entry) {
|
|
$candidates = @()
|
|
$candidates += Get-ExePathFromString $entry.DisplayIcon
|
|
if ($entry.InstallLocation) { $candidates += (Join-Path $entry.InstallLocation 'brave.exe') }
|
|
$u = Get-ExePathFromString $entry.UninstallString
|
|
if ($u -and $u -notmatch '(?i)msiexec') { $candidates += $u } # MSI-Eintraege nur ueber Dateisystem-Check
|
|
foreach ($c in $candidates) {
|
|
if ($c -and (Test-Path -LiteralPath $c -PathType Leaf)) { return $c }
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function Test-UninstallKey([string]$keyPath, [string]$label) {
|
|
Get-ItemProperty -Path "$keyPath\*" -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.DisplayName -like 'Brave*' } |
|
|
ForEach-Object {
|
|
$proof = Get-BraveEntryProof $_
|
|
if ($proof) { $findings.Add("$label '$($_.DisplayName)' -> $proof") }
|
|
else { $orphans.Add("$label '$($_.DisplayName)' [$($_.PSChildName)]") }
|
|
}
|
|
}
|
|
|
|
# 1) Systemweit (64-Bit und 32-Bit-Ansicht)
|
|
Test-UninstallKey "Registry::HKEY_LOCAL_MACHINE\$uninstallSub" 'HKLM'
|
|
Test-UninstallKey "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" 'HKLM-WOW64'
|
|
|
|
# 2) Geladene Benutzer-Hives (inkl. eigener Hive bei Ausfuehrung im Benutzerkontext)
|
|
$loadedSids = @()
|
|
Get-ChildItem 'Registry::HKEY_USERS' -ErrorAction SilentlyContinue | ForEach-Object {
|
|
$sid = $_.PSChildName
|
|
if ($sid -match '^S-1-5-21-\d+-\d+-\d+-\d+$') {
|
|
$loadedSids += $sid
|
|
Test-UninstallKey "Registry::HKEY_USERS\$sid\$uninstallSub" "HKU\$sid"
|
|
}
|
|
}
|
|
|
|
# 3) Nicht geladene Profile: NTUSER.DAT temporaer mounten
|
|
$profilePaths = @()
|
|
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' -ErrorAction SilentlyContinue | ForEach-Object {
|
|
$sid = $_.PSChildName
|
|
if ($sid -notmatch '^S-1-5-21-\d+-\d+-\d+-\d+$') { return }
|
|
$profilePath = (Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue).ProfileImagePath
|
|
if ($profilePath) { $profilePaths += $profilePath }
|
|
if ($loadedSids -contains $sid) { return }
|
|
|
|
$ntUserDat = Join-Path $profilePath 'NTUSER.DAT'
|
|
if (-not (Test-Path -LiteralPath $ntUserDat)) { return }
|
|
|
|
$mount = "HKU\TempBraveCheck_$sid"
|
|
& reg.exe load $mount "$ntUserDat" 2>$null | Out-Null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
try {
|
|
Test-UninstallKey "Registry::HKEY_USERS\TempBraveCheck_$sid\$uninstallSub" "Offline\$sid"
|
|
} finally {
|
|
[gc]::Collect(); [gc]::WaitForPendingFinalizers()
|
|
& reg.exe unload $mount 2>$null | Out-Null
|
|
}
|
|
}
|
|
}
|
|
|
|
# 4) Dateisystem: Standardpfade + alle Profilordner
|
|
$exeCandidates = @(
|
|
"$env:ProgramW6432\BraveSoftware\Brave-Browser\Application\brave.exe",
|
|
"${env:ProgramFiles(x86)}\BraveSoftware\Brave-Browser\Application\brave.exe"
|
|
)
|
|
$profilePaths += (Get-ChildItem 'C:\Users' -Directory -ErrorAction SilentlyContinue).FullName
|
|
foreach ($p in ($profilePaths | Where-Object { $_ } | Sort-Object -Unique)) {
|
|
$exeCandidates += (Join-Path $p 'AppData\Local\BraveSoftware\Brave-Browser\Application\brave.exe')
|
|
}
|
|
foreach ($exe in ($exeCandidates | Where-Object { $_ } | Sort-Object -Unique)) {
|
|
if (Test-Path -LiteralPath $exe -PathType Leaf) {
|
|
if (-not ($findings | Where-Object { $_ -like "*$exe" })) { $findings.Add("Datei -> $exe") }
|
|
}
|
|
}
|
|
|
|
# Ergebnis (Intune erfasst max. ~2048 Zeichen)
|
|
$orphanText = if ($orphans.Count) { ' | Verwaiste Eintraege (ignoriert): ' + ($orphans -join '; ') } else { '' }
|
|
|
|
if ($findings.Count -gt 0) {
|
|
$msg = 'Brave gefunden: ' + ($findings -join '; ') + $orphanText
|
|
Write-Output ($msg.Substring(0, [Math]::Min(2000, $msg.Length)))
|
|
exit 1
|
|
} else {
|
|
$msg = 'Brave nicht gefunden - System konform.' + $orphanText
|
|
Write-Output ($msg.Substring(0, [Math]::Min(2000, $msg.Length)))
|
|
exit 0
|
|
}
|