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