672 |
richard |
1 |
#!/bin/bash
|
64 |
franck |
2 |
# $Id: alcasar-iptables-bypass.sh 1062 2013-04-01 21:20:12Z 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 |
|
767 |
richard |
11 |
conf_file="/usr/local/etc/alcasar.conf"
|
|
|
12 |
private_ip_mask=`grep PRIVATE_IP= $conf_file|cut -d"=" -f2`
|
|
|
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"
|
|
|
17 |
EXTIF="eth0"
|
|
|
18 |
INTIF="eth1"
|
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
|
1062 |
richard |
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`
|
|
|
23 |
SSH=`grep SSH= $conf_file|cut -d"=" -f2` # sshd active (on/off)
|
|
|
24 |
SSH=${SSH:=off}
|
|
|
25 |
SSH_ADMIN_IP=`grep SSH_ADMIN_IP= $conf_file|cut -d"=" -f2`
|
|
|
26 |
SSH_ADMIN_IP=${SSH_ADMIN_IP:="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"
|
|
|
75 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport ssh -j ACCEPT
|
1062 |
richard |
76 |
$IPTABLES -A INPUT -i $EXTIF -s $SSH_ADMIN_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"
|
|
|
77 |
$IPTABLES -A INPUT -i $EXTIF -s $SSH_ADMIN_IP -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW,ESTABLISHED -j ACCEPT
|
990 |
franck |
78 |
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --sport ssh -m state --state ESTABLISHED -j ACCEPT
|
|
|
79 |
fi
|
|
|
80 |
|
1062 |
richard |
81 |
# Insertion de règles locales
|
|
|
82 |
# Here, we add local rules (i.e. VPN from Internet)
|
|
|
83 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
|
|
84 |
. /usr/local/etc/alcasar-iptables-local.sh
|
|
|
85 |
fi
|
|
|
86 |
|
1 |
root |
87 |
# on autorise les requêtes dhcp
|
604 |
richard |
88 |
# accept dhcp
|
1 |
root |
89 |
$IPTABLES -A INPUT -i $INTIF -p udp -m udp --sport bootpc --dport bootps -j ACCEPT
|
|
|
90 |
|
604 |
richard |
91 |
# On drop le broadcast et le multicast sur les interfaces (sans Log)
|
|
|
92 |
# Drop broadcast & multicast
|
1 |
root |
93 |
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
|
|
|
94 |
|
604 |
richard |
95 |
# On laisse passer les ICMP echo-request et echo-reply en provenance du LAN
|
|
|
96 |
# Allow ping (icmp N°0 & 8) from LAN
|
1 |
root |
97 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT
|
|
|
98 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT
|
|
|
99 |
|
362 |
franck |
100 |
# On ajoute ici les règles spécifiques de filtrage réseau (accès exterieur ...)
|
|
|
101 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
|
|
102 |
. /usr/local/etc/alcasar-iptables-local.sh
|
|
|
103 |
fi
|
1 |
root |
104 |
|
604 |
richard |
105 |
# On autorise les retours de connexions légitimes par FORWARD
|
|
|
106 |
# Conntrack on forward
|
401 |
richard |
107 |
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
|
1 |
root |
108 |
|
411 |
richard |
109 |
# On autorise les demandes de connexions sortantes
|
|
|
110 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ULOG --ulog-prefix "RULE Transfert -- ACCEPT "
|
|
|
111 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ACCEPT
|
|
|
112 |
|
1062 |
richard |
113 |
# On autorise les flux entrant ntp et dns via INTIF
|
401 |
richard |
114 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT
|
|
|
115 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
|
1 |
root |
116 |
|
401 |
richard |
117 |
# On autorise le retour des connexions entrante déjà acceptées
|
1 |
root |
118 |
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
119 |
|
|
|
120 |
# On interdit et on log le reste sur les 2 interfaces d'accès
|
|
|
121 |
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
|
|
|
122 |
$IPTABLES -A INPUT -i $EXTIF -j ULOG --ulog-prefix "RULE rej-ext -- REJECT "
|
|
|
123 |
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
|
|
|
124 |
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
|
|
|
125 |
|
|
|
126 |
# On active le masquage d'adresse par translation (NAT)
|
|
|
127 |
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
|
|
|
128 |
|
|
|
129 |
/etc/init.d/iptables save
|
|
|
130 |
# Fin du script des regles du parefeu
|