672 |
richard |
1 |
#!/bin/bash
|
64 |
franck |
2 |
# $Id: alcasar-iptables-bypass.sh 3046 2022-07-30 22:07:33Z 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" )
|
3046 |
rexy |
38 |
interlan=`grep ^INTERLAN= $CONF_FILE|cut -d"=" -f2`
|
|
|
39 |
interlan=${interlan:=off}
|
1 |
root |
40 |
|
|
|
41 |
# On vide (flush) toutes les règles existantes
|
604 |
richard |
42 |
# Flush all existing rules
|
1 |
root |
43 |
$IPTABLES -F
|
|
|
44 |
$IPTABLES -t nat -F
|
|
|
45 |
$IPTABLES -F INPUT
|
|
|
46 |
$IPTABLES -F FORWARD
|
|
|
47 |
$IPTABLES -F OUTPUT
|
|
|
48 |
|
|
|
49 |
# On indique les politiques par défaut
|
604 |
richard |
50 |
# Default policies
|
1 |
root |
51 |
$IPTABLES -P INPUT DROP
|
|
|
52 |
$IPTABLES -P FORWARD DROP
|
|
|
53 |
$IPTABLES -P OUTPUT ACCEPT
|
|
|
54 |
$IPTABLES -t nat -P PREROUTING ACCEPT
|
|
|
55 |
$IPTABLES -t nat -P POSTROUTING ACCEPT
|
|
|
56 |
$IPTABLES -t nat -P OUTPUT ACCEPT
|
|
|
57 |
|
|
|
58 |
# On efface toutes les chaînes qui ne sont pas par défaut dans les tables filter et nat
|
604 |
richard |
59 |
# Flush non default rules on filter and nat tables
|
1 |
root |
60 |
$IPTABLES -X
|
|
|
61 |
$IPTABLES -t nat -X
|
|
|
62 |
|
|
|
63 |
# On autorise tout sur loopback
|
604 |
richard |
64 |
# accept all on loopback
|
990 |
franck |
65 |
$IPTABLES -A OUTPUT -o lo -j ACCEPT
|
1 |
root |
66 |
$IPTABLES -A INPUT -i lo -j ACCEPT
|
|
|
67 |
|
3043 |
rexy |
68 |
#############################
|
|
|
69 |
# INPUT #
|
|
|
70 |
#############################
|
2454 |
tom.houday |
71 |
# SSHD rules if activate
|
3043 |
rexy |
72 |
if [ $SSH_LAN -gt 0 ]
|
990 |
franck |
73 |
then
|
3043 |
rexy |
74 |
$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"
|
|
|
75 |
$IPTABLES -A INPUT -i $INTIF -s $SSH_LAN_ADMIN_FROM -d $PRIVATE_IP -p tcp --dport $SSH_LAN -j ACCEPT
|
990 |
franck |
76 |
fi
|
3043 |
rexy |
77 |
if [ $SSH_WAN -gt 0 ]
|
|
|
78 |
then
|
|
|
79 |
$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"
|
|
|
80 |
$IPTABLES -A INPUT -i $EXTIF -s $SSH_WAN_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport $SSH_WAN -j ACCEPT
|
|
|
81 |
fi
|
|
|
82 |
|
1062 |
richard |
83 |
# Insertion de règles locales
|
|
|
84 |
# Here, we add local rules (i.e. VPN from Internet)
|
|
|
85 |
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
|
2454 |
tom.houday |
86 |
. /usr/local/etc/alcasar-iptables-local.sh
|
1062 |
richard |
87 |
fi
|
|
|
88 |
|
1 |
root |
89 |
# on autorise les requêtes dhcp
|
604 |
richard |
90 |
# accept dhcp
|
1 |
root |
91 |
$IPTABLES -A INPUT -i $INTIF -p udp -m udp --sport bootpc --dport bootps -j ACCEPT
|
|
|
92 |
|
604 |
richard |
93 |
# On drop le broadcast et le multicast sur les interfaces (sans Log)
|
|
|
94 |
# Drop broadcast & multicast
|
1 |
root |
95 |
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
|
|
|
96 |
|
3043 |
rexy |
97 |
# On autorise l'accès aux services internes
|
|
|
98 |
# Allow Internal access
|
|
|
99 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p udp --dport domain -j ACCEPT # DNS
|
|
|
100 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport domain -j ACCEPT # DNS
|
|
|
101 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p icmp --icmp-type 8 -j ACCEPT # Réponse ping # ping responce
|
|
|
102 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p icmp --icmp-type 0 -j ACCEPT # Requête ping # ping request
|
|
|
103 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport https -j ACCEPT # ACC
|
|
|
104 |
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport http -j ACCEPT # ACC
|
|
|
105 |
$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 |
106 |
|
401 |
richard |
107 |
# On autorise le retour des connexions entrante déjà acceptées
|
1 |
root |
108 |
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
109 |
|
|
|
110 |
# On interdit et on log le reste sur les 2 interfaces d'accès
|
2355 |
tom.houday |
111 |
$IPTABLES -A INPUT -i $INTIF -j NFLOG --nflog-group 1 --nflog-prefix "RULE rej-int -- REJECT "
|
|
|
112 |
$IPTABLES -A INPUT -i $EXTIF -j NFLOG --nflog-group 1 --nflog-prefix "RULE rej-ext -- REJECT "
|
1 |
root |
113 |
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
|
|
|
114 |
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
|
|
|
115 |
|
3043 |
rexy |
116 |
#############################
|
|
|
117 |
# FORWARD #
|
|
|
118 |
#############################
|
|
|
119 |
# On autorise les retours de connexions légitimes par FORWARD
|
|
|
120 |
# Conntrack on forward
|
|
|
121 |
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
122 |
|
3046 |
rexy |
123 |
# On autorise (ou pas) les utilisateurs à accéder au réseau situé entre ALCASAR et le routeur Internet
|
|
|
124 |
# Users are allowed (or not allowed) to access the network between ALCASAR and the Internet router
|
|
|
125 |
if [ "$interlan" != "on" ]
|
|
|
126 |
then
|
|
|
127 |
$IPTABLES -A FORWARD -i $TUNIF -d $public_ip_mask -j DROP
|
|
|
128 |
fi
|
|
|
129 |
|
3043 |
rexy |
130 |
# Insertion de règles de blocage
|
|
|
131 |
# Here, we add block rules
|
|
|
132 |
if [ -s /usr/local/etc/alcasar-ip-blocked ]; then
|
|
|
133 |
while read ip_line
|
|
|
134 |
do
|
|
|
135 |
ip_on=`echo $ip_line|cut -b1`
|
|
|
136 |
if [ $ip_on != "#" ]
|
|
|
137 |
then
|
|
|
138 |
ip_blocked=`echo $ip_line|cut -d" " -f1`
|
|
|
139 |
$IPTABLES -A FORWARD -d $ip_blocked -j NFLOG --nflog-group 1 --nflog-prefix "RULE IP-blocked -- REJECT "
|
|
|
140 |
$IPTABLES -A FORWARD -d $ip_blocked -j REJECT
|
|
|
141 |
fi
|
|
|
142 |
done < /usr/local/etc/alcasar-ip-blocked
|
|
|
143 |
fi
|
|
|
144 |
|
|
|
145 |
# On autorise les demandes de connexions sortantes
|
|
|
146 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j NFLOG --nflog-group 1 --nflog-prefix "RULE Transfert -- ACCEPT "
|
|
|
147 |
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ACCEPT
|
|
|
148 |
|
|
|
149 |
#############################
|
|
|
150 |
# POSTROUTING #
|
|
|
151 |
#############################
|
1 |
root |
152 |
# On active le masquage d'adresse par translation (NAT)
|
|
|
153 |
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
|
|
|
154 |
|
1069 |
richard |
155 |
# on ne sauvegarde pas les règles. En cas de reboot, on repasse ainsi automatiquement en mode normal (bypass -off)
|
1 |
root |
156 |
# Fin du script des regles du parefeu
|