-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_blocklists.sh
executable file
·445 lines (368 loc) · 16.6 KB
/
grab_blocklists.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/bin/sh
###########################################################
### This script pulls down mutliple lists of domains ###
### Then creates a bind zone file from the lists ###
###
### The idea is to block access to the domains by ###
### having them resolve to something bogus ###
### that can easily be filtered ###
###
### you need to put remove-addomains.pl in /usr/local/bin
###########################################################
# hint: put this in /etc/cron.daily
# let the other daily cron job to finish
#sleep 900
# debug > 0 print debug info and don't reload named
debug=0
# if $justgrab > 0 ; then only download the domain lists
justgrab=0
# chroot named?
if [ -z "$CHROOT" ]
then
# environment didn't set $CHROOT, lets check for chroot'ed named
if [ -e /var/named/chroot ]
then
CHROOT=1
else
CHROOT=0
fi
fi
# include the big lists?
# note: this bumps named memory footprint to ~750MB
biglists=0
# are we systemctl enabled?
SYSTEMCTL=$( which systemctl )
SERVICE=$( which service )
if [ -z "${SYSTEMCTL}" ]
then
# using good old `service`
RELOAD_NAMED="${SERVICE} named reload"
START_NAMED="${SERVICE} named start"
STOP_NAMED="${SERVICE} named stop"
else
if [ $CHROOT -gt 0 ]
then
# working with chroot'ed named
# and systemctl is being used
RELOAD_NAMED="${SYSTEMCTL} reload named-chroot"
STOP_NAMED="${SYSTEMCTL} stop named-chroot"
START_NAMED="${SYSTEMCTL} start named-chroot"
else
# non-chroot'ed named and using systemctl
RELOAD_NAMED="${SYSTEMCTL} reload named"
STOP_NAMED="${SYSTEMCTL} stop named"
START_NAMED="${SYSTEMCTL} start named"
fi
fi
# if running in alpine, we'll assume it is a container
OS="$( grep ^ID /etc/os-release | cut -d= -f2 )"
if [ "${OS}" == "alpine" ]
then
RELOAD_NAMED="kill -HUP $( ps ax | grep named | grep -v grep | awk '{ print $1 }' )"
STOP_NAMED="kill $( ps ax | grep named | grep -v grep | awk '{ print $1 }' )"
START_NAMED="/usr/sbin/named -4 -c /etc/named.conf -f -L /var/named/chroot/var/log/default.log -t /var/named/chroot"
fi
if [ $debug -gt 0 ]
then
echo "CHROOT is ${CHROOT}"
echo "RELOAD_NAMED is ${RELOAD_NAMED}"
echo "STOP_NAMED is ${STOP_NAMED}"
echo "START_NAMED is ${START_NAMED}"
fi
# Destination directory
# if the calling environment doesn't set DEST then look for one
if [ -z "${DEST}" ]
then
# check first for /var/named
if [ -e /var/named ]
then
DEST="/var/named/masters"
fi
# now check to see if named is being chroot'ed and we want it(CHROOT > 0)
if [ -e /var/named/chroot ] && [ ${CHROOT} -gt 0 ]
then
DEST="/var/named/chroot/var/named/masters"
fi
fi
# make sure we have a destination
if [ -z ${DEST} ]
then
echo "!!! FAILED to find a destination directory !!!!"
exit 5
fi
# Path and file containing the exception list aka allowed domains
# make sure allowed_domains.txt exists
if [ ! -e /etc/allowed_domains.txt ]
then
touch /etc/allowed_domains.txt
fi
ALLOWED="/etc/allowed_domains.txt"
# Path and file containing list of custom domains to block
# make sure it exists
if [ ! -e /etc/custom_blocked_domains.txt ]
then
touch /etc/custom_blocked_domains.txt
fi
BLOCKED="/etc/custom_blocked_domains.txt"
# the list of lists
echo 'ad,http://pgl.yoyo.org/adservers/serverlist.php?hostformat=bindconfig&showintro=0&mimetype=plaintext' > /tmp/thelist.$$
echo 'malware,http://mirror1.malwaredomains.com/files/domains.txt' >> /tmp/thelist.$$
echo 'zeus,https://zeustracker.abuse.ch/blocklist.php?download=baddomains' >> /tmp/thelist.$$
echo 'zeus,https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist' >> /tmp/thelist.$$
echo 'zeus,https://ransomwaretracker.abuse.ch/downloads/RW_DOMBL.txt' >> /tmp/thelist.$$
echo 'malware2,http://www.malwaredomainlist.com/hostslist/hosts.txt' >> /tmp/thelist.$$
# add lists from pi-hole
# ref: https://github.com/pi-hole/pi-hole/blob/master/automated%20install/basic-install.sh#L1200
# ref: https://github.com/pi-hole/pi-hole/wiki/Customising-sources-for-ad-lists
echo 'stevenblack,http://sbc.io/hosts/alternates/gambling-porn/hosts' >> /tmp/thelist.$$
echo 'justdomains,https://mirror1.malwaredomains.com/files/justdomains' >> /tmp/thelist.$$
echo 'justdomains,https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt' >> /tmp/thelist.$$
echo 'justdomains,https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt' >> /tmp/thelist.$$
echo 'justdomains,https://hosts-file.net/ad_servers.txt' >> /tmp/thelist.$$
if [ $biglists -gt 0 ]
then
echo 'notrackdomain,https://raw.githubusercontent.com/notracking/hosts-blocklists/master/domains.txt' >> /tmp/thelist.$$
echo 'notrackhosts,https://raw.githubusercontent.com/notracking/hosts-blocklists/master/hostnames.txt' >> /tmp/thelist.$$
fi
{ while read zone ; do
# break out the type and url
ztype="$( echo "$zone" | cut -d',' -f1 )"
zurl="$( echo "$zone" | cut -d',' -f2 )"
if [ $debug -gt 0 ]
then
echo "ztype is ${ztype}"
echo "zurl is ${zurl}"
fi
# grab the list
wget -q --no-check-certificate -O "/tmp/temp_${ztype}_file" "${zurl}"
# backup the existing list
if [ -e "${DEST}/${ztype}_block.txt" ]
then
# check if we are just downloading the domain lists
if [ $justgrab -eq 0 ]
then
mv "${DEST}/${ztype}_block.txt" "${DEST}/${ztype}_block.txt.$(date +%Y%m%d)"
fi
fi
dos2unix "/tmp/temp_${ztype}_file"
done } < /tmp/thelist.$$
# check to see if we are just downloading the domain lists
if [ $justgrab -gt 0 ]
then
if [ $debug -gt 0 ]
then
echo "justgrab is ${justgrab}"
echo "debug is ${debug}"
echo "done downloading lists."
fi
rm /tmp/thelist.$$
exit 5
fi
## Now convert the list into just a list of domains
if [ $debug -gt 0 ]
then
echo Converting the lists into just a list of domains
ls -rtl /tmp/temp*file
# this removing any trailing dots sed 's/[\.]*$//'
fi
grep -vf ${ALLOWED} /tmp/temp_ad_file | grep -v \# | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ | grep ^zone | cut -d' ' -f2 | sed 's/"//g' | sed 's/[ \t]*$//g' | sed 's/www\.//g' | sed 's/^www[[:digit:]]\+\.//g' | sed 's/[\.]*$//g' | awk ' !x[$0]++' > /tmp/ad.domains
grep -vf ${ALLOWED} /tmp/temp_malware_file | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ | grep -v \# | awk '{ print $1 }' | sed 's/"//g' | sed 's/[ \t]*$//g' | sed 's/www\.//g' | sed 's/^www[[:digit:]]\+\.//g' | sed 's/[\.]*$//g' > /tmp/malware.domains
grep -vf ${ALLOWED} /tmp/temp_zeus_file | grep -v '#' | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ | grep -v '^$' | sed 's/[ \t]*$//g' | sed 's/www\.//g' | sed 's/^www[[:digit:]]\+\.//g' | sed 's/[\.]*$//g' > /tmp/zeus.domains
grep -vf ${ALLOWED} /tmp/temp_malware2_file | grep 127.0.0.1 | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ | grep -v localhost | cut -d' ' -f3 | sed 's/[ \t]*$//g' | sed 's/www\.//g' | sed 's/^www[[:digit:]]\+\.//g' | sed 's/[\.]*$//g' > /tmp/malware2.domains
grep -vf ${ALLOWED} /tmp/temp_justdomains_file | grep -v \# | awk '{ print $NF }' | grep -v localhost | sed 's/www\.//g' | sed 's/^www[[:digit:]]\+\.//g' | sed 's/[[:digit:]]\+\.//g' | sed 's/^.*esomniture.com/esomniture.com/g' | sed 's/^.*\.ziyu.net/ziyu.net/g' | sed 's/^track\.msadcenter\+\.//g' | awk ' !x[$0]++' | sed 's/[A-Z]/\L&/g' | awk -F . 'NF!=1' | sed '/^\s*$/d' > /tmp/justdomains.domains
grep -vf ${ALLOWED} /tmp/temp_stevenblack_file | grep -v \# | grep -v edgekey | grep -v akadns | grep -v edgesuite | grep 0.0.0.0 | awk '{ print $NF }' | grep -v 0.0.0.0 | sed 's/www\.//g' | sed 's/^www[1-9]\.//g' | sed 's/[[:digit:]]\+\.//g' | sed 's/^.*esomniture.com/esomniture.com/g' | awk ' !x[$0]++' | sed 's/[A-Z]/\L&/g' | awk -F . 'NF!=1' | sed '/^\s*$/d' > /tmp/stevenblack.domains
if [ $biglists -gt 0 ]
then
grep -vf ${ALLOWED} /tmp/temp_notrackdomain_file | grep -v \# | cut -d '/' -f2 | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ | sed 's/[\.]*$//g' > /tmp/notrackdomain.domains
grep -vf ${ALLOWED} /tmp/temp_notrackhosts_file | grep -v \# | grep -v localhost | awk '{ print $2 }' | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ | sed 's/[\.]*$//g' > /tmp/notrackhosts.domains
fi
# add our custom blocked domains
cat $BLOCKED >> /tmp/ad.domains
## Now remove dupes.
if [ $debug -gt 0 ]
then
echo Removing Dupes now
fi
if [ -e /tmp/notrackdomain.domains ]
then
cat /tmp/notrackdomain.domains > /tmp/malware.$$
fi
if [ -e /tmp/notrackhosts.domains ]
then
cat /tmp/notrackhosts.domains >> /tmp/malware.$$
fi
if [ -e /tmp/malware.domains ]
then
cat /tmp/malware.domains >> /tmp/malware.$$
fi
if [ -e /tmp/zeus.domains ]
then
cat /tmp/zeus.domains >> /tmp/malware.$$
fi
if [ -e /tmp/malware2.domains ]
then
cat /tmp/malware2.domains >> /tmp/malware.$$
fi
# initial sort of the complete malware list and attempt to remove dupes
sort -u /tmp/malware.$$ > /tmp/malware_t.$$
# final removal of dupes
awk ' !x[$0]++' /tmp/malware_t.$$ > /tmp/malware.domains
# clean up the temp files from deduping malware
if [ $debug -eq 0 ]
then
rm -f /tmp/malware.$$
rm -f /tmp/malware_t.$$
fi
# remove malware domains from the ad domain list
if [ -e /tmp/ad-domain-dupes.txt ]
then
rm -f /tmp/ad-domain-dupes.txt
fi
if [ -e /tmp/ad_domains_removed_from_malware.txt ]
then
rm -f /tmp/ad_domains_removed_from_malware.txt
fi
# dedupe the pihole domains
cat /tmp/justdomains.domains /tmp/stevenblack.domains | awk ' !x[$0]++' > /tmp/pihole_t.$$
# truncate domains with more than four names/field
cat /tmp/pihole_t.$$ | sed 's/"//g' | awk -F . 'NF>4' | awk -F . '{ print $(NF-2),$(NF-1),$NF }' | sed 's/ /./g' | sed 's/^net\.//g' | sed 's/^com\.//g' | sed 's/^ssl\.//g' | sort -u | grep -v '[a-z]com$' > /tmp/shorten.domains
grep -vf /tmp/shorten.domains /tmp/pihole_t.$$ > /tmp/pihole.$$
cat /tmp/shorten.domains >> /tmp/pihole.$$
# combine with ad domains, boil-down some obvious ad domains and remove dupes
cat /tmp/pihole.$$ /tmp/ad.domains > /tmp/ad_domains_t.$$
sed 's/^.*\.ziyu.net/ziyu.net/g' -i /tmp/ad_domains_t.$$
sed 's/^.*\.spylog.com/spylog.com/g' -i /tmp/ad_domains_t.$$
sed 's/^.*\.opentracker.net/opentracker.net/g' -i /tmp/ad_domains_t.$$
sed 's/^.*\.sitemeter.com/sitemeter.com/g' -i /tmp/ad_domains_t.$$
sed 's/^.*\.marketscore.com/marketscore.com/g' -i /tmp/ad_domains_t.$$
sed 's/^.*\.realtracker.com/realtracker.com/g' -i /tmp/ad_domains_t.$$
sed 's/^.*\.a8.net/a8.net/g' -i /tmp/ad_domains_t.$$
sed 's/^.*\.moba8.net/moba8.net/g' -i /tmp/ad_domains_t.$$
sed 's/^track\.msadcenter\+\.//g' -i /tmp/ad_domains_t.$$
awk ' !x[$0]++' /tmp/ad_domains_t.$$ > /tmp/ad.domains
# remove-addomains.pl will remove ad domains from the malware list
# leaving us with /tmp/ad_domains_removed_from_malware.txt
/usr/local/bin/remove-addomains.pl
# build the ad list
#grep -vf /tmp/ad-domain-dupes.txt /tmp/ad.domains | sed 's/[ \t]*$//g' | sed 's/$/" { type master; notify no; check-names ignore; file "masters\/adserver.zone"; };/g' | sed 's/^/zone "/g' > ${DEST}/ad_block.txt
cat /tmp/ad.domains | sort -u | sed 's/[ \t]*$//g' | sed 's/$/" { type master; notify no; check-names ignore; file "masters\/adserver.zone"; };/g' | sed 's/^/zone "/g' > ${DEST}/ad_block.txt
# build the malware list
#cat /tmp/malware.domains | sed 's/[ \t]*$//g' | sed 's/$/" { type master; notify no; check-names ignore; file "masters\/malware.zone"; };/g' | sed 's/^/zone "/g' > ${DEST}/malware_block.txt
cat /tmp/ad_domains_removed_from_malware.txt | sed 's/[ \t]*$//g' | sed 's/$/" { type master; notify no; check-names ignore; file "masters\/malware.zone"; };/g' | sed 's/^/zone "/g' > ${DEST}/malware_block.txt
if [ $debug -gt 0 ]
then
ls -lrt ${DEST}/*block*
fi
## Now see if we have built non-zero byte block files
{ while read zone ; do
# break out the type
ztype=$( echo "$zone" | cut -d',' -f1 )
# remove the temp files
if [ $debug -eq 0 ]
then
rm -f "/tmp/temp_${ztype}_file"
rm -f "/tmp/${ztype}.domains"
fi
if [ $debug -gt 0 ]
then
echo "================================================================"
echo "Now checking the results for ${DEST}/${ztype}_block.txt"
if [ -e "${DEST}/${ztype}_block.txt" ]
then
ls -lrt "${DEST}/${ztype}_block.txt"
else
echo "${DEST}/${ztype}_block.txt NOT FOUND"
fi
echo ""
fi
if [ -s "${DEST}/${ztype}_block.txt" ]
then
## Success! The block file is non-zero
if [ $debug -gt 0 ]
then
echo "Success! The block file ${DEST}/${ztype}_block.txt is non-zero"
fi
# remove our backup (if it exists)
if [ -e "${DEST}/${ztype}_block.txt.old" ]
then
rm "${DEST}/${ztype}_block.txt.old"
fi
if [ -e "${DEST}/${ztype}_block.txt.$(date +%Y%m%d)" ]
then
# this just moves the current backup out of the way
# we do this in case the named reload fails, we can roll-back
mv "${DEST}/${ztype}_block.txt.$(date +%Y%m%d)" "${DEST}/${ztype}_block.txt.old"
fi
else
if [ -e "${DEST}/${ztype}_block.txt" ]
then
## Failure! The block file is zero bytes
# remove the new block file
if [ $debug -gt 0 ]
then
echo "Failure! The block file ${DEST}/${ztype}_block.txt is zero bytes"
fi
# remove the new block file, if it exists
if [ -e "${DEST}/${ztype}_block.txt" ]
then
rm "${DEST}/${ztype}_block.txt"
fi
# put the old block file back in place, if it isn't zero bytes
if [ -s "${DEST}/${ztype}_block.txt.$(date +%Y%m%d)" ]
then
mv "${DEST}/${ztype}_block.txt.$(date +%Y%m%d)" "${DEST}/${ztype}_block.txt"
else
echo "!!!! ERROR !!!! OLD ${ztype}_block.txt is ZERO Bytes"
fi
else
# we are ok because it didn't exist to begin with
# maybe it was combined in the malware zone file (e.g. zeus)
echo "Original ${DEST}/${ztype}_block.txt NOT FOUND and we are ok with that"
fi
fi
if [ $debug -gt 0 ]
then
echo "================================================================"
echo ""
fi
done } < /tmp/thelist.$$
## Cleanup temp files
if [ $debug -eq 0 ]
then
rm -f /tmp/ad-domain-dupes.txt
rm -f /tmp/zeus-domain-dupes.txt
rm -f /tmp/malware.$$
rm -f /tmp/malware_t.$$
# remove temp pihole files
rm -f /tmp/pihole_t.$$
rm -f /tmp/pihole.$$
rm -f /tmp/shorten.domains
rm -f /tmp/ad_domains_t.$$
fi
## Reload Named
if [ $debug -eq 0 ]
then
#/etc/init.d/named reload
${RELOAD_NAMED}
if [ $? -gt 0 ]
then
echo ah crap, named reload failed! Rolling back.
{ while read zone ; do
# break out the type
ztype=$( echo "$zone" | cut -d',' -f1 )
if [ -e "${DEST}/${ztype}_block.txt.old" ]
then
rm -f "${DEST}/${ztype}_block.txt"
mv "${DEST}/${ztype}_block.txt.old" "${DEST}/${ztype}_block.txt"
fi
done } < /tmp/thelist.$$
#/etc/init.d/named stop
#/etc/init.d/named start
${STOP_NAMED}
${START_NAMED}
fi
else
echo Debug not equal to ZERO, not reloading Named
fi
rm -f /tmp/thelist.$$
exit 0