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