firewall: add more pscan matches and filter INVALID conntrack packages.
[ipfire-2.x.git] / src / initscripts / init.d / firewall
CommitLineData
3a1019f6
MT
1#!/bin/sh
2
0f5c5ce7
MT
3. /etc/sysconfig/rc
4. ${rc_functions}
5
3a1019f6
MT
6eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings)
7eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
fe0cd647 8eval $(/usr/local/bin/readhash /var/ipfire/optionsfw/settings)
3a1019f6
MT
9IFACE=`/bin/cat /var/ipfire/red/iface 2> /dev/null | /usr/bin/tr -d '\012'`
10
11if [ -f /var/ipfire/red/device ]; then
12 DEVICE=`/bin/cat /var/ipfire/red/device 2> /dev/null | /usr/bin/tr -d '\012'`
13fi
14
c581b670
MT
15function iptables() {
16 /sbin/iptables --wait "$@"
17}
18
3a1019f6
MT
19iptables_init() {
20 # Flush all rules and delete all custom chains
c581b670
MT
21 iptables -F
22 iptables -t nat -F
23 iptables -t mangle -F
24 iptables -X
25 iptables -t nat -X
26 iptables -t mangle -X
3a1019f6
MT
27
28 # Set up policies
c581b670
MT
29 iptables -P INPUT DROP
30 iptables -P FORWARD DROP
31 iptables -P OUTPUT ACCEPT
3a1019f6
MT
32
33 # Empty LOG_DROP and LOG_REJECT chains
c581b670
MT
34 iptables -N LOG_DROP
35 iptables -A LOG_DROP -m limit --limit 10/minute -j LOG
36 iptables -A LOG_DROP -j DROP
37 iptables -N LOG_REJECT
38 iptables -A LOG_REJECT -m limit --limit 10/minute -j LOG
39 iptables -A LOG_REJECT -j REJECT
3a1019f6
MT
40
41 # This chain will log, then DROPs packets with certain bad combinations
42 # of flags might indicate a port-scan attempt (xmas, null, etc)
c581b670 43 iptables -N PSCAN
5595bc03 44 if [ "$DROPPORTSCAN" == "on" ]; then
c581b670
MT
45 iptables -A PSCAN -p tcp -m limit --limit 10/minute -j LOG --log-prefix "DROP_TCP Scan " -m comment --comment "DROP_TCP PScan"
46 iptables -A PSCAN -p udp -m limit --limit 10/minute -j LOG --log-prefix "DROP_UDP Scan " -m comment --comment "DROP_UDP PScan"
47 iptables -A PSCAN -p icmp -m limit --limit 10/minute -j LOG --log-prefix "DROP_ICMP Scan " -m comment --comment "DROP_ICMP PScan"
48 iptables -A PSCAN -f -m limit --limit 10/minute -j LOG --log-prefix "DROP_FRAG Scan " -m comment --comment "DROP_FRAG PScan"
5595bc03 49 fi
c581b670 50 iptables -A PSCAN -j DROP -m comment --comment "DROP_PScan"
3a1019f6
MT
51
52 # New tcp packets without SYN set - could well be an obscure type of port scan
53 # that's not covered above, may just be a broken windows machine
c581b670 54 iptables -N NEWNOTSYN
5595bc03 55 if [ "$DROPNEWNOTSYN" == "on" ]; then
c581b670 56 iptables -A NEWNOTSYN -m limit --limit 10/minute -j LOG --log-prefix "DROP_NEWNOTSYN "
5595bc03 57 fi
c581b670 58 iptables -A NEWNOTSYN -j DROP -m comment --comment "DROP_NEWNOTSYN"
3a1019f6
MT
59
60 # Chain to contain all the rules relating to bad TCP flags
c581b670 61 iptables -N BADTCP
3a1019f6 62
c581b670
MT
63 # Don't check loopback
64 iptables -A BADTCP -i lo -j RETURN
d8158ca6 65
3a1019f6 66 # Disallow packets frequently used by port-scanners
93899a21
AF
67 # NMAP FIN/URG/PSH (XMAS scan)
68 iptables -A BADTCP -p tcp --tcp-flags ALL FIN,URG,PSH -j PSCAN
69 # SYN/RST/ACK/FIN/URG
70 iptables -A BADTCP -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j PSCAN
71 # ALL/ALL
72 iptables -A BADTCP -p tcp --tcp-flags ALL ALL -j PSCAN
73 # FIN Stealth
c581b670 74 iptables -A BADTCP -p tcp --tcp-flags ALL FIN -j PSCAN
3a1019f6 75 # SYN/RST (also catches xmas variants that set SYN+RST+...)
c581b670 76 iptables -A BADTCP -p tcp --tcp-flags SYN,RST SYN,RST -j PSCAN
3a1019f6 77 # SYN/FIN (QueSO or nmap OS probe)
c581b670 78 iptables -A BADTCP -p tcp --tcp-flags SYN,FIN SYN,FIN -j PSCAN
93899a21
AF
79 # Null
80 iptables -A BADTCP -p tcp --tcp-flags ALL NONE -j PSCAN
3a1019f6 81 # NEW TCP without SYN
c581b670 82 iptables -A BADTCP -p tcp ! --syn -m conntrack --ctstate NEW -j NEWNOTSYN
b85d2a98 83
c581b670
MT
84 iptables -A INPUT -p tcp -j BADTCP
85 iptables -A FORWARD -p tcp -j BADTCP
c0359d6d 86
b85d2a98 87 # Connection tracking chain
c581b670
MT
88 iptables -N CONNTRACK
89 iptables -A CONNTRACK -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
93899a21 90 iptables -A CONNTRACK -m conntrack --ctstate INVALID -j DROP
3a1019f6 91
3a1019f6 92 # Fix for braindead ISP's
c581b670 93 iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
3a1019f6
MT
94
95 # CUSTOM chains, can be used by the users themselves
c581b670
MT
96 iptables -N CUSTOMINPUT
97 iptables -A INPUT -j CUSTOMINPUT
98 iptables -N CUSTOMFORWARD
99 iptables -A FORWARD -j CUSTOMFORWARD
100 iptables -N CUSTOMOUTPUT
101 iptables -A OUTPUT -j CUSTOMOUTPUT
102 iptables -t nat -N CUSTOMPREROUTING
103 iptables -t nat -A PREROUTING -j CUSTOMPREROUTING
104 iptables -t nat -N CUSTOMPOSTROUTING
105 iptables -t nat -A POSTROUTING -j CUSTOMPOSTROUTING
3a1019f6 106
815eaff4 107 # Guardian (IPS) chains
c581b670
MT
108 iptables -N GUARDIAN
109 iptables -A INPUT -j GUARDIAN
110 iptables -A FORWARD -j GUARDIAN
815eaff4 111
1e555330 112 # Block OpenVPN transfer networks
c581b670 113 iptables -N OVPNBLOCK
a0a5c14f 114 iptables -A INPUT -i tun+ -j OVPNBLOCK
a0a5c14f
MT
115 iptables -A FORWARD -i tun+ -j OVPNBLOCK
116 iptables -A FORWARD -o tun+ -j OVPNBLOCK
1e555330 117
51ab1de1 118 # OpenVPN transfer network translation
c581b670
MT
119 iptables -t nat -N OVPNNAT
120 iptables -t nat -A POSTROUTING -j OVPNNAT
51ab1de1 121
daa1ceba 122 # IPTV chains for IGMPPROXY
c581b670
MT
123 iptables -N IPTVINPUT
124 iptables -A INPUT -j IPTVINPUT
125 iptables -N IPTVFORWARD
126 iptables -A FORWARD -j IPTVFORWARD
daa1ceba 127
8e59a602
MT
128 # Allow to ping the firewall.
129 iptables -N ICMPINPUT
130 iptables -A INPUT -j ICMPINPUT
131 iptables -A ICMPINPUT -p icmp --icmp-type 8 -j ACCEPT
3a1019f6 132
afc611d4 133 # Accept everything on loopback
c581b670
MT
134 iptables -N LOOPBACK
135 iptables -A LOOPBACK -i lo -j ACCEPT
136 iptables -A LOOPBACK -o lo -j ACCEPT
afc611d4 137
3b9a23ce 138 # Filter all packets with loopback addresses on non-loopback interfaces.
c581b670
MT
139 iptables -A LOOPBACK -s 127.0.0.0/8 -j DROP
140 iptables -A LOOPBACK -d 127.0.0.0/8 -j DROP
3b9a23ce
MT
141
142 for i in INPUT FORWARD OUTPUT; do
c581b670 143 iptables -A ${i} -j LOOPBACK
3b9a23ce 144 done
afc611d4 145
3a1019f6 146 # Accept everything connected
b85d2a98 147 for i in INPUT FORWARD OUTPUT; do
c581b670 148 iptables -A ${i} -j CONNTRACK
b85d2a98
MT
149 done
150
8490e496
MT
151 # Allow DHCP
152 iptables -N DHCPINPUT
153 iptables -A DHCPINPUT -p udp --sport 68 --dport 67 -j ACCEPT
154 iptables -A DHCPINPUT -p tcp --sport 68 --dport 67 -j ACCEPT
155
156 iptables -N DHCPOUTPUT
157 iptables -A DHCPOUTPUT -p udp --sport 67 --dport 68 -j ACCEPT
158 iptables -A DHCPOUTPUT -p tcp --sport 67 --dport 68 -j ACCEPT
159
160 # Allow DHCP on GREEN
161 iptables -N DHCPGREENINPUT
162 iptables -N DHCPGREENOUTPUT
163 if [ -n "${GREEN_DEV}" ]; then
164 iptables -A INPUT -i "${GREEN_DEV}" -j DHCPGREENINPUT
165 iptables -A OUTPUT -o "${GREEN_DEV}" -j DHCPGREENOUTPUT
166 fi
167
168 # allow DHCP on BLUE to be turned on/off
169 iptables -N DHCPBLUEINPUT
170 iptables -N DHCPBLUEOUTPUT
171 if [ -n "${BLUE_DEV}" ]; then
172 iptables -A INPUT -i "${BLUE_DEV}" -j DHCPBLUEINPUT
173 iptables -A OUTPUT -o "${BLUE_DEV}" -j DHCPBLUEOUTPUT
174 fi
175
5fd30232 176 # trafic from ipsecX/TUN/TAP interfaces, before "-i GREEN_DEV" accept everything
c581b670
MT
177 iptables -N IPSECINPUT
178 iptables -N IPSECFORWARD
179 iptables -N IPSECOUTPUT
180 iptables -A INPUT -j IPSECINPUT
181 iptables -A FORWARD -j IPSECFORWARD
182 iptables -A OUTPUT -j IPSECOUTPUT
183 iptables -t nat -N IPSECNAT
184 iptables -t nat -A POSTROUTING -j IPSECNAT
b68e5c14 185
3a1019f6 186 # localhost and ethernet.
c0e0848f
MT
187 # Always allow accessing the web GUI from GREEN.
188 iptables -N GUIINPUT
189 iptables -A INPUT -j GUIINPUT
190 iptables -A GUIINPUT -i "${GREEN_DEV}" -p tcp --dport 444 -j ACCEPT
8490e496 191
81393987 192 # WIRELESS chains
c581b670
MT
193 iptables -N WIRELESSINPUT
194 iptables -A INPUT -m conntrack --ctstate NEW -j WIRELESSINPUT
195 iptables -N WIRELESSFORWARD
196 iptables -A FORWARD -m conntrack --ctstate NEW -j WIRELESSFORWARD
987b75bc 197
ab4876ad 198 # OpenVPN
c581b670
MT
199 iptables -N OVPNINPUT
200 iptables -A INPUT -j OVPNINPUT
ab4876ad 201
987b75bc 202 # TOR
c581b670
MT
203 iptables -N TOR_INPUT
204 iptables -A INPUT -j TOR_INPUT
12dcfbbd 205
d5f1422d 206 # Jump into the actual firewall ruleset.
c581b670
MT
207 iptables -N INPUTFW
208 iptables -A INPUT -j INPUTFW
d5f1422d 209
c581b670
MT
210 iptables -N OUTGOINGFW
211 iptables -A OUTPUT -j OUTGOINGFW
d5f1422d 212
c581b670
MT
213 iptables -N FORWARDFW
214 iptables -A FORWARD -j FORWARDFW
d5f1422d 215
fac38614 216 # SNAT rules
c581b670
MT
217 iptables -t nat -N NAT_SOURCE
218 iptables -t nat -A POSTROUTING -j NAT_SOURCE
fac38614 219
bb12dd7b 220 # Custom prerouting chains (for transparent proxy)
c581b670
MT
221 iptables -t nat -N SQUID
222 iptables -t nat -A PREROUTING -j SQUID
bb12dd7b
MT
223
224 # DNAT rules
c581b670
MT
225 iptables -t nat -N NAT_DESTINATION
226 iptables -t nat -A PREROUTING -j NAT_DESTINATION
99f11a16 227 iptables -t nat -A OUTPUT -j NAT_DESTINATION
bb12dd7b 228
6e87f0aa
MT
229 iptables -t mangle -N NAT_DESTINATION
230 iptables -t mangle -A PREROUTING -j NAT_DESTINATION
231
232 iptables -t nat -N NAT_DESTINATION_FIX
233 iptables -t nat -A POSTROUTING -j NAT_DESTINATION_FIX
234
235 iptables -t nat -A NAT_DESTINATION_FIX \
236 -m mark --mark 1 -j SNAT --to-source "${GREEN_ADDRESS}"
237
238 if [ -n "${BLUE_ADDRESS}" ]; then
239 iptables -t nat -A NAT_DESTINATION_FIX \
240 -m mark --mark 2 -j SNAT --to-source "${BLUE_ADDRESS}"
241 fi
242
243 if [ -n "${ORANGE_ADDRESS}" ]; then
244 iptables -t nat -A NAT_DESTINATION_FIX \
245 -m mark --mark 3 -j SNAT --to-source "${ORANGE_ADDRESS}"
246 fi
247
7e7495b3 248 # upnp chain for our upnp daemon
c581b670
MT
249 iptables -t nat -N UPNPFW
250 iptables -t nat -A PREROUTING -j UPNPFW
251 iptables -N UPNPFW
252 iptables -A FORWARD -m conntrack --ctstate NEW -j UPNPFW
3a1019f6 253
6e87f0aa
MT
254 # RED chain, used for the red interface
255 iptables -N REDINPUT
256 iptables -A INPUT -j REDINPUT
257 iptables -N REDFORWARD
258 iptables -A FORWARD -j REDFORWARD
259 iptables -t nat -N REDNAT
260 iptables -t nat -A POSTROUTING -j REDNAT
261
ab4876ad
MT
262 # Apply OpenVPN firewall rules
263 /usr/local/bin/openvpnctrl --firewall-rules
ff4770c7
AM
264
265 # run wirelessctrl
266 /usr/local/bin/wirelessctrl
267
c581b670
MT
268 # POLICY CHAIN
269 iptables -N POLICYIN
270 iptables -A INPUT -j POLICYIN
271 iptables -N POLICYFWD
272 iptables -A FORWARD -j POLICYFWD
273 iptables -N POLICYOUT
274 iptables -A OUTPUT -j POLICYOUT
b324de14 275
bb383423 276 # Initialize firewall policies.
5d7faa45 277 /usr/sbin/firewall-policy
690b0bd7 278
bb383423
MT
279 # Install firewall rules for the red interface.
280 iptables_red
ff4770c7 281}
3a1019f6 282
ff4770c7 283iptables_red() {
c581b670
MT
284 iptables -F REDINPUT
285 iptables -F REDFORWARD
286 iptables -t nat -F REDNAT
3a1019f6 287
ff4770c7
AM
288 # PPPoE / PPTP Device
289 if [ "$IFACE" != "" ]; then
290 # PPPoE / PPTP
291 if [ "$DEVICE" != "" ]; then
c581b670 292 iptables -A REDINPUT -i $DEVICE -j ACCEPT
ff4770c7
AM
293 fi
294 if [ "$RED_TYPE" == "PPTP" -o "$RED_TYPE" == "PPPOE" ]; then
295 if [ "$RED_DEV" != "" ]; then
c581b670 296 iptables -A REDINPUT -i $RED_DEV -j ACCEPT
ff4770c7
AM
297 fi
298 fi
3a1019f6 299 fi
ff4770c7
AM
300
301 # PPTP over DHCP
302 if [ "$DEVICE" != "" -a "$TYPE" == "PPTP" -a "$METHOD" == "DHCP" ]; then
c581b670
MT
303 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
304 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $DEVICE -j ACCEPT
3a1019f6
MT
305 fi
306
ff4770c7
AM
307 # Orange pinholes
308 if [ "$ORANGE_DEV" != "" ]; then
309 # This rule enables a host on ORANGE network to connect to the outside
310 # (only if we have a red connection)
311 if [ "$IFACE" != "" ]; then
c581b670 312 iptables -A REDFORWARD -i $ORANGE_DEV -o $IFACE -j ACCEPT
ff4770c7 313 fi
3a1019f6 314 fi
c400fe4c 315
ff4770c7
AM
316 if [ "$IFACE" != "" -a -f /var/ipfire/red/active ]; then
317 # DHCP
318 if [ "$RED_DEV" != "" -a "$RED_TYPE" == "DHCP" ]; then
c581b670
MT
319 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
320 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
ff4770c7
AM
321 fi
322 if [ "$METHOD" == "DHCP" -a "$PROTOCOL" == "RFC1483" ]; then
c581b670
MT
323 iptables -A REDINPUT -p tcp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
324 iptables -A REDINPUT -p udp --source-port 67 --destination-port 68 -i $IFACE -j ACCEPT
ff4770c7
AM
325 fi
326
327 # Outgoing masquerading (don't masqerade IPSEC (mark 50))
c581b670 328 iptables -t nat -A REDNAT -m mark --mark 50 -o $IFACE -j RETURN
c926c637
AF
329
330 if [ "$IFACE" != "$GREEN_DEV" ]; then
331 iptables -t nat -A REDNAT -o $IFACE -j MASQUERADE
332 fi
c400fe4c 333
6be0579b 334 fi
66f6b279
MT
335
336 # Reload all rules.
55a5bcae 337 /usr/local/bin/firewallctrl
ff4770c7
AM
338}
339
340# See how we were called.
341case "$1" in
342 start)
cdb725da
MT
343 boot_mesg "Loading firewall modules into the kernel"
344 modprobe iptable_nat || failed=1
345 for i in $(find /lib/modules/$(uname -r) -name nf_conntrack*); do
346 modprobe $(basename $i | cut -d. -f1) || failed=1
347 done
348 for i in $(find /lib/modules/$(uname -r) -name nf_nat*); do
349 modprobe $(basename $i | cut -d. -f1) || failed=1
350 done
351 (exit ${failed})
352 evaluate_retval
353
354 if [ -e /var/ipfire/main/disable_nf_sip ]; then
355 rmmod nf_nat_sip
356 rmmod nf_conntrack_sip
357 rmmod nf_nat_h323
358 rmmod nf_conntrack_h323
359 fi
360
7d7740a4 361 boot_mesg "Setting up firewall"
ff4770c7 362 iptables_init
7d7740a4
MT
363 evaluate_retval
364
159c55c5
MT
365 # run local firewall configuration, if present
366 if [ -x /etc/sysconfig/firewall.local ]; then
367 /etc/sysconfig/firewall.local start
368 fi
6be0579b 369 ;;
3a1019f6 370 reload)
7d7740a4 371 boot_mesg "Reloading firewall"
3a1019f6 372 iptables_red
7d7740a4
MT
373 evaluate_retval
374
3a1019f6 375 # run local firewall configuration, if present
ff4770c7 376 if [ -x /etc/sysconfig/firewall.local ]; then
3a1019f6
MT
377 /etc/sysconfig/firewall.local reload
378 fi
379 ;;
380 restart)
ff4770c7
AM
381 # run local firewall configuration, if present
382 if [ -x /etc/sysconfig/firewall.local ]; then
383 /etc/sysconfig/firewall.local stop
384 fi
3a1019f6
MT
385 $0 start
386 ;;
387 *)
ff4770c7 388 echo "Usage: $0 {start|reload|restart}"
3a1019f6
MT
389 exit 1
390 ;;
391esac
392
393exit 0