672 |
richard |
1 |
#!/bin/bash
|
64 |
franck |
2 |
# $Id: alcasar-iptables-bypass.sh 1056 2013-03-26 21:46:36Z stephane $
|
|
|
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
|
1 |
root |
21 |
|
|
|
22 |
# On vide (flush) toutes les règles existantes
|
604 |
richard |
23 |
# Flush all existing rules
|
1 |
root |
24 |
$IPTABLES -F
|
|
|
25 |
$IPTABLES -t nat -F
|
|
|
26 |
$IPTABLES -F INPUT
|
|
|
27 |
$IPTABLES -F FORWARD
|
|
|
28 |
$IPTABLES -F OUTPUT
|
|
|
29 |
|
|
|
30 |
# On indique les politiques par défaut
|
604 |
richard |
31 |
# Default policies
|
1 |
root |
32 |
$IPTABLES -P INPUT DROP
|
|
|
33 |
$IPTABLES -P FORWARD DROP
|
|
|
34 |
$IPTABLES -P OUTPUT ACCEPT
|
|
|
35 |
$IPTABLES -t nat -P PREROUTING ACCEPT
|
|
|
36 |
$IPTABLES -t nat -P POSTROUTING ACCEPT
|
|
|
37 |
$IPTABLES -t nat -P OUTPUT ACCEPT
|
|
|
38 |
|
|
|
39 |
# On efface toutes les chaînes qui ne sont pas par défaut dans les tables filter et nat
|
604 |
richard |
40 |
# Flush non default rules on filter and nat tables
|
1 |
root |
41 |
$IPTABLES -X
|
|
|
42 |
$IPTABLES -t nat -X
|
|
|
43 |
|
|
|
44 |
# On autorise tout sur loopback
|
604 |
richard |
45 |
# accept all on loopback
|
990 |
franck |
46 |
$IPTABLES -A OUTPUT -o lo -j ACCEPT
|
1 |
root |
47 |
$IPTABLES -A INPUT -i lo -j ACCEPT
|
|
|
48 |
|
856 |
franck |
49 |
# Insertion de règles de blocage (Devel)
|
|
|
50 |
# Here, we add block rules (Devel)
|
|
|
51 |
if [ -s /usr/local/etc/alcasar-iptables-block ]; then
|
|
|
52 |
while read ip_blocked
|
|
|
53 |
do
|
|
|
54 |
echo "Network Address blocked : $ip_blocked"
|
|
|
55 |
$IPTABLES -A FORWARD -d $ip_blocked -j ULOG --ulog-prefix "RULE IP-blocked -- REJECT "
|
|
|
56 |
$IPTABLES -A FORWARD -d $ip_blocked -j REJECT
|
|
|
57 |
$IPTABLES -A FORWARD -s $ip_blocked -j ULOG --ulog-prefix "RULE IP-blocked -- REJECT "
|
|
|
58 |
$IPTABLES -A FORWARD -s $ip_blocked -j REJECT
|
|
|
59 |
done < /usr/local/etc/alcasar-iptables-block
|
|
|
60 |
fi
|
|
|
61 |
|
990 |
franck |
62 |
# SSHD rules if activate
|
|
|
63 |
if [ $SSH = on ]
|
|
|
64 |
then
|
|
|
65 |
$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"
|
|
|
66 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport ssh -j ACCEPT
|
|
|
67 |
$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"
|
|
|
68 |
$IPTABLES -A INPUT -i $EXTIF -s $Admin_from_IP -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW,ESTABLISHED -j ACCEPT
|
|
|
69 |
$IPTABLES -A OUTPUT -o $EXTIF -p tcp --sport ssh -m state --state ESTABLISHED -j ACCEPT
|
|
|
70 |
fi
|
|
|
71 |
|
1 |
root |
72 |
# on autorise les requêtes dhcp
|
604 |
richard |
73 |
# accept dhcp
|
1 |
root |
74 |
$IPTABLES -A INPUT -i $INTIF -p udp -m udp --sport bootpc --dport bootps -j ACCEPT
|
|
|
75 |
|
604 |
richard |
76 |
# On drop le broadcast et le multicast sur les interfaces (sans Log)
|
|
|
77 |
# Drop broadcast & multicast
|
1 |
root |
78 |
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
|
|
|
79 |
|
604 |
richard |
80 |
# On laisse passer les ICMP echo-request et echo-reply en provenance du LAN
|
|
|
81 |
# Allow ping (icmp N°0 & 8) from LAN
|
1 |
root |
82 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT
|
|
|
83 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT
|
|
|
84 |
|
362 |
franck |
85 |
# On ajoute ici les règles spécifiques de filtrage réseau (accès exterieur ...)
|
|
|
86 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
|
|
87 |
. /usr/local/etc/alcasar-iptables-local.sh
|
|
|
88 |
fi
|
1 |
root |
89 |
|
604 |
richard |
90 |
# On autorise les retours de connexions légitimes par FORWARD
|
|
|
91 |
# Conntrack on forward
|
401 |
richard |
92 |
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
|
1 |
root |
93 |
|
411 |
richard |
94 |
# On autorise les demandes de connexions sortantes
|
|
|
95 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ULOG --ulog-prefix "RULE Transfert -- ACCEPT "
|
|
|
96 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ACCEPT
|
|
|
97 |
|
401 |
richard |
98 |
# On autorise les flux entrant ntp, dns et ssh via INTIF
|
|
|
99 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT
|
|
|
100 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
|
|
|
101 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p tcp --dport ssh -j ACCEPT
|
1 |
root |
102 |
|
401 |
richard |
103 |
# On autorise le retour des connexions entrante déjà acceptées
|
1 |
root |
104 |
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
105 |
|
|
|
106 |
# On interdit et on log le reste sur les 2 interfaces d'accès
|
|
|
107 |
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
|
|
|
108 |
$IPTABLES -A INPUT -i $EXTIF -j ULOG --ulog-prefix "RULE rej-ext -- REJECT "
|
|
|
109 |
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
|
|
|
110 |
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
|
|
|
111 |
|
|
|
112 |
# On active le masquage d'adresse par translation (NAT)
|
|
|
113 |
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
|
|
|
114 |
|
|
|
115 |
/etc/init.d/iptables save
|
|
|
116 |
# Fin du script des regles du parefeu
|