forked from bmaeser/iptables-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
firewall
363 lines (297 loc) · 13.2 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/bin/bash
### BEGIN INIT INFO
# Provides: firewall
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start/stop iptables-firewall
### END INIT INFO
# Author: Bernhard Mäser <[email protected]>
#################################################################
## PLACE THIS FILE at /etc/init.d/firewall
## MAKE SURE ITS EXECUTABLE (chmod 755 /etc/init.d/firewall)
## UPDATE YOUR RC (update-rc.d firewall defaults)
#################################################################
## BEWARE:
## WE ALLOW ALL --OUTPUT-- IPv4 TRAFFIC BY DEFAULT
#################################################################
IPTABLES=/sbin/iptables
IP6TABLES=/sbin/ip6tables
case "$1" in
start)
#################################################################
# source configuration
#################################################################
if [ -f /etc/firewall/firewall.conf ] ; then
source /etc/firewall/firewall.conf
else
echo "no configuration-file found in /etc/firewall/firewall.conf"
echo "aborting, firewall NOT startet"
exit 1
fi
echo "starting firewall..."
## DELETE OLD RULES
$IPTABLES -F
$IPTABLES -X
#################################################################
# DEFAULT POLICIES
#################################################################
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT ACCEPT
#################################################################
# CUSTOM CHAINS
#################################################################
## portscan drop
$IPTABLES -N portscan_drop
$IPTABLES -A portscan_drop -m limit --limit 60/m -j LOG --log-prefix "PORTSCAN DETECTED"
$IPTABLES -A portscan_drop -j DROP
## invalid drop
$IPTABLES -N invalid_drop
$IPTABLES -A invalid_drop -m state --state INVALID -m limit --limit 60/m -j LOG --log-prefix "INVALID PACKAGE"
$IPTABLES -A invalid_drop -m state --state INVALID -j DROP
#################################################################
# IPv4 FORWARDING
#################################################################
if $ipv4_forwarding ; then
$IPTABLES -P FORWARD ACCEPT
## enable IPv4 forwarding ( ! overwrites sysctl settings ! )
echo 1 > /proc/sys/net/ipv4/ip_forward
else
## drop IPv4 forwarding
$IPTABLES -P FORWARD DROP
## disable IPv4 forwarding ( ! overwrites sysctl settings ! )
echo 0 > /proc/sys/net/ipv4/ip_forward
echo 0 > /proc/sys/net/ipv4/conf/all/forwarding
echo 0 > /proc/sys/net/ipv4/conf/default/forwarding
fi
#################################################################
# ALLOW DEFAULTS
#################################################################
## allow anything on loopback
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A OUTPUT -o lo -j ACCEPT
## allow ICMP
$IPTABLES -A INPUT -p icmp -j ACCEPT
## allow all packets that already have a connection
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
##################################################################
# EXTERNAL CONFIGS
##################################################################
## ACCEPT ALL CONNECTIONS FROM WHITELIST FILE /etc/firewall/ip-whitelist.conf
for IP in `cat /etc/firewall/ip-whitelist.conf | sed 's/\s/_/g' | grep -v "^#"`; do
$IPTABLES -A INPUT -s $IP -m state --state NEW -j ACCEPT
done;
## DROP ALL CONNECTIONS FROM BLACKLIST FILE /etc/firewall/ip-blacklist.conf
for IP in `cat /etc/firewall/ip-blacklist.conf | sed 's/\s/_/g' | grep -v "^#"`; do
$IPTABLES -A INPUT -s $IP -m state --state NEW -j DROP
done;
# EXECUTE ALL CUSTOM SCRIPTS IN /etc/firewall/custom
if [ "$(ls -A /etc/firewall/custom)" ]; then
for F in /etc/firewall/custom/*; do
. $F
done;
fi
# ALLOWED PORTS/PROTOCOLS FROM /etc/firewall/services.conf
cat /etc/firewall/services.conf | grep -v "^#" | while read line; do
[ -z "$line" ] && continue
# INITIALIZE
PORT=""
PROTO=""
IP="0.0.0.0/0"
while IFS=' ' read -ra SERVICES; do
PORT=`echo ${SERVICES[0]} | cut -d"/" -f1`
PROTO=`echo ${SERVICES[0]} | cut -d"/" -f2`
if [ ${#SERVICES[@]} == 2 ]; then
IP=${SERVICES[1]}
fi;
$IPTABLES -A INPUT -p $PROTO -s $IP --dport $PORT -m state --state NEW -j ACCEPT
done <<< "$line"
done;
#################################################################
# drop and log invalid packages
#################################################################
if $drop_invalid ; then
$IPTABLES -A INPUT -m state --state INVALID -j invalid_drop
$IPTABLES -A OUTPUT -m state --state INVALID -j invalid_drop
fi
##################################################################
# BROADCAST AND MULTICAST
##################################################################
if $drop_broadcast ; then
$IPTABLES -A INPUT -m pkttype --pkt-type broadcast -j DROP
$IPTABLES -A INPUT -m pkttype --pkt-type multicast -j DROP
fi
##################################################################
# PORTSCAN DETECTION
##################################################################
if $drop_portscan ; then
## nmap Null scans / no flags
$IPTABLES -A INPUT -p tcp --tcp-flags ALL NONE -j portscan_drop
## nmap FIN stealth scan
$IPTABLES -A INPUT -p tcp --tcp-flags ALL FIN -j portscan_drop
## SYN + FIN
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j portscan_drop
## SYN + RST
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j portscan_drop
## FIN + RST
$IPTABLES -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j portscan_drop
## FIN + URG + PSH
$IPTABLES -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j portscan_drop
## XMAS
$IPTABLES -A INPUT -p tcp --tcp-flags ALL URG,ACK,PSH,RST,SYN,FIN -j portscan_drop
## ALL
$IPTABLES -A INPUT -p tcp --tcp-flags ALL ALL -j portscan_drop
## FIN/PSH/URG without ACK
$IPTABLES -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j portscan_drop
$IPTABLES -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j portscan_drop
$IPTABLES -A INPUT -p tcp --tcp-flags ACK,URG URG -j portscan_drop
fi
##################################################################
# USEFULL OPTIONS ( ! overwrites sysctl settings ! )
##################################################################
if $secure_redirects ; then
## allow only ICMP redirects from our own gateway
echo 1 > /proc/sys/net/ipv4/conf/all/secure_redirects
echo 1 > /proc/sys/net/ipv4/conf/default/secure_redirects
fi
if $block_redirects ; then
## dont accept ICMP redirects
echo 0 > /proc/sys/net/ipv4/conf/all/accept_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/accept_redirects
## dont sent ICMP redirects
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
fi
if $ignore_broadcast_icmp ; then
## ignore broadcast/multicast ICMP // smurf-attack prevention
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
fi
if $ignore_bogus_errors ; then
## ignore bogus error responses
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
fi
## dont ignore pings
echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all
if $block_source_route_packages ; then
## allow no source route packages
echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
echo 0 > /proc/sys/net/ipv4/conf/default/accept_source_route
fi
if $block_proxy_arp ; then
## we dont want to proxy arp
echo 0 > /proc/sys/net/ipv4/conf/all/proxy_arp
echo 0 > /proc/sys/net/ipv4/conf/default/proxy_arp
fi
if $enable_syn_cookies ; then
## enable syn-cookies // syn-flood prevention
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog
echo 5 > /proc/sys/net/ipv4/tcp_syn_retries
echo 5 > /proc/sys/net/ipv4/tcp_synack_retries
fi
if $enable_reverse_path ; then
## enable reverse path filter // RFC1812 // spoofing attack prevention
echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 1 > /proc/sys/net/ipv4/conf/default/rp_filter
fi
if $disable_bootp_relay ; then
## no relaying of bootp
echo 0 > /proc/sys/net/ipv4/conf/all/bootp_relay
echo 0 > /proc/sys/net/ipv4/conf/default/bootp_relay
fi
if $disable_martian_loging ; then
## do not log martian packets
echo 0 > /proc/sys/net/ipv4/conf/all/log_martians
echo 0 > /proc/sys/net/ipv4/conf/default/log_martians
fi
if $disable_srr ; then
## dont allow SRR
echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
echo 0 > /proc/sys/net/ipv4/conf/default/accept_source_route
fi
if $enable_sack ; then
## enable enable_sack
echo 1 > /proc/sys/net/ipv4/tcp_sack
echo 1 > /proc/sys/net/ipv4/tcp_dsack
echo 1 > /proc/sys/net/ipv4/tcp_fack
fi
#################################################################
# IPv6
#################################################################
if $ipv6_loopback_ok ; then
## allow on loopback
$IP6TABLES -A INPUT -i lo -j ACCEPT
$IP6TABLES -A OUTPUT -o lo -j ACCEPT
fi
if $ipv6_drop_all ; then
## drop all ip IPv6 traffic
$IP6TABLES -P INPUT DROP
$IP6TABLES -P OUTPUT DROP
fi
if $ipv6_forwarding ; then
$IP6TABLES -P FORWARD ACCEPT
else
## drop IPv6 forwarding
$IP6TABLES -P FORWARD DROP
## disable IPv6 forwarding ( ! overwrites sysctl settings ! )
echo 0 > /proc/sys/net/ipv6/conf/all/forwarding
echo 0 > /proc/sys/net/ipv6/conf/default/forwarding
fi
if $ipv6_disabled ; then
## disable IPv6 ( ! overwrites sysctl settings ! )
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6
fi
##################################################################
# FINAL RULES
##################################################################
## log all other packages and reject them
$IPTABLES -A INPUT -j LOG
$IPTABLES -A INPUT -j REJECT
echo "firewall started"
;;
stop)
echo "stopping firewall..."
## delete old rules
$IPTABLES -F
$IPTABLES -X
## allow anything
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
## allow all ip IPv6 traffic
$IP6TABLES -P INPUT ACCEPT
$IP6TABLES -P OUTPUT ACCEPT
$IP6TABLES -P FORWARD ACCEPT
echo "firewall stopped"
;;
status)
echo "##################################################################"
echo "## FILTER"
echo "##################################################################"
$IPTABLES -L -vn
echo "##################################################################"
echo "## NAT"
echo "##################################################################"
$IPTABLES -t nat -L -vn
echo "##################################################################"
echo "## MANGLE"
echo "##################################################################"
$IPTABLES -t mangle -L -vn
;;
version)
echo "Version: 1.1.1"
;;
restart|reload|force-reload)
$0 stop
sleep 1
$0 start
;;
*)
echo "Your are doing it wrong."
echo "Syntax: $0 {start|stop|restart|reload|force-reload|status|version}"
exit 1
;;
esac