forked from astrovm/hosty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosty.sh
executable file
·398 lines (337 loc) · 13.1 KB
/
hosty.sh
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
#!/bin/bash
echo "======== hosty v1.7.6 (10/Mar/21) ========"
echo "======== astrolince.com/hosty ========"
echo
# Check dependences
CheckDep() {
command -v "$1" >/dev/null 2>&1 || {
echo >&2 "Hosty requires '$1' but it's not installed."
exit 1
}
}
CheckDep bash
CheckDep curl
CheckDep gawk
CheckDep head
CheckDep cat
# We'll block every domain that is inside these files
BLACKLIST_SOURCES=("https://adaway.org/hosts.txt"
"https://bitbucket.org/ethanr/dns-blacklists/raw/8575c9f96e5b4a1308f2f12394abd86d0927a4a0/bad_lists/Mandiant_APT1_Report_Appendix_D.txt"
"https://gitlab.com/quidsup/notrack-blocklists/raw/master/notrack-blocklist.txt"
"https://gitlab.com/quidsup/notrack-blocklists/raw/master/notrack-malware.txt"
"https://hostfiles.frogeye.fr/firstparty-trackers-hosts.txt"
"https://osint.digitalside.it/Threat-Intel/lists/latestdomains.txt"
"https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"
"https://phishing.army/download/phishing_army_blocklist_extended.txt"
"https://raw.githubusercontent.com/anudeepND/blacklist/master/adservers.txt"
"https://raw.githubusercontent.com/bigdargon/hostsVN/master/hosts"
"https://raw.githubusercontent.com/crazy-max/WindowsSpyBlocker/master/data/hosts/spy.txt"
"https://raw.githubusercontent.com/DandelionSprout/adfilt/master/Alternate%20versions%20Anti-Malware%20List/AntiMalwareHosts.txt"
"https://raw.githubusercontent.com/FadeMind/hosts.extras/master/add.2o7Net/hosts"
"https://raw.githubusercontent.com/FadeMind/hosts.extras/master/add.Risk/hosts"
"https://raw.githubusercontent.com/FadeMind/hosts.extras/master/add.Spam/hosts"
"https://raw.githubusercontent.com/FadeMind/hosts.extras/master/UncheckyAds/hosts"
"https://raw.githubusercontent.com/PolishFiltersTeam/KADhosts/master/KADhosts_without_controversies.txt"
"https://raw.githubusercontent.com/Spam404/lists/master/main-blacklist.txt"
"https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt"
"https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt"
"https://urlhaus.abuse.ch/downloads/hostfile/"
"https://v.firebog.net/hosts/AdguardDNS.txt"
"https://v.firebog.net/hosts/Admiral.txt"
"https://v.firebog.net/hosts/Easylist.txt"
"https://v.firebog.net/hosts/Easyprivacy.txt"
"https://v.firebog.net/hosts/Prigent-Ads.txt"
"https://v.firebog.net/hosts/Shalla-mal.txt"
"https://v.firebog.net/hosts/static/w3kbl.txt"
"https://www.malwaredomainlist.com/hostslist/hosts.txt")
# We'll unblock every domain that is inside these files
WHITELIST_SOURCES=("https://raw.githubusercontent.com/anudeepND/whitelist/master/domains/optional-list.txt"
"https://raw.githubusercontent.com/anudeepND/whitelist/master/domains/referral-sites.txt"
"https://raw.githubusercontent.com/anudeepND/whitelist/master/domains/whitelist.txt"
"https://raw.githubusercontent.com/astrolince/hosty/master/lists/whitelist"
"https://raw.githubusercontent.com/brave/adblock-lists/master/brave-unbreak.txt"
"https://raw.githubusercontent.com/raghavdua1995/DNSlock-PiHole-whitelist/master/whitelist.list"
"https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/unbreak.txt"
"https://zerodot1.gitlab.io/CoinBlockerLists/hosts_browser"
"https://zerodot1.gitlab.io/CoinBlockerLists/hosts_optional")
# Set IP to redirect
IP="0.0.0.0"
# Check if running as root
if [[ "$1" != "--debug" ]] && [[ "$2" != "--debug" ]]; then
if [[ "$EUID" -ne 0 ]]; then
echo "Please run as root"
exit 1
fi
# --uninstall option
if [[ "$1" == "--uninstall" ]]; then
if [[ -d /etc/hosty ]]; then
# Ask user to remove hosty config
echo "Do you want to remove /etc/hosty configs directory? y/n"
read -r answer
echo
# Check user answer
if [[ "$answer" == "y" ]]; then
echo "Removing hosty configs directory..."
rm -R /etc/hosty
echo
elif [[ "$answer" != "n" ]]; then
echo "Bad answer, exiting..."
exit 1
fi
fi
# Remove autorun config
if [[ -f /etc/cron.daily/hosty ]]; then
echo "Removing /etc/cron.daily/hosty..."
echo
rm /etc/cron.daily/hosty
fi
if [[ -f /etc/cron.weekly/hosty ]]; then
echo "Removing /etc/cron.weekly/hosty..."
echo
rm /etc/cron.weekly/hosty
fi
if [[ -f /etc/cron.monthly/hosty ]]; then
echo "Removing /etc/cron.monthly/hosty..."
echo
rm /etc/cron.monthly/hosty
fi
echo "Uninstalling hosty..."
rm /usr/local/bin/hosty
echo
echo "Hosty uninstalled."
exit 0
fi
else
echo "******** DEBUG MODE ON ********"
echo
fi
# Copy original hosts file and handle --restore
user_hosts_file=$(mktemp)
user_hosts_linesnumber=$(gawk '/^# Ad blocking hosts generated/ {counter=NR} END{print counter-1}' /etc/hosts)
# If hosty has never been executed, don't restore anything
if [[ "$user_hosts_linesnumber" -lt 0 ]]; then
if [[ "$1" == "--restore" ]]; then
echo "There is nothing to restore."
exit 0
fi
# If it's the first time running hosty, save the whole /etc/hosts file in the tmp var
cat /etc/hosts >"$user_hosts_file"
else
# Copy original hosts lines
head -n "$user_hosts_linesnumber" /etc/hosts >"$user_hosts_file"
# If --restore is present, restore original hosts and exit
if [[ "$1" == "--restore" ]]; then
# Remove empty lines from begin and end
gawk 'NR==FNR{if (NF) { if (!beg) beg=NR; end=NR } next} FNR>=beg && FNR<=end' "$user_hosts_file" "$user_hosts_file" >/etc/hosts
echo "/etc/hosts restore completed."
exit 0
fi
fi
# Cron options
if [[ "$1" == "--autorun" ]] || [[ "$2" == "--autorun" ]]; then
echo "Configuring autorun..."
# Check system compatibility
CheckDep crontab
if [[ ! -d /etc/cron.daily ]] || [[ ! -d /etc/cron.weekly ]] || [[ ! -d /etc/cron.monthly ]]; then
echo
echo "Hosty doesn't know how to autorun in your operating system, you need to configure that by yourself."
exit 1
fi
# Ask user for autorun period
echo
echo "How often do you want to run hosty automatically?"
echo "Enter 'daily', 'weekly', 'monthly' or 'never':"
read -r period
# Check user answer
if [[ "$period" != "daily" ]] && [[ "$period" != "weekly" ]] && [[ "$period" != "monthly" ]] && [[ "$period" != "never" ]]; then
echo
echo "Bad answer, exiting..."
exit 1
else
# Remove previous config
if [[ -f /etc/cron.daily/hosty ]]; then
echo
echo "Removing /etc/cron.daily/hosty..."
rm /etc/cron.daily/hosty
fi
if [[ -f /etc/cron.weekly/hosty ]]; then
echo
echo "Removing /etc/cron.weekly/hosty..."
rm /etc/cron.weekly/hosty
fi
if [[ -f /etc/cron.monthly/hosty ]]; then
echo
echo "Removing /etc/cron.monthly/hosty..."
rm /etc/cron.monthly/hosty
fi
# Stop here if the user has chosen 'never'
if [[ "$period" == "never" ]]; then
echo
echo "Done."
exit 0
fi
# Set cron file with user choice
cron_file="/etc/cron.$period/hosty"
# Create the file
echo
echo "Creating $cron_file..."
echo '#!/bin/sh' >"$cron_file"
# If user have passed the --ignore-default-sources argument, autorun with that
if [[ "$1" != "--ignore-default-sources" ]] && [[ "$2" != "--ignore-default-sources" ]]; then
echo '/usr/local/bin/hosty' >>"$cron_file"
else
echo
echo "Config hosty with --ignore-default-sources..."
echo '/usr/local/bin/hosty --ignore-default-sources' >>"$cron_file"
fi
# Set permissions
chmod 755 "$cron_file"
echo
echo "Done."
exit 0
fi
fi
# Remove default sources if the user want that
if [[ "$1" == "--ignore-default-sources" ]] || [[ "$2" == "--ignore-default-sources" ]]; then
BLACKLIST_SOURCES=()
WHITELIST_SOURCES=()
fi
# User custom blacklists sources
if [[ -f /etc/hosty/blacklist.sources ]]; then
while read -r line; do
BLACKLIST_SOURCES+=("$line")
done </etc/hosty/blacklist.sources
fi
# User custom whitelist sources
if [[ -f /etc/hosty/whitelist.sources ]]; then
while read -r line; do
WHITELIST_SOURCES+=("$line")
done </etc/hosty/whitelist.sources
fi
# Function to download sources
downloadFile() {
tmp_downloadFile=$(mktemp)
echo "Downloading $1..."
if ! curl -fsSL -o "$tmp_downloadFile" "$1"; then
return $?
fi
if [[ $1 == *.zip ]]; then
CheckDep zcat
tmp_zcat=$(mktemp)
if ! zcat "$tmp_downloadFile" >"$tmp_zcat"; then
rm "$tmp_zcat"
return $?
fi
cat "$tmp_zcat" >"$tmp_downloadFile"
rm "$tmp_zcat"
elif [[ $1 == *.7z ]]; then
CheckDep 7z
tmp_7z=$(mktemp)
if ! 7z e -so -bd "$tmp_downloadFile" >"$tmp_7z"; then
rm "$tmp_7z"
return $?
fi
cat "$tmp_7z" >"$tmp_downloadFile"
rm "$tmp_7z"
fi
return 0
}
# Take all domains of any text file
extractDomains() {
echo
echo "Extracting domains..."
# Remove whitespace at beginning of the line
gawk -i inplace '{gsub(/^[[:space:]]*/,""); print}' "$1"
# Remove lines that start with '!'
gawk -i inplace '!/^!/' "$1"
# Remove '#' and everything that follows
gawk -i inplace '{gsub(/#.*/,""); print}' "$1"
# Replace with new lines everything that isn't letters, numbers, hyphens and dots
gawk -i inplace '{gsub(/[^a-zA-Z0-9\.\-]/,"\n"); print}' "$1"
# Remove lines that don't have dots
gawk -i inplace '/\./' "$1"
# Remove lines that don't start with a letter or number
gawk -i inplace '/^[a-zA-Z0-9]/' "$1"
# Remove lines that end with a dot
gawk -i inplace '!/\.$/' "$1"
# Removing important system ips
gawk -i inplace '!/^(127\.0\.0\.1|255\.255\.255\.255|0\.0\.0\.0|255\.255\.255\.0|localhost\.localdomain)$/' "$1"
# Remove duplicates
gawk -i inplace '!x[$0]++' "$1"
# Count extacted domains
domains_counter=$(gawk 'BEGIN{counter=0}{counter++;}END{print counter}' "$1")
echo "$domains_counter domains extracted."
return 0
}
echo "Downloading blacklists..."
blacklist_domains=$(mktemp)
# Download blacklist sources and merge into one
for i in "${BLACKLIST_SOURCES[@]}"; do
if ! downloadFile "$i"; then
echo "Error downloading $i"
rm "$tmp_downloadFile"
break
fi
cat "$tmp_downloadFile" >>"$blacklist_domains"
rm "$tmp_downloadFile"
done
echo
echo "Applying user custom blacklist..."
if [[ -f /etc/hosty/blacklist ]]; then
cat "/etc/hosty/blacklist" >>"$blacklist_domains"
fi
# Extract domains from blacklist sources
extractDomains "$blacklist_domains"
echo
echo "Downloading whitelists..."
whitelist_domains=$(mktemp)
# Download whitelist sources and merge into one
for i in "${WHITELIST_SOURCES[@]}"; do
if ! downloadFile "$i"; then
echo "Error downloading $i"
rm "$tmp_downloadFile"
break
fi
cat "$tmp_downloadFile" >>"$whitelist_domains"
rm "$tmp_downloadFile"
done
echo
echo "Applying user custom whitelist..."
if [[ -f /etc/hosty/whitelist ]]; then
cat "/etc/hosty/whitelist" >>"$whitelist_domains"
fi
# Extract domains from whitelist sources
extractDomains "$whitelist_domains"
echo
echo "Building /etc/hosts..."
final_hosts_file=$(mktemp)
# Remove empty lines from begin and end
gawk 'NR==FNR{if (NF) { if (!beg) beg=NR; end=NR } next} FNR>=beg && FNR<=end' "$user_hosts_file" "$user_hosts_file" >"$final_hosts_file"
# Add blank line at the end
{
echo
echo "# Ad blocking hosts generated $(date)" >>"$final_hosts_file"
echo "# Don't write below this line. It will be lost if you run hosty again." >>"$final_hosts_file"
} >>"$final_hosts_file"
echo
echo "Cleaning and de-duplicating..."
# Here we take the urls from the original hosts file and we add them to the whitelist to ensure that these urls behave like the user expects
gawk '/^\s*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {print $2}' "$user_hosts_file" >>"$whitelist_domains"
# Applying the whitelist and dedup
gawk -v ip=$IP 'FNR==NR {arr[$1]++} FNR!=NR {if (!arr[$1]++) print ip, $1}' "$whitelist_domains" "$blacklist_domains" >>"$final_hosts_file"
# Remove tmp files
rm "$blacklist_domains" "$whitelist_domains" "$user_hosts_file"
# Count websites blocked
websites_blocked_counter=$(gawk "/$IP/ {count++} END{print count}" "$final_hosts_file")
if [[ "$1" != "--debug" ]] && [[ "$2" != "--debug" ]]; then
cat "$final_hosts_file" >/etc/hosts
rm "$final_hosts_file"
else
echo
echo "You can see the results in $final_hosts_file"
fi
echo
echo "Done, $websites_blocked_counter websites blocked."
echo
echo "You can always restore your original hosts file with this command:"
echo " $ sudo hosty --restore"