672 |
richard |
1 |
#!/bin/bash
|
64 |
franck |
2 |
# $Id: alcasar-iptables-bypass.sh 1888 2016-05-12 21:53:44Z 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)
|
1888 |
richard |
58 |
if [ -s /usr/local/etc/alcasar-ip-blocked ]; 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
|
1888 |
richard |
68 |
done < /usr/local/etc/alcasar-ip-blocked
|
856 |
franck |
69 |
fi
|
|
|
70 |
|
990 |
franck |
71 |
# SSHD rules if activate
|
|
|
72 |
if [ $SSH = on ]
|
|
|
73 |
then
|
1888 |
richard |
74 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport ssh -m state --state NEW -j NFLOG --nflog-group 2 --nflog-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
|
1888 |
richard |
76 |
$IPTABLES -A INPUT -i $EXTIF -s $SSH_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW --syn -j NFLOG --nflog-group 2 --nflog-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
|
1062 |
richard |
79 |
# Insertion de règles locales
|
|
|
80 |
# Here, we add local rules (i.e. VPN from Internet)
|
|
|
81 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
|
|
82 |
. /usr/local/etc/alcasar-iptables-local.sh
|
|
|
83 |
fi
|
|
|
84 |
|
1 |
root |
85 |
# on autorise les requêtes dhcp
|
604 |
richard |
86 |
# accept dhcp
|
1 |
root |
87 |
$IPTABLES -A INPUT -i $INTIF -p udp -m udp --sport bootpc --dport bootps -j ACCEPT
|
|
|
88 |
|
604 |
richard |
89 |
# On drop le broadcast et le multicast sur les interfaces (sans Log)
|
|
|
90 |
# Drop broadcast & multicast
|
1 |
root |
91 |
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
|
|
|
92 |
|
604 |
richard |
93 |
# On laisse passer les ICMP echo-request et echo-reply en provenance du LAN
|
|
|
94 |
# Allow ping (icmp N°0 & 8) from LAN
|
1 |
root |
95 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT
|
|
|
96 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT
|
|
|
97 |
|
604 |
richard |
98 |
# On autorise les retours de connexions légitimes par FORWARD
|
|
|
99 |
# Conntrack on forward
|
1551 |
richard |
100 |
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
|
1 |
root |
101 |
|
1551 |
richard |
102 |
# On autorise les demandes de connexions sortantes
|
1888 |
richard |
103 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j NFLOG --nflog-prefix "RULE Transfert -- ACCEPT "
|
1551 |
richard |
104 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ACCEPT
|
|
|
105 |
|
1062 |
richard |
106 |
# On autorise les flux entrant ntp et dns via INTIF
|
401 |
richard |
107 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT
|
|
|
108 |
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
|
1 |
root |
109 |
|
401 |
richard |
110 |
# On autorise le retour des connexions entrante déjà acceptées
|
1 |
root |
111 |
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
112 |
|
|
|
113 |
# On interdit et on log le reste sur les 2 interfaces d'accès
|
1888 |
richard |
114 |
$IPTABLES -A INPUT -i $INTIF -j NFLOG --nflog-prefix "RULE rej-int -- REJECT "
|
|
|
115 |
$IPTABLES -A INPUT -i $EXTIF -j NFLOG --nflog-prefix "RULE rej-ext -- REJECT "
|
1 |
root |
116 |
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
|
|
|
117 |
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
|
|
|
118 |
|
|
|
119 |
# On active le masquage d'adresse par translation (NAT)
|
|
|
120 |
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
|
|
|
121 |
|
1069 |
richard |
122 |
# on ne sauvegarde pas les règles. En cas de reboot, on repasse ainsi automatiquement en mode normal (bypass -off)
|
1 |
root |
123 |
# Fin du script des regles du parefeu
|