Subversion Repositories ALCASAR

Rev

Rev 1469 | Rev 1551 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
672 richard 1
#!/bin/bash
64 franck 2
# $Id: alcasar-iptables-bypass.sh 1523 2014-12-20 18:30:19Z franck $
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)
1523 franck 27
PROTOCOLS_FILTERING=`grep PROTOCOLS_FILTERING= $conf_file|cut -d"=" -f2`        # Network protocols filter (on/off)
28
PROTOCOLS_FILTERING=${PROTOCOLS_FILTERING:=off}
1 root 29
 
1062 richard 30
 
1 root 31
# On vide (flush) toutes les règles existantes
604 richard 32
# Flush all existing rules
1 root 33
$IPTABLES -F
34
$IPTABLES -t nat -F
35
$IPTABLES -F INPUT
36
$IPTABLES -F FORWARD
37
$IPTABLES -F OUTPUT
38
 
39
# On indique les politiques par défaut
604 richard 40
# Default policies
1 root 41
$IPTABLES -P INPUT DROP
42
$IPTABLES -P FORWARD DROP
43
$IPTABLES -P OUTPUT ACCEPT
44
$IPTABLES -t nat -P PREROUTING ACCEPT
45
$IPTABLES -t nat -P POSTROUTING ACCEPT
46
$IPTABLES -t nat -P OUTPUT ACCEPT
47
 
48
# On efface toutes les chaînes qui ne sont pas par défaut dans les tables filter et nat
604 richard 49
# Flush non default rules on filter and nat tables
1 root 50
$IPTABLES -X
51
$IPTABLES -t nat -X
52
 
53
# On autorise tout sur loopback
604 richard 54
# accept all on loopback
990 franck 55
$IPTABLES -A OUTPUT -o lo -j ACCEPT
1 root 56
$IPTABLES -A INPUT -i lo -j ACCEPT
57
 
856 franck 58
# Insertion de règles de blocage (Devel)
59
# Here, we add block rules (Devel)
60
if [ -s /usr/local/etc/alcasar-iptables-block ]; then 
1062 richard 61
	while read ip_line
62
	do
63
		ip_on=`echo $ip_line|cut -b1`
64
		if [ $ip_on != "#" ]
65
		then	
66
			ip_blocked=`echo $ip_line|cut -d" " -f1`
67
			$IPTABLES -A FORWARD -d $ip_blocked -j ULOG --ulog-prefix "RULE IP-blocked -- REJECT "
68
			$IPTABLES -A FORWARD -d $ip_blocked -j REJECT
69
		fi
70
	done < /usr/local/etc/alcasar-iptables-block
856 franck 71
fi
72
 
990 franck 73
# SSHD rules if activate 
74
if [ $SSH = on ]
75
	then
76
	$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 77
	$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport ssh -m state --state NEW -j ACCEPT
1523 franck 78
	$IPTABLES -A INPUT -i $EXTIF -s $SSH_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW -j ULOG --ulog-nlgroup 2 --ulog-prefix "RULE ssh-from-WAN -- ACCEPT"
1157 stephane 79
	$IPTABLES -A INPUT -i $EXTIF -s $SSH_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW -j ACCEPT
990 franck 80
fi
81
 
1062 richard 82
# Insertion de règles locales
83
# Here, we add local rules (i.e. VPN from Internet)
84
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
85
        . /usr/local/etc/alcasar-iptables-local.sh
86
fi
87
 
1523 franck 88
#  If protocols filter is activate 
89
if [ $PROTOCOLS_FILTERING = on ]; then
90
        echo "PROTOCOL FILTERING = On"
91
        # Compute exception IP (IP addresses that shouldn't be filtered)
92
       nb_exceptions=`wc -l /usr/local/etc/alcasar-filter-exceptions | cut -d" " -f1`
93
       if [ $nb_exceptions != "0" ]
94
       then
95
               while read ip_exception 
96
               do
97
                       $IPTABLES -A FORWARD -i $INTIF -s $ip_exception -m state --state NEW -j ULOG --ulog-prefix "RULE IP-exception -- ACCEPT "
98
#                       $IPTABLES -A FORWARD -i $INTIF -s $ip_exception -m state --state NEW -j NETFLOW
99
                       $IPTABLES -A FORWARD -i $INTIF -s $ip_exception -m state --state NEW -j ACCEPT
100
               done < /usr/local/etc/alcasar-filter-exceptions
101
       fi
102
#       # Compute uamallowed IP (IP address of equipments connected between ALCASAR and Internet (DMZ, own servers, ...) 
103
#       nb_uamallowed=`wc -l /usr/local/etc/alcasar-uamallowed | cut -d" "  -f1`
104
#       if [ $nb_uamallowed != "0" ]
105
#       then
106
#               while read ip_allowed_line 
107
#               do
108
#                       ip_allowed=`echo $ip_allowed_line|cut -d"\"" -f2`
109
#                       $IPTABLES -A FORWARD -i $INTIF -d $ip_allowed -m state --state NEW -j ULOG --ulog-prefix "RULE IP-allowed -- ACCEPT "
110
##                       $IPTABLES -A FORWARD -i $INTIF -d $ip_allowed -m state --state NEW -j NETFLOW
111
#                       $IPTABLES -A FORWARD -i $INTIF -d $ip_allowed -m state --state NEW -j ACCEPT
112
#               done < /usr/local/etc/alcasar-uamallowed
113
#       fi
114
        # Autorisation des protocoles non commentés
115
        # Allow non comment protocols
116
        while read svc_line
117
        do
118
                svc_on=`echo $svc_line|cut -b1`
119
                if [ $svc_on != "#" ]
120
                then
121
                        svc_name=`echo $svc_line|cut -d" " -f1`
122
                        svc_port=`echo $svc_line|cut -d" " -f2`
123
                        if [ $svc_name = "icmp" ]
124
                        then
125
#                               $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp -j NETFLOW
126
                                $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp -j ACCEPT
127
                        else
128
                                echo "Service = $svc_port  pour $svc_name"
129
                                $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p tcp --dport $svc_port -m state --state NEW -j ULOG --ulog-prefix "RULE F_TCP-$svc_name -- ACCEPT "
130
#                               $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p tcp --dport $svc_port -m state --state NEW -j NETFLOW
131
                                $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p tcp --dport $svc_port -m state --state NEW -j ACCEPT
132
#                               $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p udp --dport $svc_port -m state --state NEW -j ULOG --ulog-prefix "RULE F_UDP-$svc_name -- ACCEPT "
133
##                              $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p udp --dport $svc_port -m state --state NEW -j NETFLOW
134
#                               $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p udp --dport $svc_port -m state --state NEW -j ACCEPT
135
                        fi
136
                fi
137
        done < /usr/local/etc/alcasar-services
138
        # Don't forget the HTTP port
139
	$IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p tcp --dport 80 -m state --state NEW -j ULOG --ulog-prefix "RULE F_TCP-http -- ACCEPT "
140
#       $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p tcp --dport 80 -m state --state NEW -j NETFLOW
141
        $IPTABLES -A FORWARD -i $INTIF -s $PRIVATE_NETWORK_MASK -p tcp --dport 80 -m state --state NEW -j ACCEPT
142
 
143
        # Rejet explicite des autres protocoles
144
        # reject the others protocols
145
        $IPTABLES -A FORWARD -i $INTIF -j ULOG --ulog-prefix "RULE F_filter -- REJECT "
146
        $IPTABLES -A FORWARD -i $INTIF -p tcp -j REJECT --reject-with tcp-reset
147
        $IPTABLES -A FORWARD -i $INTIF -p udp -j REJECT --reject-with icmp-port-unreachable
148
        $IPTABLES -A FORWARD -i $INTIF -p icmp -j REJECT
149
 
150
else
151
        ## On autorise les demandes de connexions sortantes
152
        echo "PROTOCOL FILTERING = Off"
153
        $IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ULOG --ulog-prefix "RULE Transfert -- ACCEPT "
154
        $IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ACCEPT
155
fi
156
 
1 root 157
# on autorise les requêtes dhcp
604 richard 158
# accept dhcp
1 root 159
$IPTABLES -A INPUT -i $INTIF -p udp -m udp --sport bootpc --dport bootps -j ACCEPT
160
 
604 richard 161
# On drop le broadcast et le multicast sur les interfaces (sans Log)
162
# Drop broadcast & multicast
1 root 163
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
164
 
604 richard 165
# On laisse passer les ICMP echo-request et echo-reply en provenance du LAN
166
# Allow ping (icmp N°0 & 8) from LAN
1 root 167
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT
168
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT
169
 
604 richard 170
# On autorise les retours de connexions légitimes par FORWARD
171
# Conntrack on forward
1523 franck 172
$IPTABLES -A FORWARD -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
173
$IPTABLES -A FORWARD -p udp -j DROP
1 root 174
 
1062 richard 175
# On autorise les flux entrant ntp et dns via INTIF
401 richard 176
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT
177
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
1 root 178
 
401 richard 179
# On autorise le retour des connexions entrante déjà acceptées
1 root 180
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
181
 
182
# On interdit et on log le reste sur les 2 interfaces d'accès
183
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
184
$IPTABLES -A INPUT -i $EXTIF -j ULOG --ulog-prefix "RULE rej-ext -- REJECT "
185
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
186
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
187
 
188
# On active le masquage d'adresse par translation (NAT)
189
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
190
 
1069 richard 191
# on ne sauvegarde pas les règles. En cas de reboot, on repasse ainsi automatiquement en mode normal (bypass -off)
1 root 192
# Fin du script des regles du parefeu