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/"
|
|
|
23 |
CNC_DNS_BL_URL=${CNC_URL}c2-dommasterlist-high.txt
|
|
|
24 |
CNC_IP_BL_URL=${CNC_URL}c2-ipmasterlist-high.txt
|
|
|
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
|
|
|
82 |
mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
|
|
|
83 |
mv $FILE_ip_tmp $DIR_IP_WL/$DOMAIN
|
|
|
84 |
fi
|
|
|
85 |
rm -f $FILE_tmp $FILE_ip_tmp
|
|
|
86 |
done
|
|
|
87 |
/usr/local/bin/alcasar-bl.sh --reload
|
|
|
88 |
else
|
|
|
89 |
echo -n "/usr/local/etc/update_cat.conf is empty ..."
|
|
|
90 |
fi
|
|
|
91 |
echo
|
|
|
92 |
;;
|
|
|
93 |
# Update C&C-Server Blacklist (TODO : check that there is a difference between two downloads)
|
|
|
94 |
-update_ossi-bl-candc | --update_ossi-bl-candc)
|
|
|
95 |
# check availability of the lists
|
|
|
96 |
echo "Downloading blacklists from ${CNC_URL}..."
|
|
|
97 |
STATUS_URL_BL=$(${CURL} --connect-timeout 5 --write-out %{http_code} --silent --output /dev/null ${CNC_DNS_BL_URL})
|
|
|
98 |
STATUS_IP_BL=$(${CURL} --connect-timeout 5 --write-out %{http_code} --silent --output /dev/null ${CNC_IP_BL_URL})
|
|
|
99 |
# if downloaded successfully
|
|
|
100 |
if [ $STATUS_URL_BL = 200 ] && [ $STATUS_IP_BL = 200 ]; then
|
|
|
101 |
## parse domain names and ips from feed (cut first 19 lines (comments) and extract first column)
|
|
|
102 |
CNC_URLS=$($CURL $CNC_DNS_BL_URL | tail -n +19 | awk -F, '{print $1}')
|
|
|
103 |
CNC_IPS=$($CURL $CNC_IP_BL_URL | tail -n +19 | awk -F, '{print $1}')
|
|
|
104 |
## create files and adapt downloaded data to alcasar structure (add newlines after each ip/domain)
|
|
|
105 |
BL_DIR=${DIR_DG_BL}/${CNC_BL_NAME}
|
|
|
106 |
rm -rf ${BL_DIR}
|
|
|
107 |
mkdir $BL_DIR
|
|
|
108 |
echo $CNC_URLS | tr " " "\n" > ${BL_DIR}/urls
|
|
|
109 |
echo $CNC_IPS | tr " " "\n" > ${BL_DIR}/domains
|
|
|
110 |
## reload ossi-blacklists to add the created blacklist to ALCASAR
|
|
|
111 |
echo "Download successfull."
|
|
|
112 |
/usr/local/bin/alcasar-bl.sh --reload
|
|
|
113 |
exit 0
|
|
|
114 |
# if server responded with a code different than 200
|
|
|
115 |
else
|
|
|
116 |
## 000 means that curl failed
|
|
|
117 |
if [ $STATUS_URL_BL = 000 ] || [ $STATUS_IP_BL = 000 ]; then
|
|
|
118 |
echo "ERROR: curl could not access the internet to download blacklists."
|
|
|
119 |
echo "This appears to be an error on your side: please check the connection to the internet."
|
|
|
120 |
else
|
|
|
121 |
echo "ERROR: could not donwload blacklists: Server returned non-200 codes:"
|
|
|
122 |
echo "${CNC_DNS_BL_URL} returned ${STATUS_URL_BL}"
|
|
|
123 |
echo "${CNC_IP_BL_URL} returned ${STATUS_IP_BL}"
|
|
|
124 |
echo "Check the availability of the sites. Maybe the server removed its content or changed its address."
|
|
|
125 |
fi
|
|
|
126 |
exit 1
|
|
|
127 |
fi
|
|
|
128 |
;;
|
|
|
129 |
esac
|