Subversion Repositories ALCASAR

Rev

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