Subversion Repositories ALCASAR

Rev

Rev 3043 | Rev 3046 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
872 richard 1
#!/bin/bash
64 franck 2
# $Id: alcasar-iptables.sh 3044 2022-07-24 21:55:18Z rexy $
675 richard 3
# Script de mise en place des regles du parefeu d'Alcasar (mode normal)
1339 richard 4
# This script writes the netfilter rules for ALCASAR
675 richard 5
# Rexy - 3abtux - CPN
6
#
7
# Reminders
1221 richard 8
# There are four channels for log :
1294 richard 9
#	1 tracability of the consultation equipment with The 'Netflow' kernel module (iptables target = NETFLOW);
2454 tom.houday 10
#	2 protection of ALCASAR with the Ulog group 1 (default group)
1294 richard 11
#	3 SSH on ALCASAR with the Ulog group 2;
12
#	4 extern access attempts on ALCASAR with the Ulog group 3.
2454 tom.houday 13
# The bootps/dhcp (67) port is always open on tun0/INTIF by coova
1469 richard 14
CONF_FILE="/usr/local/etc/alcasar.conf"
1587 richard 15
EXTIF=`grep ^EXTIF= $CONF_FILE|cut -d"=" -f2`				# EXTernal InterFace
16
INTIF=`grep ^INTIF= $CONF_FILE|cut -d"=" -f2`				# INTernal InterFace
17
TUNIF="tun0"								# listen device for chilli daemon
1469 richard 18
private_ip_mask=`grep ^PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
615 richard 19
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
783 richard 20
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1`			# ALCASAR LAN IP address
604 richard 21
private_network=`/bin/ipcalc -n $private_ip_mask|cut -d"=" -f2`		# LAN IP address (ie.: 192.168.182.0)
22
private_prefix=`/bin/ipcalc -p $private_ip_mask|cut -d"=" -f2`		# LAN prefix (ie. 24)
3043 rexy 23
PRIVATE_NETWORK_MASK=$private_network/$private_prefix			# LAN IP address + prefix (192.168.182.0/24)
1469 richard 24
public_ip_mask=`grep ^PUBLIC_IP= $CONF_FILE|cut -d"=" -f2`		# ALCASAR WAN IP address
1585 richard 25
if [[ "$public_ip_mask" == "dhcp" ]]
26
then
27
	PTN="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/([012]?[0-9]|3[0-2])\b"
1587 richard 28
	public_ip_mask=`ip addr show $EXTIF | egrep -o $PTN`
1585 richard 29
fi
783 richard 30
PUBLIC_IP=`echo $public_ip_mask | cut -d"/" -f1`
1588 richard 31
dns1=`grep ^DNS1= $CONF_FILE|cut -d"=" -f2`
32
dns2=`grep ^DNS2= $CONF_FILE|cut -d"=" -f2`
615 richard 33
dns1=${dns1:=208.67.220.220}
34
dns2=${dns2:=208.67.222.222}
1585 richard 35
DNSSERVERS="$dns1,$dns2"						# first and second public DNS servers
2688 lucas.echa 36
INT_DNS_IP=`grep INT_DNS_IP $CONF_FILE|cut -d"=" -f2`			# Adresse du serveur DNS interne
37
INT_DNS_ACTIVE=`grep INT_DNS_ACTIVE $CONF_FILE|cut -d"=" -f2`	# Activation de la redirection DNS interne
1332 richard 38
BL_IP_CAT="/usr/local/share/iptables-bl-enabled"			# categories files of the BlackListed IP
1932 richard 39
WL_IP_CAT="/usr/local/share/iptables-wl-enabled"			# categories files of the WhiteListed IP
2454 tom.houday 40
TMP_users_set_save="/tmp/users_set_save"				# tmp file for backup users set
1364 richard 41
TMP_set_save="/tmp/ipset_save"						# tmp file for blacklist and whitelist creation
2956 rexy 42
TMP_ip_gw_save="/tmp/ipset_ip_gw_save"				# tmp file for already connected ips
3042 rexy 43
SSH_LAN=`grep ^SSH_LAN= $CONF_FILE|cut -d"=" -f2`			# SSH LAN port
44
SSH_LAN=${SSH_LAN:=0}
3043 rexy 45
SSH_WAN=`grep ^SSH_WAN= $CONF_FILE|cut -d"=" -f2`		# SSH WAN port
3042 rexy 46
SSH_WAN=${SSH_WAN:=0}
47
SSH_WAN_ADMIN_FROM=`grep ^SSH_ADMIN_FROM= $CONF_FILE|cut -d"=" -f2|cut -d"/" -f2`
48
SSH_WAN_ADMIN_FROM=${SSH_WAN_ADMIN_FROM:="0.0.0.0"}
49
SSH_WAN_ADMIN_FROM=$([ "$SSH_WAN_ADMIN_FROM" == "0.0.0.0" ] && echo "0.0.0.0/0" || echo "$SSH_WAN_ADMIN_FROM" )
50
SSH_LAN_ADMIN_FROM=`grep ^SSH_ADMIN_FROM= $CONF_FILE|cut -d"=" -f2|cut -d"/" -f1`
51
SSH_LAN_ADMIN_FROM=${SSH_LAN_ADMIN_FROM:="0.0.0.0"}
52
SSH_LAN_ADMIN_FROM=$([ "$SSH_LAN_ADMIN_FROM" == "0.0.0.0" ] && echo "$PRIVATE_NETWORK_MASK" || echo "$SSH_LAN_ADMIN_FROM" )
612 richard 53
IPTABLES="/sbin/iptables"
3043 rexy 54
REHABILITED_IP="/etc/e2guardian/lists/exceptioniplist"
55
ALLOWED_SITES="/usr/local/etc/alcasar-site-direct"			# WEB Sites allowed for all (no av and no filtering for av_bl users)
2956 rexy 56
MULTIWAN=`grep ^MULTIWAN $CONF_FILE|cut -d"=" -f2`
57
PROXY=`grep ^PROXY= $CONF_FILE|cut -d"=" -f2`
58
PROXY_IP=`grep ^PROXY_IP= $CONF_FILE|cut -d"=" -f2`
59
nb_gw=`grep ^WAN $CONF_FILE|wc -l`
1 root 60
 
2688 lucas.echa 61
# Allow requests to internal DNS if activated
62
if [ "$INT_DNS_ACTIVE" = "on" ]
63
then
64
	DNSSERVERS="$DNSSERVERS,$INT_DNS_IP"
65
fi
66
 
2956 rexy 67
#ipset name list for load_balancing
68
gw_list="gw0"
69
if [ "$MULTIWAN" == "on" ] || [ "$MULTIWAN" == "On" ]; then
70
	for ((i=1 ; i<=$nb_gw ; i++)); do
71
		gw_list="${gw_list} gw$i"
72
	done
73
fi
74
 
1364 richard 75
# Sauvegarde des SET des utilisateurs connectés si ils existent
76
# Saving SET of connected users if it exists
1867 raphael.pi 77
ipset list not_filtered 1>/dev/null 2>&1
1364 richard 78
if [ $? -eq 0 ];
79
then
1867 raphael.pi 80
	ipset save not_filtered > $TMP_users_set_save
2841 rexy 81
	ipset save av >> $TMP_users_set_save
82
	ipset save av_bl >> $TMP_users_set_save
83
	ipset save av_wl >> $TMP_users_set_save
2006 raphael.pi 84
	ipset save proto_0 >> $TMP_users_set_save
85
	ipset save proto_1 >> $TMP_users_set_save
86
	ipset save proto_2 >> $TMP_users_set_save
87
	ipset save proto_3 >> $TMP_users_set_save
1364 richard 88
fi
89
 
2956 rexy 90
# Sauvegarde de la liste de toutes les IP déjà connectées pour les réintégrer dans le load balancing
91
# Saving all of the already connected IP in order to put them back in the load balancing after
92
if [ ! -f $TMP_ip_gw_save ];then
93
	# Save only if alcasar-network.sh --save has not been executed before
94
	for i in $gw_list;do
95
		ipset list $i 1>/dev/null 2>&1
96
		if [ $? -eq 0 ]
97
		then
98
			# the cut -d":" -f5 deletes all the lines with a :, i.e all the lines execpt the members
99
			ipset list $i | cut -d":" -f5 | sed '/^[[:space:]]*$/d' >> $TMP_ip_gw_save
100
		fi
101
	done
102
fi
103
 
2668 rexy 104
# Chargement de la sonde NetFlow (module noyau ipt_NETFLOW)
1291 richard 105
# loading of NetFlow probe (ipt_NETFLOW kernel module)
1159 crox53 106
modprobe ipt_NETFLOW destination=127.0.0.1:2055
107
 
498 richard 108
# Effacement des règles existantes
476 richard 109
# Flush all existing rules
1 root 110
$IPTABLES -F
111
$IPTABLES -t nat -F
112
$IPTABLES -t mangle -F
113
$IPTABLES -F INPUT
114
$IPTABLES -F FORWARD
115
$IPTABLES -F OUTPUT
116
 
498 richard 117
# Suppression des chaines utilisateurs sur les tables filter et nat
118
# Flush non default rules on filter and nat tables
119
$IPTABLES -X
120
$IPTABLES -t nat -X
121
 
122
# Stratégies par défaut
476 richard 123
# Default policies
1 root 124
$IPTABLES -P INPUT DROP
125
$IPTABLES -P FORWARD DROP
498 richard 126
$IPTABLES -P OUTPUT DROP
1 root 127
$IPTABLES -t nat -P PREROUTING ACCEPT
128
$IPTABLES -t nat -P POSTROUTING ACCEPT
129
$IPTABLES -t nat -P OUTPUT ACCEPT
130
 
1484 richard 131
#############################
132
#          IPSET            #
133
#############################
1291 richard 134
# destruction de tous les SET
1332 richard 135
# destroy all SET
1876 raphael.pi 136
ipset flush
1291 richard 137
ipset destroy
1855 raphael.pi 138
 
1484 richard 139
###### BL set  ###########
140
# Calcul de la taille / Compute the length
1932 richard 141
bl_set_length=$(wc -l $BL_IP_CAT/* | awk '{print $1}' | tail -n 1)
1484 richard 142
# Chargement / loading
1867 raphael.pi 143
echo "create bl_ip_blocked hash:net family inet hashsize 1024 maxelem $bl_set_length" > $TMP_set_save
1393 richard 144
for category in `ls -1 $BL_IP_CAT | cut -d '@' -f1`
1332 richard 145
do
1364 richard 146
	cat $BL_IP_CAT/$category >> $TMP_set_save
1332 richard 147
done
1364 richard 148
ipset -! restore < $TMP_set_save
149
rm -f $TMP_set_save
1484 richard 150
# Suppression des ip réhabilitées / Removing of rehabilitated ip
3043 rexy 151
for ip in $(cat $REHABILITED_IP)
1339 richard 152
do
2688 lucas.echa 153
	ipset -q del bl_ip_blocked $ip
1339 richard 154
done
155
 
2841 rexy 156
# ipset for exception web sites (usefull for filtered users = av_bl)
2485 franck 157
ipset create site_direct hash:net hashsize 1024
3043 rexy 158
for site in $(cat $ALLOWED_SITES)
2485 franck 159
do
2688 lucas.echa 160
    ipset add site_direct $site
2485 franck 161
done
162
 
1484 richard 163
###### WL set  ###########
2688 lucas.echa 164
# taille fixe, car peuplé par dnsmasq / fixe length due to dnsmasq dynamic loading
1896 raphael.pi 165
wl_set_length=65536
1484 richard 166
# Chargement Loading
1867 raphael.pi 167
echo "create wl_ip_allowed hash:net family inet hashsize 1024 maxelem $wl_set_length" > $TMP_set_save
1852 raphael.pi 168
#get ip-wl files from ACC
1932 richard 169
for category in `ls -1 $WL_IP_CAT |cut -d '@' -f1`
1852 raphael.pi 170
do
1932 richard 171
	cat $WL_IP_CAT/$category >> $TMP_set_save
1852 raphael.pi 172
done
1364 richard 173
ipset -! restore < $TMP_set_save
174
rm -f $TMP_set_save
175
 
2454 tom.houday 176
# Restoration des SET des utilisateurs connectés si ils existent sinon création des SET
1377 richard 177
# Restoring the connected users SETs if available, otherwise creating SETs
1364 richard 178
if [ -e $TMP_users_set_save ];
179
then
180
	ipset -! restore < $TMP_users_set_save
181
	rm -f $TMP_users_set_save
182
else
2386 tom.houday 183
	ipset create not_filtered hash:ip hashsize 1024
2841 rexy 184
	ipset create av hash:ip hashsize 1024
185
	ipset create av_bl hash:ip hashsize 1024
186
	ipset create av_wl hash:ip hashsize 1024
2668 rexy 187
	# pour les filtrages de protocole par utilisateur / For network protocols filtering by user
2386 tom.houday 188
	ipset create proto_0 hash:ip hashsize 1024
189
	ipset create proto_1 hash:ip hashsize 1024
190
	ipset create proto_2 hash:ip hashsize 1024
191
	ipset create proto_3 hash:ip hashsize 1024
1364 richard 192
fi
193
 
2956 rexy 194
#ipsets for load balancing
195
for i in $gw_list; do
196
	ipset create $i hash:ip
197
done
198
cat $TMP_ip_gw_save | while read ip; do
199
	gw_min="gw0"
200
	weight=`grep ^PUBLIC_WEIGHT= $CONF_FILE | cut -d"=" -f2`
201
	already=`ipset list $gw_min | grep Number\ of\ entries: | cut -d":" -f2`
202
	#The *1000 is here to avoid working on floats in bash
203
	gw_min_value=$((1000 * $already / $weight))
204
	i=1
205
	for gw in $gw_list;do
206
		if [ "$gw" != "gw0" ]; then
207
			weight=`grep ^WAN$i= $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F ',' '{ print $2 }'`
208
			already=`ipset list $gw | grep Number\ of\ entries: | cut -d":" -f2`
209
			value=$((1000 * $already / $weight))
210
			if [ $value -lt $gw_min_value ]
211
			then
212
				gw_min_value=$value
213
				gw_min=$gw
214
			fi
215
			i=$(($i+1))
216
		fi
217
	done
218
	ipset add $gw_min $ip
219
done
220
rm -f $TMP_ip_gw_save
221
 
472 richard 222
#############################
783 richard 223
#       PREROUTING          #
472 richard 224
#############################
2840 rexy 225
# Marquage (et journalisation) des paquets qui tentent d'accéder directement aux ports d'écoute du proxy HTTP/HTTPS (E2Guardian) pour pouvoir les rejeter en INPUT
226
# Mark (and log) the direct attempts to E2guardian listen ports in order to REJECT them in INPUT rules
2841 rexy 227
# 8080 = ipset av_bl
2355 tom.houday 228
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8080 -j NFLOG --nflog-group 1 --nflog-prefix "RULE direct-proxy -- DENY "
783 richard 229
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp -m tcp --dport 8080 -j MARK --set-mark 1
2841 rexy 230
# 8090 = ipset av_wl + av
2355 tom.houday 231
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8090 -j NFLOG --nflog-group 1 --nflog-prefix "RULE direct-proxy -- DENY "
1486 richard 232
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp -m tcp --dport 8090 -j MARK --set-mark 2
2844 rexy 233
# 8443 = tranparent HTTPS for ipsets av_bl + av_wl + av (future version)
234
#$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8443 -j NFLOG --nflog-group 1 --nflog-prefix "RULE direct-proxy -- DENY "
235
#$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp -m tcp --dport 8443 -j MARK --set-mark 6
1486 richard 236
 
2840 rexy 237
# Marquage des paquets qui tentent d'accéder directement aux ports d'écoute DNS (UNBOUND) pour pouvoir les rejeter en INPUT
238
# Mark the direct attempts to DNS ports (UNBOUND) in order to REJECT them in INPUT rules
2841 rexy 239
# 54 = ipset av_bl
1486 richard 240
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp --dport 54 -j MARK --set-mark 3
1962 richard 241
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p udp --dport 54 -j MARK --set-mark 3
2841 rexy 242
# 55 = ipset av_wl
1486 richard 243
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp --dport 55 -j MARK --set-mark 4
1962 richard 244
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p udp --dport 55 -j MARK --set-mark 4
2840 rexy 245
# 56 = blackall
1486 richard 246
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp --dport 56 -j MARK --set-mark 5
1962 richard 247
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p udp --dport 56 -j MARK --set-mark 5
1472 richard 248
 
2840 rexy 249
# redirection DNS des usagers
250
# users DNS redirection
2841 rexy 251
# 54 = ipset av_bl
252
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_bl src -p udp --dport domain -j REDIRECT --to-port 54
253
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_bl src -p tcp --dport domain -j REDIRECT --to-port 54
254
# 55 = ipset av_wl
255
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_wl src -p udp --dport domain -j REDIRECT --to-port 55
256
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_wl src -p tcp --dport domain -j REDIRECT --to-port 55
2956 rexy 257
# 53 = all other users
2517 rexy 258
$IPTABLES -A PREROUTING -t nat -i $TUNIF ! -d $PRIVATE_IP -p udp --dport domain -j REDIRECT --to-port 53
259
$IPTABLES -A PREROUTING -t nat -i $TUNIF ! -d $PRIVATE_IP -p tcp --dport domain -j REDIRECT --to-port 53
260
 
2844 rexy 261
# Redirection HTTP des usagers 'av_bl' cherchant à joindre les IP de la blacklist vers ALCASAR (page 'accès interdit')
262
# Redirect HTTP of 'av_bl' users who want blacklist IP to ALCASAR ('access denied' page)
263
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_bl src -m set --match-set bl_ip_blocked dst -p tcp --dport http -j REDIRECT --to-port 80
264
 
265
# Redirection HTTP des usagers 'av_wl' cherchant à joindre les IP qui ne sont pas dans la WL vers ALCASAR (page 'accès interdit')
266
# Redirect HTTP of 'av_wl' users who want IP not in the WL to ALCASAR ('access denied' page)
267
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_wl src -m set ! --match-set wl_ip_allowed dst -p tcp --dport http -j REDIRECT --to-port 80
268
 
269
# Journalisation des usagers "av_bl + av_wl + av" (paquets SYN uniquement). Les autres protocoles sont journalisés en FORWARD par netflow.
270
# accounting of "av_bl + av_wl + av"  users (only syn packets). Other protocols are logged in FORWARD by netflow
271
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_bl src ! -d $PRIVATE_IP -p tcp --dport http -m conntrack --ctstate NEW -j NFLOG --nflog-group 1 --nflog-prefix "RULE F_http -- ACCEPT "
272
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_wl src ! -d $PRIVATE_IP -p tcp --dport http -m conntrack --ctstate NEW -j NFLOG --nflog-group 1 --nflog-prefix "RULE F_http -- ACCEPT "
273
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av src ! -d $PRIVATE_IP -p tcp --dport http -m conntrack --ctstate NEW -j NFLOG --nflog-group 1 --nflog-prefix "RULE F_http -- ACCEPT "
274
 
275
# Redirection des requêtes HTTP des usagers "av_bl + av_wl + av" vers E2guardian
276
# Redirect outbound "av_bl + av_wl +av" users HTTP requests to E2guardian
2841 rexy 277
# 8080 = ipset av_bl
2956 rexy 278
#$IPTABLES -A PREROUTING -t mangle -i $TUNIF -m set --match-set av_bl src -m set ! --match-set site_direct dst ! -d $PRIVATE_IP  -p tcp --dport http -j MARK --set-mark 200
2841 rexy 279
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_bl src -m set ! --match-set site_direct dst ! -d $PRIVATE_IP  -p tcp --dport http -j REDIRECT --to-port 8080
280
# 8090 = ipset av_wl & av
281
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_wl src ! -d $PRIVATE_IP -p tcp --dport http -j REDIRECT --to-port 8090
282
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av src ! -d $PRIVATE_IP -p tcp --dport http -j REDIRECT --to-port 8090
2840 rexy 283
 
2844 rexy 284
# Redirection des requêtes HTTPS sortantes des usagers av_bl + av_wl + av vers E2Guardian (in a future version - don't forget to set E2guardian as a tranparent HTTPS proxy)
2841 rexy 285
# Redirect outbound HTTPS requests of av_bl + av_wl + av users to E2Guardian
2844 rexy 286
#$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_bl src -m set ! --match-set site_direct dst ! -d $PRIVATE_IP  -p tcp --dport https -j REDIRECT --to-port 8443
287
#$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av_wl src -m set ! --match-set site_direct dst ! -d $PRIVATE_IP  -p tcp --dport https -j REDIRECT --to-port 8443
288
#$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set av src -m set ! --match-set site_direct dst ! -d $PRIVATE_IP  -p tcp --dport https -j REDIRECT --to-port 8443
2840 rexy 289
 
783 richard 290
# Redirection des requêtes NTP vers le serveur NTP local
291
# Redirect NTP request in local NTP server
292
$IPTABLES -A PREROUTING -t nat -i $TUNIF -s $PRIVATE_NETWORK_MASK ! -d $PRIVATE_IP -p udp --dport ntp -j REDIRECT --to-port 123
293
 
2956 rexy 294
#Récupération de la marque associée à une gw pour chaque connection
295
$IPTABLES -A PREROUTING -t mangle -j CONNMARK --restore-mark
296
 
297
if [ "$PROXY" == "on" ] || [ "$PROXY" == "On" ];then
298
	$IPTABLES -A PREROUTING -t nat -i $TUNIF ! -d $PRIVATE_IP -p tcp -m multiport --dports http,https -j DNAT --to-destination $PROXY_IP
299
fi
300
 
301
#Marquage pour le load balancing
302
if [ "$MULTIWAN" == "on" ] || [ "$MULTIWAN" == "On" ]; then
303
	temp_index=200
304
	for i in $gw_list; do
305
		$IPTABLES -A PREROUTING -t mangle -i $TUNIF -m set --match-set $i src -j MARK --set-mark $temp_index
306
		temp_index=$(($temp_index+1))
307
	done
308
fi
309
 
472 richard 310
#############################
783 richard 311
#         INPUT             #
472 richard 312
#############################
783 richard 313
# Tout passe sur loopback
314
# accept all on loopback
315
$IPTABLES -A INPUT -i lo -j ACCEPT
990 franck 316
$IPTABLES -A OUTPUT -o lo -j ACCEPT
783 richard 317
 
318
# Rejet des demandes de connexions non conformes (FIN-URG-PUSH, XMAS, NullScan, SYN-RST et NEW not SYN)
2956 rexy 319
# Drop non standard connexions (FIN-URG-PUSH, XMAS, NullScan, SYN-RST and NEW not SYN)
472 richard 320
$IPTABLES -A INPUT -p tcp --tcp-flags FIN,URG,PSH FIN,URG,PSH -j DROP
321
$IPTABLES -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
322
$IPTABLES -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
323
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
2496 tom.houday 324
$IPTABLES -A INPUT -p tcp -m tcp ! --syn -m conntrack --ctstate NEW -j DROP
498 richard 325
 
2454 tom.houday 326
# Si configéré, on autorise les réponses DHCP
1587 richard 327
# Allow DHCP answers if configured
328
public_ip_mask=`grep ^PUBLIC_IP= $CONF_FILE|cut -d"=" -f2`		# ALCASAR WAN IP address
329
if [[ "$public_ip_mask" == "dhcp" ]]
330
then
331
	$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport 68 -j ACCEPT
332
	$IPTABLES -A OUTPUT -o $EXTIF -p udp --dport 68 -j ACCEPT
333
fi
871 richard 334
# On rejette les trame en broadcast et en multicast sur EXTIF (évite leur journalisation)
2454 tom.houday 335
# Drop broadcast & multicast on EXTIF to avoid log
1629 richard 336
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
498 richard 337
 
783 richard 338
# On autorise les retours de connexions légitimes par INPUT
339
# Conntrack on INPUT
2496 tom.houday 340
$IPTABLES -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
373 richard 341
 
2840 rexy 342
# On interdit les connexions directes aux ports d'écoute d'E2Guardian. Les packets concernés ont été marqués et loggués dans la table mangle (PREROUTING)
343
# Deny direct connections on E2Guardian listen ports. The concerned paquets have been marked and logged in mangle table (PREROUTING)
2841 rexy 344
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8080 -m mark --mark 1 -j REJECT --reject-with tcp-reset # av_bl
345
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8090 -m mark --mark 2 -j REJECT --reject-with tcp-reset # av_wl + av
2844 rexy 346
#$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8443 -m mark --mark 6 -j REJECT --reject-with tcp-reset # av_bl + av_wl + av (future version)
791 richard 347
 
2840 rexy 348
# On autorise les connexions HTTP/HTTPS légitimes vers E2Guardian
349
# Allow HTTP connections to E2Guardian
2496 tom.houday 350
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p tcp --dport 8080 -m conntrack --ctstate NEW --syn -j ACCEPT
351
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p tcp --dport 8090 -m conntrack --ctstate NEW --syn -j ACCEPT
2844 rexy 352
#$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p tcp --dport 8443 -m conntrack --ctstate NEW --syn -j ACCEPT # (future version)
1486 richard 353
 
2840 rexy 354
# On interdit les connexions directes aux ports d'écoupe DNS (UNBOUND). Les packets concernés ont été marqués dans la table mangle (PREROUTING)
355
# Deny direct connections to DNS ports (UNBOUND). The concerned paquets are marked in mangle table (PREROUTING)
1962 richard 356
$IPTABLES -A INPUT -i $TUNIF -p udp --dport 54 -m mark --mark 3 -j REJECT --reject-with icmp-port-unreachable
357
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 54 -m mark --mark 3 -j REJECT --reject-with tcp-reset
358
$IPTABLES -A INPUT -i $TUNIF -p udp --dport 55 -m mark --mark 4 -j REJECT --reject-with icmp-port-unreachable
2840 rexy 359
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 55 -m mark --mark 4 -j REJECT --reject-with tcp-reset
1962 richard 360
$IPTABLES -A INPUT -i $TUNIF -p udp --dport 56 -m mark --mark 5 -j REJECT --reject-with icmp-port-unreachable
2840 rexy 361
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 56 -m mark --mark 5 -j REJECT --reject-with tcp-reset
1472 richard 362
 
2840 rexy 363
# On autorise les connexion DNS légitime
364
# Allow DNS connections
2841 rexy 365
# ipset = av_bl
783 richard 366
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport 54 -j ACCEPT
1962 richard 367
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport 54 -j ACCEPT
2841 rexy 368
# ipset = av_wl
1364 richard 369
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport 55 -j ACCEPT
1962 richard 370
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport 55 -j ACCEPT
2840 rexy 371
# blackall
1472 richard 372
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport 56 -j ACCEPT
1962 richard 373
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport 56 -j ACCEPT
1472 richard 374
 
2840 rexy 375
# On accepte l'accès aux services internes
783 richard 376
# Internal services access
2454 tom.houday 377
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport domain -j ACCEPT	# DNS
1962 richard 378
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport domain -j ACCEPT	# DNS
786 richard 379
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p icmp --icmp-type 8 -j ACCEPT	# Réponse ping # ping responce
380
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p icmp --icmp-type 0 -j ACCEPT	# Requête  ping # ping request
381
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport https -j ACCEPT	# Pages d'authentification et MCC # authentication pages and MCC
382
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport http -j ACCEPT	# Page d'avertissement filtrage # Filtering warning pages
2370 tom.houday 383
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport 3990:3991 -j ACCEPT	# Requêtes de deconnexion usagers # Users logout requests
786 richard 384
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT	# Serveur local de temps # local time server
783 richard 385
 
3042 rexy 386
# Accès au serveur SSHD si activé en LAN et WAN
387
# SSHD server access in LAN and WAN if enabled
388
if [ $SSH_LAN -gt 0 ]
520 richard 389
	then
3042 rexy 390
	$IPTABLES -A INPUT -i $TUNIF -s $SSH_LAN_ADMIN_FROM -d $PRIVATE_IP -p tcp --dport $SSH_LAN -m conntrack --ctstate NEW -j NFLOG --nflog-group 2 --nflog-prefix "RULE ssh-from-LAN -- ACCEPT"
391
	$IPTABLES -A INPUT -i $TUNIF -s $SSH_LAN_ADMIN_FROM -d $PRIVATE_IP -p tcp --dport $SSH_LAN -j ACCEPT
520 richard 392
fi
3042 rexy 393
if [ $SSH_WAN -gt 0 ]
394
	then
395
	$IPTABLES -A INPUT -i $EXTIF -s $SSH_WAN_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport $SSH_WAN -m conntrack --ctstate NEW --syn -j NFLOG --nflog-group 2 --nflog-prefix "RULE ssh-from-WAN -- ACCEPT"
3043 rexy 396
	$IPTABLES -A INPUT -i $EXTIF -s $SSH_WAN_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport $SSH_WAN -j ACCEPT
3042 rexy 397
fi
783 richard 398
 
399
# Insertion de règles locales
400
# Here, we add local rules (i.e. VPN from Internet)
401
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
2454 tom.houday 402
	. /usr/local/etc/alcasar-iptables-local.sh
783 richard 403
fi
404
 
405
# Journalisation et rejet des connexions (autres que celles autorisées) effectuées depuis le LAN
406
# Deny and log on INPUT from the LAN
2844 rexy 407
$IPTABLES -A INPUT -i $TUNIF -m conntrack --ctstate NEW -j NFLOG --nflog-group 3 --nflog-prefix "RULE rej-int -- REJECT "
783 richard 408
$IPTABLES -A INPUT -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
409
$IPTABLES -A INPUT -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
410
 
1393 richard 411
# Interdiction d'accès à INTIF (n'est utile que lorsque chilli est arrêté).
783 richard 412
# Reject INTIF access (only when chilli is down)
2844 rexy 413
$IPTABLES -A INPUT -i $INTIF -j NFLOG --nflog-group 3 --nflog-prefix "RULE Protect1 -- REJECT "
783 richard 414
$IPTABLES -A INPUT -i $INTIF -j REJECT
415
 
416
# Journalisation et rejet des connexions initiées depuis le réseau extérieur (test des effets du paramètre --limit en cours)
417
# On EXTIF, the access attempts are log in channel 2 (we should test --limit option to avoid deny of service)
2496 tom.houday 418
$IPTABLES -A INPUT -i $EXTIF -m conntrack --ctstate NEW -j NFLOG --nflog-group 3 --nflog-threshold 10 --nflog-prefix "RULE rej-ext -- DROP"
783 richard 419
 
420
#############################
421
#        FORWARD            #
422
#############################
2841 rexy 423
# Blocage des IPs du SET bl_ip_blocked pour le SET av_bl
424
# Deny IPs of the SET bl_ip_blocked for the set av_bl
425
$IPTABLES -A FORWARD -i $TUNIF -m set --match-set av_bl src -m set --match-set bl_ip_blocked dst -p icmp -j REJECT --reject-with icmp-host-prohibited
426
$IPTABLES -A FORWARD -i $TUNIF -m set --match-set av_bl src -m set --match-set bl_ip_blocked dst -p udp -j REJECT --reject-with icmp-host-prohibited
427
$IPTABLES -A FORWARD -i $TUNIF -m set --match-set av_bl src -m set --match-set bl_ip_blocked dst -p tcp -j REJECT --reject-with tcp-reset
1364 richard 428
 
2184 richard 429
# Active le suivi de session
430
# Allow Conntrack
2496 tom.houday 431
$IPTABLES -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
790 richard 432
 
2668 rexy 433
# Compute uamallowed IP (ie : IP address of equipments connected between ALCASAR and router like DMZ, own servers, etc.)
2006 raphael.pi 434
nb_uamallowed=`wc -l /usr/local/etc/alcasar-uamallowed | cut -d" "  -f1`
435
if [ $nb_uamallowed != "0" ]
436
then
2454 tom.houday 437
	while read ip_allowed_line
1488 richard 438
	do
2006 raphael.pi 439
		ip_allowed=`echo $ip_allowed_line|cut -d"\"" -f2`
2496 tom.houday 440
		$IPTABLES -A FORWARD -i $TUNIF -d $ip_allowed -m conntrack --ctstate NEW -j NETFLOW
441
		$IPTABLES -A FORWARD -i $TUNIF -d $ip_allowed -m conntrack --ctstate NEW -j ACCEPT
2006 raphael.pi 442
	done < /usr/local/etc/alcasar-uamallowed
443
fi
492 franck 444
 
2184 richard 445
# filtrage protocole par utilisateur (profile 1 : http, https)
446
# protocols filtering for users (profil 1 : http, https)
2496 tom.houday 447
	$IPTABLES -A FORWARD -i $TUNIF -m set --match-set proto_1 src -s $PRIVATE_NETWORK_MASK -p tcp -m multiport ! --dports http,https -m conntrack --ctstate NEW -j REJECT --reject-with tcp-reset
448
	$IPTABLES -A FORWARD -i $TUNIF -m set --match-set proto_1 src -s $PRIVATE_NETWORK_MASK -p udp -m multiport ! --dports http,https -m conntrack --ctstate NEW -j REJECT --reject-with icmp-port-unreachable
2006 raphael.pi 449
 
2998 rexy 450
# filtrage protocole par utilisateur (profile 2 : http https smtp pop3 pop3s imap imaps ftp sftp ssh)
451
# protocols filtering for users (profil 2 : http https smtp pop3 pop3s imap imaps ftp sftp ssh)
2006 raphael.pi 452
 
2998 rexy 453
	$IPTABLES -A FORWARD -i $TUNIF -m set --match-set proto_2 src -s $PRIVATE_NETWORK_MASK -p tcp -m multiport ! --dports smtp,http,https,pop3,pop3s,imap,imaps,ftp,ftp-data,sftp,ssh -m conntrack --ctstate NEW -j REJECT --reject-with tcp-reset
454
	$IPTABLES -A FORWARD -i $TUNIF -m set --match-set proto_2 src -s $PRIVATE_NETWORK_MASK -p udp -m multiport ! --dports smtp,http,https,pop3,pop3s,imap,imaps,ftp,ftp-data,sftp,ssh -m conntrack --ctstate NEW -j REJECT --reject-with icmp-port-unreachable
2006 raphael.pi 455
 
2184 richard 456
# filtrage protocole par utilisateur (profile 3 : personnalisable via l'ACC)
457
# protocols filtering for users (profil 3 : customized with ACC)
458
custom_tcp_protocols_list='';custom_udp_protocols_list=''
2006 raphael.pi 459
while read svc_line
460
do
461
	svc_on=`echo $svc_line|cut -b1`
462
	if [ $svc_on != "#" ]
2688 lucas.echa 463
	then
2006 raphael.pi 464
		svc_name=`echo $svc_line|cut -d" " -f1`
465
		svc_port=`echo $svc_line|cut -d" " -f2`
466
		if [ $svc_name = "icmp" ]
467
		then
2184 richard 468
			svc_icmp="on"
2006 raphael.pi 469
		else
2184 richard 470
			if [ "$custom_tcp_protocols_list" == "" ]
471
			then
472
				custom_tcp_protocols_list=$svc_port
473
			else
474
				custom_tcp_protocols_list=`echo $custom_tcp_protocols_list","$svc_port`
475
			fi
476
			udp_svc=`egrep "[[:space:]]$svc_port/udp" /etc/services|wc -l`
477
			if [ $udp_svc = "1" ] # udp service exist
478
			then
479
				if [ "$custom_udp_protocols_list" == "" ]
480
				then
481
					custom_udp_protocols_list=$svc_port
482
				else
483
					custom_udp_protocols_list=`echo $custom_udp_protocols_list","$svc_port`
484
				fi
485
			fi
1488 richard 486
		fi
2006 raphael.pi 487
	fi
488
done < /usr/local/etc/alcasar-services
2184 richard 489
	if [ "$custom_tcp_protocols_list" == "" ]
490
	then
491
		$IPTABLES -A FORWARD -i $TUNIF -m set --match-set proto_3 src -s $PRIVATE_NETWORK_MASK -j REJECT
492
	else
493
		if [ "$svc_icmp" != "on" ]
494
		then
495
			$IPTABLES -A FORWARD -i $TUNIF -m set --match-set proto_3 src -s $PRIVATE_NETWORK_MASK -p icmp -j REJECT --reject-with icmp-proto-unreachable
496
		fi
2496 tom.houday 497
		$IPTABLES -A FORWARD -i $TUNIF -m set --match-set proto_3 src -s $PRIVATE_NETWORK_MASK -p tcp -m multiport ! --dports $custom_tcp_protocols_list -m conntrack --ctstate NEW -j REJECT --reject-with tcp-reset
498
		$IPTABLES -A FORWARD -i $TUNIF -m set --match-set proto_3 src -s $PRIVATE_NETWORK_MASK -p udp -m multiport ! --dports $custom_udp_protocols_list -m conntrack --ctstate NEW -j REJECT --reject-with icmp-port-unreachable
2184 richard 499
	fi
1488 richard 500
 
2841 rexy 501
# Blocage des usagers 'av_wl' cherchant à joindre les IP qui ne sont pas dans la WL
502
# Block 'av_wl' users who want IP not in the WL
503
$IPTABLES -A FORWARD -i $TUNIF -m set --match-set av_wl src -m set ! --match-set wl_ip_allowed dst -j DROP
2674 lucas.echa 504
 
2454 tom.houday 505
# journalisation et autorisation des connections sortant du LAN
476 richard 506
# Allow forward connections with log
2496 tom.houday 507
$IPTABLES -A FORWARD -i $TUNIF -s $PRIVATE_NETWORK_MASK -m conntrack --ctstate NEW -j NETFLOW
508
$IPTABLES -A FORWARD -i $TUNIF -s $PRIVATE_NETWORK_MASK -m conntrack --ctstate NEW -j ACCEPT
1 root 509
 
783 richard 510
#############################
511
#         OUTPUT            #
512
#############################
2844 rexy 513
# On laisse tout sortir sur la carte interne (voir les règles suivantes pour la carte externe)
514
# We let everything out on INTIF (see following rules for the EXTIF)
2213 richard 515
$IPTABLES -A OUTPUT ! -o $EXTIF -j ACCEPT
520 richard 516
 
2468 richard 517
# Si configuré, on autorise les requêtes DHCP
1587 richard 518
# Allow DHCP requests if configured
519
public_ip_mask=`grep ^PUBLIC_IP= $CONF_FILE|cut -d"=" -f2`		# ALCASAR WAN IP address
520
if [[ "$public_ip_mask" == "dhcp" ]]
521
then
522
	$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport 67 -j ACCEPT
523
	$IPTABLES -A OUTPUT -o $EXTIF -p udp --dport 67 -j ACCEPT
524
fi
525
 
2454 tom.houday 526
# On autorise les requêtes DNS vers les serveurs DNS identifiés
498 richard 527
# Allow DNS requests to identified DNS servers
2496 tom.houday 528
$IPTABLES -A OUTPUT -o $EXTIF -d $DNSSERVERS -p udp --dport domain -m conntrack --ctstate NEW -j ACCEPT
783 richard 529
 
2844 rexy 530
# On autorise les requêtes HTTP et HTTPS avec log Netflow (en provenance de E2guardian)
531
# HTTP & HTTPS requests are allowed with netflow log (from E2guardian)
1159 crox53 532
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport http -j NETFLOW
498 richard 533
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport http -j ACCEPT
2956 rexy 534
#$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport https -j NETFLOW # When E2guardian will be in HTTPS transparent proxy)
2259 tom.houday 535
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport https -j ACCEPT
536
 
1862 raphael.pi 537
# On autorise les requêtes RSYNC sortantes (maj BL de Toulouse)
2468 richard 538
# RSYNC requests are allowed (update of Toulouse BL)
1862 raphael.pi 539
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport rsync -j ACCEPT
540
 
2454 tom.houday 541
# On autorise les requêtes FTP
784 richard 542
# FTP requests are allowed
1705 richard 543
modprobe nf_conntrack_ftp
2642 rexy 544
$IPTABLES -t raw -A OUTPUT -p tcp --dport ftp -j CT --helper ftp
784 richard 545
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport ftp -j ACCEPT
2496 tom.houday 546
$IPTABLES -A OUTPUT -o $EXTIF -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
784 richard 547
 
2454 tom.houday 548
# On autorise les requêtes NTP
498 richard 549
# NTP requests are allowed
550
$IPTABLES -A OUTPUT -o $EXTIF -p udp --dport ntp -j ACCEPT
783 richard 551
 
2454 tom.houday 552
# On autorise les requêtes ICMP (ping)
503 richard 553
# ICMP (ping) requests are allowed
554
$IPTABLES -A OUTPUT -o $EXTIF -p icmp --icmp-type 8 -j ACCEPT
783 richard 555
 
2468 richard 556
# On autorise les requêtes LDAP
557
# LDAP requests are allowed
2496 tom.houday 558
$IPTABLES -A OUTPUT -o $EXTIF -p tcp -m multiport --dports ldap,ldaps -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
559
$IPTABLES -A OUTPUT -o $EXTIF -p udp -m multiport --dports ldap,ldaps -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
783 richard 560
 
561
#############################
562
#       POSTROUTING         #
563
#############################
498 richard 564
# Traduction dynamique d'adresse en sortie
476 richard 565
# Dynamic NAT on EXTIF
1 root 566
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
567
 
2956 rexy 568
#Sauvegarde de la marque associée à la connexion pour le load balancing
569
$IPTABLES -A POSTROUTING -t mangle -j CONNMARK --save-mark
570
 
2495 tom.houday 571
#############################
572
#          FAIL2BAN         #
573
#############################
574
# Reload Fail2Ban
2530 lucas.echa 575
if systemctl -q is-active fail2ban; then
2899 rexy 576
	/usr/bin/fail2ban-client ping &>/dev/null && /usr/bin/fail2ban-client -q reload &>/dev/null
2530 lucas.echa 577
fi