Subversion Repositories ALCASAR

Rev

Rev 2957 | Rev 3046 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2957 Rev 3043
Line 1... Line 1...
1
#!/bin/bash
1
#!/bin/bash
2
# $Id: alcasar-iptables-bypass.sh 2957 2021-05-26 13:06:56Z rexy $
2
# $Id: alcasar-iptables-bypass.sh 3043 2022-07-22 17:10:23Z rexy $
3
 
3
 
4
# alcasar-iptables-bypass.sh
4
# alcasar-iptables-bypass.sh
5
# by Rexy - 3abtux
5
# by Rexy - 3abtux
6
# This script is distributed under the Gnu General Public License (GPL)
6
# This script is distributed under the Gnu General Public License (GPL)
7
 
7
 
8
# applique les regles du parefeu en mode ByPass
8
# Applique les regles du parefeu en mode ByPass
9
# put the firewall rules in 'ByPass' mode
9
# Set the firewall rules in 'ByPass' mode
10
 
10
 
11
CONF_FILE="/usr/local/etc/alcasar.conf"
11
CONF_FILE="/usr/local/etc/alcasar.conf"
12
private_ip_mask=`grep ^PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
12
private_ip_mask=`grep ^PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
13
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
13
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
14
private_network=`/bin/ipcalc -n $private_ip_mask|cut -d"=" -f2`		# LAN IP address (ie.: 192.168.182.0)
14
private_network=`/bin/ipcalc -n $private_ip_mask|cut -d"=" -f2`		# LAN IP address (ie.: 192.168.182.0)
Line 23... Line 23...
23
then
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"
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`
25
	public_ip_mask=`ip addr show $EXTIF | egrep -o $PTN`
26
fi
26
fi
27
PUBLIC_IP=`echo $public_ip_mask | cut -d"/" -f1`
27
PUBLIC_IP=`echo $public_ip_mask | cut -d"/" -f1`
28
SSH=`grep ^SSH= $CONF_FILE|cut -d"=" -f2`				# sshd active (on/off)
28
SSH_LAN=`grep ^SSH_LAN= $CONF_FILE|cut -d"=" -f2`			# SSH LAN port
29
SSH=${SSH:=off}
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" )
30
SSH_ADMIN_FROM=`grep ^SSH_ADMIN_FROM= $CONF_FILE|cut -d"=" -f2`
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"}
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)
37
SSH_LAN_ADMIN_FROM=$([ "$SSH_LAN_ADMIN_FROM" == "0.0.0.0" ] && echo "$PRIVATE_NETWORK_MASK" || echo "$SSH_LAN_ADMIN_FROM" )
32
 
-
 
33
 
38
 
34
# On vide (flush) toutes les règles existantes
39
# On vide (flush) toutes les règles existantes
35
# Flush all existing rules
40
# Flush all existing rules
36
$IPTABLES -F
41
$IPTABLES -F
37
$IPTABLES -t nat -F
42
$IPTABLES -t nat -F
Line 56... Line 61...
56
# On autorise tout sur loopback
61
# On autorise tout sur loopback
57
# accept all on loopback
62
# accept all on loopback
58
$IPTABLES -A OUTPUT -o lo -j ACCEPT
63
$IPTABLES -A OUTPUT -o lo -j ACCEPT
59
$IPTABLES -A INPUT -i lo -j ACCEPT
64
$IPTABLES -A INPUT -i lo -j ACCEPT
60
 
65
 
61
# Insertion de règles de blocage (Devel)
66
#############################
62
# Here, we add block rules (Devel)
67
#         INPUT             #
63
if [ -s /usr/local/etc/alcasar-ip-blocked ]; then
-
 
64
	while read ip_line
-
 
65
	do
-
 
66
		ip_on=`echo $ip_line|cut -b1`
68
#############################
67
		if [ $ip_on != "#" ]
-
 
68
		then	
-
 
69
			ip_blocked=`echo $ip_line|cut -d" " -f1`
-
 
70
			$IPTABLES -A FORWARD -d $ip_blocked -j NFLOG --nflog-group 1 --nflog-prefix "RULE IP-blocked -- REJECT "
-
 
71
			$IPTABLES -A FORWARD -d $ip_blocked -j REJECT
-
 
72
		fi
-
 
73
	done < /usr/local/etc/alcasar-ip-blocked
-
 
74
fi
-
 
75
 
-
 
76
# SSHD rules if activate
69
# SSHD rules if activate
77
if [ $SSH = on ]
70
if [ $SSH_LAN -gt 0 ]
-
 
71
	then
-
 
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
-
 
74
fi
-
 
75
if [ $SSH_WAN -gt 0 ]
78
	then
76
	then
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"
-
 
80
	$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport ssh -m state --state NEW -j ACCEPT
-
 
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"
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"
82
	$IPTABLES -A INPUT -i $EXTIF -s $SSH_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport ssh -m state --state NEW -j ACCEPT
78
	$IPTABLES -A INPUT -i $EXTIF -s $SSH_WAN_ADMIN_FROM -d $PUBLIC_IP -p tcp --dport $SSH_WAN -j ACCEPT
83
fi
79
fi
-
 
80
 
84
# Insertion de règles locales
81
# Insertion de règles locales
85
# Here, we add local rules (i.e. VPN from Internet)
82
# Here, we add local rules (i.e. VPN from Internet)
86
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
83
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
87
	. /usr/local/etc/alcasar-iptables-local.sh
84
	. /usr/local/etc/alcasar-iptables-local.sh
88
fi
85
fi
Line 93... Line 90...
93
 
90
 
94
# On drop le broadcast et le multicast sur les interfaces (sans Log)
91
# On drop le broadcast et le multicast sur les interfaces (sans Log)
95
# Drop broadcast & multicast
92
# Drop broadcast & multicast
96
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
93
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
97
 
94
 
98
# On laisse passer les ICMP echo-request et echo-reply en provenance du LAN
95
# On autorise l'accès aux services internes
99
# Allow ping (icmp N°0 & 8) from LAN
96
# Allow Internal access
100
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT
97
$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 -p icmp --icmp-type 8 -j ACCEPT
98
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport domain -j ACCEPT	# DNS
102
 
-
 
103
# On autorise les retours de connexions légitimes par FORWARD
-
 
104
# Conntrack on forward
-
 
105
$IPTABLES -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
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
106
 
-
 
107
# On autorise les demandes de connexions sortantes
-
 
108
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j NFLOG --nflog-group 1 --nflog-prefix "RULE Transfert -- ACCEPT "
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
109
$IPTABLES -A FORWARD -i $INTIF -m state --state NEW -j ACCEPT
101
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport https -j ACCEPT	# ACC
110
 
-
 
111
# On autorise les flux entrant ntp et dns via INTIF
-
 
112
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT
102
$IPTABLES -A INPUT -i $INTIF -s $PRIVATE_NETWORK_MASK -d $PRIVATE_IP -p tcp --dport http -j ACCEPT	# ACC
113
$IPTABLES -A INPUT -i $INTIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
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
114
 
104
 
115
# On autorise le retour des connexions entrante déjà acceptées
105
# On autorise le retour des connexions entrante déjà acceptées
116
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
106
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
117
 
107
 
118
# On interdit et on log le reste sur les 2 interfaces d'accès
108
# On interdit et on log le reste sur les 2 interfaces d'accès
119
$IPTABLES -A INPUT -i $INTIF -j NFLOG --nflog-group 1 --nflog-prefix "RULE rej-int -- REJECT "
109
$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 "
110
$IPTABLES -A INPUT -i $EXTIF -j NFLOG --nflog-group 1 --nflog-prefix "RULE rej-ext -- REJECT "
121
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
111
$IPTABLES -A INPUT -p tcp -j REJECT --reject-with tcp-reset
122
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
112
$IPTABLES -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
123
 
113
 
-
 
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
#############################
124
# On active le masquage d'adresse par translation (NAT)
143
# On active le masquage d'adresse par translation (NAT)
125
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
144
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
126
 
145
 
127
# on ne sauvegarde pas les règles. En cas de reboot, on repasse ainsi automatiquement en mode normal (bypass -off)
146
# on ne sauvegarde pas les règles. En cas de reboot, on repasse ainsi automatiquement en mode normal (bypass -off)
128
# Fin du script des regles du parefeu
147
# Fin du script des regles du parefeu