Line 1... |
Line 1... |
1 |
#!/bin/sh
|
1 |
#!/bin/sh
|
2 |
# $Id: alcasar-iptables.sh 770 2011-11-16 20:53:35Z richard $
|
2 |
# $Id: alcasar-iptables.sh 783 2011-12-17 14:24:08Z richard $
|
3 |
# Script de mise en place des regles du parefeu d'Alcasar (mode normal)
|
3 |
# Script de mise en place des regles du parefeu d'Alcasar (mode normal)
|
4 |
# This script write the netfilter rules for ALCASAR
|
4 |
# This script write the netfilter rules for ALCASAR
|
5 |
# Rexy - 3abtux - CPN
|
5 |
# Rexy - 3abtux - CPN
|
6 |
#
|
6 |
#
|
7 |
# Reminders
|
7 |
# Reminders
|
8 |
# There are three channels for log :
|
8 |
# There are three channels for log :
|
9 |
# 1 (default) for tracability;
|
9 |
# 1 (default) for tracability;
|
10 |
# 2 for secure admin (ssh);
|
10 |
# 2 for secure admin (ssh);
|
11 |
# 3 for exterior access attempts.
|
11 |
# 3 for exterior access attempts.
|
12 |
# The French Security Agency (ANSSI) rules was applied by 'alcasar.sh' script
|
12 |
# The French Security Agency (ANSSI) rules was applied by 'this script
|
13 |
# The bootps/dhcp (67) port is always open on tun0/eth1 by coova
|
13 |
# The bootps/dhcp (67) port is always open on tun0/eth1 by coova
|
14 |
|
- |
|
15 |
conf_file="/usr/local/etc/alcasar.conf"
|
14 |
conf_file="/usr/local/etc/alcasar.conf"
|
16 |
private_ip_mask=`grep PRIVATE_IP= $conf_file|cut -d"=" -f2`
|
15 |
private_ip_mask=`grep PRIVATE_IP= $conf_file|cut -d"=" -f2`
|
17 |
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
|
16 |
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
|
- |
|
17 |
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1` # ALCASAR LAN IP address
|
18 |
private_network=`/bin/ipcalc -n $private_ip_mask|cut -d"=" -f2` # LAN IP address (ie.: 192.168.182.0)
|
18 |
private_network=`/bin/ipcalc -n $private_ip_mask|cut -d"=" -f2` # LAN IP address (ie.: 192.168.182.0)
|
19 |
private_prefix=`/bin/ipcalc -p $private_ip_mask|cut -d"=" -f2` # LAN prefix (ie. 24)
|
19 |
private_prefix=`/bin/ipcalc -p $private_ip_mask|cut -d"=" -f2` # LAN prefix (ie. 24)
|
- |
|
20 |
PRIVATE_NETWORK_MASK=$private_network/$private_prefix # Lan IP address + prefix (192.168.182.0/24)
|
- |
|
21 |
public_ip_mask=`grep PUBLIC_IP= $conf_file|cut -d"=" -f2` # ALCASAR WAN IP address
|
- |
|
22 |
PUBLIC_IP=`echo $public_ip_mask | cut -d"/" -f1`
|
20 |
dns1=`grep DNS1= $conf_file|cut -d"=" -f2` # first public DNS server
|
23 |
dns1=`grep DNS1= $conf_file|cut -d"=" -f2` # first public DNS server
|
21 |
dns1=${dns1:=208.67.220.220}
|
24 |
dns1=${dns1:=208.67.220.220}
|
22 |
dns2=`grep DNS2= $conf_file|cut -d"=" -f2` # second public DNS server
|
25 |
dns2=`grep DNS2= $conf_file|cut -d"=" -f2` # second public DNS server
|
23 |
dns2=${dns2:=208.67.222.222}
|
26 |
dns2=${dns2:=208.67.222.222}
|
- |
|
27 |
DNSSERVERS="$dns1,$dns2" # first and second DNS IP servers addresses
|
24 |
PROTOCOLS_FILTERING=`grep PROTOCOLS_FILTERING= $conf_file|cut -d"=" -f2` # Network protocols filter (on/off)
|
28 |
PROTOCOLS_FILTERING=`grep PROTOCOLS_FILTERING= $conf_file|cut -d"=" -f2` # Network protocols filter (on/off)
|
25 |
PROTOCOLS_FILTERING=${PROTOCOLS_FILTERING:=off}
|
29 |
PROTOCOLS_FILTERING=${PROTOCOLS_FILTERING:=off}
|
26 |
DNS_FILTERING=`grep DNS_FILTERING= $conf_file|cut -d"=" -f2` # DNS and URLs filter (on/off)
|
30 |
DNS_FILTERING=`grep DNS_FILTERING= $conf_file|cut -d"=" -f2` # DNS and URLs filter (on/off)
|
27 |
DNS_FILTERING=${DNS_FILTERING:=off}
|
31 |
DNS_FILTERING=${DNS_FILTERING:=off}
|
28 |
QOS=`grep QOS= $conf_file|cut -d"=" -f2` # QOS (on/off)
|
32 |
QOS=`grep QOS= $conf_file|cut -d"=" -f2` # QOS (on/off)
|
29 |
QOS=${QOS:=off}
|
33 |
QOS=${QOS:=off}
|
30 |
SSH=`grep SSH= $conf_file|cut -d"=" -f2` # sshd active (on/off)
|
34 |
SSH=`grep SSH= $conf_file|cut -d"=" -f2` # sshd active (on/off)
|
31 |
SSH=${SSH:=off}
|
35 |
SSH=${SSH:=off}
|
32 |
Admin_from_IP=${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ù ! )
|
36 |
Admin_from_IP=${Admin_from_IP:="0.0.0.0/0.0.0.0"} # WAN IP address to reduce ssh access (all ip allowed on LAN side)
|
33 |
LDAP=`grep LDAP= $conf_file|cut -d"=" -f2` # ldap external server active (on/off)
|
37 |
LDAP=`grep LDAP= $conf_file|cut -d"=" -f2` # LDAP external server active (on/off)
|
34 |
LDAP=${LDAP:=off}
|
38 |
LDAP=${LDAP:=off}
|
35 |
LDAP_IP=`grep LDAP_IP= $conf_file|cut -d"=" -f2` # ldap external server IP address
|
39 |
LDAP_IP=`grep LDAP_IP= $conf_file|cut -d"=" -f2` # WAN IP address to reduce LDAP WAN access (all ip allowed on LAN side)
|
36 |
LDAP_IP=${LDAP_IP:="0.0.0.0/0.0.0.0"}
|
40 |
LDAP_IP=${LDAP_IP:="0.0.0.0/0.0.0.0"}
|
37 |
PRIVATE_NETWORK_MASK=$private_network/$private_prefix # Lan IP address + prefix (192.168.182.0/24)
|
- |
|
38 |
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1` # ALCASAR LAN IP address
|
- |
|
39 |
DNSSERVERS="$dns1,$dns2" # first and second DNS IP servers addresses
|
- |
|
40 |
EXTIF="eth0"
|
41 |
EXTIF="eth0"
|
41 |
INTIF="eth1"
|
42 |
INTIF="eth1"
|
42 |
TUNIF="tun0" # listen card for chilli daemon
|
43 |
TUNIF="tun0" # listen device for chilli daemon
|
43 |
IPTABLES="/sbin/iptables"
|
44 |
IPTABLES="/sbin/iptables"
|
44 |
|
45 |
|
45 |
# Effacement des règles existantes
|
46 |
# Effacement des règles existantes
|
46 |
# Flush all existing rules
|
47 |
# Flush all existing rules
|
47 |
$IPTABLES -F
|
48 |
$IPTABLES -F
|
Line 63... |
Line 64... |
63 |
$IPTABLES -P OUTPUT DROP
|
64 |
$IPTABLES -P OUTPUT DROP
|
64 |
$IPTABLES -t nat -P PREROUTING ACCEPT
|
65 |
$IPTABLES -t nat -P PREROUTING ACCEPT
|
65 |
$IPTABLES -t nat -P POSTROUTING ACCEPT
|
66 |
$IPTABLES -t nat -P POSTROUTING ACCEPT
|
66 |
$IPTABLES -t nat -P OUTPUT ACCEPT
|
67 |
$IPTABLES -t nat -P OUTPUT ACCEPT
|
67 |
|
68 |
|
68 |
# Tout passe sur loopback
|
- |
|
69 |
# accept all on loopback
|
- |
|
70 |
$IPTABLES -A INPUT -i lo -j ACCEPT
|
- |
|
71 |
|
- |
|
72 |
#############################
|
69 |
#############################
|
73 |
# INTIF rules #
|
70 |
# PREROUTING #
|
74 |
#############################
|
71 |
#############################
|
- |
|
72 |
# Marquage (et journalisation) des paquets qui tentent d'accéder directement à DansGuardian pour pouvoir les rejeter en INPUT
|
- |
|
73 |
# mark (and log) the dansguardian bypass attempts in order to DROP them in INPUT rules
|
- |
|
74 |
# $IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8080 -j ULOG --ulog-prefix "RULE direct-proxy -- DENY "
|
- |
|
75 |
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp -m tcp --dport 8080 -j MARK --set-mark 1
|
- |
|
76 |
|
- |
|
77 |
# Marquage (et journalisation) des paquets qui tentent d'accéder directement au port udp 54 pour pouvoir les rejeter en INPUT
|
- |
|
78 |
# Mark (and log) the udp 54 direct attempts to REJECT them in INPUT rules
|
75 |
# interdit l'accès à INTIF (n'est utile que lorsque chilli est arrêté).
|
79 |
# Remarque : Ce port n'est ouvert que lorsque le filtrage est activé
|
76 |
# Reject INTIF access (only when chilli is down)
|
80 |
# Remark : this port is only open when filtering is on
|
77 |
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE Protect1 -- REJECT "
|
81 |
# $IPTABLES -A PREROUTING -t nat -i $TUNIF -p udp -d $PRIVATE_IP -m udp --dport 54 -j ULOG --ulog-prefix "RULE DNS-proxy -- DENY "
|
- |
|
82 |
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp --dport 54 -j MARK --set-mark 2
|
- |
|
83 |
|
- |
|
84 |
# Si le filtrage est activé, redirection des flux DNS vers le port 54 (dns+blackhole) sauf pour les IP en exceptions
|
- |
|
85 |
# If DNS filter is on, redirect DNS request to udp 54 (dns+blackhole) except for exception IP addresses
|
78 |
$IPTABLES -A INPUT -i $INTIF -j REJECT
|
86 |
if [ $DNS_FILTERING = on ]; then
|
- |
|
87 |
# Compute exception IP
|
- |
|
88 |
nb_exceptions=`wc -l /usr/local/etc/alcasar-filter-exceptions | cut -d" " -f1`
|
- |
|
89 |
if [ $nb_exceptions != "0" ]
|
- |
|
90 |
then
|
- |
|
91 |
while read ip_exception
|
- |
|
92 |
do
|
- |
|
93 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p udp -s $ip_exception -d $PRIVATE_IP --dport domain -j ACCEPT
|
- |
|
94 |
done < /usr/local/etc/alcasar-filter-exceptions
|
- |
|
95 |
fi
|
- |
|
96 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport domain -j REDIRECT --to-port 54
|
- |
|
97 |
fi
|
- |
|
98 |
|
- |
|
99 |
# Journalisation des requètes HTTP vers Internet (seulement les paquets SYN) - Les autres protocoles sont journalisés en FORWARD
|
- |
|
100 |
# Log HTTP requests to Internet (only syn packets) - Other protocols are log in FORWARD
|
- |
|
101 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -s $PRIVATE_NETWORK_MASK ! -d $PRIVATE_IP -p tcp --dport http -m state --state NEW -j ULOG --ulog-prefix "RULE F_http -- ACCEPT "
|
- |
|
102 |
# Redirection des requêtes HTTP vers DansGuardian (proxy transparent)
|
- |
|
103 |
# Redirect HTTP requests in DansGuardian (transparent proxy)
|
- |
|
104 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -s $PRIVATE_NETWORK_MASK ! -d $PRIVATE_IP -p tcp --dport http -j REDIRECT --to-port 8080
|
- |
|
105 |
|
- |
|
106 |
# Redirection des requêtes NTP vers le serveur NTP local
|
- |
|
107 |
# Redirect NTP request in local NTP server
|
- |
|
108 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -s $PRIVATE_NETWORK_MASK ! -d $PRIVATE_IP -p udp --dport ntp -j REDIRECT --to-port 123
|
79 |
|
109 |
|
80 |
#############################
|
110 |
#############################
|
81 |
# Local protection rules #
|
111 |
# INPUT #
|
82 |
#############################
|
112 |
#############################
|
- |
|
113 |
# Tout passe sur loopback
|
- |
|
114 |
# accept all on loopback
|
- |
|
115 |
$IPTABLES -A INPUT -i lo -j ACCEPT
|
- |
|
116 |
|
83 |
# On stoppe les demande de connexions non conformes (NullScan, XMAS (tous flags à 1), NEW not SYN, etc.)
|
117 |
# Rejet des demandes de connexions non conformes (FIN-URG-PUSH, XMAS, NullScan, SYN-RST et NEW not SYN)
|
84 |
# Drop non standard connexions (NULLscans, XMAS, "NEW not SYN", etc.)
|
118 |
# Drop non standard connexions (FIN-URG-PUSH, XMAS, NullScan, SYN-RST et NEW not SYN)
|
85 |
$IPTABLES -A INPUT -p tcp --tcp-flags FIN,URG,PSH FIN,URG,PSH -j DROP
|
119 |
$IPTABLES -A INPUT -p tcp --tcp-flags FIN,URG,PSH FIN,URG,PSH -j DROP
|
86 |
$IPTABLES -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
|
120 |
$IPTABLES -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
|
87 |
$IPTABLES -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
|
121 |
$IPTABLES -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
|
88 |
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
|
122 |
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
|
89 |
$IPTABLES -A INPUT -p tcp -m tcp ! --syn -m state --state NEW -j DROP
|
123 |
$IPTABLES -A INPUT -p tcp -m tcp ! --syn -m state --state NEW -j DROP
|
90 |
|
124 |
|
91 |
# On ne traite pas les broadcasts et multicast
|
125 |
# On rejète les trame en broadcast et en multicast sur EXTIF (pour ne pas les journaliser)
|
92 |
# Drop broadcast & multicast
|
126 |
# Drop broadcast & multicast on EXTIF to not be logged
|
93 |
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
|
127 |
$IPTABLES -A INPUT -i $EXTIF -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
|
94 |
|
128 |
|
95 |
# Rejet des tentatives de création de tunnels DNS (même pour les utilisateurs authentifiés)
|
129 |
# On autorise les retours de connexions légitimes par INPUT
|
96 |
# Deny forward DNS (even for authenticated users ...)
|
130 |
# Conntrack on INPUT
|
97 |
$IPTABLES -A FORWARD -i $TUNIF -p udp --dport domain -j REJECT --reject-with icmp-port-unreachable
|
- |
|
98 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport domain -j REJECT --reject-with tcp-reset
|
131 |
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
|
99 |
|
132 |
|
- |
|
133 |
# On interdit les connexions directes au port utilisé par DansGuardian (8080). Les packets concernés ont été marqués dans la table mangle (PREROUTING)
|
- |
|
134 |
# Deny direct connections on DansGuardian port (8080). The concerned paquets are marked in mangle table (PREROUTING)
|
- |
|
135 |
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8080 -m mark --mark 1 -j REJECT --reject-with tcp-reset
|
100 |
# On autorise les retours de connexions légitimes par FORWARD
|
136 |
# Autorisation des connexions légitimes à DansGuardian
|
101 |
# Conntrack on forward
|
137 |
# Allow connections for DansGuardian
|
102 |
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
|
138 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p tcp --dport 8080 -m state --state NEW --syn -j ACCEPT
|
103 |
|
139 |
|
- |
|
140 |
# On interdit les connexions directes au port UDP 54. Les packets concernés ont été marqués dans la table mangle (PREROUTING)
|
104 |
###############################
|
141 |
# Deny direct connections on UDP 54. The concerned paquets are marked in mangle table (PREROUTING)
|
- |
|
142 |
$IPTABLES -A INPUT -i $TUNIF -p udp --dport 54 -m mark --mark 2 -j REJECT --reject-with icmp-port-unreachable
|
105 |
# If DNS filter is activate #
|
143 |
# autorisation des connexion légitime à DNSMASQ (avec blackhole)
|
106 |
###############################
|
144 |
# Allow connections for DNSMASQ (with blackhole)
|
107 |
# Redirection des flux DNS vers le port 54 (dns+blackhole) sauf pour les IP en exceptions
|
145 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport 54 -j ACCEPT
|
- |
|
146 |
|
108 |
if [ $DNS_FILTERING = on ]; then
|
147 |
# Accès direct aux services internes
|
109 |
# Compute exception IP
|
148 |
# Internal services access
|
- |
|
149 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport domain -j ACCEPT # DNS non filtré # DNS without blackhole
|
110 |
nb_exceptions=`wc -l /usr/local/etc/alcasar-filter-exceptions | cut -d" " -f1`
|
150 |
#$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT # Requête ping # ping request
|
- |
|
151 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p icmp --icmp-type 0 -j ACCEPT # Réponse ping # ping reply
|
- |
|
152 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport https -j ACCEPT # Pages d'authentification et MCC # authentication pages and MCC
|
- |
|
153 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport http -j ACCEPT # Page d'avertissement filtrage # Filtering warning pages
|
- |
|
154 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport 3990 -j ACCEPT # Requêtes de deconnexion usagers # Users logout requests
|
- |
|
155 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT # Serveur local de temps # local time server
|
- |
|
156 |
|
- |
|
157 |
# SSHD rules if activate
|
111 |
if [ $nb_exceptions != "0" ]
|
158 |
if [ $SSH = on ]
|
112 |
then
|
159 |
then
|
113 |
while read ip_exception
|
160 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport ssh -m state --state NEW -j ULOG --ulog-nlgroup 2 --ulog-prefix "RULE ssh-from-LAN -- ACCEPT"
|
114 |
do
|
- |
|
115 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p udp -s $ip_exception -d $PRIVATE_IP --dport domain -j ACCEPT
|
161 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport ssh -j ACCEPT
|
116 |
done < /usr/local/etc/alcasar-filter-exceptions
|
162 |
$IPTABLES -A INPUT -i $EXTIF -s $Admin_from_IP -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW --syn -j ULOG --ulog-nlgroup 2 --ulog-prefix "RULE ssh-from-WAN -- ACCEPT"
|
117 |
fi
|
- |
|
118 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p udp -d $PRIVATE_IP --dport domain -j REDIRECT --to-port 54
|
163 |
$IPTABLES -A INPUT -i $EXTIF -s $Admin_from_IP -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW,ESTABLISHED -j ACCEPT
|
119 |
fi
|
164 |
fi
|
- |
|
165 |
|
- |
|
166 |
# Insertion de règles locales
|
- |
|
167 |
# Here, we add local rules (i.e. VPN from Internet)
|
- |
|
168 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
- |
|
169 |
. /usr/local/etc/alcasar-iptables-local.sh
|
- |
|
170 |
fi
|
- |
|
171 |
|
- |
|
172 |
# Journalisation et rejet des connexions (autres que celles autorisées) effectuées depuis le LAN
|
- |
|
173 |
# Deny and log on INPUT from the LAN
|
- |
|
174 |
$IPTABLES -A INPUT -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
|
- |
|
175 |
$IPTABLES -A INPUT -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
|
- |
|
176 |
$IPTABLES -A INPUT -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
|
- |
|
177 |
|
- |
|
178 |
# interdiction d'accès à INTIF (n'est utile que lorsque chilli est arrêté).
|
- |
|
179 |
# Reject INTIF access (only when chilli is down)
|
- |
|
180 |
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE Protect1 -- REJECT "
|
- |
|
181 |
$IPTABLES -A INPUT -i $INTIF -j REJECT
|
- |
|
182 |
|
- |
|
183 |
# Journalisation et rejet des connexions initiées depuis le réseau extérieur (test des effets du paramètre --limit en cours)
|
- |
|
184 |
# On EXTIF, the access attempts are log in channel 2 (we should test --limit option to avoid deny of service)
|
- |
|
185 |
$IPTABLES -A INPUT -i $EXTIF -m state --state NEW -j ULOG --ulog-nlgroup 3 --ulog-qthreshold 10 --ulog-prefix "RULE rej-ext -- DROP"
|
- |
|
186 |
|
120 |
#####################################
|
187 |
#############################
|
121 |
# If protocols filter is activate #
|
188 |
# FORWARD #
|
122 |
#####################################
|
189 |
#############################
|
- |
|
190 |
# Autorisation des retours de connexions légitimes
|
- |
|
191 |
# Allow conntrack
|
- |
|
192 |
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
|
- |
|
193 |
|
- |
|
194 |
# Rejet des requêtes DNS vers Internet
|
- |
|
195 |
# Deny forward DNS
|
- |
|
196 |
$IPTABLES -A FORWARD -i $TUNIF -p udp --dport domain -j REJECT --reject-with icmp-port-unreachable
|
- |
|
197 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport domain -j REJECT --reject-with tcp-reset
|
- |
|
198 |
|
- |
|
199 |
# If protocols filter is activate
|
123 |
if [ $PROTOCOLS_FILTERING = on ]; then
|
200 |
if [ $PROTOCOLS_FILTERING = on ]; then
|
124 |
# Compute exception IP (IP addresses that shouldn't be filtered)
|
201 |
# Compute exception IP (IP addresses that shouldn't be filtered)
|
125 |
nb_exceptions=`wc -l /usr/local/etc/alcasar-filter-exceptions | cut -d" " -f1`
|
202 |
nb_exceptions=`wc -l /usr/local/etc/alcasar-filter-exceptions | cut -d" " -f1`
|
126 |
if [ $nb_exceptions != "0" ]
|
203 |
if [ $nb_exceptions != "0" ]
|
127 |
then
|
204 |
then
|
Line 153... |
Line 230... |
153 |
svc_port=`echo $svc_line|cut -d" " -f2`
|
230 |
svc_port=`echo $svc_line|cut -d" " -f2`
|
154 |
if [ $svc_name = "icmp" ]
|
231 |
if [ $svc_name = "icmp" ]
|
155 |
then
|
232 |
then
|
156 |
$IPTABLES -A FORWARD -i $TUNIF -p icmp -j ACCEPT
|
233 |
$IPTABLES -A FORWARD -i $TUNIF -p icmp -j ACCEPT
|
157 |
else
|
234 |
else
|
158 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport $svc_port -m state --state NEW -j ULOG --ulog-prefix "RULE F_$svc_name -- ACCEPT "
|
235 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport $svc_port -m state --state NEW -j ULOG --ulog-prefix "RULE F_TCP-$svc_name -- ACCEPT "
|
159 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport $svc_port -m state --state NEW -j ACCEPT
|
236 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport $svc_port -m state --state NEW -j ACCEPT
|
- |
|
237 |
$IPTABLES -A FORWARD -i $TUNIF -p udp --dport $svc_port -m state --state NEW -j ULOG --ulog-prefix "RULE F_UDP-$svc_name -- ACCEPT "
|
- |
|
238 |
$IPTABLES -A FORWARD -i $TUNIF -p udp --dport $svc_port -m state --state NEW -j ACCEPT
|
160 |
fi
|
239 |
fi
|
161 |
fi
|
240 |
fi
|
162 |
done < /usr/local/etc/alcasar-services
|
241 |
done < /usr/local/etc/alcasar-services
|
163 |
# Rejet explicite (vers le LAN) des autres protocoles
|
242 |
# Rejet explicite des autres protocoles
|
164 |
# reject the others
|
243 |
# reject the others protocols
|
165 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp -j ULOG --ulog-prefix "RULE F_filter -- REJECT "
|
244 |
$IPTABLES -A FORWARD -i $TUNIF -j ULOG --ulog-prefix "RULE F_filter -- REJECT "
|
166 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
|
245 |
$IPTABLES -A FORWARD -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
|
167 |
$IPTABLES -A FORWARD -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
|
246 |
$IPTABLES -A FORWARD -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
|
168 |
$IPTABLES -A FORWARD -i $TUNIF -p icmp -j REJECT
|
247 |
$IPTABLES -A FORWARD -i $TUNIF -p icmp -j REJECT
|
169 |
fi
|
248 |
fi
|
170 |
|
249 |
|
Line 178... |
Line 257... |
178 |
# Autorisation des connections sortant du LAN
|
257 |
# Autorisation des connections sortant du LAN
|
179 |
# Allow forward connections with log
|
258 |
# Allow forward connections with log
|
180 |
$IPTABLES -A FORWARD -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE F_all -- ACCEPT "
|
259 |
$IPTABLES -A FORWARD -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE F_all -- ACCEPT "
|
181 |
$IPTABLES -A FORWARD -i $TUNIF -m state --state NEW -j ACCEPT
|
260 |
$IPTABLES -A FORWARD -i $TUNIF -m state --state NEW -j ACCEPT
|
182 |
|
261 |
|
183 |
#################################################################################################
|
262 |
#############################
|
184 |
# Direct input from local network (icmp, dns, ntp, https, http, ssh and 3990 (user disconnect) #
|
263 |
# OUTPUT #
|
185 |
#################################################################################################
|
264 |
#############################
|
186 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT # ping reply
|
- |
|
187 |
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT # ping request
|
- |
|
188 |
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT # dnsmasq without forward
|
- |
|
189 |
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p udp --dport 54 -j ACCEPT # dnsmasq with blackhole
|
- |
|
190 |
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
|
- |
|
191 |
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport https -j ACCEPT
|
- |
|
192 |
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport http -j ACCEPT
|
- |
|
193 |
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport 3990 -j ACCEPT
|
- |
|
194 |
# SSHD rules if activate
|
265 |
# SSHD rules if activate
|
195 |
if [ $SSH = on ]
|
266 |
if [ $SSH = on ]
|
196 |
then
|
267 |
then
|
197 |
$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"
|
- |
|
198 |
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport ssh -j ACCEPT
|
- |
|
199 |
$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"
|
- |
|
200 |
$IPTABLES -A INPUT -i $EXTIF -p tcp --dport ssh -s $Admin_from_IP -m state --state NEW,ESTABLISHED -j ACCEPT
|
- |
|
201 |
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --sport ssh -m state --state ESTABLISHED -j ACCEPT
|
268 |
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --sport ssh -m state --state ESTABLISHED -j ACCEPT
|
202 |
fi
|
269 |
fi
|
203 |
|
- |
|
204 |
# Insertion de règles locales
|
- |
|
205 |
# Here, we add local rules (i.e. VPN from Internet)
|
- |
|
206 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
- |
|
207 |
. /usr/local/etc/alcasar-iptables-local.sh
|
- |
|
208 |
fi
|
- |
|
209 |
|
- |
|
210 |
# On autorise les retours de connexions légitimes par INPUT
|
- |
|
211 |
# Conntrack on INPUT
|
- |
|
212 |
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
|
- |
|
213 |
|
- |
|
214 |
# On interdit les connexions directes au port utilisé par DansGuardian (8080)
|
- |
|
215 |
# Les packets concernés ont fait l'objet d'un marquage dans la table mangle
|
- |
|
216 |
# lors d'une règle de PREROUTING (voir plus bas)
|
- |
|
217 |
# Deny direct connections on DansGuardian port (8080)
|
- |
|
218 |
# The concerned paquets are marked by a pre-routing rule (see further)
|
- |
|
219 |
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8080 -m mark --mark 1 -j REJECT --reject-with tcp-reset
|
- |
|
220 |
# Autorisation des connexions à DansGuardian
|
- |
|
221 |
# Allow connections for DansGuardian
|
- |
|
222 |
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8080 -m state --state NEW --syn -j ACCEPT
|
- |
|
223 |
|
- |
|
224 |
# Journalisation des requètes HTTP (seulement des paquets SYN)
|
- |
|
225 |
# Log HTTP requests (only syn)
|
- |
|
226 |
$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 "
|
- |
|
227 |
# On redirige les requêtes HTTP vers DansGuardian (transparent pour les utilisateurs)
|
- |
|
228 |
# Redirect HTTP request in DansGuardian (transparent proxy)
|
- |
|
229 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp ! -d $PRIVATE_IP --dport http -j REDIRECT --to-port 8080
|
- |
|
230 |
# Journalisation et marquage des paquets qui tentent d'accéder directement à DansGuardian
|
- |
|
231 |
# pour pouvoir les supprimer en INPUT (voir plus haut)
|
- |
|
232 |
# Mark the dansguardian bypass attempts
|
- |
|
233 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8080 -j ULOG --ulog-prefix "RULE direct-proxy -- DENY "
|
- |
|
234 |
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8080 -j MARK --set-mark 1
|
- |
|
235 |
# On redirige les requêtes NTP vers le serveur NTP local
|
- |
|
236 |
# Redirect NTP request in local NTP server
|
- |
|
237 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p udp ! -d $PRIVATE_IP --dport ntp -j REDIRECT --to-port 123
|
- |
|
238 |
|
- |
|
239 |
# Journalisation et rejet des connexions (autres que celles autorisées) effectuées depuis le LAN
|
- |
|
240 |
# Deny and log on INPUT from the LAN
|
- |
|
241 |
$IPTABLES -A INPUT -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
|
- |
|
242 |
$IPTABLES -A INPUT -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
|
- |
|
243 |
$IPTABLES -A INPUT -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
|
- |
|
244 |
|
- |
|
245 |
# Journalisation et rejet des connexions initiées depuis le réseau extérieur (test des effets du paramètre --limit en cours)
|
- |
|
246 |
# On EXTIF, the access attempts are log in channel 2 (we should test --limit option to avoid deny of service)
|
- |
|
247 |
$IPTABLES -A INPUT -i $EXTIF -m state --state NEW -j ULOG --ulog-nlgroup 3 --ulog-qthreshold 10 --ulog-prefix "RULE rej-ext -- DROP"
|
- |
|
248 |
|
- |
|
249 |
#############################
|
- |
|
250 |
# filtering outside OUTPUT. #
|
- |
|
251 |
#############################
|
- |
|
252 |
# On laisse tout sortir sur toutes les cartes sauf celle qui est connectée sur l'extérieur
|
270 |
# On laisse tout sortir sur toutes les cartes sauf celle qui est connectée sur l'extérieur
|
253 |
# Everything is allowed but traffic through outside network interface
|
271 |
# Everything is allowed but traffic through outside network interface
|
254 |
$IPTABLES -A OUTPUT ! -o $EXTIF -j ACCEPT
|
272 |
$IPTABLES -A OUTPUT ! -o $EXTIF -j ACCEPT
|
255 |
|
273 |
|
256 |
# On autorise les requêtes DNS vers les serveurs DNS identifiés
|
274 |
# On autorise les requêtes DNS vers les serveurs DNS identifiés
|
257 |
# Allow DNS requests to identified DNS servers
|
275 |
# Allow DNS requests to identified DNS servers
|
258 |
$IPTABLES -A OUTPUT -o $EXTIF -d $DNSSERVERS -p udp --dport domain -m state --state NEW -j ACCEPT
|
276 |
$IPTABLES -A OUTPUT -o $EXTIF -d $DNSSERVERS -p udp --dport domain -m state --state NEW -j ACCEPT
|
- |
|
277 |
|
259 |
# On autorise les requêtes HTTP sortantes
|
278 |
# On autorise les requêtes HTTP sortantes
|
260 |
# HTTP requests are allowed
|
279 |
# HTTP requests are allowed
|
261 |
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport http -j ACCEPT
|
280 |
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --dport http -j ACCEPT
|
- |
|
281 |
|
262 |
# On autorise les requêtes NTP
|
282 |
# On autorise les requêtes NTP
|
263 |
# NTP requests are allowed
|
283 |
# NTP requests are allowed
|
264 |
$IPTABLES -A OUTPUT -o $EXTIF -p udp --dport ntp -j ACCEPT
|
284 |
$IPTABLES -A OUTPUT -o $EXTIF -p udp --dport ntp -j ACCEPT
|
- |
|
285 |
|
265 |
# On autorise les requêtes ICMP (ping)
|
286 |
# On autorise les requêtes ICMP (ping)
|
266 |
# ICMP (ping) requests are allowed
|
287 |
# ICMP (ping) requests are allowed
|
267 |
$IPTABLES -A OUTPUT -o $EXTIF -p icmp --icmp-type 8 -j ACCEPT
|
288 |
$IPTABLES -A OUTPUT -o $EXTIF -p icmp --icmp-type 8 -j ACCEPT
|
- |
|
289 |
|
268 |
# On autorise les requêtes LDAP si un serveur externe est configué
|
290 |
# On autorise les requêtes LDAP si un serveur externe est configué
|
269 |
# LDAP requests are allowed if an external server is declared
|
291 |
# LDAP requests are allowed if an external server is declared
|
270 |
if [ $LDAP = on ]
|
292 |
if [ $LDAP = on ]
|
271 |
then
|
293 |
then
|
272 |
$IPTABLES -A OUTPUT -p tcp -d $LDAP_IP -m multiport --dports ldap,ldaps -m state --state NEW,ESTABLISHED -j ACCEPT
|
294 |
$IPTABLES -A OUTPUT -p tcp -d $LDAP_IP -m multiport --dports ldap,ldaps -m state --state NEW,ESTABLISHED -j ACCEPT
|
273 |
$IPTABLES -A OUTPUT -p udp -d $LDAP_IP -m multiport --dports ldap,ldaps -m state --state NEW,ESTABLISHED -j ACCEPT
|
295 |
$IPTABLES -A OUTPUT -p udp -d $LDAP_IP -m multiport --dports ldap,ldaps -m state --state NEW,ESTABLISHED -j ACCEPT
|
274 |
# $IPTABLES -A INPUT -p tcp -s $LDAP_IP -m multiports --sports ldap,ldaps -m state --state ESTABLISHED -j ACCEPT
|
296 |
# $IPTABLES -A INPUT -p tcp -s $LDAP_IP -m multiports --sports ldap,ldaps -m state --state ESTABLISHED -j ACCEPT
|
275 |
# $IPTABLES -A INPUT -p udp -s $LDAP_IP -m multiports --sports ldap,ldaps -m state --state ESTABLISHED -j ACCEPT
|
297 |
# $IPTABLES -A INPUT -p udp -s $LDAP_IP -m multiports --sports ldap,ldaps -m state --state ESTABLISHED -j ACCEPT
|
276 |
fi
|
298 |
fi
|
- |
|
299 |
|
- |
|
300 |
|
- |
|
301 |
#############################
|
- |
|
302 |
# POSTROUTING #
|
- |
|
303 |
#############################
|
277 |
# Traduction dynamique d'adresse en sortie
|
304 |
# Traduction dynamique d'adresse en sortie
|
278 |
# Dynamic NAT on EXTIF
|
305 |
# Dynamic NAT on EXTIF
|
279 |
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
|
306 |
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
|
280 |
|
307 |
|
281 |
# Save all rules
|
308 |
# Save all rules
|