[b4b71892] | 1 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
| 2 | <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
|
---|
| 3 | "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
|
---|
| 4 | <!ENTITY % general-entities SYSTEM "../../general.ent">
|
---|
| 5 | %general-entities;
|
---|
| 6 | ]>
|
---|
| 7 |
|
---|
[dd362e5] | 8 | <sect1 id="fw-firewall" xreflabel="Firewalling">
|
---|
[a0f03b0] | 9 | <sect1info>
|
---|
[5cd0959d] | 10 | <othername>$LastChangedBy$</othername>
|
---|
| 11 | <date>$Date$</date>
|
---|
[a0f03b0] | 12 | </sect1info>
|
---|
[bae6e15] | 13 | <?dbhtml filename="firewall.html"?>
|
---|
[f45b1953] | 14 | <title>Setting up a network firewall</title>
|
---|
| 15 |
|
---|
[0e3848e3] | 16 | <para>Before you read this part of the chapter, you should
|
---|
[36ae4612] | 17 | have already installed iptables as described in the previous section.</para>
|
---|
[f45b1953] | 18 |
|
---|
[dd362e5] | 19 | <sect2 id="fw-intro" xreflabel="Firewalling Introduction">
|
---|
[b4b71892] | 20 | <title>Introduction to Firewall Creation</title>
|
---|
| 21 |
|
---|
[0e3848e3] | 22 | <para>The general purpose of a firewall is to protect a computer or a network
|
---|
| 23 | against malicious access.</para>
|
---|
| 24 |
|
---|
| 25 | <para>In a perfect world, every daemon or service
|
---|
| 26 | on every machine is perfectly configured and immune to flaws such as
|
---|
| 27 | buffer overflows or other problems regarding its
|
---|
| 28 | security. Furthermore, you trust every user accessing your services.
|
---|
| 29 | In this world, you do not need to have a firewall.</para>
|
---|
| 30 |
|
---|
| 31 | <para>In the real world however, daemons may be misconfigured
|
---|
| 32 | and exploits against essential services are freely available. You
|
---|
| 33 | may wish to choose which services are accessible by certain machines or
|
---|
| 34 | you may wish to limit which machines or applications are allowed external
|
---|
| 35 | access. Alternatively, you may simply not trust some of your
|
---|
| 36 | applications or users. You are probably connected to the Internet. In this
|
---|
| 37 | world, a firewall is essential.</para>
|
---|
| 38 |
|
---|
| 39 | <para>Don't assume however, that having a firewall makes careful configuration
|
---|
| 40 | redundant, or that it makes any negligent misconfiguration harmless. It doesn't
|
---|
| 41 | prevent anyone from exploiting a service you intentionally offer but haven't
|
---|
| 42 | recently updated or patched after an exploit went public. Despite having a
|
---|
| 43 | firewall, you need to keep applications and daemons on your system properly
|
---|
| 44 | configured and up to date. A firewall is not a cure all, but should be an
|
---|
| 45 | essential part of your overall security startegy.</para>
|
---|
[b4b71892] | 46 |
|
---|
| 47 | </sect2>
|
---|
| 48 |
|
---|
| 49 | <sect2>
|
---|
[0e3848e3] | 50 | <title>Meaning of the word "firewall"</title>
|
---|
[b4b71892] | 51 |
|
---|
| 52 | <para>The word firewall can have several different meanings.</para>
|
---|
| 53 |
|
---|
[dd362e5] | 54 | <sect3><title><xref linkend="fw-persFw"/></title>
|
---|
[b4b71892] | 55 |
|
---|
[0e3848e3] | 56 | <para>This is a hardware device or software program commercially sold by
|
---|
| 57 | companies such as Symantec which claims that it
|
---|
| 58 | secures a home or desktop computer with Internet access. This type of firewall is
|
---|
| 59 | highly relevant for users who do not know how their computers
|
---|
| 60 | might be accessed via the Internet or how to disable that access,
|
---|
[b4b71892] | 61 | especially if they are always online and connected via
|
---|
| 62 | broadband links.</para></sect3>
|
---|
| 63 |
|
---|
[0e3848e3] | 64 | <sect3>
|
---|
| 65 | <title><xref linkend="fw-masqRouter"/></title>
|
---|
| 66 |
|
---|
| 67 | <para>This is a system placed between the Internet and an intranet. To minimize
|
---|
| 68 | the risk of compromising the firewall itself, it should generally have only one
|
---|
| 69 | role—that of protecting the intranet. Although not completely risk free,
|
---|
| 70 | the tasks of doing the routing and IP masquerading (rewriting IP headers of
|
---|
| 71 | the packets it routes from clients with private IP addresses onto the Internet
|
---|
| 72 | so that they seem to come from the firewall itself) are commonly considered
|
---|
| 73 | relatively secure.</para>
|
---|
| 74 | </sect3>
|
---|
| 75 |
|
---|
| 76 | <sect3>
|
---|
| 77 | <title><xref linkend="fw-busybox"/></title>
|
---|
| 78 |
|
---|
| 79 | <para>This is often an old computer you may have retired and nearly forgotten,
|
---|
| 80 | performing masquerading or routing functions, but offering non-firewall
|
---|
| 81 | services such as a web-cache or mail. This may be used for home
|
---|
| 82 | networks, but is not be considered as secure as a firewall only
|
---|
| 83 | machine because the combination of server and router/firewall on one machine
|
---|
| 84 | raises the complexity of the setup.</para>
|
---|
| 85 | </sect3>
|
---|
| 86 |
|
---|
| 87 | <sect3>
|
---|
| 88 | <title>Firewall with a demilitarized zone [not further described here]</title>
|
---|
[b4b71892] | 89 | <para>This box performs masquerading or routing, but grants public access to
|
---|
| 90 | some branch of your network which, because of public IP's and a physically
|
---|
[0e3848e3] | 91 | separated structure, is essentially a separate network with direct Internet access.
|
---|
| 92 | The servers on this network are those which must be easily accessible
|
---|
| 93 | from both the Internet and intranet. The firewall protects
|
---|
| 94 | both networks. This type of firewall has a minimum of three network interfaces.</para>
|
---|
| 95 | </sect3>
|
---|
[b4b71892] | 96 |
|
---|
[0e3848e3] | 97 | <sect3>
|
---|
| 98 | <title>Packetfilter</title>
|
---|
| 99 | <para>This type of firewall does routing or masquerading, but does not maintain
|
---|
| 100 | a state table of ongoing communication streams. It is fast, but quite limited
|
---|
| 101 | in its ability to block inappropriate packets without blocking desired
|
---|
| 102 | packets.</para>
|
---|
| 103 | </sect3>
|
---|
[b4b71892] | 104 | </sect2>
|
---|
| 105 |
|
---|
[0e3848e3] | 106 | <sect2 id="fw-writing" xreflabel="writing the firewalling-setup-scripts">
|
---|
| 107 | <title>Now you can start to build your Firewall</title>
|
---|
[b4b71892] | 108 |
|
---|
[0e3848e3] | 109 | <caution><para>This introduction on how to setup a firewall
|
---|
[b4b71892] | 110 | is not a complete guide to securing systems. Firewalling is a complex issue
|
---|
| 111 | that requires careful configuration. The scripts quoted here are simply
|
---|
[0e3848e3] | 112 | intended to give examples of how a firewall works. They are not intended to
|
---|
| 113 | fit into any particular configuration and may not provide complete protection
|
---|
| 114 | from an attack.</para>
|
---|
[b4b71892] | 115 |
|
---|
| 116 | <para>Customization of these scripts for your specific situation will
|
---|
| 117 | be necessary for an optimal configuration, but you should make a serious
|
---|
[dd362e5] | 118 | study of the iptables documentation and creating firewalls in general before
|
---|
| 119 | hacking away. Have a look at the list of
|
---|
| 120 | <xref linkend="fw-library"/> at the end of this section for
|
---|
[0e3848e3] | 121 | more details. There you will find a list of URLs that contain quite
|
---|
[dd362e5] | 122 | comprehensive information about building your own firewall.</para>
|
---|
[0e3848e3] | 123 | </caution>
|
---|
[b4b71892] | 124 |
|
---|
[0e3848e3] | 125 | <para>The firewall configuration script installed in the last section differs
|
---|
| 126 | from the standard configuration script. It only has two of the standard
|
---|
| 127 | targets: start and status. The other targets are clear and lock. For instance when you
|
---|
| 128 | run:
|
---|
[b4b71892] | 129 |
|
---|
[0e3848e3] | 130 | <screen><userinput><command>/etc/rc.d/init.d/iptables start</command></userinput></screen>
|
---|
[b4b71892] | 131 |
|
---|
[0e3848e3] | 132 | the firewall will be restarted just as it is upon system startup. The status target
|
---|
| 133 | will present a list of all currently implemented rules. The clear target turns off all
|
---|
| 134 | firewall rules and the lock target will block all packets in and out of the computer
|
---|
| 135 | with the exception of the loopback interface.</para>
|
---|
[b4b71892] | 136 |
|
---|
[0e3848e3] | 137 | <para>The main startup firewall is located in the file
|
---|
| 138 | <filename>/etc/rc.d/rc.iptables</filename>. The sections below provide three different
|
---|
| 139 | approaches that can be used for a system.</para>
|
---|
| 140 |
|
---|
| 141 | <note><para>You should always run your firewall rules from a script. This ensures
|
---|
| 142 | consistency and a record of what was done. It also allows retention of comments
|
---|
| 143 | that are essential for understanding the rules long after they were written.
|
---|
| 144 | </para></note>
|
---|
[b4b71892] | 145 |
|
---|
[dd362e5] | 146 | <sect3 id="fw-persFw" xreflabel="Personal Firewall">
|
---|
[b4b71892] | 147 | <title>Personal Firewall</title>
|
---|
| 148 |
|
---|
[0e3848e3] | 149 | <para>A Personal Firewall is designed to let you access all the services
|
---|
[b4b71892] | 150 | offered on the Internet, but keep your box secure and your data private.</para>
|
---|
| 151 |
|
---|
| 152 | <para>Below is a slightly modified version of Rusty Russell's recommendation
|
---|
[0e3848e3] | 153 | from the
|
---|
| 154 | <ulink url="http://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO.html">
|
---|
| 155 | Linux 2.4 Packet Filtering HOWTO</ulink>. It is still applicable to the Linux 2.6 kernels.</para>
|
---|
[b4b71892] | 156 |
|
---|
[0e3848e3] | 157 | <screen><userinput><command>cat > /etc/rc.d/rc.iptables << "EOF"</command>
|
---|
[b4b71892] | 158 | #!/bin/sh
|
---|
| 159 |
|
---|
[0e3848e3] | 160 | # Begin $rc_base/rc.iptables
|
---|
[b4b71892] | 161 |
|
---|
[dd362e5] | 162 | # Insert connection-tracking modules
|
---|
| 163 | # (not needed if built into the kernel)
|
---|
[b4b71892] | 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 |
|
---|
[0e3848e3] | 171 | # Enable broadcast echo Protection
|
---|
| 172 | echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
|
---|
[dd362e5] | 173 |
|
---|
[0e3848e3] | 174 | # Disable Source Routed Packets
|
---|
| 175 | echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
|
---|
[b4b71892] | 176 |
|
---|
[0e3848e3] | 177 | # Enable TCP SYN Cookie Protection
|
---|
| 178 | echo 1 > /proc/sys/net/ipv4/tcp_syncookies
|
---|
[b4b71892] | 179 |
|
---|
[0e3848e3] | 180 | # Disable ICMP Redirect Acceptance
|
---|
| 181 | echo 0 > /proc/sys/net/ipv4/conf/all/accept_redirects
|
---|
| 182 |
|
---|
| 183 | # Don¹t send Redirect Messages
|
---|
| 184 | echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
|
---|
| 185 |
|
---|
| 186 | # Drop Spoofed Packets coming in on an interface, where responses
|
---|
| 187 | # would result in the reply going out a different interface.
|
---|
| 188 | echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
|
---|
[b4b71892] | 189 |
|
---|
[0e3848e3] | 190 | # Log packets with impossible addresses.
|
---|
| 191 | echo 1 > /proc/sys/net/ipv4/conf/all/log_martians
|
---|
[b4b71892] | 192 |
|
---|
[dd362e5] | 193 | # be verbose on dynamic ip-addresses (not needed in case of static IP)
|
---|
[b4b71892] | 194 | echo 2 > /proc/sys/net/ipv4/ip_dynaddr
|
---|
| 195 |
|
---|
[0e3848e3] | 196 | # disable Explicit Congestion Notification
|
---|
[dd362e5] | 197 | # too many routers are still ignorant
|
---|
[b4b71892] | 198 | echo 0 > /proc/sys/net/ipv4/tcp_ecn
|
---|
| 199 |
|
---|
[0e3848e3] | 200 | # Set a known state
|
---|
| 201 | iptables -P INPUT DROP
|
---|
| 202 | iptables -P FORWARD DROP
|
---|
| 203 | iptables -P OUTPUT DROP
|
---|
| 204 |
|
---|
| 205 | # These lines are here in case rules are already in place and the
|
---|
| 206 | # script is ever rerun on the fly. We want to remove all rules and
|
---|
| 207 | # pre-exisiting user defined chains before we implement new rules.
|
---|
| 208 | iptables -F
|
---|
| 209 | iptables -X
|
---|
| 210 | iptables -Z
|
---|
| 211 |
|
---|
| 212 | iptables -t nat -F
|
---|
| 213 |
|
---|
| 214 | # Allow local-only connections
|
---|
| 215 | iptables -A INPUT -i lo -j ACCEPT
|
---|
| 216 |
|
---|
| 217 | # Free output on any interface to any ip for any service
|
---|
| 218 | # (equal to -P ACCEPT)
|
---|
| 219 | iptables -A OUTPUT -j ACCEPT
|
---|
| 220 |
|
---|
| 221 | # Permit answers on already established connections
|
---|
| 222 | # and permit new connections related to established ones
|
---|
| 223 | # (e.g. port mode ftp)
|
---|
| 224 | iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
|
---|
| 225 |
|
---|
| 226 | # Log everything else. What's Windows' latest exploitable vulnerability?
|
---|
| 227 | iptables -A INPUT -j LOG --log-prefix "FIREWALL:INPUT "
|
---|
| 228 |
|
---|
| 229 | # End $rc_base/rc.iptables
|
---|
[b4b71892] | 230 | <command>EOF</command></userinput></screen>
|
---|
| 231 |
|
---|
[0e3848e3] | 232 | <para>This script is quite simple, it drops all traffic coming in into your
|
---|
[b4b71892] | 233 | computer that wasn't initiated from your box, but as long as you are simply
|
---|
| 234 | surfing the Internet you are unlikely to exceed its limits.</para>
|
---|
| 235 |
|
---|
| 236 | <para>If you frequently encounter certain delays at accessing ftp-servers,
|
---|
[0e3848e3] | 237 | take a look at <xref linkend="fw-BB-4"/>.</para>
|
---|
[b4b71892] | 238 |
|
---|
[0e3848e3] | 239 | <para>Even if you have daemons or services running on your system, these
|
---|
| 240 | will be inaccessible everywhere but from your computer itself.
|
---|
[dd362e5] | 241 | If you want to allow access to services on your machine, such as ssh or
|
---|
[0e3848e3] | 242 | ping, take a look at <xref linkend="fw-busybox"/>.</para>
|
---|
[b4b71892] | 243 |
|
---|
| 244 | </sect3>
|
---|
| 245 |
|
---|
[dd362e5] | 246 | <sect3 id="fw-masqRouter" xreflabel="Masquerading Router">
|
---|
[b4b71892] | 247 | <title>Masquerading Router</title>
|
---|
| 248 |
|
---|
[0e3848e3] | 249 | <para>A true Firewall has two interfaces, one connected to an intranet, in this
|
---|
| 250 | example <emphasis role="strong">eth0</emphasis>, and one connected to the
|
---|
| 251 | Internet, here <emphasis role="strong">ppp0</emphasis>. To provide the
|
---|
| 252 | maximum security for the firewall itself, make sure that there
|
---|
| 253 | are no unnecessary servers running on it such as <application>X11</application> et
|
---|
| 254 | al. As a general principle, the firewall itself should not access any
|
---|
| 255 | untrusted service (Think of a remote server giving answers that makes a daemin on
|
---|
| 256 | your system
|
---|
| 257 | crash, or, even worse, that implements a worm via a buffer-overflow).</para>
|
---|
| 258 |
|
---|
| 259 | <screen><userinput><command>cat > /etc/rc.d/rc.iptables << "EOF"</command>
|
---|
[b4b71892] | 260 | #!/bin/sh
|
---|
| 261 |
|
---|
[0e3848e3] | 262 | # Begin $rc_base/rc.iptables
|
---|
[b4b71892] | 263 |
|
---|
| 264 | echo
|
---|
[0e3848e3] | 265 | echo "You're using the example configuration for a setup of a firewall"
|
---|
| 266 | echo "from Beyond Linux From Scratch."
|
---|
[b4b71892] | 267 | echo "This example is far from being complete, it is only meant"
|
---|
| 268 | echo "to be a reference."
|
---|
| 269 | echo "Firewall security is a complex issue, that exceeds the scope"
|
---|
[0e3848e3] | 270 | echo "of the configuration rules below."
|
---|
| 271 | echo "You can find additional information"
|
---|
[b4b71892] | 272 | echo "about firewalls in Chapter 4 of the BLFS book."
|
---|
| 273 | echo "http://www.linuxfromscratch.org/blfs"
|
---|
| 274 | echo
|
---|
| 275 |
|
---|
| 276 | # Insert iptables modules (not needed if built into the kernel).
|
---|
| 277 |
|
---|
| 278 | modprobe ip_tables
|
---|
| 279 | modprobe iptable_filter
|
---|
| 280 | modprobe ip_conntrack
|
---|
| 281 | modprobe ip_conntrack_ftp
|
---|
| 282 | modprobe ipt_state
|
---|
| 283 | modprobe iptable_nat
|
---|
| 284 | modprobe ip_nat_ftp
|
---|
| 285 | modprobe ipt_MASQUERADE
|
---|
| 286 | modprobe ipt_LOG
|
---|
| 287 | modprobe ipt_REJECT
|
---|
| 288 |
|
---|
[0e3848e3] | 289 | # Enable broadcast echo Protection
|
---|
| 290 | echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
|
---|
| 291 |
|
---|
| 292 | # Disable Source Routed Packets
|
---|
| 293 | echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
|
---|
| 294 |
|
---|
| 295 | # Enable TCP SYN Cookie Protection
|
---|
| 296 | echo 1 > /proc/sys/net/ipv4/tcp_syncookies
|
---|
| 297 |
|
---|
| 298 | # Disable ICMP Redirect Acceptance
|
---|
| 299 | echo 0 > /proc/sys/net/ipv4/conf/all/accept_redirects
|
---|
| 300 |
|
---|
| 301 | # Don¹t send Redirect Messages
|
---|
| 302 | echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
|
---|
| 303 |
|
---|
| 304 | # Drop Spoofed Packets coming in on an interface where responses
|
---|
| 305 | # would result in the reply going out a different interface.
|
---|
| 306 | echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
|
---|
| 307 |
|
---|
| 308 | # Log packets with impossible addresses.
|
---|
| 309 | echo 1 > /proc/sys/net/ipv4/conf/all/log_martians
|
---|
| 310 |
|
---|
| 311 | # Be verbose on dynamic ip-addresses (not needed in case of static IP)
|
---|
| 312 | echo 2 > /proc/sys/net/ipv4/ip_dynaddr
|
---|
| 313 |
|
---|
| 314 | # Disable Explicit Congestion Notification
|
---|
| 315 | # Too many routers are still ignorant
|
---|
| 316 | echo 0 > /proc/sys/net/ipv4/tcp_ecn
|
---|
| 317 |
|
---|
| 318 | # Set a known state
|
---|
| 319 | iptables -P INPUT DROP
|
---|
| 320 | iptables -P FORWARD DROP
|
---|
| 321 | iptables -P OUTPUT DROP
|
---|
| 322 |
|
---|
| 323 | # These lines are here in case rules are already in place and the
|
---|
| 324 | # script is ever rerun on the fly. We want to remove all rules and
|
---|
| 325 | # pre-exisiting user defined chains before we implement new rules.
|
---|
| 326 | iptables -F
|
---|
| 327 | iptables -X
|
---|
| 328 | iptables -Z
|
---|
| 329 |
|
---|
| 330 | iptables -t nat -F
|
---|
| 331 |
|
---|
| 332 | # Allow local connections
|
---|
[b4b71892] | 333 | iptables -A INPUT -i lo -j ACCEPT
|
---|
| 334 | iptables -A OUTPUT -o lo -j ACCEPT
|
---|
| 335 |
|
---|
[0e3848e3] | 336 | # Allow forwarding if the initiated on the intranet
|
---|
[b4b71892] | 337 | iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
|
---|
[0e3848e3] | 338 | iptables -A FORWARD -i ! ppp+ -m state --state NEW -j ACCEPT
|
---|
[b4b71892] | 339 |
|
---|
[0e3848e3] | 340 | # Do masquerading
|
---|
[dd362e5] | 341 | # (not needed if intranet is not using private ip-addresses)
|
---|
[b4b71892] | 342 | iptables -t nat -A POSTROUTING -o ppp+ -j MASQUERADE
|
---|
| 343 |
|
---|
[dd362e5] | 344 | # Log everything for debugging
|
---|
[0e3848e3] | 345 | # (last of all rules, but before policy rules)
|
---|
[b4b71892] | 346 | iptables -A INPUT -j LOG --log-prefix "FIREWALL:INPUT "
|
---|
| 347 | iptables -A FORWARD -j LOG --log-prefix "FIREWALL:FORWARD"
|
---|
| 348 | iptables -A OUTPUT -j LOG --log-prefix "FIREWALL:OUTPUT "
|
---|
| 349 |
|
---|
[0e3848e3] | 350 | # Enable IP Forwarding
|
---|
[b4b71892] | 351 | echo 1 > /proc/sys/net/ipv4/ip_forward
|
---|
| 352 | <command>EOF</command></userinput></screen>
|
---|
| 353 |
|
---|
[0e3848e3] | 354 | <para>With this script your intranet should be reasonably secure against
|
---|
[b4b71892] | 355 | external attacks. No one should be able to setup a new connection to any
|
---|
[0e3848e3] | 356 | internal service and, if it's masqueraded, makes your intranet invisible to the
|
---|
| 357 | Internet. Furthermore, your firewall should be relatively safe because there
|
---|
| 358 | are no services running that a cracker could attack.</para>
|
---|
| 359 |
|
---|
| 360 | <note><para>If the interface you're connecting to the Internet doesn't connect
|
---|
| 361 | via ppp, you will need to change <replaceable>ppp+</replaceable> to the name of
|
---|
| 362 | the interface, e.g. <emphasis role="strong">eth1</emphasis>, which you are using.
|
---|
| 363 | </para></note>
|
---|
[b4b71892] | 364 |
|
---|
| 365 | </sect3>
|
---|
| 366 |
|
---|
[dd362e5] | 367 | <sect3 id="fw-busybox" xreflabel="BusyBox">
|
---|
[b4b71892] | 368 | <title>BusyBox</title>
|
---|
| 369 |
|
---|
[0e3848e3] | 370 | <para>This scenario isn't too different from the <xref linkend="fw-masqRouter"/>,
|
---|
| 371 | but additionally offers some services to your intranet.
|
---|
| 372 | Examples of this can be when you want to administer your firewall from another host
|
---|
| 373 | on your intranet or use it as a proxy or a name server.</para>
|
---|
| 374 |
|
---|
| 375 | <note><para>Outlining a true concept of how to protect a server that offers
|
---|
| 376 | services on the Internet goes far beyond the scope of this document. See the references
|
---|
| 377 | at the end of this section for more information.</para></note>
|
---|
[b4b71892] | 378 |
|
---|
[0e3848e3] | 379 | <para>Be cautious. Every service you have enabled makes your
|
---|
| 380 | setup more complex and your firewall less secure. You are exposed to the risks of
|
---|
[dd362e5] | 381 | misconfigured services or running a service with an exploitable bug. A
|
---|
| 382 | firewall should generally not run any extra services. See the introduction to
|
---|
[0e3848e3] | 383 | the <xref linkend="fw-masqRouter"/> for some more details.</para>
|
---|
[b4b71892] | 384 |
|
---|
[0e3848e3] | 385 | <para>If you want to add services such as internal samba or name servers that do not
|
---|
| 386 | need to access the Internet themselves, the additional statements are quite
|
---|
[b4b71892] | 387 | simple and should still be acceptable from a security standpoint.
|
---|
[0e3848e3] | 388 | Just add the following lines
|
---|
| 389 | into the script <emphasis>before</emphasis> the logging rules.</para>
|
---|
[b4b71892] | 390 |
|
---|
| 391 | <screen>iptables -A INPUT -i ! ppp+ -j ACCEPT
|
---|
| 392 | iptables -A OUTPUT -o ! ppp+ -j ACCEPT</screen>
|
---|
| 393 |
|
---|
[0e3848e3] | 394 | <para>If daemons, such as squid, have to access the Internet themselves,
|
---|
| 395 | you could open OUTPUT generally and restrict INPUT.</para>
|
---|
[b4b71892] | 396 |
|
---|
[0e3848e3] | 397 | <screen>iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
|
---|
[dd362e5] | 398 | iptables -A OUTPUT -j ACCEPT</screen>
|
---|
[b4b71892] | 399 |
|
---|
[dd362e5] | 400 | <para>However, it is generally not advisable to leave OUTPUT unrestricted. You
|
---|
[0e3848e3] | 401 | lose any control over trojans who would like to "call home", and a bit of
|
---|
| 402 | redundancy in case you've (mis-)configured a service so that it broadcasts
|
---|
[dd362e5] | 403 | its existence to the world.</para>
|
---|
[b4b71892] | 404 |
|
---|
[0e3848e3] | 405 | <para>To accomplish this, you should restrict INPUT and OUTPUT
|
---|
[b4b71892] | 406 | on all ports except those that it's absolutely necessary to have open.
|
---|
| 407 | Which ports you have to open depends on your needs: mostly you will find them
|
---|
[0e3848e3] | 408 | by looking for failed accesses in your log files.</para>
|
---|
| 409 | <itemizedlist spacing="compact" role='iptables'>
|
---|
| 410 |
|
---|
[b4b71892] | 411 | <title>Have a look at the following examples:</title>
|
---|
| 412 |
|
---|
| 413 | <listitem><para>Squid is caching the web:</para>
|
---|
[dd362e5] | 414 | <screen>iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
|
---|
[0e3848e3] | 415 | iptables -A INPUT -p tcp --sport 80 -m state --state ESTABLISHED \
|
---|
| 416 | -j ACCEPT</screen>
|
---|
[dd362e5] | 417 | </listitem>
|
---|
[b4b71892] | 418 |
|
---|
[0e3848e3] | 419 | <listitem><para>Your caching name server (e.g., named) does its
|
---|
[b4b71892] | 420 | lookups via udp:</para>
|
---|
[0e3848e3] | 421 | <screen>iptables -A OUTPUT -p udp --dport 53 -j ACCEPT</screen>
|
---|
[dd362e5] | 422 | </listitem>
|
---|
| 423 |
|
---|
[0e3848e3] | 424 | <listitem><para>You want to be able to ping your box to
|
---|
[dd362e5] | 425 | ensure it's still alive:</para>
|
---|
[b4b71892] | 426 |
|
---|
| 427 | <screen>iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j ACCEPT
|
---|
[dd362e5] | 428 | iptables -A OUTPUT -p icmp -m icmp --icmp-type echo-reply -j ACCEPT</screen>
|
---|
| 429 | </listitem>
|
---|
[b4b71892] | 430 |
|
---|
[0e3848e3] | 431 | <listitem><para><anchor id='fw-BB-4' xreflabel="BusyBox example number 4"/>If you are
|
---|
| 432 | frequently accessing ftp servers or enjoy chatting, you might notice certain
|
---|
| 433 | delays because some implementations of these daemons have the feature of
|
---|
| 434 | querying an identd on your system to obtain usernames. Although there's really
|
---|
| 435 | little harm in this, having an identd running is not recommended because many
|
---|
| 436 | security experts feel the service gives out too much additional information.</para>
|
---|
[b4b71892] | 437 |
|
---|
| 438 | <para>To avoid these delays you could reject the requests
|
---|
| 439 | with a 'tcp-reset':</para>
|
---|
| 440 |
|
---|
[0e3848e3] | 441 | <screen>iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset</screen>
|
---|
[dd362e5] | 442 | </listitem>
|
---|
[b4b71892] | 443 |
|
---|
[0e3848e3] | 444 | <listitem><para>To log and drop invalid packets (packets
|
---|
[b4b71892] | 445 | that came in after netfilter's timeout or some types of network scans):</para>
|
---|
| 446 |
|
---|
[0e3848e3] | 447 | <screen>iptables -I INPUT -p tcp -m state --state INVALID \
|
---|
| 448 | -j LOG --log-prefix "FIREWALL:INVALID"
|
---|
| 449 | iptables -I INPUT -p tcp -m state --state INVALID -j DROP</screen></listitem>
|
---|
[b4b71892] | 450 |
|
---|
| 451 | <listitem><para>Anything coming from the outside should not have a
|
---|
[0e3848e3] | 452 | private address, this is a common attack called IP-spoofing:
|
---|
| 453 |
|
---|
| 454 | <screen>iptables -A INPUT -i ppp+ -s 10.0.0.0/8 -j DROP
|
---|
| 455 | iptables -A INPUT -i ppp+ -s 172.16.0.0/12 -j DROP
|
---|
| 456 | iptables -A INPUT -i ppp+ -s 192.168.0.0/16 -j DROP</screen>
|
---|
| 457 |
|
---|
| 458 | There are other addresses that you may also want to drop: 0.0.0.0/8,
|
---|
| 459 | 127.0.0.0/8, 224.0.0.0/3 (multicast and experimental), 169.254.0.0/16 (Link
|
---|
| 460 | Local Networks), and 192.0.2.0/24 (IANA defined test network).</para>
|
---|
| 461 | </listitem>
|
---|
| 462 |
|
---|
| 463 | <listitem><para>If your firewall is a DHCP client, you need to allow
|
---|
| 464 | those packets:</para>
|
---|
[b4b71892] | 465 |
|
---|
[0e3848e3] | 466 | <screen>iptables -A INPUT -i ppp0 -p udp -s 0.0.0.0 --sport 67 \
|
---|
| 467 | -d 255.255.255.255 --dport 68 -j ACCEPT</screen>
|
---|
[dd362e5] | 468 | </listitem>
|
---|
[b4b71892] | 469 |
|
---|
| 470 | <listitem><para>To simplify debugging and be fair to anyone who'd like to
|
---|
[0e3848e3] | 471 | access a service you have disabled, purposely or by mistake, you could REJECT
|
---|
[b4b71892] | 472 | those packets that are dropped.</para>
|
---|
| 473 |
|
---|
| 474 | <para>Obviously this must be done directly after logging as the very
|
---|
| 475 | last lines before the packets are dropped by policy:</para>
|
---|
| 476 |
|
---|
[0e3848e3] | 477 | <screen>iptables -A INPUT -j REJECT</screen>
|
---|
| 478 | </listitem>
|
---|
[b4b71892] | 479 | </itemizedlist>
|
---|
| 480 |
|
---|
[0e3848e3] | 481 | <para>These are only examples to show you some of the capabilities of the
|
---|
| 482 | firewall code in Linux. Have a look at the man page of iptables.
|
---|
| 483 | There you will find much more information. The port numbers needed for this can be
|
---|
| 484 | found in <filename>/etc/services</filename>, in case you didn't find them by
|
---|
| 485 | trial and error in your log file.</para>
|
---|
| 486 |
|
---|
| 487 | </sect3>
|
---|
[b4b71892] | 488 | </sect2>
|
---|
| 489 |
|
---|
[dd362e5] | 490 | <sect2 id="fw-finale" xreflabel="Conclusion">
|
---|
[b4b71892] | 491 | <title>Conclusion</title>
|
---|
| 492 |
|
---|
[0e3848e3] | 493 | <para>Finally, there is one fact you must not forget: The effort spent
|
---|
| 494 | attacking a system corresponds to the value the cracker expects to gain from
|
---|
| 495 | it. If you are responsible for valuable information, you need to spend the
|
---|
| 496 | time to protect it properly.</para>
|
---|
[b4b71892] | 497 |
|
---|
| 498 | </sect2>
|
---|
| 499 |
|
---|
| 500 | <sect2 id="postlfs-security-fw-extra" xreflabel="Extra Information">
|
---|
| 501 | <title>Extra Information</title>
|
---|
| 502 |
|
---|
[dd362e5] | 503 | <sect3 id="fw-library" xreflabel="Links for further reading">
|
---|
[b4b71892] | 504 | <title>Where to start with further reading on firewalls.</title>
|
---|
| 505 |
|
---|
| 506 | <para><blockquote><literallayout>
|
---|
| 507 | <ulink url="http://www.netfilter.org/">www.netfilter.org - Homepage of the netfilter/iptables project</ulink>
|
---|
| 508 | <ulink url="http://www.netfilter.org/documentation/FAQ/netfilter-faq.html">Netfilter related FAQ</ulink>
|
---|
| 509 | <ulink url="http://www.netfilter.org/documentation/index.html#HOWTO">Netfilter related HOWTO's</ulink>
|
---|
| 510 | <ulink url="http://en.tldp.org/LDP/nag2/x-087-2-firewall.html">en.tldp.org/LDP/nag2/x-087-2-firewall.html</ulink>
|
---|
| 511 | <ulink url="http://en.tldp.org/HOWTO/Security-HOWTO.html">en.tldp.org/HOWTO/Security-HOWTO.html</ulink>
|
---|
| 512 | <ulink url="http://en.tldp.org/HOWTO/Firewall-HOWTO.html">en.tldp.org/HOWTO/Firewall-HOWTO.html</ulink>
|
---|
| 513 | <ulink url="http://www-106.ibm.com/developerworks/security/library/s-fire.html">www.ibm.com/developerworks/security/library/s-fire.html</ulink>
|
---|
| 514 | <ulink url="http://www-106.ibm.com/developerworks/security/library/s-fire2.html">www.ibm.com/developerworks/security/library/s-fire2.html</ulink>
|
---|
| 515 | <ulink url="http://www.interhack.net/pubs/fw-faq/">www.interhack.net/pubs/fw-faq/</ulink>
|
---|
| 516 | <ulink url="http://www.linuxsecurity.com/docs/">www.linuxsecurity.com/docs/</ulink>
|
---|
| 517 | <ulink url="http://www.little-idiot.de/firewall">www.little-idiot.de/firewall (German & outdated, but very comprehensive)</ulink>
|
---|
| 518 | <ulink url="http://www.linuxgazette.com/issue65/stumpel.html">www.linuxgazette.com/issue65/stumpel.html</ulink>
|
---|
| 519 | <ulink url="http://linux.oreillynet.com/pub/a/linux/2000/03/10/netadmin/ddos.html">linux.oreillynet.com/pub/a/linux/2000/03/10/netadmin/ddos.html</ulink>
|
---|
| 520 | <ulink url="http://staff.washington.edu/dittrich/misc/ddos">staff.washington.edu/dittrich/misc/ddos</ulink>
|
---|
| 521 | <ulink url="http://www.e-infomax.com/ipmasq">www.e-infomax.com/ipmasq</ulink>
|
---|
| 522 | <ulink url="http://www.circlemud.org/~jelson/writings/security/index.htm">www.circlemud.org/~jelson/writings/security/index.htm</ulink>
|
---|
| 523 | <ulink url="http://www.securityfocus.com">www.securityfocus.com</ulink>
|
---|
| 524 | <ulink url="http://www.cert.org/tech_tips/">www.cert.org - tech_tips</ulink>
|
---|
| 525 | <ulink url="http://security.ittoolbox.com/">security.ittoolbox.com</ulink>
|
---|
| 526 | <ulink url="http://www.linux-firewall-tools.com/linux/">www.linux-firewall-tools.com/linux/</ulink>
|
---|
| 527 | <ulink url="http://logi.cc/linux/athome-firewall.php3">logi.cc/linux/athome-firewall.php3</ulink>
|
---|
| 528 | <ulink url="http://www.insecure.org/reading.html">www.insecure.org/reading.html</ulink>
|
---|
| 529 | <ulink url="http://www.robertgraham.com/pubs/firewall-seen.html">www.robertgraham.com/pubs/firewall-seen.html</ulink>
|
---|
| 530 | </literallayout></blockquote></para>
|
---|
| 531 | </sect3>
|
---|
| 532 |
|
---|
| 533 | </sect2>
|
---|
[f45b1953] | 534 | </sect1>
|
---|
| 535 |
|
---|