Subversion Repositories ALCASAR

Rev

Rev 688 | Rev 726 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
675 richard 1
#!/bin/sh
64 franck 2
# $Id: alcasar-iptables.sh 694 2011-08-03 04:38:09Z franck $
675 richard 3
# Script de mise en place des regles du parefeu d'Alcasar (mode normal)
568 richard 4
# This script write the netfilter rules for ALCASAR
675 richard 5
# Rexy - 3abtux - CPN
6
#
7
# Reminders
568 richard 8
# There are three channels for log :
498 richard 9
#	1 (default) for tracability;
10
#	2 for secure admin (ssh);
11
#	3 for exterior access attempts.
568 richard 12
# The French Security Agency (ANSSI) rules was applied by 'alcasar.sh' script
675 richard 13
# The bootps/dhcp (67) port is always open on tun0/eth1 by coova 
1 root 14
 
612 richard 15
conf_file="/usr/local/etc/alcasar.conf"
16
private_ip_mask=`grep PRIVATE_IP $conf_file|cut -d"=" -f2`
615 richard 17
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
604 richard 18
private_network=`/bin/ipcalc -n $private_ip_mask|cut -d"=" -f2`		# LAN IP address (ie.: 192.168.182.0)
19
private_prefix=`/bin/ipcalc -p $private_ip_mask|cut -d"=" -f2`		# LAN prefix (ie. 24)
612 richard 20
dns1=`grep DNS1 $conf_file|cut -d"=" -f2`				# first public DNS server
615 richard 21
dns1=${dns1:=208.67.220.220}
612 richard 22
dns2=`grep DNS2 $conf_file|cut -d"=" -f2`				# second public DNS server
615 richard 23
dns2=${dns2:=208.67.222.222}
24
PROTOCOLS_FILTERING=`grep PROTOCOLS_FILTERING $conf_file|cut -d"=" -f2`	# Network protocols filter (on/off)
25
PROTOCOLS_FILTERING=${PROTOCOLS_FILTERING:=off}
26
DNS_FILTERING=`grep DNS_FILTERING $conf_file|cut -d"=" -f2`		# DNS and URLs filter (on/off)
27
DNS_FILTERING=${DNS_FILTERING:=off}
28
QOS=`grep QOS $conf_file|cut -d"=" -f2`					# QOS (on/off)
29
QOS=${QOS:=off}
30
SSH=`grep SSH $conf_file|cut -d"=" -f2`					# sshd active (on/off)
31
SSH=${SSH:=off}
694 franck 32
Admin_from_IP=${Admin_from_IP:="0.0.0.0/0.0.0.0"}			# Une @IP fixe peut-être fournie pour restreindre l'accès en ssh depuis l'extérieur (ex: 80.22.21.53/24) ( 0.0.0.0/0.0.0.0  = de n'importe où ! )
615 richard 33
LDAP=`grep LDAP $conf_file|cut -d"=" -f2`				# ldap external server active (on/off)
34
LDAP=${LDAP:=off}
694 franck 35
LDAP_IP=${LDAP_IP:="0.0.0.0"}
612 richard 36
PRIVATE_NETWORK_MASK=$private_network/$private_prefix			# Lan IP address + prefix (192.168.182.0/24)
37
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1`			# ALCASAR LAN IP address
38
DNSSERVERS="$dns1,$dns2"						# first and second DNS IP servers addresses
675 richard 39
EXTIF="eth0"
1 root 40
INTIF="eth1"
604 richard 41
TUNIF="tun0"								# listen card for chilli daemon
612 richard 42
IPTABLES="/sbin/iptables"
1 root 43
 
498 richard 44
# Effacement des règles existantes
476 richard 45
# Flush all existing rules
1 root 46
$IPTABLES -F
47
$IPTABLES -t nat -F
48
$IPTABLES -t mangle -F
49
$IPTABLES -F INPUT
50
$IPTABLES -F FORWARD
51
$IPTABLES -F OUTPUT
52
 
498 richard 53
# Suppression des chaines utilisateurs sur les tables filter et nat
54
# Flush non default rules on filter and nat tables
55
$IPTABLES -X
56
$IPTABLES -t nat -X
57
 
58
# Stratégies par défaut
476 richard 59
# Default policies
1 root 60
$IPTABLES -P INPUT DROP
61
$IPTABLES -P FORWARD DROP
498 richard 62
$IPTABLES -P OUTPUT DROP
1 root 63
$IPTABLES -t nat -P PREROUTING ACCEPT
64
$IPTABLES -t nat -P POSTROUTING ACCEPT
65
$IPTABLES -t nat -P OUTPUT ACCEPT
66
 
498 richard 67
# Tout passe sur loopback
476 richard 68
# accept all on loopback
1 root 69
$IPTABLES -A INPUT -i lo -j ACCEPT
492 franck 70
 
472 richard 71
#############################
72
#       INTIF rules         #
73
#############################
675 richard 74
# interdit l'accès à INTIF (n'est utile que lorsque chilli est arrêté).
75
# Reject INTIF access (only when chilli is down)
1 root 76
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE Protect1 -- REJECT "
77
$IPTABLES -A INPUT -i $INTIF -j REJECT
78
 
472 richard 79
#############################
80
#  Local protection rules   #
81
#############################
675 richard 82
# On stoppe les demande de connexions non conformes (NullScan, XMAS (tous flags à 1), NEW not SYN, etc.)
83
# Drop non standard connexions (NULLscans, XMAS, "NEW not SYN", etc.) 
472 richard 84
$IPTABLES -A INPUT -p tcp --tcp-flags FIN,URG,PSH FIN,URG,PSH -j DROP
85
$IPTABLES -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
86
$IPTABLES -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
87
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
675 richard 88
$IPTABLES -A INPUT -p tcp -m tcp ! --syn -m state --state NEW -j DROP
498 richard 89
 
675 richard 90
# On ne traite pas les broadcasts et multicast
476 richard 91
# Drop broadcast & multicast
472 richard 92
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
498 richard 93
 
94
# Rejet des tentatives de création de tunnels DNS (même pour les utilisateurs authentifiés)  
476 richard 95
# Deny forward DNS (even for authenticated users ...)
373 richard 96
$IPTABLES -A FORWARD -i $TUNIF -p udp --dport domain -j REJECT --reject-with icmp-port-unreachable
97
$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport domain -j REJECT --reject-with tcp-reset
98
 
498 richard 99
# On autorise les retours de connexions légitimes par FORWARD
476 richard 100
# Conntrack on forward
373 richard 101
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
102
 
520 richard 103
###############################
104
#  If DNS filter is activate  #
105
###############################
106
# Redirection des flux DNS vers le port 54 (dns+blackhole) sauf pour les IP en exceptions 
612 richard 107
if [ $DNS_FILTERING = on ]; then
520 richard 108
	# Compute exception IP
688 richard 109
	nb_exceptions=`wc -l /usr/local/etc/alcasar-filter-exceptions | cut -d" " -f1`
520 richard 110
	if [ $nb_exceptions != "0" ]
111
	then
112
		while read ip_exception 
113
		do
114
			$IPTABLES -A PREROUTING -t nat -i $TUNIF -p udp -s $ip_exception -d $PRIVATE_IP --dport domain -j ACCEPT
115
		done < /usr/local/etc/alcasar-filter-exceptions
116
	fi
117
		$IPTABLES -A PREROUTING -t nat -i $TUNIF -p udp -d $PRIVATE_IP --dport domain -j REDIRECT --to-port 54
118
fi
476 richard 119
#####################################
120
#  If protocols filter is activate  #
121
#####################################
612 richard 122
if [ $PROTOCOLS_FILTERING = on ]; then
688 richard 123
	# Compute exception IP (IP addresses that shouldn't be filtered)
124
	nb_exceptions=`wc -l /usr/local/etc/alcasar-filter-exceptions | cut -d" " -f1`
373 richard 125
	if [ $nb_exceptions != "0" ]
126
	then
127
		while read ip_exception 
128
		do
129
			$IPTABLES -A FORWARD -i $TUNIF -s $ip_exception -m state --state NEW -j ULOG --ulog-prefix "RULE IP-exception -- ACCEPT "
130
			$IPTABLES -A FORWARD -i $TUNIF -s $ip_exception -m state --state NEW -j ACCEPT
131
		done < /usr/local/etc/alcasar-filter-exceptions
132
	fi
688 richard 133
	# Compute uamallowed IP (IP address of equipments connect between ALCASAR and Internet (DMZ, own servers, ...) 
134
	nb_exceptions=`wc -l /usr/local/etc/alcasar-uamallowed | cut -d" "  -f1`
135
	if [ $nb_exceptions != "0" ]
136
	then
137
		while read ip_allowed_line 
138
		do
139
			ip_allowed=`echo $ip_allowed_line|cut -d"\"" -f2`
140
			$IPTABLES -A FORWARD -i $TUNIF -d $ip_allowed -m state --state NEW -j ULOG --ulog-prefix "RULE IP-allowed -- ACCEPT "
141
			$IPTABLES -A FORWARD -i $TUNIF -d $ip_allowed -m state --state NEW -j ACCEPT
142
		done < /usr/local/etc/alcasar-uamallowed
143
	fi
520 richard 144
	# Autorisation des protocoles non commentés
476 richard 145
	# Allow non comment protocols
373 richard 146
	while read svc_line
147
	do
148
		svc_on=`echo $svc_line|cut -b1`
149
		if [ $svc_on != "#" ]
150
		then	
151
			svc_name=`echo $svc_line|cut -d" " -f1`
152
			svc_port=`echo $svc_line|cut -d" " -f2`
153
			if [ $svc_name = "icmp" ]
154
			then
155
				$IPTABLES -A FORWARD -i $TUNIF -p icmp -j ACCEPT 
156
			else
157
				$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport $svc_port -m state --state NEW -j ULOG --ulog-prefix "RULE F_$svc_name -- ACCEPT "
158
				$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport $svc_port -m state --state NEW -j ACCEPT
159
			fi
160
		fi
161
	done < /usr/local/etc/alcasar-services
498 richard 162
	# Rejet explicite (vers le LAN) des autres protocoles
476 richard 163
	# reject the others
373 richard 164
	$IPTABLES -A FORWARD -i $TUNIF -p tcp -j ULOG --ulog-prefix "RULE F_filter -- REJECT "
165
	$IPTABLES -A FORWARD -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
166
	$IPTABLES -A FORWARD -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
167
	$IPTABLES -A FORWARD -i $TUNIF -p icmp -j REJECT 
1 root 168
fi
492 franck 169
 
170
########################
171
#  If QOS is activate  #
172
########################
612 richard 173
if [ $QOS = on ] && [ -e /usr/local/etc/alcasar-iptables-qos.sh ]; then
492 franck 174
	. /usr/local/etc/alcasar-iptables-qos.sh 	
175
fi
176
 
498 richard 177
# Autorisation des connections sortant du LAN  
476 richard 178
# Allow forward connections with log
179
$IPTABLES -A FORWARD -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE F_all -- ACCEPT "
411 richard 180
$IPTABLES -A FORWARD -i $TUNIF -m state --state NEW -j ACCEPT
1 root 181
 
604 richard 182
#################################################################################################
183
#  Direct input from local network (icmp, dns, ntp, https, http, ssh and 3990 (user disconnect) #
184
#################################################################################################
185
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT # ping reply
186
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT # ping request
520 richard 187
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT # dnsmasq without forward
188
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p udp --dport 54 -j ACCEPT # dnsmasq with blackhole
339 richard 189
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
190
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport https -j ACCEPT
191
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport http -j ACCEPT
192
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport 3990 -j ACCEPT
604 richard 193
# SSHD rules if activate 
612 richard 194
if [ $SSH = on ]
604 richard 195
	then
196
	$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport ssh -m state --state NEW -j ULOG --ulog-nlgroup 2 --ulog-prefix "RULE ssh-from-LAN -- ACCEPT"
197
	$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport ssh -j ACCEPT
198
	$IPTABLES -A INPUT -i $EXTIF -p tcp --dport ssh -s $Admin_from_IP -m state --state NEW --syn -j ULOG --ulog-nlgroup 2 --ulog-prefix "RULE ssh-from-WAN -- ACCEPT"
199
	$IPTABLES -A INPUT -i $EXTIF -p tcp --dport ssh -s $Admin_from_IP -m state --state NEW,ESTABLISHED -j ACCEPT
200
	$IPTABLES -A OUTPUT -o $EXTIF -p tcp --sport ssh -m state --state ESTABLISHED -j ACCEPT
201
fi
202
 
203
# Insertion de règles locales
694 franck 204
# Here, we add local rules (i.e. VPN from Internet)
604 richard 205
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
206
        . /usr/local/etc/alcasar-iptables-local.sh
207
fi
208
 
498 richard 209
# On autorise les retours de connexions légitimes par INPUT
476 richard 210
# Conntrack on INPUT
1 root 211
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
212
 
520 richard 213
# On interdit les connexions directes au port utilisé par DansGuardian (8080)
498 richard 214
# Les packets concernés ont fait l'objet d'un marquage dans la table mangle 
215
# lors d'une règle de PREROUTING (voir plus bas)
520 richard 216
# Deny direct connections on DansGuardian port (8080)
476 richard 217
# The concerned paquets are marked by a pre-routing rule (see further)
1 root 218
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8080 -m mark --mark 1 -j DROP
520 richard 219
# Autorisation des connexions à DansGuardian 
220
# Allow connections for DansGuardian
1 root 221
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8080 -m state --state NEW --syn -j ACCEPT
222
 
498 richard 223
# Journalisation des requètes HTTP (seulement des paquets SYN)
476 richard 224
# Log HTTP requests (only syn)
225
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp ! -d $PRIVATE_IP --dport http -m state --state NEW -j ULOG --ulog-prefix "RULE F_http -- ACCEPT "
498 richard 226
# On redirige les requêtes HTTP vers DansGuardian (transparent pour les utilisateurs)
476 richard 227
# Redirect HTTP request in DansGuardian (transparent proxy)
1 root 228
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp ! -d $PRIVATE_IP --dport http -j REDIRECT --to-port 8080
520 richard 229
# Journalisation et marquage des paquets qui tentent d'accéder directement à DansGuardian 
498 richard 230
# pour pouvoir les supprimer en INPUT (voir plus haut)
520 richard 231
# Mark the dansguardian bypass attempts
1 root 232
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8080 -j ULOG --ulog-prefix "RULE direct-proxy -- DENY "
233
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8080 -j MARK --set-mark 1
234
 
498 richard 235
# Journalisation et rejet des connexions (autres que celles autorisées) effectuées depuis le LAN
476 richard 236
# Deny and log on INPUT from the LAN
237
$IPTABLES -A INPUT -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
472 richard 238
$IPTABLES -A INPUT -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
239
$IPTABLES -A INPUT -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
492 franck 240
 
498 richard 241
# Journalisation et rejet des connexions initiées depuis le réseau extérieur (test des effets du paramètre --limit en cours)
476 richard 242
# On EXTIF, the access attempts are log in channel 2 (we should test --limit option to avoid deny of service)
243
$IPTABLES -A INPUT -i $EXTIF -m state --state NEW -j ULOG --ulog-nlgroup 3 --ulog-qthreshold 10 --ulog-prefix "RULE rej-ext -- DROP"
1 root 244
 
498 richard 245
#############################
246
# filtering outside OUTPUT. #
247
#############################
248
# On laisse tout sortir sur toutes les cartes sauf celle qui est connectée sur l'extérieur
249
# Everything is allowed but traffic through outside network interface
250
$IPTABLES -A OUTPUT ! -o $EXTIF -j ACCEPT
520 richard 251
 
503 richard 252
# On autorise les requêtes DNS vers les serveurs DNS identifiés 
498 richard 253
# Allow DNS requests to identified DNS servers
254
$IPTABLES -A OUTPUT -o $EXTIF -d $DNSSERVERS -p udp --dport domain -m state --state NEW -j ACCEPT
615 richard 255
# On autorise les requêtes HTTP sortantes
498 richard 256
# HTTP requests are allowed
257
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport http -j ACCEPT
615 richard 258
# On autorise les requêtes NTP 
498 richard 259
# NTP requests are allowed
260
$IPTABLES -A OUTPUT -o $EXTIF -p udp --dport ntp -j ACCEPT
503 richard 261
# On autorise les requêtes ICMP (ping) 
262
# ICMP (ping) requests are allowed
263
$IPTABLES -A OUTPUT -o $EXTIF -p icmp --icmp-type 8 -j ACCEPT
615 richard 264
# On autorise les requêtes LDAP si un serveur externe est configué
265
# LDAP requests are allowed if an external server is declared
266
if [ $LDAP = on ]
267
	then
694 franck 268
	$IPTABLES -A OUTPUT -p tcp -d $LDAP_IP -m multiport --dports ldap,ldaps -m state --state NEW,ESTABLISHED -j ACCEPT
269
	$IPTABLES -A OUTPUT -p udp -d $LDAP_IP -m multiport --dports ldap,ldaps -m state --state NEW,ESTABLISHED -j ACCEPT
270
#	$IPTABLES -A INPUT  -p tcp -s $LDAP_IP -m multiports --sports ldap,ldaps -m state --state ESTABLISHED -j ACCEPT
271
#	$IPTABLES -A INPUT  -p udp -s $LDAP_IP -m multiports --sports ldap,ldaps -m state --state ESTABLISHED -j ACCEPT
615 richard 272
fi
498 richard 273
# Traduction dynamique d'adresse en sortie
476 richard 274
# Dynamic NAT on EXTIF
1 root 275
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
276
 
476 richard 277
# Save all rules
1 root 278
/etc/init.d/iptables save
279
 
476 richard 280
# End of script
1 root 281