Subversion Repositories ALCASAR

Rev

Rev 478 | Rev 493 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
1 root 1
#!/bin/sh
64 franck 2
# $Id: alcasar-iptables.sh 492 2011-02-14 06:40:53Z franck $
1 root 3
# script de mise en place des regles du parefeu d'Alcasar (mode normal)
4
# Rexy - 3abtux - CPN
476 richard 5
# there are three channels for log : 1 (default) for tracability, 2 for secure admin (ssh), 3 for exterior access attempts,
1 root 6
 
7
IPTABLES="/sbin/iptables"
478 richard 8
FILTERING="no"
492 franck 9
QOS="no"
1 root 10
EXTIF="eth0"
11
INTIF="eth1"
12
TUNIF="tun0"
411 richard 13
PRIVATE_NETWORK_MASK="192.168.182.0/24"
14
PRIVATE_IP="192.168.182.1"
1 root 15
 
476 richard 16
# Flush all existing rules
1 root 17
$IPTABLES -F
18
$IPTABLES -t nat -F
19
$IPTABLES -t mangle -F
20
$IPTABLES -F INPUT
21
$IPTABLES -F FORWARD
22
$IPTABLES -F OUTPUT
492 franck 23
$IPTABLES -N SYN-FLOOD
1 root 24
 
476 richard 25
# Default policies
1 root 26
$IPTABLES -P INPUT DROP
27
$IPTABLES -P FORWARD DROP
28
$IPTABLES -P OUTPUT ACCEPT
29
$IPTABLES -t nat -P PREROUTING ACCEPT
30
$IPTABLES -t nat -P POSTROUTING ACCEPT
31
$IPTABLES -t nat -P OUTPUT ACCEPT
32
 
476 richard 33
# Flush non default rules on filter and nat tables
1 root 34
$IPTABLES -X
35
$IPTABLES -t nat -X
36
 
476 richard 37
# accept all on loopback
1 root 38
$IPTABLES -A INPUT -i lo -j ACCEPT
39
 
492 franck 40
# Block all attempts to spoof the loopback address
41
$IPTABLES -A INPUT -s 127.0.0.0/8 -j DROP
42
$IPTABLES -A INPUT -d 127.0.0.0/8 -j DROP
43
 
44
# Block all attempts to spoof the local IP address
45
$IPTABLES -A INPUT -s $PRIVATE_IP -j DROP
46
 
47
# Block Syn Flood attacks
48
$IPTABLES -A INPUT -p tcp -m tcp --syn -j SYN-FLOOD
49
 
50
# Syn flood filtering chain
51
$IPTABLES -A SYN-FLOOD -m limit --limit 1/s --limit-burst 4 -j RETURN
52
$IPTABLES -A SYN-FLOOD -j DROP
53
 
54
# Ensure that TCP connections start with syn packets
55
$IPTABLES -A INPUT -p tcp -m tcp ! --syn -m state --state NEW -j DROP
56
 
472 richard 57
#############################
58
#       INTIF rules         #
59
#############################
476 richard 60
# accept dhcp
1 root 61
$IPTABLES -A INPUT -i $INTIF -p udp -m udp --sport bootpc --dport bootps -j ACCEPT
476 richard 62
# INTIF is closed (all by TUNIF)
1 root 63
$IPTABLES -A INPUT -i $INTIF -j ULOG --ulog-prefix "RULE Protect1 -- REJECT "
64
$IPTABLES -A INPUT -i $INTIF -j REJECT
65
 
472 richard 66
#############################
67
#  Local protection rules   #
68
#############################
476 richard 69
# Drop XMAS & NULLscans 
472 richard 70
$IPTABLES -A INPUT -p tcp --tcp-flags FIN,URG,PSH FIN,URG,PSH -j DROP
71
$IPTABLES -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
72
$IPTABLES -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
73
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
476 richard 74
# Drop broadcast & multicast
472 richard 75
$IPTABLES -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
476 richard 76
# Antispoofing rules with log
1 root 77
$IPTABLES -A INPUT -i $TUNIF ! -s $PRIVATE_NETWORK_MASK -j ULOG --ulog-prefix "RULE Antispoof1 -- DENY "
78
$IPTABLES -A INPUT -i $TUNIF ! -s $PRIVATE_NETWORK_MASK -j DROP
79
$IPTABLES -A INPUT -i $EXTIF -s $PRIVATE_NETWORK_MASK -j ULOG --ulog-prefix "RULE Antispoof2 -- DENY "
80
$IPTABLES -A INPUT -i $EXTIF -s $PRIVATE_NETWORK_MASK -j DROP
476 richard 81
# Allow ping (icmp N°0 & 8) from LAN
1 root 82
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 0 -j ACCEPT
83
$IPTABLES -A INPUT -i $TUNIF -s $PRIVATE_NETWORK_MASK -p icmp --icmp-type 8 -j ACCEPT
84
 
476 richard 85
#  Here, we add local rules (i.e. ssh from Internet)
300 franck 86
if [ -f /usr/local/etc/alcasar-iptables-local.sh ]; then
87
        . /usr/local/etc/alcasar-iptables-local.sh
187 franck 88
fi
373 richard 89
 
476 richard 90
# Deny forward DNS (even for authenticated users ...)
373 richard 91
$IPTABLES -A FORWARD -i $TUNIF -p udp --dport domain -j REJECT --reject-with icmp-port-unreachable
92
$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport domain -j REJECT --reject-with tcp-reset
93
 
476 richard 94
# Conntrack on forward
373 richard 95
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
96
 
476 richard 97
#####################################
98
#  If protocols filter is activate  #
99
#####################################
373 richard 100
if [ $FILTERING = "yes" ]; then
476 richard 101
	# Compute exception IP
373 richard 102
	nb_exceptions=`wc -w /usr/local/etc/alcasar-filter-exceptions | cut -d" " -f1`
103
	if [ $nb_exceptions != "0" ]
104
	then
105
		while read ip_exception 
106
		do
107
			echo $ip_exception 
108
			$IPTABLES -A FORWARD -i $TUNIF -s $ip_exception -m state --state NEW -j ULOG --ulog-prefix "RULE IP-exception -- ACCEPT "
109
			$IPTABLES -A FORWARD -i $TUNIF -s $ip_exception -m state --state NEW -j ACCEPT
110
		done < /usr/local/etc/alcasar-filter-exceptions
111
	fi
476 richard 112
	# Allow non comment protocols
373 richard 113
	while read svc_line
114
	do
115
		svc_on=`echo $svc_line|cut -b1`
116
		if [ $svc_on != "#" ]
117
		then	
118
			svc_name=`echo $svc_line|cut -d" " -f1`
119
			svc_port=`echo $svc_line|cut -d" " -f2`
120
			if [ $svc_name = "icmp" ]
121
			then
122
				$IPTABLES -A FORWARD -i $TUNIF -p icmp -j ACCEPT 
123
			else
124
				$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport $svc_port -m state --state NEW -j ULOG --ulog-prefix "RULE F_$svc_name -- ACCEPT "
125
				$IPTABLES -A FORWARD -i $TUNIF -p tcp --dport $svc_port -m state --state NEW -j ACCEPT
126
			fi
127
		fi
128
	done < /usr/local/etc/alcasar-services
476 richard 129
	# reject the others
373 richard 130
	$IPTABLES -A FORWARD -i $TUNIF -p tcp -j ULOG --ulog-prefix "RULE F_filter -- REJECT "
131
	$IPTABLES -A FORWARD -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
132
	$IPTABLES -A FORWARD -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
133
	$IPTABLES -A FORWARD -i $TUNIF -p icmp -j REJECT 
1 root 134
fi
492 franck 135
 
136
########################
137
#  If QOS is activate  #
138
########################
139
if [ $QOS = "yes" ]; then
140
	. /usr/local/etc/alcasar-iptables-qos.sh 	
141
fi
142
 
476 richard 143
# Allow forward connections with log
144
$IPTABLES -A FORWARD -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE F_all -- ACCEPT "
411 richard 145
$IPTABLES -A FORWARD -i $TUNIF -m state --state NEW -j ACCEPT
1 root 146
 
476 richard 147
####################################################################################
148
#  Imput from local network (dns, ntp, https, http, ssh and 3990 (user disconnect) #
149
####################################################################################
339 richard 150
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p udp --dport domain -j ACCEPT
151
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p udp --dport ntp -j ACCEPT
152
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport https -j ACCEPT
153
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport http -j ACCEPT
476 richard 154
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport ssh -m state --state NEW -j ULOG --ulog-nlgroup 2 --ulog-prefix "RULE ssh-from-LAN -- ACCEPT"
339 richard 155
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport ssh -j ACCEPT
156
$IPTABLES -A INPUT -i $TUNIF -d $PRIVATE_IP -p tcp --dport 3990 -j ACCEPT
1 root 157
 
476 richard 158
# Conntrack on INPUT
1 root 159
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
160
 
476 richard 161
# Deny direct connections on DansGuardian port (8080)
162
# The concerned paquets are marked by a pre-routing rule (see further)
1 root 163
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8080 -m mark --mark 1 -j DROP
476 richard 164
# Allow connections for DansGuardian
1 root 165
$IPTABLES -A INPUT -i $TUNIF -p tcp --dport 8080 -m state --state NEW --syn -j ACCEPT
166
 
476 richard 167
# Log HTTP requests (only syn)
168
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp ! -d $PRIVATE_IP --dport http -m state --state NEW -j ULOG --ulog-prefix "RULE F_http -- ACCEPT "
169
# Redirect HTTP request in DansGuardian (transparent proxy)
1 root 170
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp ! -d $PRIVATE_IP --dport http -j REDIRECT --to-port 8080
476 richard 171
# Mark the dansguardian bypass attempts
1 root 172
$IPTABLES -A PREROUTING -t nat -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8080 -j ULOG --ulog-prefix "RULE direct-proxy -- DENY "
173
$IPTABLES -A PREROUTING -t mangle -i $TUNIF -p tcp -d $PRIVATE_IP -m tcp --dport 8080 -j MARK --set-mark 1
174
 
476 richard 175
# Deny and log on INPUT from the LAN
176
$IPTABLES -A INPUT -i $TUNIF -m state --state NEW -j ULOG --ulog-prefix "RULE rej-int -- REJECT "
472 richard 177
$IPTABLES -A INPUT -i $TUNIF -p tcp -j REJECT --reject-with tcp-reset
178
$IPTABLES -A INPUT -i $TUNIF -p udp -j REJECT --reject-with icmp-port-unreachable
492 franck 179
 
476 richard 180
# On EXTIF, the access attempts are log in channel 2 (we should test --limit option to avoid deny of service)
181
$IPTABLES -A INPUT -i $EXTIF -m state --state NEW -j ULOG --ulog-nlgroup 3 --ulog-qthreshold 10 --ulog-prefix "RULE rej-ext -- DROP"
182
# Drop on EXTIF
472 richard 183
$IPTABLES -A INPUT -i $EXTIF -j DROP
1 root 184
 
476 richard 185
# Dynamic NAT on EXTIF
1 root 186
$IPTABLES -A POSTROUTING -t nat -o $EXTIF -j MASQUERADE
187
 
476 richard 188
# Save all rules
1 root 189
/etc/init.d/iptables save
190
 
476 richard 191
# no martians log (for mdv2009 only)
1 root 192
echo 0 > /proc/sys/net/ipv4/conf/all/log_martians
193
 
476 richard 194
# End of script
1 root 195