-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathConfig-uBlock-Origin.ps1
More file actions
363 lines (304 loc) · 14.8 KB
/
Config-uBlock-Origin.ps1
File metadata and controls
363 lines (304 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<#
.SYNOPSIS
Configure uBlock Origin extension via Group Policy for Microsoft Edge and Google Chrome
.DESCRIPTION
Deploys uBlock Origin (Manifest V2) extension configuration via registry-based Group Policy.
Runs in SYSTEM context and configures HKLM registry keys.
Provides full-featured configuration with detailed user settings, filter lists, and custom filters.
.NOTES
Author: Martin Bengtsson
Requires: Administrator privileges
Extension: uBlock Origin (Manifest V2)
Note: Chrome is deprecating Manifest V2 - consider uBlock Origin Lite for long-term Chrome support
#>
#Requires -RunAsAdministrator
# ============================================
# Configuration
# ============================================
# --- Script Behavior ---
$RemoveConfiguration = $true # Set to $true to REMOVE all uBlock Origin policies instead of configuring them
# --- Browser Selection ---
$ConfigureEdge = $true # Configure Microsoft Edge
$ConfigureChrome = $true # Configure Google Chrome
# --- Extension Installation ---
$InstallExtension = $true # Force-install the extension automatically
# --- Advanced: Chrome Manifest V2 Policy ---
# Chrome is deprecating Manifest V2. Setting this to $true allows MV2 extensions that are force-installed.
$EnableManifestV2 = $true
# --- Internal ---
$LogPrefix = "[uBlock-Origin]"
# ============================================
# Browser Definitions
# ============================================
$browsers = @{
"Edge" = @{
"Enabled" = $ConfigureEdge
"PolicyPath" = "HKLM:\SOFTWARE\Policies\Microsoft\Edge"
"ExtensionID" = "odfafepnkmbhccpbejgmiehpchacaeak"
"UpdateURL" = "https://edge.microsoft.com/extensionwebstorebase/v1/crx"
}
"Chrome" = @{
"Enabled" = $ConfigureChrome
"PolicyPath" = "HKLM:\SOFTWARE\Policies\Google\Chrome"
"ExtensionID" = "cjpalhdlnbpafiamejdnhcphjbkeiagm"
"UpdateURL" = "https://clients2.google.com/service/update2/crx"
}
}
# ============================================
# Process Each Browser
# ============================================
ForEach ($browserName in $browsers.Keys) {
$browser = $browsers[$browserName]
If (-Not $browser.Enabled) {
Write-Output "$LogPrefix Skipping $browserName (disabled in configuration)"
Continue
}
# Build registry paths
$uBlockPolicyPath = "$($browser.PolicyPath)\3rdparty\Extensions\$($browser.ExtensionID)\policy"
$extensionInstallPath = "$($browser.PolicyPath)\ExtensionInstallForcelist"
# ============================================
# Remove Configuration Mode
# ============================================
If ($RemoveConfiguration) {
Write-Output "$LogPrefix REMOVING uBlock Origin configuration for $browserName"
# Remove from force install list
If (Test-Path -Path $extensionInstallPath) {
Write-Output "$LogPrefix Checking extension force install list..."
$existingItems = Get-ItemProperty -Path $extensionInstallPath -ErrorAction SilentlyContinue
If ($existingItems) {
$existingProps = $existingItems.PSObject.Properties | Where-Object { $_.Name -match '^\d+$' }
$removed = $false
ForEach ($prop in $existingProps) {
If ($prop.Value -like "$($browser.ExtensionID)*") {
Remove-ItemProperty -Path $extensionInstallPath -Name $prop.Name -ErrorAction SilentlyContinue
Write-Output "$LogPrefix Removed uBlock Origin from force install list (index: $($prop.Name))."
$removed = $true
}
}
If (-Not $removed) {
Write-Output "$LogPrefix uBlock Origin was not found in the force install list."
}
}
} Else {
Write-Output "$LogPrefix Extension force install list does not exist for $browserName."
}
# Remove uBlock policy configuration
If (Test-Path -Path $uBlockPolicyPath) {
Remove-Item -Path $uBlockPolicyPath -Recurse -Force -ErrorAction SilentlyContinue
Write-Output "$LogPrefix Removed uBlock Origin policy configuration for $browserName."
} Else {
Write-Output "$LogPrefix uBlock Origin policy configuration does not exist for $browserName."
}
Write-Output "$LogPrefix $browserName configuration removal completed!"
Continue
}
# ============================================
# Normal Configuration Mode
# ============================================
Write-Output "$LogPrefix Configuring uBlock Origin for $browserName"
# ============================================
# Extension Force Installation
# ============================================
If ($InstallExtension) {
Write-Output "$LogPrefix Configuring extension force installation for $browserName..."
Try {
If (-Not (Test-Path -Path $extensionInstallPath)) {
New-Item -Force -Path $extensionInstallPath -ErrorAction Stop | Out-Null
Write-Output "$LogPrefix Created ExtensionInstallForcelist registry key for $browserName."
}
$extensionValue = "$($browser.ExtensionID);$($browser.UpdateURL)"
# Find next available index
$existingItems = Get-ItemProperty -Path $extensionInstallPath -ErrorAction SilentlyContinue
$nextIndex = 1
If ($existingItems) {
$existingProps = $existingItems.PSObject.Properties | Where-Object { $_.Name -match '^\d+$' }
If ($existingProps) {
$nextIndex = ($existingProps.Name | ForEach-Object { [int]$_ } | Measure-Object -Maximum).Maximum + 1
}
}
# Check if already in list
$alreadyInstalled = $false
If ($existingItems) {
$existingProps = $existingItems.PSObject.Properties | Where-Object { $_.Name -match '^\d+$' }
ForEach ($prop in $existingProps) {
If ($prop.Value -like "$($browser.ExtensionID)*") {
$alreadyInstalled = $true
Write-Output "$LogPrefix uBlock Origin is already in the $browserName force install list (index: $($prop.Name))."
Break
}
}
}
If (-Not $alreadyInstalled) {
Set-ItemProperty -Path $extensionInstallPath -Name "$nextIndex" -Value $extensionValue -Type String -ErrorAction Stop
Write-Output "$LogPrefix Added uBlock Origin to $browserName extension force install list (index: $nextIndex)."
Write-Output "$LogPrefix The extension will be automatically installed when $browserName is restarted."
}
}
Catch {
Write-Output "$LogPrefix ERROR: Failed to configure extension force installation for $browserName - $($_.Exception.Message)"
Exit 1
}
} Else {
Write-Output "$LogPrefix Skipping extension force installation for $browserName (disabled in configuration)."
}
# ============================================
# Chrome Manifest V2 Support Configuration
# ============================================
If ($browserName -eq "Chrome" -and $EnableManifestV2) {
Write-Output "$LogPrefix Configuring Manifest V2 support for Chrome..."
Try {
$manifestV2Path = "$($browser.PolicyPath)"
If (-Not (Test-Path -Path $manifestV2Path)) {
New-Item -Force -Path $manifestV2Path -ErrorAction Stop | Out-Null
}
Set-ItemProperty -Path $manifestV2Path -Name "ExtensionManifestV2Availability" -Value 3 -Type DWord -ErrorAction Stop
Write-Output "$LogPrefix Set ExtensionManifestV2Availability to 3 (Manifest V2 enabled for force-installed extensions only)."
}
Catch {
Write-Output "$LogPrefix ERROR: Failed to configure Manifest V2 support - $($_.Exception.Message)"
Exit 1
}
}
# ============================================
# uBlock Origin Configuration
# ============================================
Write-Output "$LogPrefix Configuring uBlock Origin for $browserName..."
# Create policy path if it doesn't exist
Try {
If (-Not (Test-Path -Path $uBlockPolicyPath)) {
New-Item -Force -Path $uBlockPolicyPath -ErrorAction Stop | Out-Null
Write-Output "$LogPrefix Created uBlock Origin policy registry path for $browserName."
}
}
Catch {
Write-Output "$LogPrefix ERROR: Failed to create policy path - $($_.Exception.Message)"
Exit 1
}
Write-Output "$LogPrefix Applying uBlock Origin configuration for $browserName..."
# ============================================
# User Settings Configuration
# Set to "true" or "false" to enable/disable
# ============================================
# General Settings
$hidePlaceholders = "true" # Hide placeholders of blocked elements
$showIconBadge = "false" # Show blocked count on icon
$contextMenuEnabled = "true" # Enable right-click context menu
$cloudStorageEnabled = "false" # Enable cloud storage support
# Privacy Settings
$prefetchingDisabled = "true" # Disable prefetching
$hyperlinkAuditingDisabled = "true" # Disable hyperlink auditing/tracking
$blockCSPReports = "false" # Block CSP reports
$cnameUncloakEnabled = "false" # Uncloak canonical names (Firefox only)
# Appearance Settings
$uiTheme = "auto" # Theme: "light", "dark", or "auto"
$colorBlindFriendly = "false" # Color-blind friendly mode
$tooltipsDisabled = "false" # Disable tooltips
# Default Behavior (Global per-site switches)
$noCosmeticFiltering = "false" # Disable cosmetic filtering globally
$noLargeMedia = "false" # Block large media elements globally
$noRemoteFonts = "false" # Block remote fonts globally
$noScripting = "false" # Disable JavaScript globally
# Advanced Settings
$advancedUserEnabled = "false" # Enable advanced user mode
$dynamicFilteringEnabled = "false" # Enable dynamic filtering (requires advanced mode)
# Build userSettings array
$userSettingsArray = @(
"[ `"contextMenuEnabled`", `"$contextMenuEnabled`" ]",
"[ `"showIconBadge`", `"$showIconBadge`" ]",
"[ `"hidePlaceholders`", `"$hidePlaceholders`" ]",
"[ `"cloudStorageEnabled`", `"$cloudStorageEnabled`" ]",
"[ `"prefetchingDisabled`", `"$prefetchingDisabled`" ]",
"[ `"hyperlinkAuditingDisabled`", `"$hyperlinkAuditingDisabled`" ]",
"[ `"blockCSPReports`", `"$blockCSPReports`" ]",
"[ `"cnameUncloakEnabled`", `"$cnameUncloakEnabled`" ]",
"[ `"uiTheme`", `"$uiTheme`" ]",
"[ `"colorBlindFriendly`", `"$colorBlindFriendly`" ]",
"[ `"tooltipsDisabled`", `"$tooltipsDisabled`" ]",
"[ `"noCosmeticFiltering`", `"$noCosmeticFiltering`" ]",
"[ `"noLargeMedia`", `"$noLargeMedia`" ]",
"[ `"noRemoteFonts`", `"$noRemoteFonts`" ]",
"[ `"noScripting`", `"$noScripting`" ]",
"[ `"advancedUserEnabled`", `"$advancedUserEnabled`" ]",
"[ `"dynamicFilteringEnabled`", `"$dynamicFilteringEnabled`" ]"
)
$userSettingsValue = "[ " + ($userSettingsArray -join ", ") + " ]"
# Apply userSettings
$userSettings = @{
"Force" = $true
"Path" = "$uBlockPolicyPath"
"Type" = "String"
"Name" = "userSettings"
"Value" = $userSettingsValue
}
Try {
Set-ItemProperty @userSettings -ErrorAction Stop
Write-Output "$LogPrefix Applied userSettings for $browserName."
}
Catch {
Write-Output "$LogPrefix ERROR: Failed to apply userSettings - $($_.Exception.Message)"
Exit 1
}
# ============================================
# Filter Lists and Custom Filters
# ============================================
# Filter Lists - Add or remove filter list tokens
$filterLists = @(
"user-filters",
"ublock-filters",
"ublock-badware",
"ublock-privacy",
"ublock-abuse",
"ublock-unbreak",
"easylist",
"easyprivacy",
"urlhaus-1",
"adguard-annoyance",
"ublock-annoyances",
"plowe-0"
)
# Trusted Sites - Sites where uBlock will be disabled
$trustedSites = @(
"chrome-extension-scheme"
"moz-extension-scheme",
"imab.dk",
"mindcore.dk"
# Add more trusted sites below (one per line):
# "example.com",
# "trusted-site.org"
)
# Custom Filters - Add custom blocking rules
$customFilters = @(
# "! Test filters",
# "||test.com^`$important",
# "||example.com^`$important"
)
# Build the toOverwrite JSON value
$filterListsJson = ($filterLists | ForEach-Object { "`"$_`"" }) -join ", "
$trustedSitesJson = ($trustedSites | ForEach-Object { "`"$_`"" }) -join ", "
$customFiltersJson = ($customFilters | ForEach-Object { "`"$_`"" }) -join ", "
$toOverwriteValue = "{ `"filterLists`": [ $filterListsJson ], `"trustedSiteDirectives`": [ $trustedSitesJson ], `"filters`": [ $customFiltersJson ] }"
# Apply toOverwrite configuration
$toOverwrite = @{
"Force" = $true
"Path" = "$uBlockPolicyPath"
"Type" = "String"
"Name" = "toOverwrite"
"Value" = $toOverwriteValue
}
Try {
Set-ItemProperty @toOverwrite -ErrorAction Stop
Write-Output "$LogPrefix Applied filter lists and custom filters for $browserName."
}
Catch {
Write-Output "$LogPrefix ERROR: Failed to apply filter configuration - $($_.Exception.Message)"
Exit 1
}
Write-Output "$LogPrefix $browserName configuration completed successfully!"
} # End ForEach browser
If ($RemoveConfiguration) {
Write-Output "$LogPrefix All browser configuration removals completed successfully!"
} Else {
Write-Output "$LogPrefix All browser configurations completed successfully!"
}
# Exit with success code for Intune
Exit 0