forked from metalmini/Linux-Firewall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall
292 lines (243 loc) · 8.61 KB
/
firewall
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/sh
#
# /etc/init.d/firewall
# firewall Bring up or down
#
# chkconfig: 2345 11 90
# description: Firewall, use /etc/ports.allow as config to allow certain hosts
#
SETUP=/sbin/iptables
PRIVPORT=1:1023
UNPRIVPORT=1024:65535
ALLPORT=1:65535
UNIVERSE="0.0.0.0/0"
fw_help()
{
echo
echo "Do not edit anything in this file without permission"
echo "This file will be overwritten after each update"
echo
echo "It works like this:"
echo "When you want extra ports open, use file: /etc/ports.allow"
echo "Ports out means open for all ip connections to the port"
echo "NONE=no access, ALL=All have access or use ip numbers seperated by a ','"
echo "priv,unpriv and all can be used instead of port numbers."
echo
}
fw_stop()
{
logger -t `basename $0`[$$] "Firewall stopping"
$SETUP -P INPUT ACCEPT
$SETUP -P OUTPUT ACCEPT
$SETUP -P FORWARD DROP
$SETUP -F
$SETUP -X
#flush and remove user defined chains
chains=`cat /proc/net/ip_tables_names 2>/dev/null`
for i in $chains
do
$SETUP -t $i -F
done
for i in $chains
do
$SETUP -t $i -X
done
for mod in ipt_LOG iptable_nat iptable_filter ip_conntrack ip_conntrack_ftp ipt_limit iptable_mangle ip_tables
do
/sbin/rmmod $mod &>/dev/null
done
logger -t `basename $0`[$$] "Firewall stopped"
}
fw_start()
{
logger -t `basename $0`[$$] "Firewall starting"
$SETUP -P INPUT DROP
$SETUP -P FORWARD DROP
$SETUP -P OUTPUT DROP
$SETUP -F INPUT
$SETUP -F OUTPUT
$SETUP -F FORWARD
#Define log and drop chain
$SETUP -N LDROP
$SETUP -A LDROP -j LOG --log-level 1 --log-prefix 'iptables: LDROP '
$SETUP -A LDROP -j DROP
# Load iptables module for ftp tracking.
/sbin/modprobe ip_conntrack_ftp
# Do xen specific stuff.
if [ -d /proc/xen ]
then
IFCONFIG=/tmp/setup_firewall-ifconfig.$$
/sbin/ifconfig > $IFCONFIG
INTERFACES=`cut -c1-10 $IFCONFIG | grep -v : | grep -v lo`
for interface in $INTERFACES
do
/sbin/ethtool -K $interface tx off
done
fi
# Allow total access to al xendomainU, ,each xendomainU has to run its own firewall.
if [ -x "/usr/sbin/xm" ]
then
$SETUP -A FORWARD -m physdev --physdev-in vif+ -j ACCEPT
$SETUP -A FORWARD -m physdev --physdev-out vif+ -j ACCEPT
fi
#Accept everything from localhost
$SETUP -A INPUT -i lo -j ACCEPT
$SETUP -A OUTPUT -o lo -j ACCEPT
#Accept all icmp
$SETUP -A INPUT -p icmp -j ACCEPT
$SETUP -A OUTPUT -p icmp -j ACCEPT
########Overall policy#######
#Be sure NEW tcp connections are SYN packets
$SETUP -A INPUT -p tcp ! --syn -m state --state NEW -j LDROP
#Test, drop fragments
$SETUP -A INPUT -f -j LOG --log-prefix "IPTABLES FRAGMENTS: "
$SETUP -A INPUT -f -j DROP
#Accept incoming rules from /ets/ports.allow
for line in `cat /etc/ports.allow | grep '^in:'`
do
service=`echo $line | sed -e 's/:/ /g' | awk '{ print $2 }'`
proto=`echo $line | sed -e 's/:/ /g' | awk '{ print $3 }'`
port=`echo $line | sed -e 's/:/ /g' | awk '{ print $4 }'`
if [ $port = "priv" ]
then
port="1:1023"
fi
if [ $port = "unpriv" ]
then
port="1024:65535"
fi
if [ $port = "all" ]
then
port="0:65535"
fi
allhosts=`echo $line | sed -e 's/:/ /g' | awk '{ print $5 }'`
HOSTS=`echo $allhosts | sed -e 's/^.*://'`
HOSTS=`echo $HOSTS | sed -e 's/,/ /g' | cut -d " " -f 1-`
for host in $HOSTS
do
if [ $host = ALL ]
then
host=$UNIVERSE
fi
if [ $host != NONE ]
then
if [ $port = 21 ]
then
## FTP
# Allow ftp inbound.
$SETUP -A INPUT -p tcp -s $host --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
$SETUP -A OUTPUT -p tcp -d $host --sport 21 -m state --state ESTABLISHED -j ACCEPT
# 1) Active ftp.
$SETUP -A INPUT -p tcp -s $host --dport 20 -m state --state ESTABLISHED -j ACCEPT
$SETUP -A OUTPUT -p tcp -d $host --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
# 2) Passive ftp.
$SETUP -A INPUT -p tcp -s $host --sport $UNPRIVPORT --dport $UNPRIVPORT -m state --state ESTABLISHED,RELATED -j ACCEPT
$SETUP -A OUTPUT -p tcp -d $host --sport $UNPRIVPORT --dport $UNPRIVPORT -m state --state ESTABLISHED -j ACCEPT
else
#$SETUP -A INPUT -p $proto -s $host --dport $port -m state --state NEW,ESTABLISHED -j ACCEPT
#$SETUP -A OUTPUT -p $proto -d $host --sport $port -m state --state ESTABLISHED -j ACCEPT
$SETUP -A INPUT -p $proto -s $host --dport $port -j ACCEPT
$SETUP -A OUTPUT -p $proto -d $host --sport $port -j ACCEPT
fi
fi
done
done
#Accept outgoing rules from /etc/hosts.allow, and use /ets/ports.allow
for line in `cat /etc/ports.allow | grep '^out:'`
do
service=`echo $line | sed -e 's/:/ /g' | awk '{ print $2 }'`
proto=`echo $line | sed -e 's/:/ /g' | awk '{ print $3 }'`
port=`echo $line | sed -e 's/:/ /g' | awk '{ print $4 }'`
if [ $port = "priv" ]
then
port="1:1023"
fi
if [ $port = "unpriv" ]
then
port="1024:65535"
fi
if [ $port = "all" ]
then
port="0:65535"
fi
allhosts=`echo $line | sed -e 's/:/ /g' | awk '{ print $5 }'`
HOSTS=`echo $allhosts | sed -e 's/^.*://'`
HOSTS=`echo $HOSTS | sed -e 's/,/ /g' | cut -d " " -f 1-`
for host in $HOSTS
do
if [ $host = ALL ]
then
host=$UNIVERSE
fi
if [ $host = NONE ]
then
POLICY=LDROP
else
POLICY=ACCEPT
fi
if [ $port = 21 ]
then
## FTP
# Allow ftp outbound.
$SETUP -A INPUT -p tcp -s $host --sport 21 -m state --state ESTABLISHED -j $POLICY
$SETUP -A OUTPUT -p tcp -d $host --dport 21 -m state --state NEW,ESTABLISHED -j $POLICY
# 1) Active ftp.
$SETUP -A INPUT -p tcp -s $host --sport 20 -m state --state ESTABLISHED,RELATED -j $POLICY
$SETUP -A OUTPUT -p tcp -d $host --dport 20 -m state --state ESTABLISHED -j $POLICY
# 2) Passive ftp.
$SETUP -A INPUT -p tcp -s $host --sport $UNPRIVPORT --dport $UNPRIVPORT -m state --state ESTABLISHED -j ACCEPT
$SETUP -A OUTPUT -p tcp -d $host --sport $UNPRIVPORT --dport $UNPRIVPORT -m state --state ESTABLISHED,RELATED -j ACCEPT
else
#$SETUP -A INPUT -p $proto -s $host --sport $port -m state --state ESTABLISHED -j $POLICY
#$SETUP -A OUTPUT -p $proto -d $host --dport $port -m state --state NEW,ESTABLISHED -j $POLICY
$SETUP -A INPUT -p $proto -s $host --sport $port -j $POLICY
$SETUP -A OUTPUT -p $proto -d $host --dport $port -j $POLICY
fi
done
done
#Reject AUTH server
# I need to do this for a broken mailhost that won't accept my mail if I just drop its ident probe.
$SETUP -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset
$SETUP -A OUTPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset
## TRACEROUTE
# Outgoing traceroute anywhere.
# The reply to a traceroute is an icmp time-exceeded which is dealt with by the next rule.
$SETUP -A INPUT -p udp --sport $UNPRIVPORT --dport 33434:33523 -m state --state NEW -j ACCEPT
$SETUP -A OUTPUT -p udp --sport $UNPRIVPORT --dport 33434:33523 -m state --state NEW -j ACCEPT
#Drop broadcast before logging
$SETUP -A INPUT -d 255.255.255.255 -j DROP
#Drop netbios before logging
$SETUP -A INPUT -p tcp --dport 137:139 -j DROP
$SETUP -A INPUT -p udp --dport 137:139 -j DROP
#Drop multicasting before logging
$SETUP -A INPUT -d 224.0.0.1 -j DROP
$SETUP -A OUTPUT -d 224.0.0.1 -j DROP
#Reject all tcp connetions to port 25.
$SETUP -A INPUT -p tcp --dport 25 -j REJECT
#Drop all input/output
$SETUP -A INPUT -j LDROP
$SETUP -A OUTPUT -j LDROP
logger -t `basename $0`[$$] "Firewall started"
}
case "$1" in
start)
fw_start
;;
stop)
fw_stop
;;
restart)
fw_stop
sleep 1
fw_start
;;
help)
fw_help
;;
status)
$SETUP -L -n
;;
*)
echo "Usage: `basename $0` {start|stop|restart|status|help}"
exit 1
esac