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