<# .SYNOPSIS Cyber Platform Browser Guard: forces the Cyber Platform extension into Chrome, Edge and Brave on this PC and keeps it there. .DESCRIPTION Writes machine-level browser policies (HKLM\SOFTWARE\Policies): the extension is installed from the Cyber Platform server's update feed and cannot be removed or disabled by the user; developer mode, incognito/InPrivate and guest windows are switched off; extensions from outside the stores are blocked. A scheduled task re-applies the policies every hour and at start-up, so hand edits are undone. Nothing else on the PC is changed. Run as administrator. Chrome and Edge accept an off-store forced install (-Source feed) only on managed PCs: domain-joined, Entra-joined, MDM-enrolled or enrolled in Chrome Browser Cloud Management. Everywhere else the extension must come from the stores (-Source store, the default). .PARAMETER Server The Cyber Platform server, for example https://custode.example.com or http://localhost:5080. Required unless -Uninstall or -Reapply. .PARAMETER Source Where the browsers get the extension from. "store" (default): the Chrome Web Store and the Edge Add-ons store, which is the only source Chrome and Edge accept on a PC that is not domain-joined or cloud-managed. "feed": this server's own update feed, accepted only on managed PCs (Active Directory, Entra ID or Chrome Browser Cloud Management). Both need the extension published for "store". .PARAMETER ChromeStoreId The extension id on the Chrome Web Store (also used by Brave). Defaults to the id the server reports, which is kept at publication. .PARAMETER EdgeStoreId The extension id on the Edge Add-ons store. Required for Source "store" on Edge; without it Edge is skipped. .PARAMETER LockOtherExtensions Also block every other extension except the ones the administrator allows (ExtensionInstallBlocklist = *). .PARAMETER Uninstall Remove every policy and the scheduled task this script created. .PARAMETER Reapply Internal: used by the scheduled task. Re-applies the saved policies without network access. .PARAMETER DryRun Print what would be written instead of writing it. .EXAMPLE powershell -NoProfile -ExecutionPolicy Bypass -File Install-BrowserGuard.ps1 -Server http://localhost:5080 .EXAMPLE powershell -NoProfile -ExecutionPolicy Bypass -File Install-BrowserGuard.ps1 -Uninstall #> [CmdletBinding()] param( [string]$Server, [ValidateSet("store", "feed")][string]$Source = "store", [string]$ChromeStoreId = "", [string]$EdgeStoreId = "", [switch]$LockOtherExtensions, [switch]$Uninstall, [switch]$Reapply, [switch]$DryRun ) $ErrorActionPreference = "Stop" $Product = "Cyber Platform" $StateDir = Join-Path $env:ProgramData $Product $StateFile = Join-Path $StateDir "guard.json" $TaskName = "$Product Browser Guard" $Browsers = @( @{ Name = "Chrome"; Key = "HKLM:\SOFTWARE\Policies\Google\Chrome"; Private = "IncognitoModeAvailability"; Store = "chrome" }, @{ Name = "Edge"; Key = "HKLM:\SOFTWARE\Policies\Microsoft\Edge"; Private = "InPrivateModeAvailability"; Store = "edge" }, @{ Name = "Brave"; Key = "HKLM:\SOFTWARE\Policies\BraveSoftware\Brave"; Private = "IncognitoModeAvailability"; Store = "chrome" } ) function Test-Admin { ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } function Say($t) { Write-Host "[$Product] $t" } function Set-Reg($key, $name, $value, $type) { if ($DryRun) { Say "would set $key\$name = $value ($type)"; return } if (-not (Test-Path $key)) { New-Item -Path $key -Force | Out-Null } New-ItemProperty -Path $key -Name $name -Value $value -PropertyType $type -Force | Out-Null } function Remove-Reg($key, $name) { if ($DryRun) { Say "would remove $key\$name"; return } if (Test-Path $key) { Remove-ItemProperty -Path $key -Name $name -ErrorAction SilentlyContinue } } function Remove-ListEntries($key, $match) { # Removes the entries of a numbered list policy (ExtensionInstallForcelist etc.) whose value matches, leaving others alone. if (-not (Test-Path $key)) { return } $item = Get-Item $key foreach ($n in $item.GetValueNames()) { if (($item.GetValue($n) -as [string]) -like $match) { Remove-Reg $key $n } } } function Add-ListEntry($key, $value) { if (-not $DryRun -and -not (Test-Path $key)) { New-Item -Path $key -Force | Out-Null } $used = @(); if (Test-Path $key) { $used = (Get-Item $key).GetValueNames() | ForEach-Object { [int]$_ } } foreach ($n in $used) { if ((Get-ItemProperty $key).$n -eq $value) { return } } $i = 1; while ($used -contains $i) { $i++ } Set-Reg $key "$i" $value "String" } function ForceValue($state, $b) { # feed: ";" from this server; store: the store id alone (the browser uses its own store update address) if ($state.source -eq "feed") { return $state.id + ";" + $state.updateUrl } if ($b.Store -eq "edge") { if ($state.edgeStoreId) { return $state.edgeStoreId } else { return $null } } if ($state.chromeStoreId) { return $state.chromeStoreId } else { return $state.id } } function Apply($state) { foreach ($b in $Browsers) { $k = $b.Key Remove-ListEntries "$k\ExtensionInstallForcelist" ($state.id + "*"); if ($state.chromeStoreId) { Remove-ListEntries "$k\ExtensionInstallForcelist" ($state.chromeStoreId + "*") }; if ($state.edgeStoreId) { Remove-ListEntries "$k\ExtensionInstallForcelist" ($state.edgeStoreId + "*") } $v = ForceValue $state $b if ($v) { Add-ListEntry "$k\ExtensionInstallForcelist" $v } else { Say ("no " + $b.Name + " store id given: " + $b.Name + " gets the other policies but no forced install") } Set-Reg $k "ExtensionDeveloperModeSettings" 1 "DWord" # developer mode off (Chrome/Edge 128+; older versions ignore it) Set-Reg $k "DeveloperToolsAvailability" 0 "DWord" # DevTools blocked on force-installed extensions only Set-Reg $k $b.Private 1 "DWord" # no incognito / InPrivate windows (the extension does not run there) Set-Reg $k "BrowserGuestModeEnabled" 0 "DWord" # no guest windows Set-Reg $k "BlockExternalExtensions" 1 "DWord" # no side-loaded extensions if ($state.lock) { Remove-ListEntries "$k\ExtensionInstallBlocklist" "*" Add-ListEntry "$k\ExtensionInstallBlocklist" "*" Add-ListEntry "$k\ExtensionInstallAllowlist" $state.id } Say ("policies written for " + $b.Name) } } function Unapply($state) { foreach ($b in $Browsers) { $k = $b.Key if ($state -and $state.id) { foreach ($i in @($state.id, $state.chromeStoreId, $state.edgeStoreId)) { if ($i) { Remove-ListEntries "$k\ExtensionInstallForcelist" ($i + "*"); Remove-ListEntries "$k\ExtensionInstallAllowlist" $i } } } Remove-ListEntries "$k\ExtensionInstallBlocklist" "*" foreach ($n in "ExtensionDeveloperModeSettings", "DeveloperToolsAvailability", $b.Private, "BrowserGuestModeEnabled", "BlockExternalExtensions") { Remove-Reg $k $n } if (-not $DryRun) { foreach ($sub in "ExtensionInstallForcelist", "ExtensionInstallBlocklist", "ExtensionInstallAllowlist") { $kp = "$k\$sub"; if ((Test-Path $kp) -and ((Get-Item $kp).GetValueNames().Count -eq 0)) { Remove-Item $kp -Force } } if ((Test-Path $k) -and ((Get-Item $k).GetValueNames().Count -eq 0) -and ((Get-Item $k).GetSubKeyNames().Count -eq 0)) { Remove-Item $k -Force } } # leave no empty policy keys behind Say ("policies removed for " + $b.Name) } } function Register-Guard { if ($DryRun) { Say "would register the scheduled task '$TaskName' (hourly and at start-up, as SYSTEM)"; return } $script = Join-Path $StateDir "BrowserGuard.ps1" Copy-Item -Path $PSCommandPath -Destination $script -Force $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$script`" -Reapply" $triggers = @((New-ScheduledTaskTrigger -AtStartup), (New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 1))) $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $triggers -Principal $principal -Force | Out-Null Say "scheduled task '$TaskName' registered" } if (-not $DryRun -and -not (Test-Admin)) { throw "Run this script as administrator (it writes machine policies under HKLM)." } if ($Uninstall) { $state = $null; if (Test-Path $StateFile) { $state = Get-Content $StateFile -Raw | ConvertFrom-Json } Unapply $state if (-not $DryRun) { Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue if (Test-Path $StateDir) { Remove-Item $StateDir -Recurse -Force } } Say "removed. Restart the browsers; chrome://policy and edge://policy should no longer list these policies." return } if ($Reapply) { if (-not (Test-Path $StateFile)) { throw "No saved state at $StateFile" } Apply (Get-Content $StateFile -Raw | ConvertFrom-Json) return } if (-not $Server) { throw "Give the server address: -Server https://custode.example.com" } $Server = $Server.TrimEnd("/") try { $info = Invoke-RestMethod -Uri "$Server/ext/info.json" -UseBasicParsing } catch { throw "The server at $Server has no extension package ($($_.Exception.Message)). Run node scripts/pack-extension.mjs on the server first." } if (-not $info.id -or -not $info.updateUrl) { throw "Unexpected answer from $Server/ext/info.json" } $state = [ordered]@{ server = $Server; id = $info.id; version = $info.version; updateUrl = $info.updateUrl; source = $Source; chromeStoreId = $(if ($ChromeStoreId) { $ChromeStoreId } else { $info.chromeStoreId }); edgeStoreId = $(if ($EdgeStoreId) { $EdgeStoreId } else { $info.edgeStoreId }); lock = [bool]$LockOtherExtensions; installedAt = (Get-Date).ToString("s") } Say ("extension " + $info.id + " version " + $info.version + ", source: " + $Source) if ($Source -eq "feed") { Say "NOTE: a PC that is not domain-joined or cloud-managed refuses forced installs from a feed (chrome://policy shows the entry as BLOCKED). Use -Source store once the extension is published." } if ($Source -eq "store" -and -not $state.edgeStoreId) { Say "NOTE: no Edge Add-ons id known: Edge will be locked down but will not install the extension until -EdgeStoreId is given." } if (-not $DryRun) { New-Item -ItemType Directory -Force -Path $StateDir | Out-Null; $state | ConvertTo-Json | Set-Content -Path $StateFile -Encoding UTF8 } Apply $state Register-Guard Say "done. Restart Chrome, Edge and Brave: the extension installs itself and shows 'installed by your administrator'. chrome://policy lists the policies." Say "To undo: powershell -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Uninstall"