Subversion Repositories ALCASAR

Rev

Rev 2688 | Rev 2770 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log

Rev 2688 Rev 2769
1
#!/bin/bash
1
#!/bin/bash
2
 
2
 
3
# $Id: alcasar-bl.sh 2688 2019-01-18 23:15:49Z lucas.echard $
3
# $Id: alcasar-bl.sh 2769 2019-11-24 22:31:36Z rexy $
4
 
4
 
5
# alcasar-bl.sh
5
# alcasar-bl.sh
6
# by Franck BOUIJOUX and Richard REY
6
# by Franck BOUIJOUX and Richard REY
7
# This script is distributed under the Gnu General Public License (GPL)
7
# This script is distributed under the Gnu General Public License (GPL)
8
 
8
 
9
# Gestion de la BL pour le filtrage de domaine (via unbound) et d'URL (via E2guardian)
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)
10
# Manage the BL for domain filtering (with unbound) and URL filtering (with E2guardian)
11
 
11
 
12
DIR_CONF="/usr/local/etc"
12
DIR_CONF="/usr/local/etc"
13
CONF_FILE="$DIR_CONF/alcasar.conf"
13
CONF_FILE="$DIR_CONF/alcasar.conf"
14
private_ip_mask=`grep ^PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
14
private_ip_mask=`grep ^PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
15
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
15
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
16
DIR_tmp="/tmp/blacklists"
16
DIR_tmp="/tmp/blacklists"
17
FILE_tmp="/tmp/filesfilter.txt"
17
FILE_tmp="/tmp/filesfilter.txt"
18
FILE_ip_tmp="/tmp/filesipfilter.txt"
18
FILE_ip_tmp="/tmp/filesipfilter.txt"
19
DIR_DG="/etc/e2guardian/lists"
19
DIR_DG="/etc/e2guardian/lists"
20
DIR_DG_BL="$DIR_DG/blacklists"
20
DIR_DG_BL="$DIR_DG/blacklists"
21
GLOBAL_USAGE="$DIR_CONF/alcasar-global-usage"				# file containing the description of the lists
21
GLOBAL_USAGE="$DIR_CONF/alcasar-global-usage"				# file containing the description of the lists
22
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
22
BL_CATEGORIES="$DIR_CONF/alcasar-bl-categories"				# list of names of the 	BL categories
23
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#	'	'	WL categories
23
WL_CATEGORIES="$DIR_CONF/alcasar-wl-categories"				#	'	'	WL categories
24
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
24
BL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-bl-categories-enabled"		#	'	'	BL enabled categories
25
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
25
WL_CATEGORIES_ENABLED="$DIR_CONF/alcasar-wl-categories-enabled"		#	'	'	WL enabled categories
26
DIR_SHARE="/usr/local/share"
26
DIR_SHARE="/usr/local/share"
27
DIR_DNS_BL="$DIR_SHARE/unbound-bl"					# all the BL in the Unbound format
27
DIR_DNS_BL="$DIR_SHARE/unbound-bl"					# all the BL in the Unbound format
28
DIR_DNS_WL="$DIR_SHARE/unbound-wl"					# all the WL	'	'	'
28
DIR_DNS_WL="$DIR_SHARE/unbound-wl"					# all the WL	'	'	'
29
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL
29
DIR_IP_BL="$DIR_SHARE/iptables-bl"					# all the IP addresses of the BL
30
DIR_IP_WL="$DIR_SHARE/iptables-wl"					# IP ossi disabled WL
30
DIR_IP_WL="$DIR_SHARE/iptables-wl"					# IP ossi disabled WL
31
DIR_DNS_BL_ENABLED="$DIR_SHARE/unbound-bl-enabled"			# symbolic link to the domains BL (only enabled categories)
31
DIR_DNS_BL_ENABLED="$DIR_SHARE/unbound-bl-enabled"			# symbolic link to the domains BL (only enabled categories)
32
DIR_DNS_WL_ENABLED="$DIR_SHARE/unbound-wl-enabled"			#	'	'	'	WL	'	'
32
DIR_DNS_WL_ENABLED="$DIR_SHARE/unbound-wl-enabled"			#	'	'	'	WL	'	'
33
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"			#	'	'	ip BL (only enabled categories)
33
DIR_IP_BL_ENABLED="$DIR_SHARE/iptables-bl-enabled"			#	'	'	ip BL (only enabled categories)
34
DIR_IP_WL_ENABLED="$DIR_SHARE/iptables-wl-enabled"			#	'	'	ip WL (ossi and ossi-* imported from ACC)
34
DIR_IP_WL_ENABLED="$DIR_SHARE/iptables-wl-enabled"			#	'	'	ip WL (ossi and ossi-* imported from ACC)
35
REHABILITATED_DNS_FILE="/etc/unbound/conf.d/blacklist/rehabilitated.conf"
35
REHABILITATED_DNS_FILE="/etc/unbound/conf.d/blacklist/rehabilitated.conf"
-
 
36
CNC_BL_NAME="ossi-bl-candc"
36
BL_SERVER="dsi.ut-capitole.fr"
37
BL_SERVER="dsi.ut-capitole.fr"
37
SED="/bin/sed -i"
38
SED="/bin/sed -i"
38
 
39
 
39
# enable/disable the BL & WL categories
40
# enable/disable the BL & WL categories
40
function cat_choice (){
41
function cat_choice (){
41
	mkdir -p $DIR_tmp
42
	mkdir -p $DIR_tmp
42
	for LIST in $DIR_IP_BL_ENABLED $DIR_DNS_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_WL_ENABLED
43
	for LIST in $DIR_IP_BL_ENABLED $DIR_DNS_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_WL_ENABLED
43
	do
44
	do
44
		if [ ! -e $LIST ] #  only on install stage
45
		if [ ! -e $LIST ] #  only on install stage
45
		then
46
		then
46
			mkdir $LIST
47
			mkdir $LIST
47
		else
48
		else
48
			rm -rf ${LIST:?}/*
49
			rm -rf ${LIST:?}/*
49
		fi
50
		fi
50
		chown root:apache $LIST
51
		chown root:apache $LIST
51
		chmod 770 $LIST
52
		chmod 770 $LIST
52
	done
53
	done
53
	# update categories with rsync
54
	# update categories with rsync
54
	if [ ! -e $DIR_CONF/update_cat.conf ]
55
	if [ ! -e $DIR_CONF/update_cat.conf ]
55
	then
56
	then
56
		touch $DIR_CONF/update_cat.conf
57
		touch $DIR_CONF/update_cat.conf
57
		chown root:apache $DIR_CONF/update_cat.conf
58
		chown root:apache $DIR_CONF/update_cat.conf
58
		chmod 660 $DIR_CONF/update_cat.conf
59
		chmod 660 $DIR_CONF/update_cat.conf
59
	fi
60
	fi
60
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
61
	$SED "/\.Include/d" $DIR_DG/bannedsitelist $DIR_DG/bannedurllist # cleaning for DG
61
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
62
	$SED "s?^[^#]?#&?g" $BL_CATEGORIES $WL_CATEGORIES # cleaning BL & WL categories file (comment all lines)
62
 
63
 
63
	# process the file $BL_CATEGORIES with the choice of categories
64
	# process the file $BL_CATEGORIES with the choice of categories
64
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
65
	for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
65
	do
66
	do
66
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
67
		$SED "/\/$ENABLE_CATEGORIE$/d" $BL_CATEGORIES
67
		$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
68
		$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $BL_CATEGORIES
68
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
69
		ln -sf $DIR_DNS_BL/$ENABLE_CATEGORIE.conf $DIR_DNS_BL_ENABLED/$ENABLE_CATEGORIE
69
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
70
		ln -sf $DIR_IP_BL/$ENABLE_CATEGORIE $DIR_IP_BL_ENABLED/$ENABLE_CATEGORIE
70
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by unbound
71
		# echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/domains>" >> $DIR_DG/bannedsitelist  # Blacklisted domains are managed by unbound
71
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
72
		echo ".Include<$DIR_DG_BL/$ENABLE_CATEGORIE/urls>" >> $DIR_DG/bannedurllist
72
	done
73
	done
73
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
74
	sort +0.0 -0.2 $BL_CATEGORIES -o $FILE_tmp
74
	mv $FILE_tmp $BL_CATEGORIES
75
	mv $FILE_tmp $BL_CATEGORIES
75
	sort +0.0 -0.2 $BL_CATEGORIES_ENABLED -o $FILE_tmp
76
	sort +0.0 -0.2 $BL_CATEGORIES_ENABLED -o $FILE_tmp
76
	mv $FILE_tmp $BL_CATEGORIES_ENABLED
77
	mv $FILE_tmp $BL_CATEGORIES_ENABLED
77
	chown root:apache $BL_CATEGORIES $BL_CATEGORIES_ENABLED
78
	chown root:apache $BL_CATEGORIES $BL_CATEGORIES_ENABLED
78
	chmod 660 $BL_CATEGORIES $BL_CATEGORIES_ENABLED
79
	chmod 660 $BL_CATEGORIES $BL_CATEGORIES_ENABLED
79
 
80
 
80
# process the file $WL_CATEGORIES with the choice of categories
81
# process the file $WL_CATEGORIES with the choice of categories
81
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
82
	for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
82
	do
83
	do
83
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES
84
		$SED "/\/$ENABLE_CATEGORIE$/d" $WL_CATEGORIES
84
		$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
85
		$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ENABLE_CATEGORIE" $WL_CATEGORIES
85
		ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
86
		ln -sf $DIR_DNS_WL/$ENABLE_CATEGORIE.conf $DIR_DNS_WL_ENABLED/$ENABLE_CATEGORIE
86
	done
87
	done
87
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
88
	sort +0.0 -0.2 $WL_CATEGORIES -o $FILE_tmp
88
	mv $FILE_tmp $WL_CATEGORIES
89
	mv $FILE_tmp $WL_CATEGORIES
89
	sort +0.0 -0.2 $WL_CATEGORIES_ENABLED -o $FILE_tmp
90
	sort +0.0 -0.2 $WL_CATEGORIES_ENABLED -o $FILE_tmp
90
	mv $FILE_tmp $WL_CATEGORIES_ENABLED
91
	mv $FILE_tmp $WL_CATEGORIES_ENABLED
91
	chown root:apache $WL_CATEGORIES $WL_CATEGORIES_ENABLED
92
	chown root:apache $WL_CATEGORIES $WL_CATEGORIES_ENABLED
92
	chmod 660 $WL_CATEGORIES $WL_CATEGORIES_ENABLED
93
	chmod 660 $WL_CATEGORIES $WL_CATEGORIES_ENABLED
93
}
94
}
94
 
95
 
95
# cleaning file and split it ("domains" in $FILE_tmp & "IP" in $FILE_ip_tmp)
96
# cleaning file and split it ("domains" in $FILE_tmp & "IP" in $FILE_ip_tmp)
96
function clean_split (){
97
function clean_split (){
97
	$SED '/^#.*/d' $FILE_tmp # remove commented lines
98
	$SED '/^#.*/d' $FILE_tmp # remove commented lines
98
	$SED '/^\s*$/d' $FILE_tmp # remove empty lines
99
	$SED '/^\s*$/d' $FILE_tmp # remove empty lines
99
	$SED '/[äâëêïîöôüû@,]/d' $FILE_tmp # remove line with "chelou" characters
100
	$SED '/[äâëêïîöôüû@,]/d' $FILE_tmp # remove line with "chelou" characters
100
	# extract ip addresses for iptables.
101
	# extract ip addresses for iptables.
101
	awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $FILE_tmp > $FILE_ip_tmp
102
	awk '/^([0-9]{1,3}\.){3}[0-9]{1,3}$/{print "add bl_ip_blocked " $0}' $FILE_tmp > $FILE_ip_tmp
102
	# extract domain names for unbound.
103
	# extract domain names for unbound.
103
	$SED -n '/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/!p' $FILE_tmp
104
	$SED -n '/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/!p' $FILE_tmp
104
	# Retrieve max Top Level Domain for domain name synthax
105
	# Retrieve max Top Level Domain for domain name synthax
105
	#MAX_TLD=$(curl http://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep -v '-' | grep -v '#' | wc -L)
106
	#MAX_TLD=$(curl http://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep -v '-' | grep -v '#' | wc -L)
106
	#if [ $(echo $MAX_TLD | wc -c) -eq 0 ];then
107
	#if [ $(echo $MAX_TLD | wc -c) -eq 0 ];then
107
	#	MAX_TLD=18
108
	#	MAX_TLD=18
108
	#fi
109
	#fi
109
	# search for correction	egrep "([a-zA-Z0-9_-.]+\.){1,2}[a-zA-Z]{2,$MAX_TLD}" $ossi_custom_dir/domains > $FILE_tmp
110
	# search for correction	egrep "([a-zA-Z0-9_-.]+\.){1,2}[a-zA-Z]{2,$MAX_TLD}" $ossi_custom_dir/domains > $FILE_tmp
110
}
111
}
111
 
112
 
112
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -update_cat or --update_cat }"
113
usage="Usage: alcasar-bl.sh { -cat_choice or --cat_choice } | { -download or --download } | { -adapt or --adapt } | { -reload or --reload } | { -install_ossi_candc or --install_ossi_candc }"
113
nb_args=$#
114
nb_args=$#
114
args=$1
115
args=$1
115
if [ $nb_args -eq 0 ]
116
if [ $nb_args -eq 0 ]
116
then
117
then
117
	args="-h"
118
	args="-h"
118
fi
119
fi
119
case $args in
120
case $args in
120
	-\? | -h* | --h*)
121
	-\? | -h* | --h*)
121
		echo "$usage"
122
		echo "$usage"
122
		exit 0
123
		exit 0
123
		;;
124
		;;
124
	# Retrieve Toulouse University BL
125
	# Retrieve Toulouse University BL
125
	-download | --download)
126
	-download | --download)
126
		rm -rf /tmp/con_ok.html
127
		rm -rf /tmp/con_ok.html
127
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
128
		`/usr/bin/curl $BL_SERVER -# -o /tmp/con_ok.html`
128
		if [ ! -e /tmp/con_ok.html ]
129
		if [ ! -e /tmp/con_ok.html ]
129
		then
130
		then
130
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
131
			echo "Erreur : le serveur de blacklist ($BL_SERVER) n'est pas joignable"
131
		else
132
		else
132
			rm -rf /tmp/con_ok.html $DIR_tmp
133
			rm -rf /tmp/con_ok.html $DIR_tmp
133
			mkdir $DIR_tmp
134
			mkdir $DIR_tmp
134
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
135
			wget -P $DIR_tmp http://$BL_SERVER/blacklists/download/blacklists.tar.gz
135
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
136
			md5sum $DIR_tmp/blacklists.tar.gz | cut -d" " -f1 > $DIR_tmp/md5sum
136
			chown -R apache:apache $DIR_tmp
137
			chown -R apache:apache $DIR_tmp
137
		fi
138
		fi
138
		;;
139
		;;
139
	# enable/disable categories (used only during the alcasar install process)
140
	# enable/disable categories (used only during the alcasar install process)
140
	-cat_choice | --cat_choice)
141
	-cat_choice | --cat_choice)
141
		cat_choice
142
		cat_choice
142
		;;
143
		;;
143
	# Adapt Toulouse University BL to ALCASAR architecture (unbound + DG + iptables)
144
	# Adapt Toulouse University BL to ALCASAR architecture (unbound + DG + iptables)
144
	-adapt | --adapt)
145
	-adapt | --adapt)
145
		echo -n "Adaptation process of Toulouse University blackList. Please wait : "
146
		echo -n "Adaptation process of Toulouse University blackList. Please wait : "
146
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
147
		if [ -f $DIR_tmp/blacklists.tar.gz ] # when downloading the last version of the BL
147
		then
148
		then
148
			# keep custom files (ossi)
149
			# keep custom files (ossi)
149
			for x in $(ls -1 $DIR_DG_BL | grep "^ossi-*")
150
			for x in $(ls -1 $DIR_DG_BL | grep "^ossi-*")
150
			do
151
			do
151
				mv $DIR_DG_BL/$x $DIR_tmp
152
				mv $DIR_DG_BL/$x $DIR_tmp
152
			done
153
			done
153
			rm -rf $DIR_DG_BL $DIR_IP_BL
154
			rm -rf $DIR_DG_BL $DIR_IP_BL
154
			mkdir $DIR_DG_BL $DIR_IP_BL
155
			mkdir $DIR_DG_BL $DIR_IP_BL
155
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
156
			tar zxf $DIR_tmp/blacklists.tar.gz --directory=$DIR_DG/
156
			# replace the global_usage file of the archive
157
			# replace the global_usage file of the archive
157
			cp -f $GLOBAL_USAGE $DIR_DG_BL/global_usage
158
			cp -f $GLOBAL_USAGE $DIR_DG_BL/global_usage
158
			chown -R e2guardian:apache $DIR_DG
159
			chown -R e2guardian:apache $DIR_DG
159
			chmod -R 770 $DIR_DG
160
			chmod -R 770 $DIR_DG
160
			# Add the two local categories (ossi-bl & ossi-wl) to the usage file
161
			# Add the two local categories (ossi-bl & ossi-wl) to the usage file
161
			# Add the custom categories (ossi-tor_nodes) to the usage file
162
			# Add the custom categories (ossi-tor_nodes) to the usage file
162
			cat <<EOF >> $DIR_DG_BL/global_usage
163
			cat <<EOF >> $DIR_DG_BL/global_usage
163
 
164
 
164
NAME: ossi-bl
165
NAME: ossi-bl
165
DEFAULT_TYPE: black
166
DEFAULT_TYPE: black
166
SOURCE: ALCASAR Team
167
SOURCE: ALCASAR Team
167
DESC FR: sites blacklistés ajoutés localement
168
DESC FR: sites blacklistés ajoutés localement
168
DESC EN: blacklisted sites add locally
169
DESC EN: blacklisted sites add locally
169
NAME FR: ossi-bl
170
NAME FR: ossi-bl
170
NAME EN: ossi-bl
171
NAME EN: ossi-bl
171
 
172
 
172
NAME: ossi-wl
173
NAME: ossi-wl
173
DEFAULT_TYPE: white
174
DEFAULT_TYPE: white
174
SOURCE: ALCASAR Team
175
SOURCE: ALCASAR Team
175
DESC FR: sites autorisés ajoutés localement
176
DESC FR: sites autorisés ajoutés localement
176
DESC EN: whitelisted sites add locally
177
DESC EN: whitelisted sites add locally
177
NAME FR: ossi-wl
178
NAME FR: ossi-wl
178
NAME EN: ossi-wl
179
NAME EN: ossi-wl
179
 
180
 
180
NAME: ossi-bl-tor_nodes
181
NAME: ossi-bl-tor_nodes
181
DEFAULT_TYPE: black
182
DEFAULT_TYPE: black
182
SOURCE: ALCASAR Team
183
SOURCE: ALCASAR Team
183
DESC FR: Adresses IP des noeuds (routeurs) d'entrée du réseau TOR
184
DESC FR: Adresses IP des noeuds (routeurs) d'entrée du réseau TOR
184
DESC EN: IP addresses of input TOR nodes (routers)
185
DESC EN: IP addresses of input TOR nodes (routers)
185
NAME FR: Noeuds TOR
186
NAME FR: Noeuds TOR
186
NAME EN: TOR nodes
187
NAME EN: TOR nodes
-
 
188
 
-
 
189
NAME: ossi-bl-ultrasurf
-
 
190
DEFAULT_TYPE: black
-
 
191
SOURCE: ALCASAR Team
-
 
192
DESC FR: Adresses IP des point de sortie ULTRASURF
-
 
193
DESC EN: IP addresses of output points of ULTRASURF
-
 
194
NAME FR: Points de sortie ULTRASURF
-
 
195
NAME EN: ULTRASURF output points
-
 
196
 
-
 
197
NAME: ossi-bl-candc
-
 
198
DEFAULT_TYPE: black
-
 
199
SOURCE: Bambenek Consulting: https://osint.bambenekconsulting.com
-
 
200
DESC FR: liste des URLs et IPs des serveurs command & control
-
 
201
DESC EN: list of URLs and IPs of command & control servers
-
 
202
NAME FR: Serveurs Command & Control
-
 
203
NAME EN: Command & Control Server
-
 
204
 
187
EOF
205
EOF
188
			# Retrieve custom files (ossi)
206
# Retrieve custom files (ossi)
189
			for x in $(ls -1 $DIR_tmp | grep "^ossi-*")
207
			for x in $(ls -1 $DIR_tmp | grep "^ossi-*")
190
			do
208
			do
191
				mv $DIR_tmp/$x $DIR_DG_BL
209
				mv $DIR_tmp/$x $DIR_DG_BL
192
			done
210
			done
193
		fi
211
		fi
194
		rm -f $BL_CATEGORIES $WL_CATEGORIES
212
		rm -f $BL_CATEGORIES $WL_CATEGORIES
195
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
213
		rm -rf $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
196
		rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
214
		rm -rf $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
197
		touch $BL_CATEGORIES $WL_CATEGORIES
215
		touch $BL_CATEGORIES $WL_CATEGORIES
198
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
216
		mkdir $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
199
		mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
217
		mkdir $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED
200
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
218
		find $DIR_DG_BL/ -type f -name domains > $FILE_tmp # retrieve directory name where a domain file exist
201
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
219
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
202
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
220
		for dir_categorie in `cat $FILE_tmp` # create the blacklist and the whitelist files
203
		do
221
		do
204
			categorie=`echo $dir_categorie|cut -d "/" -f6`
222
			categorie=`echo $dir_categorie|cut -d "/" -f6`
205
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie\$ $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
223
			categorie_type=`grep -A1 ^NAME:[$' '$'\t']*$categorie\$ $DIR_DG_BL/global_usage | grep ^DEFAULT_TYPE | cut -d":" -f2 | tr -d " \t"`
206
			if [ "$categorie_type" == "white" ]
224
			if [ "$categorie_type" == "white" ]
207
			then
225
			then
208
				echo "$dir_categorie" >> $WL_CATEGORIES
226
				echo "$dir_categorie" >> $WL_CATEGORIES
209
			else
227
			else
210
				echo "$dir_categorie" >> $BL_CATEGORIES
228
				echo "$dir_categorie" >> $BL_CATEGORIES
211
			fi
229
			fi
212
		done
230
		done
213
		rm -f $FILE_tmp
231
		rm -f $FILE_tmp
214
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
232
		# Verify that the enabled categories are effectively in the BL (need after an update of the BL)
215
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
233
		for ENABLE_CATEGORIE in `cat $BL_CATEGORIES_ENABLED`
216
		do
234
		do
217
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
235
			ok=`grep /$ENABLE_CATEGORIE$ $BL_CATEGORIES|wc -l`
218
			if [ $ok != "1" ]
236
			if [ $ok != "1" ]
219
			then
237
			then
220
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
238
				$SED "/^$ENABLE_CATEGORIE$/d" $BL_CATEGORIES_ENABLED
221
			fi
239
			fi
222
		done
240
		done
223
		# Verify that the enabled categories are effectively in the WL (need after an update of the WL)
241
		# Verify that the enabled categories are effectively in the WL (need after an update of the WL)
224
		for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
242
		for ENABLE_CATEGORIE in `cat $WL_CATEGORIES_ENABLED`
225
		do
243
		do
226
			ok=`grep /$ENABLE_CATEGORIE$ $WL_CATEGORIES|wc -l`
244
			ok=`grep /$ENABLE_CATEGORIE$ $WL_CATEGORIES|wc -l`
227
			if [ $ok != "1" ]
245
			if [ $ok != "1" ]
228
			then
246
			then
229
				$SED "/^$ENABLE_CATEGORIE$/d" $WL_CATEGORIES_ENABLED
247
				$SED "/^$ENABLE_CATEGORIE$/d" $WL_CATEGORIES_ENABLED
230
			fi
248
			fi
231
		done
249
		done
232
 
250
 
233
		# Creation of Unbound and Iptables BL and WL
251
		# Creation of Unbound and Iptables BL and WL
234
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
252
		for LIST in $BL_CATEGORIES $WL_CATEGORIES	# for each list (bl and wl)
235
		do
253
		do
236
			for PATH_FILE in `cat $LIST` # for each category
254
			for PATH_FILE in `cat $LIST` # for each category
237
			do
255
			do
238
				DOMAIN=`basename $PATH_FILE`
256
				DOMAIN=`basename $PATH_FILE`
239
				echo -n "$DOMAIN, "
257
				echo -n "$DOMAIN, "
240
				if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
258
				if [ ! -f $PATH_FILE/urls ] # create 'urls' file if it doesn't exist
241
				then
259
				then
242
					touch $PATH_FILE/urls
260
					touch $PATH_FILE/urls
243
					chown e2guardian:apache $PATH_FILE/urls
261
					chown e2guardian:apache $PATH_FILE/urls
244
				fi
262
				fi
245
				cp $PATH_FILE/domains $FILE_tmp
263
				cp $PATH_FILE/domains $FILE_tmp
246
				clean_split # clean ossi custom files & split them for unbound and for iptables
264
				clean_split # clean ossi custom files & split them for unbound and for iptables
247
				if [ "$LIST" == "$BL_CATEGORIES" ]
265
				if [ "$LIST" == "$BL_CATEGORIES" ]
248
				then
266
				then
249
					# adapt to the unbound syntax for the blacklist
267
					# adapt to the unbound syntax for the blacklist
250
					$SED "s?.*?local-zone: & typetransparent\nlocal-zone-tag: & blacklist?g" $FILE_tmp
268
					$SED "s?.*?local-zone: & typetransparent\nlocal-zone-tag: & blacklist?g" $FILE_tmp
251
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
269
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
252
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAIN
270
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAIN
253
				else
271
				else
254
					# adapt to the unbound syntax for the whitelist
272
					# adapt to the unbound syntax for the whitelist
255
					$SED "s?.*?local-zone: & transparent?g" $FILE_tmp
273
					$SED "s?.*?local-zone: & transparent?g" $FILE_tmp
256
					mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
274
					mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
257
				fi
275
				fi
258
			done
276
			done
259
		done
277
		done
260
		echo
278
		echo
261
		chown -R root:apache $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
279
		chown -R root:apache $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
262
		chmod 770 $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
280
		chmod 770 $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
263
		chmod -f 660 $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
281
		chmod -f 660 $BL_CATEGORIES $WL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES_ENABLED $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
264
		rm -f $FILE_tmp $FILE_ip_tmp
282
		rm -f $FILE_tmp $FILE_ip_tmp
265
		rm -rf $DIR_tmp
283
		rm -rf $DIR_tmp
266
		;;
284
		;;
267
	# Rsync the categories which are listed in "/usr/local/etc/update_cat.conf". Cron run this function every 12h
-
 
268
	-update_cat | --update_cat)
285
    # install C&C-Server Blacklist.
269
		if [ $(cat /usr/local/etc/update_cat.conf | wc -l) -ne 0 ]
-
 
270
		then
-
 
271
			echo -n "Updating categories in /usr/local/etc/update_cat.conf ..."
-
 
272
			cat /usr/local/etc/update_cat.conf | while read LIGNE_RSYNC
-
 
273
			do
-
 
274
				CATEGORIE=$(echo $LIGNE_RSYNC | cut -d' ' -f1)
-
 
275
				URL=$(echo $LIGNE_RSYNC | cut -d' ' -f2)
-
 
276
				PATH_FILE=$(find $DIR_DG_BL/ -type d -name $CATEGORIE) # retrieve directory name of the category
-
 
277
				rsync -rv $URL $(dirname $PATH_FILE ) #rsync inside of the blacklist directory
-
 
278
				# Creation of unbound and Iptables BL and WL
286
    -install_ossi_candc | --install_ossi_candc)
279
				DOMAIN=$(basename $PATH_FILE)
-
 
280
				cp $PATH_FILE/domains $FILE_tmp
-
 
281
				clean_split  # clean ossi custom files & split them for unbound and for iptables
-
 
282
				black=`grep black $PATH_FILE/usage |wc -l`
-
 
283
				if [ $black == "1" ]
287
        ## download
284
				then
-
 
285
					# adapt to the unbound syntax for the blacklist
288
        /usr/local/bin/alcasar-bl-autoupdate.sh --update_ossi-bl-candc
286
					$SED "s?.*?local-zone: & typetransparent\nlocal-zone-tag: & blacklist?g" $FILE_tmp
289
        ## enable blacklist by default by creating symlinks
287
					mv $FILE_tmp $DIR_DNS_BL/$DOMAIN.conf
290
        if [ -e ${DIR_DNS_BL_ENABLED}/${CNC_BL_NAME} ]; then
288
					mv $FILE_ip_tmp $DIR_IP_BL/$DOMAIN
291
		    rm ${DIR_DNS_BL_ENABLED}/${CNC_BL_NAME}
289
				else
292
	    fi
290
					# adapt to the unbound syntax for the whitelist
-
 
291
					$SED "s?.*?local-zone: & transparent?g" $FILE_tmp
-
 
292
					mv $FILE_tmp $DIR_DNS_WL/$DOMAIN.conf
293
	    if [ -e ${DIR_IP_BL_ENABLED}/${CNC_BL_NAME} ]; then
293
					mv $FILE_ip_tmp $DIR_IP_WL/$DOMAIN
294
		    rm ${DIR_IP_BL_ENABLED}/${CNC_BL_NAME}
294
				fi
295
	    fi
295
				rm -f $FILE_tmp $FILE_ip_tmp
-
 
296
			done
-
 
297
			/usr/bin/systemctl restart unbound-whitelist
-
 
298
			/usr/bin/systemctl restart dnsmasq-whitelist
-
 
299
			/usr/bin/systemctl restart unbound-blacklist
296
 		echo "Creating symlinks to enable blacklist"
300
			/usr/bin/systemctl restart e2guardian
297
        ln -s ${DIR_DNS_BL}/${CNC_BL_NAME}.conf ${DIR_DNS_BL_ENABLED}/${CNC_BL_NAME}
301
			/usr/local/bin/alcasar-iptables.sh
-
 
302
		else
-
 
303
			echo -n "/usr/local/etc/update_cat.conf is empty ..."
298
        ln -s ${DIR_IP_BL}/${CNC_BL_NAME} ${DIR_IP_BL_ENABLED}/${CNC_BL_NAME}
304
		fi
-
 
305
		echo
299
		exit 0
306
		;;
300
		;;
307
	# reload when selected categories are changed or when ossi change his custom files
301
	# reload when selected categories are changed or when ossi change his custom files
308
	-reload | --reload)
302
	-reload | --reload)
309
		# for DG
303
		# for E2Gardian
310
		cat_choice
304
		cat_choice
311
		#  for unbound (rehabilitated domain names)
305
		#  for unbound (rehabilitated domain names)
312
		rm -f $REHABILITATED_DNS_FILE
306
		rm -f $REHABILITATED_DNS_FILE
313
		if [ "$(wc -w $DIR_DG/exceptionsitelist | cut -d " " -f1)" != "0" ]
307
		if [ "$(wc -w $DIR_DG/exceptionsitelist | cut -d " " -f1)" != "0" ]
314
		then
308
		then
315
			touch $REHABILITATED_DNS_FILE
309
			touch $REHABILITATED_DNS_FILE
316
			while read -r domain; do
310
			while read -r domain; do
317
				[ -z "$domain" ] && continue
311
				[ -z "$domain" ] && continue
318
				echo "local-zone: $domain typetransparent" >> $REHABILITATED_DNS_FILE
312
				echo "local-zone: $domain typetransparent" >> $REHABILITATED_DNS_FILE
319
				echo "local-zone-tag: $domain \"\"" >> $REHABILITATED_DNS_FILE
313
				echo "local-zone-tag: $domain \"\"" >> $REHABILITATED_DNS_FILE
320
			done < $DIR_DG/exceptionsitelist
314
			done < $DIR_DG/exceptionsitelist
321
		fi
315
		fi
322
		# adapt OSSI BL & WL custom files
316
		# adapt OSSI BL & WL custom files
323
		for dir in $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
317
		for dir in $DIR_DNS_BL_ENABLED $DIR_DNS_WL_ENABLED $DIR_IP_BL_ENABLED $DIR_IP_WL_ENABLED $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
324
		do
318
		do
325
			rm -f $dir/ossi*
319
			rm -f $dir/ossi*
326
		done
320
		done
327
		find $DIR_DG_BL/ -type f -name domains | grep ossi- > $FILE_tmp # retrieve ossi directories name where a domain file exist
321
		find $DIR_DG_BL/ -type f -name domains | grep ossi- > $FILE_tmp # retrieve ossi directories name where a domain file exist
328
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
322
		$SED "s?\/domains??g" $FILE_tmp # remove "/domains" suffix
329
		for ossi_custom_dir in `cat $FILE_tmp` # create the blacklist and the whitelist files
323
		for ossi_custom_dir in `cat $FILE_tmp` # create the blacklist and the whitelist files
330
		do
324
		do
331
			chown -R root:apache $ossi_custom_dir
325
			chown -R root:apache $ossi_custom_dir
332
			chmod 770 $ossi_custom_dir
326
			chmod 770 $ossi_custom_dir
333
			ossi_categorie=`echo $ossi_custom_dir|cut -d "/" -f6`
327
			ossi_categorie=`echo $ossi_custom_dir|cut -d "/" -f6`
334
			short_categorie=`echo "$ossi_categorie" | cut -d"-" -f2`
328
			short_categorie=`echo "$ossi_categorie" | cut -d"-" -f2`
335
			if [ $short_categorie == "bl" ]
329
			if [ $short_categorie == "bl" ]
336
			then
330
			then
337
				categorie_type="black"
331
				categorie_type="black"
338
			else
332
			else
339
				categorie_type="white"
333
				categorie_type="white"
340
			fi
334
			fi
341
			$SED "s/\r//" $ossi_custom_dir/domains $ossi_custom_dir/urls # remove Windows <CR> from custom file
335
			$SED "s/\r//" $ossi_custom_dir/domains $ossi_custom_dir/urls # remove Windows <CR> from custom file
342
			cp $ossi_custom_dir/domains $FILE_tmp
336
			cp $ossi_custom_dir/domains $FILE_tmp
343
			clean_split # clean ossi custom files & split them for unbound and for iptables
337
			clean_split # clean ossi custom files & split them for unbound and for iptables
344
			if [ $categorie_type == "white" ]
338
			if [ $categorie_type == "white" ]
345
			then
339
			then
346
			# adapt the file to the unbound syntax and enable it if needed
340
			# adapt the file to the unbound syntax and enable it if needed
347
			# for the WL
341
			# for the WL
348
				$SED "s?.*?local-zone: & transparent?g" $FILE_tmp
342
				$SED "s?.*?local-zone: & transparent?g" $FILE_tmp
349
				mv $FILE_tmp $DIR_DNS_WL/$ossi_categorie.conf
343
				mv $FILE_tmp $DIR_DNS_WL/$ossi_categorie.conf
350
				mv $FILE_ip_tmp $DIR_IP_WL/$ossi_categorie
344
				mv $FILE_ip_tmp $DIR_IP_WL/$ossi_categorie
351
				enabled=`grep ^$ossi_categorie$ $WL_CATEGORIES_ENABLED | wc -l`
345
				enabled=`grep ^$ossi_categorie$ $WL_CATEGORIES_ENABLED | wc -l`
352
				if [ $enabled == "1" ]
346
				if [ $enabled == "1" ]
353
				then
347
				then
354
					$SED "/\/$ossi_categorie$/d" $WL_CATEGORIES
348
					$SED "/\/$ossi_categorie$/d" $WL_CATEGORIES
355
					$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ossi_categorie" $WL_CATEGORIES
349
					$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ossi_categorie" $WL_CATEGORIES
356
					ln -sf $DIR_DNS_WL/$ossi_categorie.conf $DIR_DNS_WL_ENABLED/$ossi_categorie
350
					ln -sf $DIR_DNS_WL/$ossi_categorie.conf $DIR_DNS_WL_ENABLED/$ossi_categorie
357
					ln -sf $DIR_IP_WL/$ossi_categorie $DIR_IP_WL_ENABLED/$ossi_categorie
351
					ln -sf $DIR_IP_WL/$ossi_categorie $DIR_IP_WL_ENABLED/$ossi_categorie
358
				fi
352
				fi
359
			else
353
			else
360
			# for the BL
354
			# for the BL
361
				$SED "s?.*?local-zone: & typetransparent\nlocal-zone-tag: & blacklist?g" $FILE_tmp
355
				$SED "s?.*?local-zone: & typetransparent\nlocal-zone-tag: & blacklist?g" $FILE_tmp
362
				mv $FILE_tmp $DIR_DNS_BL/$ossi_categorie.conf
356
				mv $FILE_tmp $DIR_DNS_BL/$ossi_categorie.conf
363
				mv $FILE_ip_tmp $DIR_IP_BL/$ossi_categorie
357
				mv $FILE_ip_tmp $DIR_IP_BL/$ossi_categorie
364
				enabled=`grep ^$ossi_categorie$ $BL_CATEGORIES_ENABLED | wc -l`
358
				enabled=`grep ^$ossi_categorie$ $BL_CATEGORIES_ENABLED | wc -l`
365
				if [ $enabled == "1" ]
359
				if [ $enabled == "1" ]
366
				then
360
				then
367
					$SED "/\/$ossi_categorie$/d" $BL_CATEGORIES
361
					$SED "/\/$ossi_categorie$/d" $BL_CATEGORIES
368
					$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ossi_categorie" $BL_CATEGORIES
362
					$SED "1i\/etc\/e2guardian\/lists\/blacklists\/$ossi_categorie" $BL_CATEGORIES
369
					ln -sf $DIR_DNS_BL/$ossi_categorie.conf $DIR_DNS_BL_ENABLED/$ossi_categorie
363
					ln -sf $DIR_DNS_BL/$ossi_categorie.conf $DIR_DNS_BL_ENABLED/$ossi_categorie
370
					ln -sf $DIR_IP_BL/$ossi_categorie $DIR_IP_BL_ENABLED/$ossi_categorie
364
					ln -sf $DIR_IP_BL/$ossi_categorie $DIR_IP_BL_ENABLED/$ossi_categorie
371
				fi
365
				fi
372
			fi
366
			fi
373
		done
367
		done
374
		for file in $BL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES $WL_CATEGORIES_ENABLED
368
		for file in $BL_CATEGORIES $BL_CATEGORIES_ENABLED $WL_CATEGORIES $WL_CATEGORIES_ENABLED
375
		do
369
		do
376
			sort +0.0 -0.2 $file -o $FILE_tmp
370
			sort +0.0 -0.2 $file -o $FILE_tmp
377
			mv $FILE_tmp $file
371
			mv $FILE_tmp $file
378
			chown root:apache $file
372
			chown root:apache $file
379
			chmod 660 $file
373
			chmod 660 $file
380
		done
374
		done
381
		chown -R root:apache $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
375
		chown -R root:apache $DIR_DNS_BL $DIR_DNS_WL $DIR_IP_BL $DIR_IP_WL
382
		chmod 660 $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
376
		chmod 660 $DIR_DNS_BL/* $DIR_DNS_WL/* $DIR_IP_BL/* $DIR_IP_WL/*
383
		if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
377
		if [ "$PARENT_SCRIPT" != "alcasar-conf.sh" ] # don't launch on install stage
384
		then
378
		then
385
			/usr/bin/systemctl restart unbound-blacklist
379
			/usr/bin/systemctl restart unbound-blacklist
386
			/usr/bin/systemctl restart unbound-whitelist
380
			/usr/bin/systemctl restart unbound-whitelist
387
			/usr/bin/systemctl restart dnsmasq-whitelist
381
			/usr/bin/systemctl restart dnsmasq-whitelist
388
			/usr/bin/systemctl restart e2guardian
382
			/usr/bin/systemctl restart e2guardian
389
			/usr/local/bin/alcasar-iptables.sh
383
			/usr/local/bin/alcasar-iptables.sh
390
		fi
384
		fi
391
		;;
385
		;;
392
	*)
386
	*)
393
		echo "Argument inconnu :$1";
387
		echo "Argument inconnu :$1";
394
		echo "$usage"
388
		echo "$usage"
395
		exit 1
389
		exit 1
396
		;;
390
		;;
397
esac
391
esac
398
 
392