/
pfblockerng.sh
1366 lines (1151 loc) · 44.2 KB
/
pfblockerng.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# pfBlockerNG Shell Function Script - By BBcan177@gmail.com - 04-12-14
# Copyright (c) 2015-2023 BBcan177@gmail.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2 as
# published by the Free Software Foundation. You may not use, modify or
# distribute this program under any other version of the GNU General
# Public License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
DEBUG=0
now=$(/bin/date +%m/%d/%y' '%T)
# Application Locations
pathgrepcidr="/usr/local/bin/grepcidr"
pathaggregate="/usr/local/bin/iprange"
pathgeoip="/usr/local/bin/mmdblookup"
pathcurl="/usr/local/bin/curl"
pathjq="/usr/local/bin/jq"
pathgunzip=/usr/bin/gunzip
pathhost=/usr/bin/host
pathtar=/usr/bin/tar
pathpfctl=/sbin/pfctl
# Script Arguments
alias="${2}"
max="${3}"
dedup="${4}"
cc="$(echo ${5} | sed 's/,/, /g')"
ccwhite="$(echo ${6} | tr '[A-Z]' '[a-z]')"
ccblack="$(echo ${7} | tr '[A-Z]' '[a-z]')"
etblock="$(echo ${8} | sed 's/,/, /g')"
etmatch="$(echo ${9} | sed 's/,/, /g')"
# File Locations
aliasarchive="/usr/local/etc/aliastables.tar.bz2"
pathgeoipdat="/usr/local/share/GeoIP/GeoLite2-Country.mmdb"
pfbsuppression=/var/db/pfblockerng/pfbsuppression.txt
pfbdnsblsuppression=/var/db/pfblockerng/pfbdnsblsuppression.txt
pfbalexa=/var/db/pfblockerng/pfbalexawhitelist.txt
masterfile=/var/db/pfblockerng/masterfile
mastercat=/var/db/pfblockerng/mastercat
geoiplog=/var/log/pfblockerng/geoip.log
errorlog=/var/log/pfblockerng/error.log
dnsbl_file=/var/unbound/pfb_dnsbl
# Folder Locations
etdir=/var/db/pfblockerng/ET
tmpxlsx=/tmp/xlsx/
dnsbl_tmp=/tmp/DNSBL_TMP/
pfbdb=/var/db/pfblockerng/
pfbdeny=/var/db/pfblockerng/deny/
pfborig=/var/db/pfblockerng/original/
pfbmatch=/var/db/pfblockerng/match/
pfbpermit=/var/db/pfblockerng/permit/
pfbnative=/var/db/pfblockerng/native/
pfsensealias=/var/db/aliastables/
pfbdomain=/var/db/pfblockerng/dnsbl/
pfbdomainorig=/var/db/pfblockerng/dnsblorig/
# Store 'Match' d-dedups in matchdedup.txt file
matchdedup=matchdedup_v4.txt
# Randomize temporary variables
rvar="$(/usr/bin/jot -r 1 1000 100000)"
tempfile=/tmp/pfbtemp1_$rvar
tempfile2=/tmp/pfbtemp2_$rvar
dupfile=/tmp/pfbtemp3_$rvar
dedupfile=/tmp/pfbtemp4_$rvar
addfile=/tmp/pfbtemp5_$rvar
syncfile=/tmp/pfbtemp6_$rvar
matchfile=/tmp/pfbtemp7_$rvar
tempmatchfile=/tmp/pfbtemp8_$rvar
domainmaster=/tmp/pfbtemp9_$rvar
asntemp=/tmp/pfbtemp10_$rvar
dnsbl_tld_remove=/tmp/dnsbl_tld_remove
dnsbl_add=/tmp/dnsbl_add
dnsbl_add_zone=/tmp/dnsbl_add_zone
dnsbl_add_data=/tmp/dnsbl_add_data
dnsbl_remove=/tmp/dnsbl_remove
dnsbl_remove_zone=/tmp/dnsbl_remove_zone
dnsbl_remove_data=/tmp/dnsbl_remove_data
dnsbl_python_data=/var/unbound/pfb_py_data.txt
dnsbl_python_zone=/var/unbound/pfb_py_zone.txt
dnsbl_python_count=/var/unbound/pfb_py_count
ip_placeholder="$(/usr/local/sbin/read_xml_tag.sh string installedpackages/pfblockerngipsettings/config/ip_placeholder)"
if [ -z "${ip_placeholder}" ]; then
ip_placeholder=127.1.7.7
fi
ip_placeholder2="$(echo ${ip_placeholder} | sed 's/\./\\\./g')"
ip_placeholder3="$(echo ${ip_placeholder} | cut -d '.' -f 1-3)"
USE_MFS_TMPVAR="$(/usr/bin/grep -c use_mfs_tmpvar /cf/conf/config.xml)"
DISK_NAME="$(/bin/df /var/db/rrd | /usr/bin/tail -1 | /usr/bin/awk '{print $1;}')"
DISK_TYPE="$(/usr/bin/basename ${DISK_NAME} | /usr/bin/cut -c1-2)"
if [ ! -d "${pfbdb}" ]; then mkdir "${pfbdb}"; fi
if [ ! -d "${pfsensealias}" ]; then mkdir "${pfsensealias}"; fi
if [ ! -d "${pfbmatch}" ]; then mkdir "${pfbmatch}"; fi
if [ ! -d "${etdir}" ]; then mkdir "${etdir}"; fi
if [ ! -d "${tmpxlsx}" ]; then mkdir "${tmpxlsx}"; fi
if [ ! -f "${masterfile}" ]; then touch "${masterfile}"; fi
if [ ! -f "${mastercat}" ]; then touch "${mastercat}"; fi
# Remove temp files before exiting.
exitnow() {
rm -f /tmp/pfbtemp*_"${rvar}"
exit
}
# Function to restore IP aliastables and DNSBL database from archive on reboot. ( Ramdisk installations only )
aliastables() {
if [ ${USE_MFS_TMPVAR} -gt 0 ] || [ "${DISK_TYPE}" = 'md' ]; then
if [ ! -d '/var/unbound' ]; then
mkdir '/var/unbound'
chown -f unbound:unbound /var/unbound
chgrp -f unbound /var/unbound
fi
[ -f "${aliasarchive}" ] && cd / && /usr/bin/tar -Pxvf "${aliasarchive}"
fi
}
# Function to write IP Placeholder IP to 'empty' final blocklist files.
emptyfiles() {
emptyfiles="$(find ${pfbdeny}*.txt -size 0 2>/dev/null)"
for i in ${emptyfiles}; do
echo "${ip_placeholder}" > "${i}";
done
}
# Function to remove lists from masterfiles and delete associated files.
remove() {
echo; echo
for i in ${cc}; do
header="$(echo ${i%*,})"
if [ ! -z "${header}" ]; then
# Make sure that alias exists in masterfile before removal.
query="${header} "
masterchk="$(grep -m1 ${query} ${masterfile})"
if [ ! -z "${masterchk}" ]; then
# Grep header with a trailing space character
grep "${header}[[:space:]]" "${masterfile}" > "${tempfile}"
awk 'FNR==NR{a[$0];next}!($0 in a)' "${tempfile}" "${masterfile}" > "${tempfile2}"; mv -f "${tempfile2}" "${masterfile}"
fi
rm -f "${pfborig}${header}"*; rm -f "${pfbdeny}${header}"*; rm -f "${pfbmatch}${header}"*
rm -f "${pfbpermit}${header}"*; rm -f "${pfbnative}${header}"*
echo "The Following List has been REMOVED [ ${header} ]"
fi
done
cut -d ' ' -f2 "${masterfile}" > "${mastercat}"
# Delete masterfiles if they are empty
if [ ! -s "${masterfile}" ]; then
rm -f "${masterfile}"; rm -f "${mastercat}"
fi
}
# Function to remove IPs if exists over 253 IPs in a range and replace with a single /24 block. (excl. '0' & '255')
process255() {
> "${dedupfile}"
data255="$(cut -d '.' -f 1-3 ${pfbdeny}${alias}.txt | awk '{a[$0]++}END{for(i in a){if(a[i] > 253){print i}}}')"
if [ ! -z "${data255}" ]; then
cp "${pfbdeny}${alias}.txt" "${tempfile}"
for ip in ${data255}; do
ii="$(echo ^${ip}. | sed 's/\./\\\./g')"
grep "${ii}" "${tempfile}" >> "${dedupfile}"
done
awk 'FNR==NR{a[$0];next}!($0 in a)' "${dedupfile}" "${tempfile}" > "${pfbdeny}${alias}.txt"
for ip in ${data255}; do echo "${ip}.0/24" >> "${pfbdeny}${alias}.txt"; done
fi
}
# Process to remove suppressed entries.
suppress() {
if [ ! -x "${pathgrepcidr}" ]; then
log="Application [ grepcidr ] Not found. Cannot proceed."
echo "${log}" | tee -a "${errorlog}"
return
fi
if [ -e "${pfbsuppression}" ] && [ -s "${pfbsuppression}" ]; then
data="$(cat ${pfbsuppression} | sort | uniq)"
if [ ! -z "${data}" ] && [ ! -z "${alias}" ]; then
if [ "${alias}" == 'suppressheader' ]; then
echo; echo '===[ Suppression Stats ]==================================='; echo
printf "%-20s %-10s %-10s %-10s\n" 'List' 'Pre' 'Suppress' 'Master'
echo '-----------------------------------------------------------'
return
fi
pfbfolder="${max}/"
counter=0; > "${dupfile}"
if [ ! -z "${alias}" ]; then
countg="$(grep -c ^ ${pfbfolder}${alias}.txt)"
cp "${pfbfolder}${alias}.txt" "${tempfile}"
for ip in ${data}; do
found=''; dcheck='';
mask="$(echo ${ip##*/})"
iptrim="$(echo "${ip%.*}")"
ip="$(echo ${ip%%/*})"
found="$(grep -m1 ${iptrim}.0/24 ${tempfile})"
# If a suppression is '/32' and a blocklist has a full '/24' block, execute the following.
if [ ! -z "${found}" ] && [ "${mask}" -eq 32 ]; then
echo " Suppression ${alias}: ${iptrim}.0/24 (Excluding: ${ip}/32)"
octet4="$(echo ${ip##*.})"
dcheck="$(grep ${iptrim}.0/24 ${dupfile})"
if [ -z "${dcheck}" ]; then
echo "${iptrim}.0/24" >> "${dupfile}"
counter="$((counter + 1))"
# Add individual IP addresses from range excluding suppressed IP
for i in $(/usr/bin/jot 255); do
if [ "${i}" != "${octet4}" ]; then
echo "${iptrim}.${i}" >> "${tempfile}"
counter="$((counter + 1))"
fi
done
fi
fi
done
if [ -s "${dupfile}" ]; then
# Remove '/24' suppressed ranges
awk 'FNR==NR{a[$0];next}!($0 in a)' "${dupfile}" "${tempfile}" > "${tempfile2}"; mv -f "${tempfile2}" "${tempfile}"
fi
# Remove all other suppressions from list
"${pathgrepcidr}" -vf "${pfbsuppression}" "${tempfile}" > "${pfbfolder}${alias}.txt"
# Update masterfiles. Don't execute if duplication process is disabled
if [ "${dedup}" == 'on' ]; then
# Don't execute if alias doesn't exist in masterfile
lcheck="$(grep -m1 ${alias} ${masterfile})"
if [ ! -z "${lcheck}" ]; then
# Replace masterfile with changes to list.
grep "${alias}[[:space:]]" "${masterfile}" > "${tempfile}"
awk 'FNR==NR{a[$0];next}!($0 in a)' "${tempfile}" "${masterfile}" > "${tempfile2}"
mv -f "${tempfile2}" "${masterfile}"
sed -e 's/^/'$alias' /' "${pfbfolder}${alias}.txt" >> "${masterfile}"
cut -d ' ' -f2 "${masterfile}" > "${mastercat}"
fi
fi
countk="$(grep -c ^ ${masterfile})"
countx="$(grep -c ^ ${pfbfolder}${alias}.txt)"
counto="$((countx - counter))"
printf "%-20s %-10s %-10s %-10s\n" "${alias}" "${countg}" "${counto}" "${countk}"
fi
fi
fi
}
# Function to optimise CIDRs
cidr_aggregate() {
if [ ! -x "${pathaggregate}" ]; then
log="Application [ iprange ] Not found. Cannot proceed."
echo "${log}" | tee -a "${errorlog}"
return
fi
if [ "${agg_folder}" == true ]; then
# Use $3 folder path
pfbfolder="${max}/"
else
pfbfolder="${pfbdeny}"
fi
counto="$(grep -c ^ ${pfbfolder}${alias}.txt)"
"${pathaggregate}" "${pfbfolder}${alias}.txt" > "${tempfile}" && mv -f "${tempfile}" "${pfbfolder}${alias}.txt"
countf="$(grep -c ^ ${pfbfolder}${alias}.txt)"
if [ "${counto}" != "${countf}" ]; then
echo; echo ' Aggregation Stats:'
echo ' ------------------'
printf "%-10s %-10s \n" ' Original' 'Final'
echo ' ------------------'
printf "%-10s %-10s \n" " ${counto}" "${countf}"
echo ' ------------------'
fi
}
# Function to remove duplicate entries in each list individually.
duplicate() {
if [ ! -x "${pathgrepcidr}" ]; then
log="Application [ grepcidr ] Not found. Cannot proceed."
echo "${log}" | tee -a "${errorlog}"
return
fi
dupcheck=1
# Check if masterfile is empty
hcheck="$(grep -cv ^$ ${masterfile})"; if [ "${hcheck}" -eq 0 ]; then dupcheck=0; fi
# Check if alias exists in masterfile
lcheck="$(grep -m1 ${alias} ${masterfile})"; if [ -z "${lcheck}" ]; then dupcheck=0; fi
# Check for single alias in masterfile
aliaslist="$(cut -d ' ' -f1 ${masterfile} | sort | uniq)"; if [ "${alias}" == "${aliaslist}" ]; then hcheck=0; fi
# Only execute if 'Alias' exists in masterfile
if [ "${dupcheck}" -eq 1 ]; then
# Grep alias with a trailing space character
grep "${alias}[[:space:]]" "${masterfile}" > "${tempfile}"
awk 'FNR==NR{a[$0];next}!($0 in a)' "${tempfile}" "${masterfile}" > "${tempfile2}"; mv -f "${tempfile2}" "${masterfile}"
cut -d ' ' -f2 "${masterfile}" > "${mastercat}"
fi
# Don't execute when only a single 'Alias' exists in masterfile
if [ ! "${hcheck}" -eq 0 ]; then
sort "${pfbdeny}${alias}.txt" | uniq > "${tempfile}"; mv -f "${tempfile}" "${pfbdeny}${alias}.txt"
"${pathgrepcidr}" -vf "${mastercat}" "${pfbdeny}${alias}.txt" > "${tempfile}"; mv -f "${tempfile}" "${pfbdeny}${alias}.txt"
fi
sed -e 's/^/'$alias' /' "${pfbdeny}${alias}.txt" >> "${masterfile}"
cut -d ' ' -f2 "${masterfile}" > "${mastercat}"
counto="$(grep -cv '^#\|^$' ${pfborig}${alias}.orig)"
countm="$(grep -c ${alias} ${masterfile})"
countf="$(grep -c ^ ${pfbdeny}${alias}.txt)"
if [ "${countm}" -eq "${countf}" ]; then
sanity='Pass'
else
sanity=' ==> FAILED <== '
fi
echo ' ------------------------------'
printf "%-10s %-10s %-10s\n" ' Original' 'Master' 'Final'
echo ' ------------------------------'
printf "%-10s %-10s %-10s %-10s\n" " ${counto}" "${countm}" "${countf}" " [ ${sanity} ]"
echo ' -----------------------------------------------------------------'
emptyfiles # Call emptyfiles function
}
# Function for DNSBL (De-Duplication, Whitelist, and Alexa Whitelist)
dnsbl_scrub() {
counto="$(grep -c ^ ${pfbdomain}${alias}.bk)"
alexa_enable="${max}"
# Process De-Duplication
sort "${pfbdomain}${alias}.bk" | uniq > "${pfbdomain}${alias}.bk2"
countu="$(grep -c ^ ${pfbdomain}${alias}.bk2)"
if [ -d "${pfbdomain}" ] && [ "$(ls -A ${pfbdomain}*.txt 2>/dev/null)" ]; then
find "${pfbdomain}"*.txt ! -name "${alias}.txt" | xargs cat > "${domainmaster}"
# Only execute awk command, if master domain file contains data.
query_size="$(grep -c ^ ${domainmaster})"
if [ "${query_size}" -gt 0 ]; then
# Unbound blocking mode dedup
if [ "${dedup}" == '' ]; then
awk 'FNR==NR{a[$2];next}!($2 in a)' "${domainmaster}" "${pfbdomain}${alias}.bk2" > "${pfbdomain}${alias}.bk"
# Unbound python blocking mode dedup
else
awk -F',' 'FNR==NR{a[$2];next}!($2 in a)' "${domainmaster}" "${pfbdomain}${alias}.bk2" > "${pfbdomain}${alias}.bk"
fi
fi
rm -f "${domainmaster}"
else
mv -f "${pfbdomain}${alias}.bk2" "${pfbdomain}${alias}.bk"
fi
countf="$(grep -c ^ ${pfbdomain}${alias}.bk)"
countd="$((countu - countf))"
rm -f "${pfbdomain}${alias}.bk2"
# Remove Whitelisted Domains and Sub-Domains, if configured
if [ -s "${pfbdnsblsuppression}" ] && [ -s "${pfbdomain}${alias}.bk" ]; then
/usr/local/bin/ggrep -vF -f "${pfbdnsblsuppression}" "${pfbdomain}${alias}.bk" > "${pfbdomain}${alias}.bk2"
countx="$(grep -c ^ ${pfbdomain}${alias}.bk2)"
countw="$((countf - countx))"
if [ "${countw}" -gt 0 ]; then
if [ "${dedup}" == '' ]; then
data="$(awk 'FNR==NR{a[$0];next}!($0 in a)' ${pfbdomain}${alias}.bk2 ${pfbdomain}${alias}.bk | \
cut -d '"' -f2 | cut -d ' ' -f1 | sort | uniq | tr '\n' '|')"
else
data="$(awk 'FNR==NR{a[$0];next}!($0 in a)' ${pfbdomain}${alias}.bk2 ${pfbdomain}${alias}.bk | \
cut -d ',' -f2 | sort | uniq | tr '\n' '|')"
fi
if [ -z "${data}" ]; then
if [ "${dedup}" == '' ]; then
data="$(cut -d '"' -f2 ${pfbdomain}${alias}.bk | cut -d ' ' -f1 | sort | uniq | tr '\n' '|')"
else
data="$(cut -d ',' -f2 ${pfbdomain}${alias}.bk | sort | uniq | tr '\n' '|')"
fi
fi
echo " Whitelist: ${data}"
mv -f "${pfbdomain}${alias}.bk2" "${pfbdomain}${alias}.bk"
fi
else
countw=0
fi
# Process TOP1M Whitelist
if [ "${alexa_enable}" == "on" ] && [ -s "${pfbalexa}" ] && [ -s "${pfbdomain}${alias}.bk" ]; then
countf="$(grep -c ^ ${pfbdomain}${alias}.bk)"
/usr/local/bin/ggrep -vF -f "${pfbalexa}" "${pfbdomain}${alias}.bk" > "${pfbdomain}${alias}.bk2"
countx="$(grep -c ^ ${pfbdomain}${alias}.bk2)"
counta="$((countf - countx))"
if [ "${counta}" -gt 0 ]; then
if [ "${dedup}" == '' ]; then
data="$(awk 'FNR==NR{a[$0];next}!($0 in a)' ${pfbdomain}${alias}.bk2 ${pfbdomain}${alias}.bk | \
cut -d '"' -f2 | cut -d ' ' -f1 | sort | uniq | tr '\n' '|')"
else
data="$(awk 'FNR==NR{a[$0];next}!($0 in a)' ${pfbdomain}${alias}.bk2 ${pfbdomain}${alias}.bk | \
cut -d ',' -f2 | sort | uniq | tr '\n' '|')"
fi
if [ -z "${data}" ]; then
if [ "${dedup}" == '' ]; then
data="$(cut -d '"' -f2 ${pfbdomain}${alias}.bk | cut -d ' ' -f1 | sort | uniq | tr '\n' '|')"
else
data="$(cut -d ',' -f2 ${pfbdomain}${alias}.bk | sort | uniq | tr '\n' '|')"
fi
fi
echo " TOP1M Whitelist: ${data}"
mv -f "${pfbdomain}${alias}.bk2" "${pfbdomain}${alias}.bk"
fi
else
counta=0
fi
countf="$(grep -c ^ ${pfbdomain}${alias}.bk)"
rm -f "${pfbdomain}${alias}.bk2"
echo ' ----------------------------------------------------------------------'
printf "%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n" ' Orig.' 'Unique' '# Dups' '# White' '# TOP1M' 'Final'
echo ' ----------------------------------------------------------------------'
printf "%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n" " ${counto}" "${countu}" "${countd}" "${countw}" "${counta}" "${countf}"
echo ' ----------------------------------------------------------------------'
}
# Function to process TLD
domaintld() {
# List of Feeds
dnsbl_files="${cc}";
> "${tempfile}"; > "${tempfile2}"; > "${dupfile}"
if [ -s "${dnsbl_file}.raw" ]; then
sort "${dnsbl_file}.raw" | uniq > "${tempfile}" && mv -f "${tempfile}" "${dnsbl_file}.raw"
countto="$(grep -v '"transparent"\|\"static\"' ${dnsbl_file}.raw | grep -c ^)"
else
countto=0
fi
if [ -s "${dnsbl_tld_remove}" ]; then
sort "${dnsbl_tld_remove}" | uniq > "${tempfile}" && mv -f "${tempfile}" "${dnsbl_tld_remove}"
counttm="$(grep -c '^\.' ${dnsbl_tld_remove})"
else
counttm=0
fi
if [ "${DEBUG}" == 1 ] && [ -e "${dnsbl_file}.raw" ]; then
cp "${dnsbl_file}.raw" "${dnsbl_file}.raw.orig"
fi
printf "."
# 'Redirect zone'
# Collect DNSBL TLD files (by smallest line count first) and merge
dnsbl_tmp_files="$(grep -Hc ^ ${dnsbl_tmp}DNSBL_*.txt | sort -t : -k 2,2n | cut -d':' -f1)"
if [ ! -z "${dnsbl_tmp_files}" ]; then
for file in ${dnsbl_tmp_files}; do
# For each file, place 'local-zone' before 'local-data'
head -1 "${file}" >> "${dupfile}"
tail -n +2 "${file}" | sort | uniq >> "${dupfile}"
done
# Remove redundant Domains (in 'redirect zone')
if [ -s "${dnsbl_tld_remove}" ] && [ -s "${dupfile}" ]; then
if [ "${DEBUG}" == 1 ]; then
cp "${dupfile}" "${dnsbl_file}.dup"
fi
/usr/local/bin/ggrep -vF -f "${dnsbl_tld_remove}" "${dupfile}" > "${tempfile}"
else
mv -f "${dupfile}" "${tempfile}"
fi
fi
# 'Transparent zone'
# Remove redundant Domains (in 'transparent zone')
if [ -s "${dnsbl_tld_remove}.tsp" ] && [ -s "${dnsbl_file}.tsp" ]; then
/usr/local/bin/ggrep -vF -f "${dnsbl_tld_remove}.tsp" "${dnsbl_file}.tsp" | sort | uniq > "${tempfile2}"
else
echo "XXX"
# XXXX to be confirmed!
# mv -f "${dnsbl_tld_remove}.tsp" "${tempfile2}"
fi
# Merge all TLD files
if [ -f "${tempfile}" ] || [ -f "${tempfile2}" ]; then
> "${dnsbl_file}.raw"
cat "${tempfile}" "${tempfile2}" >> "${dnsbl_file}.raw"
fi
if [ "${DEBUG}" == 1 ] && [ -e "${dnsbl_file}.raw" ]; then
cp "${dnsbl_file}.raw" "${dnsbl_file}.raw.final"
fi
# Sort 'Transparent zone' remove file
if [ -s "${dnsbl_tld_remove}.tsp" ]; then
sort "${dnsbl_tld_remove}.tsp" | uniq > "${tempfile}" && mv -f "${tempfile}" "${dnsbl_tld_remove}.tsp"
fi
# Remove redundant Domains in DNSBL files
# Need to re-process all Feeds for TLD (Remove any recently added TLD Domains)
if [ -s "${dnsbl_tld_remove}.tsp" ]; then
for i in ${dnsbl_files}; do
alias="$(echo ${i%*,})"
printf "."
if [ "${DEBUG}" == 1 ] && [ -f "${pfbdomain}${alias}.txt" ]; then
cp "${pfbdomain}${alias}.txt" "${pfbdomain}${alias}.xxx"
fi
# Remove redundant TLD Domains
if [ -s "${pfbdomain}${alias}.txt" ]; then
/usr/local/bin/ggrep -vF -f "${dnsbl_tld_remove}.tsp" "${pfbdomain}${alias}.txt" > "${tempfile}"
mv -f "${tempfile}" "${pfbdomain}${alias}.txt"
fi
done
fi
counttf="$(grep -v '"transparent"\|\"static\"' ${dnsbl_file}.raw | grep -c ^)"
counttr="$((countto - counttf))"
echo
echo ' ----------------------------------------'
printf "%-12s %-10s %-10s %-10s\n" ' Original' 'Matches' 'Removed' 'Final'
echo ' ----------------------------------------'
printf "%-12s %-10s %-10s %-10s\n" " ${countto}" "${counttm}" "${counttr}" "${counttf}"
printf ' -----------------------------------------'
}
# Function to process TLD python
domaintldpy() {
# List of Feeds
dnsbl_files="${cc}";
if [ -s "${dnsbl_python_data}.raw" ]; then
sort "${dnsbl_python_data}.raw" | uniq > "${tempfile}" && mv -f "${tempfile}" "${dnsbl_python_data}.raw"
countd="$(grep -c ^ ${dnsbl_python_data}.raw)"
else
countd=0
fi
if [ -s "${dnsbl_python_zone}.raw" ]; then
sort "${dnsbl_python_zone}.raw" | uniq > "${tempfile}" && mv -f "${tempfile}" "${dnsbl_python_zone}.raw"
countz="$(grep -c ^ ${dnsbl_python_zone}.raw)"
else
countz=0
fi
printf "."
countto="$((countd + countz))"
if [ -s "${dnsbl_tld_remove}" ]; then
sort "${dnsbl_tld_remove}" | uniq > "${tempfile}" && mv -f "${tempfile}" "${dnsbl_tld_remove}"
counttm="$(grep -c '^\.' ${dnsbl_tld_remove})"
else
counttm=0
fi
printf "."
# Remove redundant Domains (in data)
if [ -s "${dnsbl_tld_remove}" ] && [ -s "${dnsbl_python_data}.raw" ]; then
/usr/local/bin/ggrep -vF -f "${dnsbl_tld_remove}" "${dnsbl_python_data}.raw" > "${dnsbl_python_data}"
elif [ -e "${dnsbl_python_data}.raw" ]; then
mv "${dnsbl_python_data}.raw" "${dnsbl_python_data}"
fi
printf "."
# Remove redundant Domains (in zone)
if [ -s "${dnsbl_tld_remove}" ] && [ -s "${dnsbl_python_zone}.raw" ]; then
/usr/local/bin/ggrep -vF -f "${dnsbl_tld_remove}" "${dnsbl_python_zone}.raw" > "${dnsbl_python_zone}"
elif [ -e "${dnsbl_python_zone}.raw" ]; then
mv "${dnsbl_python_zone}.raw" "${dnsbl_python_zone}"
fi
counttf="$(cat ${dnsbl_python_data} ${dnsbl_python_zone} | grep -c ^)"
counttr="$((countto - counttf))"
echo
echo ' ----------------------------------------'
printf "%-12s %-10s %-10s %-10s\n" ' Original' 'Matches' 'Removed' 'Final'
echo ' ----------------------------------------'
printf "%-12s %-10s %-10s %-10s\n" " ${countto}" "${counttm}" "${counttr}" "${counttf}"
printf ' -----------------------------------------'
echo "${counttr}" > "${dnsbl_python_count}"
}
# Function to compare previous and current DNSBL Unbound conf file, and create Add/Remove files for unbound-control cmds
dnsbl_livesync() {
if [ "${DEBUG}" == 1 ]; then
if [ -e "${dnsbl_file}.conf" ]; then
cp "${dnsbl_file}.conf" "${dnsbl_file}.bkr"
fi
if [ -e "${dnsbl_file}.raw" ]; then
cp "${dnsbl_file}.raw" "${dnsbl_file}.bkraw"
fi
fi
rm -f "${dnsbl_add}"*
rm -f "${dnsbl_remove}"*
> "${dnsbl_add}"
> "${dnsbl_add_zone}"
> "${dnsbl_add_data}"
> "${dnsbl_remove}"
> "${dnsbl_remove_zone}"
> "${dnsbl_remove_data}"
if [ -s "${dnsbl_file}.conf" ] && [ -s "${dnsbl_file}.raw" ]; then
# Collect all changes to DNSBL (add/remove)
printf "."
awk 'FNR==NR{a[$0];next}!($0 in a)' "${dnsbl_file}.conf" "${dnsbl_file}.raw" > "${dnsbl_add}"
printf "."
awk 'FNR==NR{a[$0];next}!($0 in a)' "${dnsbl_file}.raw" "${dnsbl_file}.conf" > "${dnsbl_remove}"
printf "."
elif [ -s "${dnsbl_file}.raw" ]; then
printf "."
cp "${dnsbl_file}.raw" "${dnsbl_add}"
# Add a file marker to instruct Unbound to do a reload
> "${dnsbl_file}.reload"
else
printf "."
# Add a file marker to instruct Unbound to do a reload
> "${dnsbl_file}.reload"
fi
# Example Unbound sinkhole lines:
# local-zone: "example.com" redirect local-data: "example.com 60 IN A 10.10.10.1"
# local-data: "example.com 60 IN A 10.10.10.1"
# local-zone: "com" "transparent"
# local-zone: "ru" "static"
# Read 'Remove' file and format local-zone/local-data removal files
if [ -s "${dnsbl_remove}" ]; then
# Collect any local-zone removals
grep "local-zone:" "${dnsbl_remove}" | cut -d '"' -f2 > "${dnsbl_remove_zone}"
# Collect any local-data removals
grep "^local-data:" "${dnsbl_remove}" | cut -d ' ' -f2 | tr -d '"' > "${dnsbl_remove_data}"
fi
# Read 'Add' file and format local-zone/local-data addition files
if [ -s "${dnsbl_add}" ]; then
# Collect local-zone additions
grep '"transparent"\|\"static\"' "${dnsbl_add}" | cut -d '"' -f2,4 | tr '"', ' ' > "${dnsbl_add_zone}"
grep "^local-zone:" "${dnsbl_add}" | cut -d ' ' -f2-3 | tr -d '"' >> "${dnsbl_add_zone}"
# Collect local-data additions
grep -v '"transparent"\|\"static\"' "${dnsbl_add}" | grep "^local-zone:" | cut -d ' ' -f5-9 | tr -d '"' >> "${dnsbl_add_data}"
grep "^local-data:" "${dnsbl_add}" | awk '{gsub (/" local/,"\nlocal")}1' | cut -d ' ' -f2-6 | tr -d '"' >> "${dnsbl_add_data}"
# Create 'transparent' TLD zone for any local-data
if [ -s "${dnsbl_add_data}" ]; then
cat "${dnsbl_add_data}" | cut -d ' ' -f1 | rev | cut -d '.' -f1 | rev | sed 's/$/ transparent/' >> "${dnsbl_add_zone}"
fi
fi
if [ -e "${dnsbl_file}.raw" ]; then
mv -f "${dnsbl_file}.raw" "${dnsbl_file}.conf"
fi
}
# Function to convert Domains/ASs to its respective IP addresses
whoisconvert() {
vtype="${max}"
custom_list="$(echo ${dedup} | tr ',' ' ')"
multiple="$(echo ${dedup} | tr -cd , | wc -c | tr -d ' ')"
if [ "${vtype}" == '_v4' ]; then
_type=A
_bgp_type=4
_ip_type='\.'
else
_type=AAAA
_bgp_type=6
_ip_type=':'
fi
# Backup previous orig file
if [ -e "${pfborig}${alias}.orig" ]; then
mv "${pfborig}${alias}.orig" "${pfborig}${alias}.bk"
fi
echo
found=false
for host in ${custom_list}; do
# Determine if host is a Domain or an AS
host_check="$(echo ${host} | grep '\.')"
if [ ! -z "${host_check}" ]; then
found=true
printf " Collecting host IP: ${host}"
echo "### Domain: ${host} ###" >> "${pfborig}${alias}.orig"
"${pathhost}" -t ${_type} ${host} | sed 's/^.* //' >> "${pfborig}${alias}.orig"
echo "... completed"
else
asn="$(echo ${host} | tr -d 'AaSs')"
printf " Downloading ASN: ${asn}"
ua="pfSense/pfBlockerNG cURL download agent-"
guid="$(/usr/sbin/gnid)"
ua_final="${ua}${guid}"
bgp_url="https://api.bgpview.io/asn/${asn}/prefixes"
unavailable=''
for i in 1 2 3 4 5; do
printf "."
"${pathcurl}" -A "${ua_final}" -sS1 "${bgp_url}" > "${asntemp}"
if [ -e "${asntemp}" ] && [ -s "${asntemp}" ]; then
printf "."
unavailable="$(grep 'Service Temporarily Unavailable\|Server Error' ${asntemp})"
if [ -z "${unavailable}" ]; then
found=true
echo ". completed"
echo "### AS${asn}: ${host} ###" >> "${pfborig}${alias}.orig"
cat "${asntemp}" | "${pathjq}" -r ".data.ipv${_bgp_type}_prefixes[].prefix" >> "${pfborig}${alias}.orig"
break
else
sleep_val="$((i * 2))"
sleep "${sleep_val}"
fi
fi
done
if [ ! -z "${unavailable}" ]; then
echo ". Failed to download ASN"
touch "${pfborig}${alias}.fail"
fi
if [ "${multiple}" -gt 0 ]; then
sleep 1
fi
fi
done
# Restore previous orig file
if [ "${found}" == false ]; then
if [ -e "${pfborig}${alias}.bk" ]; then
mv "${pfborig}${alias}.bk" "${pfborig}${alias}.orig"
else
echo > "${pfborig}${alias}.orig"
fi
else
if [ -e "${pfborig}${alias}.bk" ]; then
rm -f "${pfborig}${alias}.bk"
fi
fi
}
# Function to convert IP to ASN
iptoasn() {
host="${alias}"
ua="pfSense/pfBlockerNG cURL download agent-"
guid="$(/usr/sbin/gnid)"
ua_final="${ua}${guid}"
bgp_url="https://api.bgpview.io/ip/${host}"
unavailable=''
found=false
for i in 1 2 3 4 5; do
"${pathcurl}" -A "${ua_final}" -sS1 "${bgp_url}" > "${asntemp}"
if [ -e "${asntemp}" ] && [ -s "${asntemp}" ]; then
unavailable="$(grep 'Service Temporarily Unavailable\|Server Error' ${asntemp})"
if [ -z "${unavailable}" ]; then
found=true
break
else
sleep_val="$((i * 2))"
sleep "${sleep_val}"
fi
fi
done
if [ "${found}" == false ]; then
echo ""
else
asn_final="$(cat ${asntemp} | ${pathjq} -r '.data.prefixes[0] | {ASN: .asn.asn, Name: .name, Desc: .description, Prefix: .prefix} | tostring' | tr ',' '|' | tr -d '"{}')"
echo "${asn_final}"
fi
}
# Function to check for Reputation application dependencies.
reputation_depends() {
if [ ! -x "${pathgeoip}" ]; then
log="Application [ mmdblookup ] Not found, cannot proceed. [ ${now} ]"
echo "${log}" | tee -a "${errorlog}"
return
fi
# Download MaxMind GeoLite2-Country.mmdb on first install.
if [ ! -f "${pathgeoipdat}" ]; then
echo "Downloading [ MaxMind GeoLite2-Country.mmdb ] [ ${now} ]" >> "${geoiplog}"
/usr/local/bin/php /usr/local/www/pfblockerng/pfblockerng.php bu
fi
# Exit if GeoLite2-Country.mmdb is not found
if [ ! -f "${pathgeoipdat}" ]; then
log="Database GeoIP [ GeoLite2-Country.mmdb ] not found. Reputation function terminated."
echo "${log}" | tee -a "${errorlog}"
return
fi
# Clear variables and tempfiles
count=0; countb=0; countm=0; counts=0; countr=0
}
# Reputation function to condense an IP range if a 'Max' amount of IP addresses are found in a /24 range per individual list.
reputation_max() {
sort "${pfbdeny}${alias}.txt" | uniq > "${tempfile}"
data="$(cut -d '.' -f 1-3 ${tempfile} | awk -v max=${max} '{a[$0]++}END{for(i in a){if(a[i] > max){print i}}}')"
# Classify repeat offenders by Country code
if [ ! -z "${data}" ]; then
for ip in ${data}; do
ccheck="$(${pathgeoip} -f ${pathgeoipdat} -i ${ip}.1 country iso_code 2>&1 | grep -v 'Could\|Got\|^$' | cut -d '"' -f2)"
case "${cc}" in
*$ccheck*)
countr="$((countr + 1))"
if [ "${ccwhite}" == 'match' ] || [ "${ccblack}" == 'match' ]; then
echo "${ip}." >> "${matchfile}"
fi
;;
*)
count="$((count + 1))"
echo "${ip}." >> "${dupfile}"
;;
esac
done
else
countr=0; count=0
fi
# Collect match file details
if [ -s "${matchfile}" ] && [ "${dedup}" != 'on' ] && [ "${ccwhite}" == 'match' ]; then
mon="$(sed -e 's/^/^/' -e 's/\./\\\./g' ${matchfile})"
for ip in ${mon}; do
grep "${ip}" "${tempfile}" >> "${tempfile2}"
done
counts="$(grep -c ^ ${tempfile2})"
if [ "${ccwhite}" == 'match' ]; then
sed 's/$/0\/24/' "${matchfile}" >> "${tempmatchfile}"
sed 's/^/\!/' "${tempfile2}" >> "${tempmatchfile}"
fi
fi
# If no matches found remove previous matchoutfile if exists.
matchoutfile="match${header}.txt"
if [ ! -s "${tempmatchfile}" ] && [ -f "${matchoutfile}" ]; then rm -r "${matchoutfile}"; fi
# Move match file to the match folder by individual blocklist name
if [ -s "${tempmatchfile}" ]; then mv -f "${tempmatchfile}" "${pfbmatch}${matchoutfile}"; fi
# Find repeat offenders in each individual blocklist outfile
if [ -s "${dupfile}" ]; then
> "${tempfile2}"
dup="$(sed -e 's/^/^/' -e 's/\./\\\./g' ${dupfile})"
for ip in ${dup}; do
grep "${ip}" "${tempfile}" >> "${tempfile2}"
done
countb="$(grep -c ^ ${tempfile2})"
if [ "${ccblack}" == 'block' ]; then
awk 'FNR==NR{a[$0];next}!($0 in a)' "${tempfile2}" "${tempfile}" > "${pfbdeny}${alias}.txt"
sed 's/$/0\/24/' "${dupfile}" >> "${pfbdeny}${alias}.txt"
elif [ "${ccblack}" == 'match' ]; then
sed 's/$/0\/24/' "${dupfile}" >> "${tempmatchfile}"
sed 's/^/\!/' "${tempfile2}" >> "${tempmatchfile}"
else
:
fi
fi
if [ "${count}" -gt 0 ]; then
echo; echo " Reputation (Max=${max}) - Range(s)"
cat "${dupfile}" | tr '\n' '|'; echo
sort "${pfbdeny}${alias}.txt" | uniq > "${tempfile}"; mv -f "${tempfile}" "${pfbdeny}${alias}.txt"
fi
if [ "${count}" -gt 0 ] || [ "${countr}" -gt 0 ]; then
echo; echo ' Reputation -Max Stats'
echo ' ------------------------------'
printf "%-17s %-10s\n" ' Blacklisted' 'Match'
printf "%-8s %-8s %-8s %-8s\n" ' Ranges' 'IPs' 'Ranges' 'IPs'
echo ' ------------------------------'
printf "%-8s %-8s %-8s %-8s\n" " ${count}" "${countb}" "${countr}" "${counts}"
echo
fi
}
# Reputation function 'dMax' utilizing MaxMind GeoIP Country code.
reputation_dmax() {
echo; echo '===[ Reputation - dMax ]======================================'
echo; echo " Querying for repeat offenders ( dMax=${max} ) [ ${now} ]"
data="$(find ${pfbdeny}*.txt ! -name pfB*.txt ! -name *_v6.txt -type f | xargs cut -d '.' -f 1-3 | \
awk -v max=${max} '{a[$0]++}END{for(i in a){if(a[i] > max){print i}}}' | grep -v ^${ip_placeholder3}$)"
# Classify repeat offenders by Country code
if [ ! -z "${data}" ]; then
echo ' Classifying repeat offenders by GeoIP'
for ip in ${data}; do
ccheck="$(${pathgeoip} -f ${pathgeoipdat} -i ${ip}.1 country iso_code 2>&1 | grep -v 'Could\|Got\|^$' | cut -d '"' -f2)"
case "${cc}" in
*$ccheck*)
countr="$((countr + 1))"
if [ "${ccwhite}" == 'match' ] || [ "${ccblack}" == 'match' ]; then
echo "${ip}." >> "${matchfile}"
fi
;;
*)
count="$((count + 1))"
echo "${ip}." >> "${dupfile}"
;;
esac
done
else
countr=0; count=0
fi
if [ "${ccwhite}" == 'match' ] && [ -s "${matchfile}" ]; then
echo ' Processing [ Match ] IPs'
match="$(sed -e 's/^/^/' -e 's/\./\\\./g' ${matchfile})"
for mfile in ${match}; do
grep "${mfile}" "${pfbdeny}"*.txt >> "${tempfile}"
done
sed 's/$/0\/24/' "${matchfile}" >> "${tempmatchfile}"
sed -e 's/.*://' -e 's/^/\!/' "${tempfile}" >> "${tempmatchfile}"
mv -f "${tempmatchfile}" "${pfbmatch}${matchdedup}"
countm="$(grep -c ^ ${tempfile})"
counts="$((countm + counts))"
fi