672 |
richard |
1 |
#!/bin/bash
|
64 |
franck |
2 |
# $Id: alcasar-iptables-bypass.sh 1551 2015-01-10 16:00:48Z richard $
|
|
|
3 |
|
672 |
richard |
4 |
# alcasar-iptables-bypass.sh
|
|
|
5 |
# by Rexy - 3abtux
|
|
|
6 |
# This script is distributed under the Gnu General Public License (GPL)
|
1 |
root |
7 |
|
672 |
richard |
8 |
# applique les regles du parefeu en mode ByPass
|
|
|
9 |
# put the firewall rules in 'ByPass' mode
|
|
|
10 |
|
1469 |
richard |
11 |
CONF_FILE="/usr/local/etc/alcasar.conf"
|
|
|
12 |
private_ip_mask=`grep PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
|
767 |
richard |
13 |
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
|
604 |
richard |
14 |
private_network=`/bin/ipcalc -n $private_ip_mask|cut -d"=" -f2` # LAN IP address (ie.: 192.168.182.0)
|
|
|
15 |
private_prefix=`/bin/ipcalc -p $private_ip_mask|cut -d"=" -f2` # LAN prefix (ie. 24)
|
1 |
root |
16 |
IPTABLES="/sbin/iptables"
|
1469 |
richard |
17 |
EXTIF=`grep ^EXTIF= $CONF_FILE|cut -d"=" -f2` # EXTernal InterFace
|
|
|
18 |
INTIF=`grep ^INTIF= $CONF_FILE|cut -d"=" -f2` # INTernal InterFace
|
604 |
richard |
19 |
PRIVATE_NETWORK_MASK=$private_network/$private_prefix # Lan IP address + prefix (192.168.182.0/24)
|
|
|
20 |
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1` # ALCASAR LAN IP address
|
1469 |
richard |
21 |
public_ip_mask=`grep PUBLIC_IP= $CONF_FILE|cut -d"=" -f2` # ALCASAR WAN IP address
|
1062 |
richard |
22 |
PUBLIC_IP=`echo $public_ip_mask | cut -d"/" -f1`
|
1469 |
richard |
23 |
SSH=`grep SSH= $CONF_FILE|cut -d"=" -f2` # sshd active (on/off)
|
1062 |
richard |
24 |
SSH=${SSH:=off}
|
1469 |
richard |
25 |
SSH_ADMIN_FROM=`grep SSH_ADMIN_FROM= $CONF_FILE|cut -d"=" -f2`
|
1063 |
richard |
26 |
SSH_ADMIN_FROM=${SSH_ADMIN_FROM:="0.0.0.0/0.0.0.0"} # WAN IP address to reduce ssh access (all ip allowed on LAN side)
|
1 |
root |
27 |
|
1062 |
richard |
28 |
|
1 |
root |
29 |
# On vide (flush) toutes les règles existantes
|
604 |
richard |
30 |
# Flush all existing rules
|
1 |
root |
31 |
$IPTABLES -F
|
|
|
32 |
$IPTABLES -t nat -F
|
|
|
33 |
$IPTABLES -F INPUT
|
|
|
34 |
$IPTABLES -F FORWARD
|
|
|
35 |
$IPTABLES -F OUTPUT
|
|
|
36 |
|
|
|
37 |
# On indique les politiques par défaut
|
604 |
richard |
38 |
# Default policies
|
1 |
root |
39 |
$IPTABLES -P INPUT DROP
|
|
|
40 |
$IPTABLES -P FORWARD DROP
|
|
|
41 |
$IPTABLES -P OUTPUT ACCEPT
|
|
|
42 |
$IPTABLES -t nat -P PREROUTING ACCEPT
|
|
|
43 |
$IPTABLES -t nat -P POSTROUTING ACCEPT
|
|
|
44 |
$IPTABLES -t nat -P OUTPUT ACCEPT
|
|
|
45 |
|
|
|
46 |
# On efface toutes les chaînes qui ne sont pas par défaut dans les tables filter et nat
|
604 |
richard |
47 |
# Flush non default rules on filter and nat tables
|
1 |
root |
48 |
$IPTABLES -X
|
|
|
49 |
$IPTABLES -t nat -X
|
|
|
50 |
|
|
|
51 |
# On autorise tout sur loopback
|
604 |
richard |
52 |
# accept all on loopback
|
990 |
franck |
53 |
$IPTABLES -A OUTPUT -o lo -j ACCEPT
|
1 |
root |
54 |
$IPTABLES -A INPUT -i lo -j ACCEPT
|
|
|
55 |
|
856 |
franck |
56 |
# Insertion de règles de blocage (Devel)
|
|
|
57 |
# Here, we add block rules (Devel)
|
|
|
58 |
if [ -s /usr/local/etc/alcasar-iptables-block ]; then
|
1062 |
richard |
59 |
while read ip_line
|
|
|
60 |
do
|
|
|
61 |
ip_on=`echo $ip_line|cut -b1`
|
|
|
62 |
if [ $ip_on != "#" ]
|
|
|
63 |
then
|
|
|
64 |
ip_blocked=`echo $ip_line|cut -d" " -f1`
|
|
|
65 |
$IPTABLES -A FORWARD -d $ip_blocked -j ULOG --ulog-prefix "RULE IP-blocked -- REJECT "
|
|
|
66 |
$IPTABLES -A FORWARD -d $ip_blocked -j REJECT
|
|
|
67 |
fi
|
|
|
68 |
done < /usr/local/etc/alcasar-iptables-block
|
856 |
franck |
69 |
fi
|
|
|
70 |
|
990 |
franck |
71 |
# SSHD rules if activate
|
|
|
72 |
if [ $SSH = on ]
|
|
|
73 |
then
|
|
|
74 |
$IPTABLES -A INPUT -i $INTIF -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"
|
1157 |
stephane |
75 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport ssh -m state --state NEW -j ACCEPT
|
1551 |
richard |
76 |
$IPTABLES -A INPUT -i $EXTIF -s $SSH_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW --syn -j ULOG --ulog-nlgroup 2 --ulog-prefix "RULE ssh-from-WAN -- ACCEPT"
|
1157 |
stephane |
77 |
$IPTABLES -A INPUT -i $EXTIF -s $SSH_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW -j ACCEPT
|
990 |
franck |
78 |
fi
|
|
|
79 |
|
1062 |
richard |
80 |
# Insertion de règles locales
|
|
|
81 |
# Here, we add local rules (i.e. VPN from Internet)
|
|
|
82 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
|
|
83 |
. /usr/local/etc/alcasar-iptables-local.sh
|
|
|
84 |
fi
|
|
|
85 |
|
1 |
root |
86 |
# on autorise les requêtes dhcp
|
604 |
richard |
87 |
# accept dhcp
|
1 |
root |
88 |
$IPTABLES -A INPUT -i $INTIF -p udp -m udp --sport bootpc --dport bootps -j ACCEPT
|
|
|
89 |
|
604 |
richard |
90 |
# On drop le broadcast et le multicast sur les interfaces (sans Log)
|
|
|
91 |
# Drop broadcast & multicast
|
1 |
root |
92 |
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
|
|
|
93 |
|
604 |
richard |
94 |
# On laisse passer les ICMP echo-request et echo-reply en provenance du LAN
|
|
|
95 |
# Allow ping (icmp N°0 & 8) from LAN
|
1 |
root |
96 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT
|
|
|
97 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT
|
|
|
98 |
|
1551 |
richard |
99 |
# On ajoute ici les règles spécifiques de filtrage réseau (accès exterieur ...)
|
|
|
100 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
|
|
101 |
. /usr/local/etc/alcasar-iptables-local.sh
|
|
|
102 |
fi
|
|
|
103 |
|
604 |
richard |
104 |
# On autorise les retours de connexions légitimes par FORWARD
|
|
|
105 |
# Conntrack on forward
|
1551 |
richard |
106 |
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
|
1 |
root |
107 |
|
1551 |
richard |
108 |
# On autorise les demandes de connexions sortantes
|
|
|
109 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ULOG --ulog-prefix "RULE Transfert -- ACCEPT "
|
|
|
110 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ACCEPT
|
|
|
111 |
|
1062 |
richard |
112 |
# On autorise les flux entrant ntp et dns via INTIF
|
401 |
richard |
113 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT
|
|
|
114 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
|
1 |
root |
115 |
|
401 |
richard |
116 |
# On autorise le retour des connexions entrante déjà acceptées
|
1 |
root |
117 |
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
118 |
|
|
|
119 |
# On interdit et on log le reste sur les 2 interfaces d'accès
|
|
|
120 |
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
|
|
|
121 |
$IPTABLES -A INPUT -i $EXTIF -j ULOG --ulog-prefix "RULE rej-ext -- REJECT "
|
|
|
122 |
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
|
|
|
123 |
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
|
|
|
124 |
|
|
|
125 |
# On active le masquage d'adresse par translation (NAT)
|
|
|
126 |
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
|
|
|
127 |
|
1069 |
richard |
128 |
# on ne sauvegarde pas les règles. En cas de reboot, on repasse ainsi automatiquement en mode normal (bypass -off)
|
1 |
root |
129 |
# Fin du script des regles du parefeu
|