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