-
Notifications
You must be signed in to change notification settings - Fork 1
/
updateHostsFile.ps1
424 lines (341 loc) · 13.3 KB
/
updateHostsFile.ps1
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<#
.CREATION
Created by : Eddy Erkel
Version : 1.51
Date : 27 October 2018
.DISCLAMER
This script is provided "as is", without warranty of any kind.
Use it at your own risk. I assume no liability for damages,
direct or consequential, that may result from the use of this script.
Grateful for my work and in a generous mood?
BTC: 18JNWyGhfAmhkWs7jzuuHn54jEZRPj81Jx
ETH: 0x067e8b995f7dbaf32081bc32927f6fac29b32055
LTC: LLqwyRiKiuvxkx76grFmbxEeoChLnxvaKH
.SYNOPSIS
Update your hosts file by using different sources from the internet.
.DESCRIPTION
This PowerShell script will combine local host files with online host files into one, unique host file for safe internet browsing.
Online hosts file sources are provided by Steven Black at https://github.com/StevenBlack/hosts.
RUN THIS SCRIPT WITH ADMINISTRATOR PRIVELAGES.
The script needs to be run with administrator privelages, therefor open PowerShell by rightclicking the PowerShell icon and select 'Run as administrator'.
.PARAMETER Gambling
Block gambling domains.
.PARAMETER Porn
Block porn domains.
.PARAMETER Social
Block social domains.
.PARAMETER Backup
Create a backup of the current active hosts file.
Provide the number of backup files to keep. Use 0 to keep all files.
.PARAMETER IP
Replace IP-addresses. Default IP-address is 0.0.0.0.
.PARAMETER Replace
Replace current active hosts file and and flush the DNS cache.
.PARAMETER UseHostsDir
Use the hosts directory for downloading and processing. By default the script directory is used.
.PARAMETER ReportDomains
Display added, white-listed and black-listed hosts.
.INPUT-FILE myhosts
This script will look for the file "myhosts.txt" (unless otherwise specified below) and when found it will add all entries found to the end of the hosts file.
The myhost file format is the same as you would add to the hosts file.
Myhosts file format:
10.0.0.1 www.domain-name1.com
10.0.0.2 www.domain-name2.com
.INPUT-FILE whitelist
This script will look for the file "whitelist.txt" (unless otherwise specified below) and when found it will search through the hosts file and
whitelist all domains found in the whitelist file by adding a hash (#) sign at the start of the line.
Whitelist file format:
domain-name3.com
www.domain-name4.com
* domain-name3.com -> all domains ending with domain-name1.com will be whitelisted e.g. www.domain-name1.com, home.domain-name1.com, etc.
* www.domain-name4.com -> will whitelist domains ending with www.domain-name2.com (most likely only www.domain-name2.com).
.INPUT-FILE blacklist
This script will look for the file "blacklist.txt" (unless otherwise specified below) and when found it will add all entries found to the end of the hosts file.
The blacklist file format is the same as you would add to the hosts file.
Blacklist file format:
www.domain-name5.com
www.domain-name6.com
.EXAMPLE
.\updateHostsFile.ps1 -Gambling -Social -Porn -Replace -Backup 6 -IP 1.2.3.4 -UseHostsDir
.\updateHostsFile.ps1 -Gambling -Social -Porn -IP 127.0.0.1 -Backup 0 -Replace
.\updateHostsFile.ps1 -Gambling -Social -Porn -Backup 3 -Replace
.\updateHostsFile.ps1 -Replace
To bypass the PowerShell Execution Policy type:
powershell.exe -executionpolicy bypass -file .\updateHostsFile.ps1 -Gambling -Replace
To create a scheduled task use:
Program/script: powershell.exe
Add arguments : -executionpolicy bypass -file <Path-to-script>\updateHostsFile.ps1
Start in : Leave blank / empty
#>
Param (
[Parameter(Mandatory=$false)]
[switch]$Gambling,
[Parameter(Mandatory=$false)]
[switch]$Porn,
[Parameter(Mandatory=$false)]
[switch]$Social,
[Parameter(Mandatory=$false)]
[int]$Backup,
[Parameter(Mandatory=$false)]
[string]$IP,
[Parameter(Mandatory=$false)]
[switch]$Replace,
[Parameter(Mandatory=$false)]
[switch]$UseHostsDir,
[Parameter(Mandatory=$false)]
[switch]$ReportDomains
)
# Read start time
$startTime = Get-Date
$timeStamp = (Get-Date -format yyyyMMdd-HHmmss)
# Set defaults IP address
$defaultIP = "0.0.0.0"
# Set My Hosts file
$myHosts = "myhosts.txt"
# Set Whitelist file
$whiteList = "whitelist.txt"
# Set Blacklist file
$blackList = "blacklist.txt"
# Read SystemRoot environment variable
$systemRoot = $Env:SystemRoot
# Set hosts file directory
$hostsDir = "$systemRoot\System32\drivers\etc"
# Get script path
$scriptDir = $PSScriptRoot
If (!($scriptPath))
{
# Get script path via Powershell v2 command
$scriptDir = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
}
Write-Host `n
# Check hosts file last modify date
$hostFileDate = (Get-Item $hostsDir\hosts).LastWriteTime
$daysSinceUpdate=(New-TimeSpan -Start $hostFileDate -End $startTime).Days
Write-Host "Your systems current active hosts file was last updated on $hostFileDate ($daysSinceUpdate days ago)."
Write-Host `n
# Set replacement IP address
If (!($IP))
{
$IP = $defaultIP
}
Write-Host "Using `"$IP`" as replace/blacklist IP-address."
# Set process directory
If ($UseHostsDir)
{
Write-Host "Using $hostsDir\ for downloading and processing."
$processDir = $hostsDir
}
Else
{
Write-Host "Using $scriptDir\ for downloading and processing."
$processDir = $scriptDir
}
Write-Host `n
# Hosts extensions selection
If ($Gambling -eq $False -AND $Porn -eq $False -AND $Social -eq $False)
{
# Unified hosts = (Adware + Malware)
Write-Host "Selected Unified hosts (Adware + Malware)."
$hostsUrl = "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
$newHosts = "hosts.unified"
}
# Unified hosts + Gambling
If ($Gambling -eq $True -AND $Porn -eq $False -AND $Social -eq $False)
{
Write-Host "Selected Unified hosts (Adware + Malware) + Gambling."
$hostsUrl = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling/hosts"
$newHosts = "hosts.gambling"
}
# Unified hosts + Porn
If ($Gambling -eq $False -AND $Porn -eq $True -AND $Social -eq $False)
{
Write-Host "Selected Unified hosts (Adware + Malware) + Porn"
$hostsUrl = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/porn/hosts"
$newHosts = "hosts.porn"
}
# Unified hosts + Social
If ($Gambling -eq $False -AND $Porn -eq $False -AND $Social -eq $True)
{
Write-Host "Selected Unified hosts (Adware + Malware) + Social"
$hostsUrl = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/social/hosts"
$newHosts = "hosts.social"
}
# Unified hosts + gambling + porn
If ($Gambling -eq $True -AND $Porn -eq $True -AND $Social -eq $False)
{
Write-Host "Selected Unified hosts (Adware + Malware) + Gambling + Porn."
$hostsUrl = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling-porn/hosts"
$newHosts = "hosts.gambling-porn"
}
# Unified hosts + gambling + social
If ($Gambling -eq $True -AND $Porn -eq $False -AND $Social -eq $True)
{
Write-Host "Selected Unified hosts (Adware + Malware) + Gambling + Social."
$hostsUrl = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling-social/hosts"
$newHosts = "hosts.gambling-social"
}
# Unified hosts + porn + social
If ($Gambling -eq $False -AND $Porn -eq $True -AND $Social -eq $True)
{
Write-Host "Selected Unified hosts (Adware + Malware) + Porn + Social."
$hostsUrl = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/porn-social/hosts"
$newHosts = "hosts.porn-social"
}
# Unified hosts + gambling + porn + social
If ($Gambling -eq $True -AND $Porn -eq $True -AND $Social -eq $True)
{
Write-Host "Selected Unified hosts (Adware + Malware) + Gambling + Porn + Social."
$hostsUrl = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling-porn-social/hosts"
$newHosts = "hosts.gambling-porn-social"
}
# Download hosts file
Write-Host "Downloading $newHosts to $processDir\."
Invoke-WebRequest -Uri $hostsUrl -OutFile "$processDir\$newHosts"
If (!(Test-Path "$processDir\$newHosts"))
{
Write-Host "Failed to download $newHosts." -ForeGroundColor Red
Exit
}
Write-Host `n
# Replace IP addresses
(Get-Content "$processDir\$newHosts") | `
ForEach-Object { $_ -replace "127.0.0.1", "$IP" } | `
ForEach-Object { $_ -replace "0.0.0.0", "$IP" } | `
ForEach-Object { $_ -replace "$IP local", "127.0.0.1 local" } | Set-Content "$processDir\$newHosts"
# Add myhosts file contents if file exists
If (Test-Path "$processDir\$myHosts")
{
Write-Host "File `"$processDir\$myHosts`" exists."
Write-Host "Adding custom domains found in input-file `"$myHosts`"."
If ($ReportDomains) {
Write-Host "Custom domains:"
Get-Content "$processDir\$myHosts" | Where-Object {$_ -notmatch "^#"}
}
Add-Content -Path "$processDir\$newHosts" -Value ""
Add-Content -Path "$processDir\$newHosts" -Value "# Domains added by updateHostsFile.ps1 using input-file $myHosts."
Add-Content -Path "$processDir\$newHosts" -Value (Get-Content "$processDir\$myHosts" | Where-Object {$_ -notmatch "^#"} )
}
Else
{
Write-Host "File `"$processDir\$myHosts`" not found."
Write-Host "No custom domains added."
}
Write-Host `n
# Add blacklist file contents if file exists
If (Test-Path "$processDir\$blackList")
{
Write-Host "File `"$processDir\$blackList`" exists."
Write-Host "Adding black-listed domains found in input-file `"$blackList`"."
Add-Content -Path "$processDir\$newHosts" -Value ""
Add-Content -Path "$processDir\$newHosts" -Value "# Domains added by updateHostsFile.ps1 using input-file $blackList."
If ($ReportDomains) {
Write-Host "Black-listed domains:"
Get-Content "$processDir\$blackList" | Where-Object {$_ -notmatch "^#"}
}
#Add-Content -Path "$processDir\$newHosts" -Value (Get-Content "$processDir\$blackList" | Where-Object {$_ -notmatch "^#"} )
(Get-Content "$processDir\$blackList" | Where-Object {$_ -notmatch "^#"}) | `
ForEach-Object { Add-Content -Path "$processDir\$newHosts" -Value "$IP $_" }
}
Else
{
Write-Host "File `"$processDir\$blackList`" not found."
Write-Host "No additional domains black-listed."
}
Write-Host `n
# Whitelist domains
If (Test-Path "$processDir\$whiteList")
{
Write-Host "File `"$processDir\$whiteList`" exists."
Write-Host "White-listing domains found in input-file `"$whiteList`" (this may take a while)."
If ($ReportDomains) {
Write-Host "White-listed domains:"
}
ForEach ($domain in (Get-Content "$processDir\$whiteList"| Where-Object {$_ -notmatch "^#"}))
{
$domain = $domain.Trim()
If ($ReportDomains) {
Write-Host "$domain"
}
ForEach ($hostname in (Get-Content "$processDir\$newHosts" | Where-Object {$_ -notmatch "^#"}) | Select-String -Pattern "$domain")
{
(Get-Content "$processDir\$newHosts") | ForEach-Object { $_ -replace "$hostname" , "# $hostname" } | Set-Content "$processDir\$newHosts"
}
}
}
Else
{
Write-Host "File `"$processDir\$whiteList`" not found."
Write-Host "No domains white-listed."
}
Write-Host `n
# Verify hosts file exists
If (Test-Path "$hostsDir\hosts")
{
# Create backup copy of the current hosts file
If ( ($Backup) -or ($Backup -eq 0) )
{
Write-Host "Creating backup copy of hosts to hosts.backup.$timeStamp."
Copy-Item "$hostsDir\hosts" "$hostsDir\hosts.backup.$timeStamp"
# Cleanup hosts file backups
If ($Backup -gt 0) {
Write-Host "Cleanup backup hosts files. Latest $Backup backup files are preserved."
Get-ChildItem $hostsDir hosts.backup* | Sort-Object CreationTime -Descending | Select -Skip $Backup | Remove-Item -Force
}
Write-Host `n
}
}
Else
{
Write-Host "Hosts file $hostsDir\hosts does not exist. A backup copy will not be created!" -ForeGroundColor Red
Write-Host `n
}
# Compare new hosts file to current active hosts file
Write-Host "Comparing new hosts file to current active hosts file (this may take a while)."
If (Compare-Object $(Get-Content $hostsDir\hosts) $(Get-Content $processDir\$newHosts))
{
Write-Host "Updates found."
Write-Host `n
# Replace hosts file with newly created hosts file
If ($Replace)
{
If (Test-Path "$processDir\$newHosts")
{
# Replace hosts file
If (Test-Path "$hostsDir\hosts")
{
Write-Host "Replace current active hosts file by $newHosts."
}
Else
{
Write-Host "Copy $newHosts to hosts."
}
Write-Host "Copy $processDir\$newHosts to $hostsDir\hosts"
Copy-Item -Force "$processDir\$newHosts" "$hostsDir\hosts"
Write-Host `n
# Clear DNS Cache
Write-Host "Flushing the DNS cache to utilize new hosts file."
Clear-DnsClientCache
Write-Host "DNS Cache has been flushed."
}
Else
{
Write-Host "Hosts file $processDir\$newHosts does not exist!"
}
}
Else
{
Write-Host "New hosts file with updates $processDir\$newHosts has been created."
Write-Host `n
Write-Host "The current active hosts file was not replaced and activated!" -Foregroundcolor Red
Write-Host "To replace and activate new hosts file add `"-Replace`" option."
}
}
Else
{
Write-Host "No updates found."
}
Write-Host `n
$endTime = Get-Date
$runTime=(New-TimeSpan -Start $startTime -End $endTime).ToString()
Write-Output "This script was executed in $(($endTime).Subtract($startTime).Minutes) minutes and $(($endTime).Subtract($startTime).Seconds) seconds."
Write-Host `n