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