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