Typo
[linux_from_scratch_hints.git] / OLD / firewall.txt
blobc5248959c937d7405c11ed2b36a18b0dc9fb8735
1 TITLE:          Firewall.txt
2 LFS VERSION:    any
3                 Kernel   >= 2.4.9
4                 iptables >= 1.2.3
5 AUTHOR:         Henning Rohde   Henning.Rohde(at)uni-bayreuth.de
7 SYNOPSIS:       
8         Question:       What's a firewall?
9         Answer:         a fire-resistant wall separating a building into 
10                         departments, designed to prevent the spread of fire
11 And on Networks?        A box that restricts the malicious (eg crackers, 
12                         worms, trojans) out of your intranet.
13 How Do I build one?     fetch iptables and read the following:
15 Personally I prefer the following definition:
16 Answer:         A wall of fire that only the Saints can pass through!
17 @Networks:      Just a box, that permits only sane packets to pass!
19 HINT:
20 --------------
21 |Introduction|
22 --------------
23 The general purpose of a firewall is to save the labour of securing every
24 box by securing a single firewall.
25 This means that the firewall is a single point of failure, but it makes
26 the admin's life a lot easier.
28 But please, don't assume that having a firewall makes careful 
29 configuration redundant!
31 If you knew every daemon or service on every machine was correctly configured
32 and trustworthy, and trusted every user accessing your services to cause 
33 no harm, you wouldn't need to do firewalling!
34 But if you'd like to choose which services are accessible only from your 
35 intranet, which machines or applications are allowed to have internet access,
36 or if you don't trust some of your apps or users, you might benefit by using
37 a firewall.
39 When you read the word "firewall" there's more than one way to interpret it:
40 a) "Personal Firewall": 
41         Program sold by e.g., Symantec, that is claimed to secure a home / 
42         desktop-pc with internet access. Quite relevant for users being 
43         always online with (flat rate) broadband links.
44 b) Firewall as it was originally meant: 
45         A box placed between the internet and intranet doing, besides routing, 
46         nothing but protecting the intranet. 
47         This could include the function of masquerading: rewriting IP-headers
48         of the packets it routes from clients with private IP-adresses onto 
49         the internet, so that they seem to come from the firewall itself.
50 c) Firewall offering services:
51         Some old box you may have retired and nearly forgotten, doing (B), 
52         but offering a bunch of services, e.g., web-cache, mail, etc.
53         This may be very commonly used for home networks, but it severely 
54         violates some principles of (B).
55 d) Firewall with a demilitarized zone   [not described here]:
56         Doing (B), but giving public access to some branch of your network,
57         that is, because of public IP's and a physically separated structure,
58         neither considered to be part of the inter- nor intranet.
59         Here those servers are connected that must be easily accessible
60         from both the inter- and intranet. The firewall protects them all.
61 e) Packetfilter / partly accessible net [only partly described here, see (C)]:
62         Doing (B) but permitting only selected services to be accessible,
63         sometimes only by selected internal users or boxes; mostly used in
64         highly secure business contexts, sometimes by distrusting employers.
65         This was the common configuration of a firewall at the time of 
66         the Linux 2.2 kernel.
67         It's still possible to configure a firewall this way, but it makes 
68         the rules quite complex and lengthy.
71 ------------
72 |DISCLAIMER|
73 ------------
74 I AM NOT RESPONSIBLE FOR ANY DAMAGES INCURRED DUE TO ACTIONS TAKEN BASED 
75 ON THIS DOCUMENT. 
76 This document is meant as an introduction to how to setup a firewall. 
77 I am not, nor do I pretend to be, a security expert.            ;-)
78 I am just some guy who still has not read enough and whose computers
79 still like to play tricks on him if he wants to tweak them.     ;-)
80 Please, I am writing this to help people get acquainted with this subject, 
81 and I am not ready to stake my life on the accuracy of what is in here.
82 (Taken from www.linuxdoc.org/HOWTO/Firewall-HOWTO.html, slightly modified)
84 FIREWALL-SECURITY IS A COMPLEX ISSUE THAT REQUIRES GOOD CONFIGURATION.
85 The scripts quoted here are not at the very least meant to give you 
86 the one and perfect firewall, being perfect until the universe collapses.
87 They may not fit into any imaginable configuration and may not prevent 
88 any imaginable attack.
90 The purpose of this text is simply to give you a hint on how to get started
91 with firewalling.
93 Customization of these scripts for your specific situation is likely to be 
94 necessary for an optimal configuration, but you should make a serious study 
95 of the iptables documentation and firewalling in general before hacking away.
96 Have a look at Appendix I. There you'll find a list of URLs that contain quite 
97 comprehensive information about building your own firewall.
100 --------------------------------------
101 |Getting a firewalling-enabled Kernel|
102 --------------------------------------
103 If you want your Linux-Box to do firewalling you must first ensure that you 
104 have an appropriate kernel and the appropriate tools:
106 But, before you do a 'make menuconfig', consider patching your kernel with 
107 the latest iptables enhancements. To get them, download the latest version 
108 of iptables from http://netfilter.samba.org.
110 Having current kernel sources in /usr/src/linux, unpack iptables and 'cd' into 
111 its top-level directory.
112 Now enter 'make pending-patches', as an user who is allowed to patch the kernel.
113 There's no need to apply every patch, but e.g. the ipt_REJECT-checkentry-patch
114 could be sensible, depending on your needs. 
116 Applying a patch may result in errors, mostly because the hooks for the
117 patches have changed or because the runme script doesn't recognize that 
118 a patch has already been incorporated.
119 If you do get errors don't worry too much; the important thing is that
120 iptables has scanned the kernel for the already accepted / incorporated 
121 patches. The default kernel should be adaequate for most needs!
123 Now configure the kernel:
124 Personally I prefer to have a maximally modularized kernel, but for highest
125 security you could configure the kernel with this code built in:
126     Networking options:
127         Network packet filtering        = CONFIG_NETFILTER
128         IP: TCP/IP networking           = CONFIG_INET
129         IP: advanced router             = CONFIG_IP_ADVANCED_ROUTER
130         IP: verbose route monitoring    = CONFIG_IP_ROUTE_VERBOSE
131         IP: TCP Explicit Congestion Notification support
132                                         = CONFIG_INET_ECN
133         IP: TCP syncookie support       = CONFIG_SYN_COOKIES
134         IP: Netfilter Configuration: every option
135                                         = CONFIG_IP_NF_*
136                 BUT NO ipchains- and ipfwadm-compatibility.
137                                         w\ CONFIG_IP_NF_COMPAT_*
138     Although you are unlikely to have enabled it, please make sure not to 
139     select "Fast switching"             w\ CONFIG_NET_FASTROUTE
140     because it would currently bypass your firewalling-rules.
142 Now compile and install your new kernel, update your bootloader and reboot.
144 -------------------
145 |Building iptables|
146 -------------------
147 Before compiling you might want to edit the Makefile to adapt install-dir's.
148 Now compile and install iptables via 'make && make install'.
151 -----------------------------------------
152 |Now we can start to build your Firewall|
153 -----------------------------------------
154 (A) 
155 A Personal Firewall is supposed to let you access the all services offered 
156 on the internet, but keep your box secure and your data private.
158 Below is a slightly modified version of Rusty Russel's recommendation,
159 (http://netfilter.samba.org/unreliable-guides/packet-filtering-HOWTO):
161 #!/bin/sh
162 ##/etc/init.d/firewall
163 # Insert connection-tracking modules    (not needed if built into kernel).
164 modprobe ip_tables
165 modprobe iptable_filter
166 modprobe ip_conntrack
167 modprobe ip_conntrack_ftp
168 modprobe ipt_state
169 modprobe ipt_LOG
170 # free output on any interface to any ip for any service   (equal to -P ACCEPT)
171 iptables -A OUTPUT                                              -j ACCEPT
172 # permit answers on already established connections
173 # and permit new connections related to established ones (eg active-ftp)
174 iptables -A INPUT       -m state --state ESTABLISHED,RELATED    -j ACCEPT
175 # Log everything else:  What's the latest exploitable vulnerability?
176 iptables -A INPUT               -j LOG --log-prefix "FIREWALL:INPUT  "
177 # set a sane policy:    everything not accepted > /dev/null
178 iptables -P INPUT               DROP
179 iptables -P FORWARD             DROP
180 iptables -P OUTPUT              DROP
181 # be verbose on dynamic ip-adresses     (not needed in case of static IP)
182 echo 2 > /proc/sys/net/ipv4/ip_dynaddr
183 # disable ExplicitCongestionNotification - too many routers are still ignorant
184 echo 0 > /proc/sys/net/ipv4/tcp_ecn
186 His script is quite simple, but simply surfing the internet you are unlikely 
187 to exceed its limits.
189 Even if you have daemons / services running on your box, these should be
190 inaccessible everywhere but on your box itself.
191 The case to be cautious about is misconfigured daemons that could broadcast
192 to the public to announce their service.
193 If you have such daemons (e.g., cups, samba) running and if you are not 
194 confident in their configuration, restrict OUTPUT and INPUT, see (C) and (E).
198 A true Firewall has two interfaces, one connected to an intranet, 
199 in this example, eth0, and one connected to the internet, here, ppp0.
200 To provide the maximum security against the box itself being broken into, e.g.,
201 by exploiting an offered service, make sure that there are no servers running 
202 on it, especially not X11 et al., and, as a principle, that it does not itself
203 access any services:
205 #!/bin/sh
206 ##/etc/init.d/firewall
207 echo
208 echo -n "You're using the example-config "
209 echo    "for a setup of a firewall "
210 echo -n "from the firewalling-hint "
211 echo    "written for LinuxFromScratch. "
212 echo -n "This example is far from being "
213 echo    "complete, it is only meant "
214 echo    "to be a reference. "
215 echo -n "Firewall security one could rely "
216 echo    "on is a complex issue, "
217 echo -n "that exceeds the scope of the "
218 echo    "quoted configuration rules. "
219 echo -n "You can find some quite "
220 echo    "comprehensive information "
221 echo    "about firewalling at Appendix I of "
222 echo -n "http://hints.linuxfromscratch.org/hints/firewall.txt"
223 echo    "."
224 echo    "Be cautious!"
225 modprobe ip_tables
226 modprobe iptable_filter
227 modprobe ip_conntrack
228 modprobe ip_conntrack_ftp
229 modprobe ipt_state
230 modprobe iptable_nat
231 modprobe ip_nat_ftp
232 modprobe ipt_MASQUERADE
233 modprobe ipt_LOG
234 modprobe ipt_REJECT             # needed for (C), example 4
235 # allow local-only connections
236 iptables -A INPUT       -i lo                                   -j ACCEPT
237 iptables -A OUTPUT              -o lo                           -j ACCEPT
238 # allow forwarding
239 iptables -A FORWARD -m state --state ESTABLISHED,RELATED        -j ACCEPT
240 iptables -A FORWARD -m state --state NEW        -i ! ppp+       -j ACCEPT
241 # do masquerading    (not needed if intranet is not using private ip-adresses)
242 iptables -t nat -A POSTROUTING  -o ppp+                         -j MASQUERADE
243 # Log everything for debugging (last of all rules, but before DROP/REJECT)
244 iptables -A INPUT                       -j LOG --log-prefix "FIREWALL:INPUT  "
245 iptables -A FORWARD                     -j LOG --log-prefix "FIREWALL:FORWARD"
246 iptables -A OUTPUT                      -j LOG --log-prefix "FIREWALL:OUTPUT "
247 # set a sane policy
248 iptables -P INPUT               DROP
249 iptables -P FORWARD             DROP
250 iptables -P OUTPUT              DROP
251 # be verbose on dynamic ip-adresses (not needed in case of static IP)
252 echo 2 > /proc/sys/net/ipv4/ip_dynaddr
253 # disable ExplicitCongestionNotification
254 echo 0 > /proc/sys/net/ipv4/tcp_ecn
255 # activate TCPsyncookies
256 echo 1 > /proc/sys/net/ipv4/tcp_syncookies
257 # activate Route-Verification = IP-Spoofing_protection
258 for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
259         echo 1 > $f
260     done
261 # activate IP-Forwarding 
262 echo 1 > /proc/sys/net/ipv4/ip_forward
264 With this script your net should be sufficiently secure against external 
265 attacks: your intranet should be invisible because it's masqueraded or, 
266 at least, no one should be able to setup a new connection to its services, 
267 and your firewall should be immune because there are no services running 
268 that a cracker could attack.
270 If you are in the need of stronger security (e.g., against DOS, connection 
271 highjacking, spoofing, etc.) see Appendix.1 and start to read a bit!
275 This scenario is not too different from (B), but you'd like to offer some 
276 services to your intranet. This gets relevant when you want to admin your 
277 box from another host on your intranet or use it as a proxy or a nameserver.
279 Be cautious: every service you do offer and have enabled makes your setup 
280 more complex and your box less secure. See introduction to (B)!
282 If the services you'd like to offer do not need to access the internet 
283 themselves, like an internal-only mail- or name-server, it's quite simple 
284 and should still be acceptable from a security standpoint.
285 Just add the following lines _before_ the logging-rules of script (B):
286 iptables -A INPUT       -i ! ppp+                               -j ACCEPT
287 iptables -A OUTPUT      -o ! ppp+                               -j ACCEPT
289 If your daemons have to access the web themselves, like squid would need to, 
290 you could open OUTPUT generally and restrict INPUT.
291 iptables -A INPUT       -m state --state ESTABLISHED,RELATED    -j ACCEPT
292 iptables -A OUTPUT                                              -j ACCEPT
294 Personally I am not so confident in the configuration of my own daemons that 
295 I would feel comportable with this, I think that this way it is too risky. 
296 Every misconfigured daemon and any trojan could easily open a connection to 
297 the public and expose private data.
298 As a result, I prefer to restrict not only INPUT but both INPUT and OUTPUT 
299 on all ports except those that it's absolutely necessary to have open.
300 Which ports you have to open depends on your needs: you will find them 
301 by looking at the FIREWALL-lines in your log-files.
303 Have a look at the following examples:
304 1) Squid is requesting data:
305 iptables -A OUTPUT -p tcp --dport 80                              -j ACCEPT
306 iptables -A INPUT  -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
308 2) Your caching-nameserver (e.g., dnscache) does its lookups via udp:
309 iptables -A OUTPUT -p udp --dport 53                              -j ACCEPT
310 iptables -A INPUT  -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT
312 3) Alternativly, if you want to ping your box to ensure it's still alive:
313 iptables -A INPUT  -p icmp -m icmp --icmp-type echo-request     -j ACCEPT
314 iptables -A OUTPUT -p icmp -m icmp --icmp-type echo-reply       -j ACCEPT
316 These are only examples to show you some of the capabilities of the new 
317 firewalling-code in Linux-Kernel 2.4. Have a look at the man-page of iptables.
318 There you will find more of them. The port-numbers you'll need for this can be
319 found in /etc/services, if you didn't find them in your logfile.
321 If you add any of your offered or accessed services such as the above,
322 maybe even in FORWARD and for intranet-communication, and delete the general 
323 clauses, you get an old fashioned packet filter, not unlike that one 
324 mentioned in (E).
326 4) If you are frequently accessing ftp-servers or enjoy chatting you might 
327 notice a certain delay because some oldfashioned implementations of these 
328 daemons insist on querying an identd on your box for your username.
329 Although there's no harm in this, I wouldn't recommend using an
330 identd, as many of them are known to be vulnerable.
331 To avoid these delays you could reject the requests with a 'tcp-reset':
332 iptables -A INPUT  -p tcp --dport 113   -j REJECT --reject-with tcp-reset
333 iptables -A OUTPUT -p tcp --sport 113   -m state --state RELATED   -j ACCEPT
335 5) To log and drop invalid packets, mostly harmless packets that came in 
336 after netfilter's timeout, sometimes scans.
337 iptables -I INPUT 1     -p tcp  -m state --state INVALID        -j LOG  \
338         --log-prefix "FIREWALL:INVALID"
339 iptables -I INPUT 2     -p tcp  -m state --state INVALID        -j DROP
341 6) Anything coming from the outside should not have a private address, 
342 this is a common attack called IP-spoofing:
343 iptables -t nat -A PREROUTING   -i ppp+ -s 192.168.0.0/16       -j DROP
344 iptables -t nat -A PREROUTING   -i ppp+ -s 10.0.0.0/8           -j DROP
345 iptables -t nat -A PREROUTING   -i ppp+ -s 172.16.0.0/12        -j DROP
347 7) To simplify debugging and be fair to anyone who'd like to access a service
348 you have disabled, purposely or by mistake, you should REJECT those packets 
349 that are dropped.
350 Obviously this must be done directly after logging as the very last lines 
351 before the packets are dropped by policy:
352 iptables -A INPUT       -i eth+         -j REJECT
353 iptables -A OUTPUT              -p icmp --icmp-type 3           -j ACCEPT
356 ---------
357 |THE END|
358 ---------
359 Finally, I'd like to remind you of one fact we must not forget:
360 The effort spent attacking a system corresponds to the value the cracker
361 expects to gain from it.
362 If you are responsible for such valuable assets that you expect great
363 effort to be made by potential crackers, you hopefully won't be in need 
364 of this hint!
366 Be cautious!
367                 Henning Rohde
368         (Henning.Rohde@uni-bayreuth.de)
370 PS: And always do remember:
371         SecureIT is not a matter of a status-quo 
372         but one of never stopping to take care!
373 PPS: If any of these scripts fail, please tell me. I will try to trace
374      any faults.
377 --------
378 |THANKS|
379 --------
380 Jeff Bauman     jbauman(at)adsl-63-193-249-142.dsl.snfc21.pacbell.net
381         He calls himself to be paranoid, at least in matters of security;  ;-)
382         I'd like to thank him for our discussion about security-related 
383         philosophies and furtherly for reviewing the hint and for giving
384         not only a bit of help with the fine points of wording.
387 ------------
388 |APPENDICES|
389 ------------
391 Nowadays, we must face the threat of denial of service attacks (DoS) even 
392 against private users (this seems to be quite common if you do online-gaming),
393 trojans (read on IRC for commands), and worms exploiting the internet as if
394 someone was doing a blitzkrieg.
396 There may be ways to protect both your router and your intranet, but any
397 solution I'm able to give here could become insufficent tomorrow
398 and would give you a false sense of security.
399 If you are really concerned, this is not the document to help you out!
401 But have a look, here's where I'd suggest you start reading:
402 http://netfilter.samba.org/unreliable-guides
403 http://netfilter.samba.org/netfilter-faq.html
404 http://www.linuxdoc.org/HOWTO/Firewall-HOWTO.html (IIRC outdated!)
405 http://www-106.ibm.com/developerworks/security/library/s-fire.html +s-fire2.html
406 http://www.interhack.net/pubs/fwfaq/
407 http://csrc.nist.gov/isptg/html/ISPTG-6.html
408 http://www.linuxsecurity.com/docs/
409 http://www.little-idiot.de/firewall (German & outdated, but very comprehensive)
410 http://www.linuxgazette.com/issue65/stumpel.html
411 http://linux.oreillynet.com/pub/a/linux/2000/03/10/netadmin/ddos.html
412 http://staff.washington.edu/dittrich/misc/ddos
413 http://ipmasq.cjb.net/
414 http://www.e-infomax.com/ipmasq
415 http://www.circlemud.org/~jelson/writings/security/index.htm
416 http://www.securityfocus.com
417 http://www.cert.org/tech_tips/
418 http://www.uni-siegen.de/security/pointers.html
419 http://security.ittoolbox.com/
420 http://www.linux-firewall-tools.com/linux/
421 http://logi.cc/linux/athome-firewall.php3
422 http://www.insecure.org/reading.html
423 http://www.robertgraham.com/pubs/firewall-seen.html
425 If a link proves to be dead or if you think I missed one, please mail!
428 If you need to turn firewalling off, this script will do it:
430 #!/bin/sh
431 ##/etc/init.d/firewall.stop
432 # deactivate IP-Forwarding 
433 echo 0 > /proc/sys/net/ipv4/ip_forward
434 iptables -Z
435 iptables -F
436 iptables -t nat         -F PREROUTING
437 iptables -t nat         -F OUTPUT
438 iptables -t nat         -F POSTROUTING
439 iptables -t mangle      -F PREROUTING
440 iptables -t mangle      -F OUTPUT
441 iptables -X
442 iptables -P INPUT       ACCEPT
443 iptables -P FORWARD     ACCEPT
444 iptables -P OUTPUT      ACCEPT
447 If you'd like to have a look at the chains your firewall consists of and 
448 the order in which the rules take effect:
450 #!/bin/sh
451 ##/etc/init.d/firewall.status
452 echo "iptables.mangling:"
453 iptables -t mangle      -v -L -n --line-numbers
454 echo
455 echo "iptables.nat:"
456 iptables -t nat         -v -L -n --line-numbers
457 echo
458 echo "iptables.filter:"
459 iptables                -v -L -n --line-numbers