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