2769 |
rexy |
1 |
#!/bin/bash
|
|
|
2 |
|
|
|
3 |
# $Id: alcasar-bl.sh 2688 2019-01-18 23:15:49Z lucas.echard $
|
|
|
4 |
|
|
|
5 |
# alcasar-autoupdate.sh
|
|
|
6 |
# by Sven RATH and Rexy
|
|
|
7 |
# This script is distributed under the Gnu General Public License (GPL)
|
|
|
8 |
|
|
|
9 |
# Gestion de la BL pour le filtrage de domaine (via unbound) et d'URL (via E2guardian)
|
|
|
10 |
# Manage the BL for DnsBlackHole (unbound) and URL filtering (E2guardian)
|
|
|
11 |
|
|
|
12 |
FILE_tmp="/tmp/filesfilter.txt"
|
|
|
13 |
FILE_ip_tmp="/tmp/filesipfilter.txt"
|
|
|
14 |
DIR_DG="/etc/e2guardian/lists"
|
|
|
15 |
DIR_DG_BL="$DIR_DG/blacklists"
|
|
|
16 |
DIR_SHARE="/usr/local/share"
|
|
|
17 |
DIR_DNS_BL="$DIR_SHARE/unbound-bl" # all the BL in the Unbound format
|
|
|
18 |
DIR_DNS_WL="$DIR_SHARE/unbound-wl" # all the WL ' ' '
|
|
|
19 |
DIR_IP_BL="$DIR_SHARE/iptables-bl" # all the IP addresses of the BL
|
|
|
20 |
DIR_IP_WL="$DIR_SHARE/iptables-wl" # IP ossi disabled WL
|
|
|
21 |
CNC_BL_NAME="ossi-bl-candc"
|
|
|
22 |
CNC_URL="https://osint.bambenekconsulting.com/feeds/"
|
2770 |
rexy |
23 |
CNC_DNS=${CNC_URL}c2-dommasterlist-high.txt
|
|
|
24 |
CNC_IP=${CNC_URL}c2-ipmasterlist-high.txt
|
2769 |
rexy |
25 |
SED="/bin/sed -i"
|
|
|
26 |
CURL="/usr/bin/curl"
|
|
|
27 |
|
|
|
28 |
# cleaning file and split it ("domains" in $FILE_tmp & "IP" in $FILE_ip_tmp)
|
|
|
29 |
function clean_split (){
|
|
|
30 |
$SED '/^#.*/d' $FILE_tmp # remove commented lines
|
|
|
31 |
$SED '/^\s*$/d' $FILE_tmp # remove empty lines
|
|
|
32 |
$SED '/[äâëêïîöôüû@,]/d' $FILE_tmp # remove line with "chelou" characters
|
|
|
33 |
# extract ip addresses for iptables.
|
|
|
34 |
awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $FILE_tmp > $FILE_ip_tmp
|
|
|
35 |
# extract domain names for unbound.
|
|
|
36 |
$SED -n '/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/!p' $FILE_tmp
|
|
|
37 |
# Retrieve max Top Level Domain for domain name synthax
|
|
|
38 |
#MAX_TLD=$(curl http://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep -v '-' | grep -v '#' | wc -L)
|
|
|
39 |
#if [ $(echo $MAX_TLD | wc -c) -eq 0 ];then
|
|
|
40 |
# MAX_TLD=18
|
|
|
41 |
#fi
|
|
|
42 |
# search for correction egrep "([a-zA-Z0-9_-.]+\.){1,2}[a-zA-Z]{2,$MAX_TLD}" $ossi_custom_dir/domains > $FILE_tmp
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
usage="Usage: alcasar-bl-autoupdate.sh { -update_cat or --update_cat | -update_ossi-bl-candc or --update_ossi-bl-candc }"
|
|
|
46 |
nb_args=$#
|
|
|
47 |
args=$1
|
|
|
48 |
if [ $nb_args -eq 0 ]
|
|
|
49 |
then
|
|
|
50 |
args="-h"
|
|
|
51 |
fi
|
|
|
52 |
case $args in
|
|
|
53 |
-\? | -h* | --h*)
|
|
|
54 |
echo "$usage"
|
|
|
55 |
exit 0
|
|
|
56 |
;;
|
|
|
57 |
# Update the categories of Toulouse BL listed in "/usr/local/etc/update_cat.conf" (via rsync). Cron runs this function every 12h
|
|
|
58 |
-update_cat | --update_cat)
|
|
|
59 |
if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
|
|
|
60 |
then
|
|
|
61 |
echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
|
|
|
62 |
cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
|
|
|
63 |
do
|
|
|
64 |
CATEGORIE=$(echo $LIGNE_RSYNC | cut -d' ' -f1)
|
|
|
65 |
URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
|
|
|
66 |
PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
|
|
|
67 |
rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
|
|
|
68 |
# Creation of unbound and Iptables BL and WL
|
|
|
69 |
DOMAIN=$(basename $PATH_FILE)
|
|
|
70 |
cp $PATH_FILE/domains $FILE_tmp
|
|
|
71 |
clean_split # clean ossi custom files & split them for unbound and for iptables
|
|
|
72 |
black=`grep black $PATH_FILE/usage |wc -l`
|
|
|
73 |
if [ $black == "1" ]
|
|
|
74 |
then
|
|
|
75 |
# adapt to the unbound syntax for the blacklist
|
|
|
76 |
$SED "s?.*?local-zone: & typetransparent\nlocal-zone-tag: & blacklist?g" $FILE_tmp
|
|
|
77 |
mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
|
|
|
78 |
mv $FILE_ip_tmp $DIR_IP_BL/$DOMAIN
|
|
|
79 |
else
|
|
|
80 |
# adapt to the unbound syntax for the whitelist
|
|
|
81 |
$SED "s?.*?local-zone: & transparent?g" $FILE_tmp
|
3100 |
rexy |
82 |
$SED "p; s? transparent? ipset?g" $FILE_tmp # duplicate lines to enable ipset module
|
2769 |
rexy |
83 |
mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
|
|
|
84 |
mv $FILE_ip_tmp $DIR_IP_WL/$DOMAIN
|
|
|
85 |
fi
|
|
|
86 |
rm -f $FILE_tmp $FILE_ip_tmp
|
|
|
87 |
done
|
|
|
88 |
/usr/local/bin/alcasar-bl.sh --reload
|
|
|
89 |
else
|
|
|
90 |
echo -n "/usr/local/etc/update_cat.conf is empty ..."
|
|
|
91 |
fi
|
|
|
92 |
echo
|
|
|
93 |
;;
|
|
|
94 |
# Update C&C-Server Blacklist (TODO : check that there is a difference between two downloads)
|
|
|
95 |
-update_ossi-bl-candc | --update_ossi-bl-candc)
|
|
|
96 |
# check availability of the lists
|
|
|
97 |
echo "Downloading blacklists from ${CNC_URL}..."
|
2770 |
rexy |
98 |
STATUS_DNS_BL=$(${CURL} --connect-timeout 5 --write-out %{http_code} --silent --output /dev/null ${CNC_DNS})
|
|
|
99 |
STATUS_IP_BL=$(${CURL} --connect-timeout 5 --write-out %{http_code} --silent --output /dev/null ${CNC_IP})
|
2769 |
rexy |
100 |
# if downloaded successfully
|
2770 |
rexy |
101 |
if [ $STATUS_DNS_BL = 200 ] && [ $STATUS_IP_BL = 200 ]; then
|
2769 |
rexy |
102 |
## parse domain names and ips from feed (cut first 19 lines (comments) and extract first column)
|
2770 |
rexy |
103 |
CNC_DOMAINS=$($CURL $CNC_DNS | tail -n +19 | awk -F, '{print $1}')
|
|
|
104 |
CNC_IPS=$($CURL $CNC_IP | tail -n +19 | awk -F, '{print $1}')
|
2769 |
rexy |
105 |
## create files and adapt downloaded data to alcasar structure (add newlines after each ip/domain)
|
|
|
106 |
BL_DIR=${DIR_DG_BL}/${CNC_BL_NAME}
|
|
|
107 |
rm -rf ${BL_DIR}
|
|
|
108 |
mkdir $BL_DIR
|
2770 |
rexy |
109 |
echo $CNC_DOMAINS | tr " " "\n" > ${BL_DIR}/urls
|
2769 |
rexy |
110 |
echo $CNC_IPS | tr " " "\n" > ${BL_DIR}/domains
|
|
|
111 |
## reload ossi-blacklists to add the created blacklist to ALCASAR
|
|
|
112 |
echo "Download successfull."
|
|
|
113 |
/usr/local/bin/alcasar-bl.sh --reload
|
|
|
114 |
exit 0
|
|
|
115 |
# if server responded with a code different than 200
|
|
|
116 |
else
|
|
|
117 |
## 000 means that curl failed
|
2770 |
rexy |
118 |
if [ $STATUS_DNS_BL = 000 ] || [ $STATUS_IP_BL = 000 ]; then
|
2769 |
rexy |
119 |
echo "ERROR: curl could not access the internet to download blacklists."
|
|
|
120 |
echo "This appears to be an error on your side: please check the connection to the internet."
|
|
|
121 |
else
|
|
|
122 |
echo "ERROR: could not donwload blacklists: Server returned non-200 codes:"
|
2770 |
rexy |
123 |
echo "${CNC_DNS} returned ${STATUS_DNS_BL}"
|
|
|
124 |
echo "${CNC_IP} returned ${STATUS_IP_BL}"
|
2769 |
rexy |
125 |
echo "Check the availability of the sites. Maybe the server removed its content or changed its address."
|
|
|
126 |
fi
|
|
|
127 |
exit 1
|
|
|
128 |
fi
|
|
|
129 |
;;
|
|
|
130 |
esac
|