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