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