| Line 1... |
Line 1... |
| 1 |
#!/bin/bash
|
1 |
#!/bin/bash
|
| 2 |
# $Id: alcasar-iptables.sh 1392 2014-06-18 22:23:02Z richard $
|
2 |
# $Id: alcasar-iptables.sh 1393 2014-06-19 08:44:51Z 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 writes the netfilter rules for ALCASAR
|
4 |
# This script writes the netfilter rules for ALCASAR
|
| 5 |
# Rexy - 3abtux - CPN
|
5 |
# Rexy - 3abtux - CPN
|
| 6 |
#
|
6 |
#
|
| 7 |
# Reminders
|
7 |
# Reminders
|
| Line 28... |
Line 28... |
| 28 |
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)
|
| 29 |
PROTOCOLS_FILTERING=${PROTOCOLS_FILTERING:=off}
|
29 |
PROTOCOLS_FILTERING=${PROTOCOLS_FILTERING:=off}
|
| 30 |
BL_IP_CAT="/usr/local/share/iptables-bl-enabled" # categories files of the BlackListed IP
|
30 |
BL_IP_CAT="/usr/local/share/iptables-bl-enabled" # categories files of the BlackListed IP
|
| 31 |
BL_IP_OSSI="/usr/local/share/iptables-bl/ossi" # ossi categoty
|
31 |
BL_IP_OSSI="/usr/local/share/iptables-bl/ossi" # ossi categoty
|
| 32 |
WL_IP_OSSI="/usr/local/share/ossi-ip-wl" # ip of the whitelist
|
32 |
WL_IP_OSSI="/usr/local/share/ossi-ip-wl" # ip of the whitelist
|
| - |
|
33 |
DNSMASQ_WL_ENABLED="/usr/local/share/dnsmasq-wl-enabled" # enabled domain names for the Whitelist
|
| 33 |
TMP_users_set_save="/tmp/users_set_save" # tmp file for backup users set
|
34 |
TMP_users_set_save="/tmp/users_set_save" # tmp file for backup users set
|
| 34 |
TMP_set_save="/tmp/ipset_save" # tmp file for blacklist and whitelist creation
|
35 |
TMP_set_save="/tmp/ipset_save" # tmp file for blacklist and whitelist creation
|
| 35 |
QOS=`grep ^QOS= $conf_file|cut -d"=" -f2` # QOS (on/off)
|
36 |
QOS=`grep ^QOS= $conf_file|cut -d"=" -f2` # QOS (on/off)
|
| 36 |
QOS=${QOS:=off}
|
37 |
QOS=${QOS:=off}
|
| 37 |
SSH=`grep ^SSH= $conf_file|cut -d"=" -f2` # sshd active (on/off)
|
38 |
SSH=`grep ^SSH= $conf_file|cut -d"=" -f2` # sshd active (on/off)
|
| Line 90... |
Line 91... |
| 90 |
# destroy all SET
|
91 |
# destroy all SET
|
| 91 |
ipset destroy
|
92 |
ipset destroy
|
| 92 |
|
93 |
|
| 93 |
# Calcul de la taille du set de la blacklist
|
94 |
# Calcul de la taille du set de la blacklist
|
| 94 |
# Compute the blacklist set length
|
95 |
# Compute the blacklist set length
|
| 95 |
cd $BL_IP_CAT
|
- |
|
| 96 |
set_bl_length=$(($(wc -l * | awk '{print $1}' | tail -n 1)+$(wc -l $BL_IP_OSSI | awk '{print $1}')))
|
96 |
bl_set_length=$(($(wc -l $BL_IP_CAT/* | awk '{print $1}' | tail -n 1)+$(wc -l $BL_IP_OSSI | awk '{print $1}')))
|
| 97 |
|
97 |
|
| 98 |
# Création du fichier set temporaire, remplissage, chargement et suppression
|
98 |
# Création du fichier set temporaire, remplissage, chargement et suppression
|
| 99 |
# Creating the temporary set file, filling, loading and deleting
|
99 |
# Creating the temporary set file, filling, loading and deleting
|
| 100 |
echo "create blacklist_ip_blocked hash:net family inet hashsize 1024 maxelem $set_bl_length" > $TMP_set_save
|
100 |
echo "create blacklist_ip_blocked hash:net family inet hashsize 1024 maxelem $bl_set_length" > $TMP_set_save
|
| 101 |
for category in `ls -1 | cut -d '@' -f1`
|
101 |
for category in `ls -1 $BL_IP_CAT | cut -d '@' -f1`
|
| 102 |
do
|
102 |
do
|
| 103 |
cat $BL_IP_CAT/$category >> $TMP_set_save
|
103 |
cat $BL_IP_CAT/$category >> $TMP_set_save
|
| 104 |
done
|
104 |
done
|
| 105 |
cat $BL_IP_OSSI >> $TMP_set_save
|
105 |
cat $BL_IP_OSSI >> $TMP_set_save
|
| 106 |
ipset -! restore < $TMP_set_save
|
106 |
ipset -! restore < $TMP_set_save
|
| Line 111... |
Line 111... |
| 111 |
for ip in $(cat $IP_REHABILITEES)
|
111 |
for ip in $(cat $IP_REHABILITEES)
|
| 112 |
do
|
112 |
do
|
| 113 |
ipset del blacklist_ip_blocked $ip
|
113 |
ipset del blacklist_ip_blocked $ip
|
| 114 |
done
|
114 |
done
|
| 115 |
|
115 |
|
| - |
|
116 |
# Calcul de la taille du set de la whitelist
|
| - |
|
117 |
# Compute the whitelist set length
|
| - |
|
118 |
wl_set_length=$(($(wc -l $DNSMASQ_WL_ENABLED/* | awk '{print $1}' | tail -n 1)*3))
|
| - |
|
119 |
|
| 116 |
# Création du fichier set temporaire, remplissage, chargement et suppression
|
120 |
# Création du fichier set temporaire, remplissage, chargement et suppression
|
| 117 |
# Creating the temporary set file, filling, loading and deleting
|
121 |
# Creating the temporary set file, filling, loading and deleting
|
| 118 |
echo "create whitelist_ip_allowed hash:net family inet hashsize 1024" > $TMP_set_save
|
122 |
echo "create whitelist_ip_allowed hash:net family inet hashsize 1024 maxelem $wl_set_length" > $TMP_set_save
|
| 119 |
cat $WL_IP_OSSI >> $TMP_set_save
|
123 |
cat $WL_IP_OSSI >> $TMP_set_save
|
| 120 |
ipset -! restore < $TMP_set_save
|
124 |
ipset -! restore < $TMP_set_save
|
| 121 |
rm -f $TMP_set_save
|
125 |
rm -f $TMP_set_save
|
| 122 |
|
126 |
|
| 123 |
# Restoration des SET des utilisateurs connectés si ils existent sinon création des SET
|
127 |
# Restoration des SET des utilisateurs connectés si ils existent sinon création des SET
|
| Line 156... |
Line 160... |
| 156 |
|
160 |
|
| 157 |
# Marquage (et journalisation) des paquets qui tentent d'accéder directement au port 8091 pour pouvoir les rejeter en INPUT
|
161 |
# Marquage (et journalisation) des paquets qui tentent d'accéder directement au port 8091 pour pouvoir les rejeter en INPUT
|
| 158 |
# Mark (and log) the 8091 direct attempts to REJECT them in INPUT rules
|
162 |
# Mark (and log) the 8091 direct attempts to REJECT them in INPUT rules
|
| 159 |
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp -m tcp --dport 8091 -j MARK --set-mark 5
|
163 |
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -d $PRIVATE_IP -p tcp -m tcp --dport 8091 -j MARK --set-mark 5
|
| 160 |
|
164 |
|
| 161 |
# Aiguillage des flux DNS
|
- |
|
| 162 |
# Switching DNS streams
|
- |
|
| 163 |
# havp_bl_set --> redirection vers le port 54
|
165 |
# havp_bl_set --> redirection vers le port 54
|
| 164 |
# havp_bl_set --> redirect to port 54
|
166 |
# havp_bl_set --> redirect to port 54
|
| 165 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set havp_bl_set src -d $PRIVATE_IP -p udp --dport domain -j REDIRECT --to-port 54
|
167 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set havp_bl_set src -d $PRIVATE_IP -p udp --dport domain -j REDIRECT --to-port 54
|
| - |
|
168 |
|
| 166 |
# havp_wl_set --> redirection vers le port 55
|
169 |
# havp_wl_set --> redirection vers le port 55
|
| 167 |
# havp_wl_set --> redirect to port 55
|
170 |
# havp_wl_set --> redirect to port 55
|
| 168 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set havp_wl_set src -d $PRIVATE_IP -p udp --dport domain -j REDIRECT --to-port 55
|
171 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set havp_wl_set src -d $PRIVATE_IP -p udp --dport domain -j REDIRECT --to-port 55
|
| 169 |
|
172 |
|
| 170 |
# Redirection des requêtes HTTP des IP de la blacklist vers ALCASAR (page 'accès interdit') pour le set havp_bl_set
|
173 |
# Redirection des requêtes HTTP des IP de la blacklist vers ALCASAR (page 'accès interdit') pour le set havp_bl_set
|
| - |
|
174 |
# Redirect outbound HTTP requests from blacklist IP to ALCASAR ('access denied' page) for the set havp_bl_set
|
| 171 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set havp_bl_set src -m set --match-set blacklist_ip_blocked dst -p tcp --dport http -j REDIRECT --to-port 80
|
175 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set havp_bl_set src -m set --match-set blacklist_ip_blocked dst -p tcp --dport http -j REDIRECT --to-port 80
|
| 172 |
|
176 |
|
| - |
|
177 |
# Redirection des requêtes HTTP des IP qui ne sont pas dans la whitelist vers ALCASAR (page 'accès interdit') pour le set havp_wl_set
|
| - |
|
178 |
# Redirect outbound HTTP requests from IP which are not in the whitelist to ALCASAR ('access denied' page) for the set havp_wl_set
|
| - |
|
179 |
$IPTABLES -A PREROUTING -t nat -i $TUNIF -m set --match-set havp_wl_set src -m set ! --match-set whitelist_ip_allowed dst -p tcp --dport http -j REDIRECT --to-port 80
|
| - |
|
180 |
|
| 173 |
# Journalisation des requètes HTTP vers Internet (seulement les paquets SYN) - Les autres protocoles sont journalisés en FORWARD par netflow
|
181 |
# Journalisation des requètes HTTP vers Internet (seulement les paquets SYN) - Les autres protocoles sont journalisés en FORWARD par netflow
|
| 174 |
## Log HTTP requests to Internet (only syn packets) - Other protocols are log in FORWARD by netflow
|
182 |
## Log HTTP requests to Internet (only syn packets) - Other protocols are log in FORWARD by netflow
|
| 175 |
$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 "
|
183 |
$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 "
|
| 176 |
|
184 |
|
| 177 |
# Redirection des requêtes HTTP sortantes vers HAVP (8091) pour le set havp_set
|
185 |
# Redirection des requêtes HTTP sortantes vers HAVP (8091) pour le set havp_set
|
| Line 284... |
Line 292... |
| 284 |
# Deny and log on INPUT from the LAN
|
292 |
# Deny and log on INPUT from the LAN
|
| 285 |
$IPTABLES -A INPUT -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
|
293 |
$IPTABLES -A INPUT -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
|
| 286 |
$IPTABLES -A INPUT -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
|
294 |
$IPTABLES -A INPUT -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
|
| 287 |
$IPTABLES -A INPUT -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
|
295 |
$IPTABLES -A INPUT -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
|
| 288 |
|
296 |
|
| 289 |
# interdiction d'accès à INTIF (n'est utile que lorsque chilli est arrêté).
|
297 |
# Interdiction d'accès à INTIF (n'est utile que lorsque chilli est arrêté).
|
| 290 |
# Reject INTIF access (only when chilli is down)
|
298 |
# Reject INTIF access (only when chilli is down)
|
| 291 |
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE Protect1 -- REJECT "
|
299 |
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE Protect1 -- REJECT "
|
| 292 |
$IPTABLES -A INPUT -i $INTIF -j REJECT
|
300 |
$IPTABLES -A INPUT -i $INTIF -j REJECT
|
| 293 |
|
301 |
|
| 294 |
# Journalisation et rejet des connexions initiées depuis le réseau extérieur (test des effets du paramètre --limit en cours)
|
302 |
# Journalisation et rejet des connexions initiées depuis le réseau extérieur (test des effets du paramètre --limit en cours)
|