672 |
richard |
1 |
#!/bin/bash
|
64 |
franck |
2 |
# $Id: alcasar-iptables-bypass.sh 3043 2022-07-22 17:10:23Z 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 |
|
3043 |
rexy |
8 |
# Applique les regles du parefeu en mode ByPass
|
|
|
9 |
# Set the firewall rules in 'ByPass' mode
|
672 |
richard |
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`
|
3043 |
rexy |
28 |
SSH_LAN=`grep ^SSH_LAN= $CONF_FILE|cut -d"=" -f2` # SSH LAN port
|
|
|
29 |
SSH_LAN=${SSH_LAN:=0}
|
|
|
30 |
SSH_WAN=`grep ^SSH_WAN= $CONF_FILE|cut -d"=" -f2` # SSH WAN port
|
|
|
31 |
SSH_WAN=${SSH_WAN:=0}
|
|
|
32 |
SSH_WAN_ADMIN_FROM=`grep ^SSH_ADMIN_FROM= $CONF_FILE|cut -d"=" -f2|cut -d"/" -f2`
|
|
|
33 |
SSH_WAN_ADMIN_FROM=${SSH_WAN_ADMIN_FROM:="0.0.0.0"}
|
|
|
34 |
SSH_WAN_ADMIN_FROM=$([ "$SSH_WAN_ADMIN_FROM" == "0.0.0.0" ] && echo "0.0.0.0/0" || echo "$SSH_WAN_ADMIN_FROM" )
|
|
|
35 |
SSH_LAN_ADMIN_FROM=`grep ^SSH_ADMIN_FROM= $CONF_FILE|cut -d"=" -f2|cut -d"/" -f1`
|
|
|
36 |
SSH_LAN_ADMIN_FROM=${SSH_LAN_ADMIN_FROM:="0.0.0.0"}
|
|
|
37 |
SSH_LAN_ADMIN_FROM=$([ "$SSH_LAN_ADMIN_FROM" == "0.0.0.0" ] && echo "$PRIVATE_NETWORK_MASK" || echo "$SSH_LAN_ADMIN_FROM" )
|
1 |
root |
38 |
|
|
|
39 |
# On vide (flush) toutes les règles existantes
|
604 |
richard |
40 |
# Flush all existing rules
|
1 |
root |
41 |
$IPTABLES -F
|
|
|
42 |
$IPTABLES -t nat -F
|
|
|
43 |
$IPTABLES -F INPUT
|
|
|
44 |
$IPTABLES -F FORWARD
|
|
|
45 |
$IPTABLES -F OUTPUT
|
|
|
46 |
|
|
|
47 |
# On indique les politiques par défaut
|
604 |
richard |
48 |
# Default policies
|
1 |
root |
49 |
$IPTABLES -P INPUT DROP
|
|
|
50 |
$IPTABLES -P FORWARD DROP
|
|
|
51 |
$IPTABLES -P OUTPUT ACCEPT
|
|
|
52 |
$IPTABLES -t nat -P PREROUTING ACCEPT
|
|
|
53 |
$IPTABLES -t nat -P POSTROUTING ACCEPT
|
|
|
54 |
$IPTABLES -t nat -P OUTPUT ACCEPT
|
|
|
55 |
|
|
|
56 |
# On efface toutes les chaînes qui ne sont pas par défaut dans les tables filter et nat
|
604 |
richard |
57 |
# Flush non default rules on filter and nat tables
|
1 |
root |
58 |
$IPTABLES -X
|
|
|
59 |
$IPTABLES -t nat -X
|
|
|
60 |
|
|
|
61 |
# On autorise tout sur loopback
|
604 |
richard |
62 |
# accept all on loopback
|
990 |
franck |
63 |
$IPTABLES -A OUTPUT -o lo -j ACCEPT
|
1 |
root |
64 |
$IPTABLES -A INPUT -i lo -j ACCEPT
|
|
|
65 |
|
3043 |
rexy |
66 |
#############################
|
|
|
67 |
# INPUT #
|
|
|
68 |
#############################
|
2454 |
tom.houday |
69 |
# SSHD rules if activate
|
3043 |
rexy |
70 |
if [ $SSH_LAN -gt 0 ]
|
990 |
franck |
71 |
then
|
3043 |
rexy |
72 |
$IPTABLES -A INPUT -i $INTIF -s $SSH_LAN_ADMIN_FROM -d $PRIVATE_IP -p tcp --dport $SSH_LAN -m conntrack --ctstate NEW -j NFLOG --nflog-group 2 --nflog-prefix "RULE ssh-from-LAN -- ACCEPT"
|
|
|
73 |
$IPTABLES -A INPUT -i $INTIF -s $SSH_LAN_ADMIN_FROM -d $PRIVATE_IP -p tcp --dport $SSH_LAN -j ACCEPT
|
990 |
franck |
74 |
fi
|
3043 |
rexy |
75 |
if [ $SSH_WAN -gt 0 ]
|
|
|
76 |
then
|
|
|
77 |
$IPTABLES -A INPUT -i $EXTIF -s $SSH_WAN_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport $SSH_WAN -m conntrack --ctstate NEW --syn -j NFLOG --nflog-group 2 --nflog-prefix "RULE ssh-from-WAN -- ACCEPT"
|
|
|
78 |
$IPTABLES -A INPUT -i $EXTIF -s $SSH_WAN_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport $SSH_WAN -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
|
2454 |
tom.houday |
84 |
. /usr/local/etc/alcasar-iptables-local.sh
|
1062 |
richard |
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 |
|
3043 |
rexy |
95 |
# On autorise l'accès aux services internes
|
|
|
96 |
# Allow Internal access
|
|
|
97 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport domain -j ACCEPT # DNS
|
|
|
98 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport domain -j ACCEPT # DNS
|
|
|
99 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p icmp --icmp-type 8 -j ACCEPT # Réponse ping # ping responce
|
|
|
100 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p icmp --icmp-type 0 -j ACCEPT # Requête ping # ping request
|
|
|
101 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport https -j ACCEPT # ACC
|
|
|
102 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport http -j ACCEPT # ACC
|
|
|
103 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT # Serveur local de temps # local time server
|
1 |
root |
104 |
|
401 |
richard |
105 |
# On autorise le retour des connexions entrante déjà acceptées
|
1 |
root |
106 |
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
107 |
|
|
|
108 |
# On interdit et on log le reste sur les 2 interfaces d'accès
|
2355 |
tom.houday |
109 |
$IPTABLES -A INPUT -i $INTIF -j NFLOG --nflog-group 1 --nflog-prefix "RULE rej-int -- REJECT "
|
|
|
110 |
$IPTABLES -A INPUT -i $EXTIF -j NFLOG --nflog-group 1 --nflog-prefix "RULE rej-ext -- REJECT "
|
1 |
root |
111 |
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
|
|
|
112 |
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
|
|
|
113 |
|
3043 |
rexy |
114 |
#############################
|
|
|
115 |
# FORWARD #
|
|
|
116 |
#############################
|
|
|
117 |
# On autorise les retours de connexions légitimes par FORWARD
|
|
|
118 |
# Conntrack on forward
|
|
|
119 |
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
120 |
|
|
|
121 |
# Insertion de règles de blocage
|
|
|
122 |
# Here, we add block rules
|
|
|
123 |
if [ -s /usr/local/etc/alcasar-ip-blocked ]; then
|
|
|
124 |
while read ip_line
|
|
|
125 |
do
|
|
|
126 |
ip_on=`echo $ip_line|cut -b1`
|
|
|
127 |
if [ $ip_on != "#" ]
|
|
|
128 |
then
|
|
|
129 |
ip_blocked=`echo $ip_line|cut -d" " -f1`
|
|
|
130 |
$IPTABLES -A FORWARD -d $ip_blocked -j NFLOG --nflog-group 1 --nflog-prefix "RULE IP-blocked -- REJECT "
|
|
|
131 |
$IPTABLES -A FORWARD -d $ip_blocked -j REJECT
|
|
|
132 |
fi
|
|
|
133 |
done < /usr/local/etc/alcasar-ip-blocked
|
|
|
134 |
fi
|
|
|
135 |
|
|
|
136 |
# On autorise les demandes de connexions sortantes
|
|
|
137 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j NFLOG --nflog-group 1 --nflog-prefix "RULE Transfert -- ACCEPT "
|
|
|
138 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ACCEPT
|
|
|
139 |
|
|
|
140 |
#############################
|
|
|
141 |
# POSTROUTING #
|
|
|
142 |
#############################
|
1 |
root |
143 |
# On active le masquage d'adresse par translation (NAT)
|
|
|
144 |
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
|
|
|
145 |
|
1069 |
richard |
146 |
# on ne sauvegarde pas les règles. En cas de reboot, on repasse ainsi automatiquement en mode normal (bypass -off)
|
1 |
root |
147 |
# Fin du script des regles du parefeu
|