source: postlfs/security/iptables.xml@ b512e97

12.1 ken/TL2024 ken/tuningfonts lazarus plabs/newcss python3.11 rahul/power-profiles-daemon renodr/vulkan-addition trunk xry111/llvm18
Last change on this file since b512e97 was 23b4981a, checked in by Douglas R. Reno <renodr@…>, 10 months ago

Fix some links (reported by rhubarbpieguy):

  • Transcode: host on anduin for now
  • iptables: ftp.netfilter.org has been discontinued
  • mariadb: the mirror service only keeps the most current versions available.
  • Property mode set to 100644
File size: 34.8 KB
Line 
1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
4 <!ENTITY % general-entities SYSTEM "../../general.ent">
5 %general-entities;
6
7 <!ENTITY iptables-download-http "https://www.netfilter.org/projects/iptables/files/iptables-&iptables-version;.tar.xz">
8 <!--<!ENTITY iptables-download-ftp "ftp://ftp.netfilter.org/pub/iptables/iptables-&iptables-version;.tar.xz">-->
9 <!ENTITY iptables-download-ftp " ">
10 <!ENTITY iptables-md5sum "ffa00f68d63e723c21b8a091c5c0271b">
11 <!ENTITY iptables-size "633 KB">
12 <!ENTITY iptables-buildsize "16 MB">
13 <!ENTITY iptables-time "0.1 SBU">
14]>
15
16<sect1 id="iptables" xreflabel="iptables-&iptables-version;">
17 <?dbhtml filename="iptables.html"?>
18
19
20 <title>iptables-&iptables-version;</title>
21
22 <indexterm zone="iptables">
23 <primary sortas="a-iptables">iptables</primary>
24 </indexterm>
25
26 <sect2 role="package">
27 <title>Introduction to iptables</title>
28
29 <para>
30 <application>iptables</application> is a userspace command line program
31 used to configure the Linux 2.4 and later kernel packet filtering ruleset.
32 </para>
33
34 &lfs120_checked;
35
36 <bridgehead renderas="sect3">Package Information</bridgehead>
37 <itemizedlist spacing="compact">
38 <listitem>
39 <para>
40 Download (HTTP): <ulink url="&iptables-download-http;"/>
41 </para>
42 </listitem>
43 <listitem>
44 <para>
45 Download (FTP): <ulink url="&iptables-download-ftp;"/>
46 </para>
47 </listitem>
48 <listitem>
49 <para>
50 Download MD5 sum: &iptables-md5sum;
51 </para>
52 </listitem>
53 <listitem>
54 <para>
55 Download size: &iptables-size;
56 </para>
57 </listitem>
58 <listitem>
59 <para>
60 Estimated disk space required: &iptables-buildsize;
61 </para>
62 </listitem>
63 <listitem>
64 <para>
65 Estimated build time: &iptables-time;
66 </para>
67 </listitem>
68 </itemizedlist>
69
70 <bridgehead renderas="sect3">iptables Dependencies</bridgehead>
71
72 <bridgehead renderas="sect4">Optional</bridgehead>
73 <para role="optional">
74<!-- <xref linkend="nftables"/>, -->
75 <xref linkend="libpcap"/> (required for BPF compiler or nfsynproxy support),
76 <ulink url="https://github.com/tadamdam/bpf-utils">bpf-utils</ulink>
77 (required for Berkeley Packet Filter support),
78 <ulink url="https://netfilter.org/projects/libnfnetlink/">libnfnetlink</ulink>
79 (required for connlabel support),
80 <ulink url="https://netfilter.org/projects/libnetfilter_conntrack/">libnetfilter_conntrack</ulink>
81 (required for connlabel support), and
82 <ulink url="https://netfilter.org/projects/nftables/">nftables</ulink>
83 </para>
84
85 </sect2>
86
87 <sect2 role="kernel" id="iptables-kernel">
88 <title>Kernel Configuration</title>
89
90 <para>
91 A firewall in Linux is accomplished through the netfilter interface. To
92 use <application>iptables</application> to configure netfilter, the
93 following kernel configuration parameters are required:
94 </para>
95
96 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
97 href="iptables-kernel.xml"/>
98
99 <para>
100 Include any connection tracking protocols that will be used, as well as
101 any protocols that you wish to use for match support under the
102 "Core Netfilter Configuration" section. The above options are enough
103 for running <xref linkend="fw-persFw-ipt"/> below.
104 </para>
105
106 <indexterm zone="iptables iptables-kernel">
107 <primary sortas="d-iptables">iptables</primary>
108 </indexterm>
109
110 </sect2>
111
112 <sect2 role="installation">
113 <title>Installation of iptables</title>
114
115 <note>
116 <para>
117 The installation below does not include building some specialized
118 extension libraries which require the raw headers in the
119 <application>Linux</application> source code. If you wish to build the
120 additional extensions (if you aren't sure, then you probably don't), you
121 can look at the <filename>INSTALL</filename> file to see an example of
122 how to change the <parameter>KERNEL_DIR=</parameter> parameter to point
123 at the <application>Linux</application> source code. Note that if you
124 upgrade the kernel version, you may also need to recompile
125 <application>iptables</application> and that the BLFS team has not
126 tested using the raw kernel headers.
127 </para>
128 </note>
129
130 <para>
131 Install <application>iptables</application> by running the following
132 commands:
133 </para>
134
135<screen><userinput>./configure --prefix=/usr \
136 --disable-nftables \
137 --enable-libipq &amp;&amp;
138make</userinput></screen>
139
140 <para>
141 <!-- there is a iptables-test.py script which seems to run a
142 lot of tests. Unfortunately, success rate is 50% only. Needs
143 to be clarified how to use and which prerequisites are required
144 to run to 100%. Contacted Shekhar Sharma for that (Dec'19),
145 waiting for answer (thomas). -->
146 This package does not come with a test suite.
147 </para>
148
149 <para>
150 Now, as the <systemitem class="username">root</systemitem> user:
151 </para>
152
153<screen role="root"><userinput>make install</userinput></screen>
154
155 </sect2>
156
157 <sect2 role="commands">
158 <title>Command Explanations</title>
159
160 <para>
161 <parameter>--disable-nftables</parameter>: This switch disables building
162 nftables compatibility. <!--Omit this switch if you have installed
163 <xref linkend="nftables"/>.-->
164 </para>
165
166 <para>
167 <parameter>--enable-libipq</parameter>: This switch enables building
168 of <filename class="libraryfile">libipq.so</filename> which
169 can be used by some packages outside of BLFS.
170 </para>
171
172 <para>
173 <option>--enable-nfsynproxy</option>: This switch enables installation
174 of <application>nfsynproxy</application> SYNPROXY configuration tool.
175 </para>
176
177 </sect2>
178
179 <sect2 role="configuration">
180 <title>Configuring iptables</title>
181<!--
182 <note>
183 <para>
184 If you intend to use <xref linkend="firewalld"/> to configure your
185 firewall rules, you should not use the example configurations provided
186 here, nor should you enable the
187 <phrase revision="sysv">bootscript.</phrase>
188 <phrase revision="systemd">systemd unit.</phrase>
189 </para>
190 </note>
191-->
192 <note>
193 <para>
194 In the following example configurations, <emphasis
195 role="strong">LAN1</emphasis> is used for the internal LAN interface,
196 and <emphasis role="strong">WAN1</emphasis> is used for the external
197 interface connected to the Internet. You will need to replace these
198 values with appropriate interface names for your system.
199 </para>
200 </note>
201
202 <sect3 id="fw-persFw-ipt"
203 xreflabel="Creating a Personal Firewall With iptables">
204 <title>Personal Firewall</title>
205
206 <para>
207 A Personal Firewall is designed to let you access all the
208 services offered on the Internet while keeping your computer secure and
209 your data private.
210 </para>
211
212 <para>
213 Below is a slightly modified version of Rusty Russell's
214 recommendation from the <ulink
215 url="https://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO.html">
216 Linux 2.4 Packet Filtering HOWTO</ulink>. It is still applicable
217 to the Linux 5.x kernels.
218 </para>
219
220<screen role="root" revision="sysv"><?dbfo keep-together="auto"?><userinput>cat &gt; /etc/rc.d/rc.iptables &lt;&lt; "EOF"
221<literal>#!/bin/sh
222
223# Begin rc.iptables
224
225# Insert connection-tracking modules
226# (not needed if built into the kernel)
227modprobe nf_conntrack
228modprobe xt_LOG
229
230# Enable broadcast echo Protection
231echo 1 &gt; /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
232
233# Disable Source Routed Packets
234echo 0 &gt; /proc/sys/net/ipv4/conf/all/accept_source_route
235echo 0 &gt; /proc/sys/net/ipv4/conf/default/accept_source_route
236
237# Enable TCP SYN Cookie Protection
238echo 1 &gt; /proc/sys/net/ipv4/tcp_syncookies
239
240# Disable ICMP Redirect Acceptance
241echo 0 &gt; /proc/sys/net/ipv4/conf/default/accept_redirects
242
243# Do not send Redirect Messages
244echo 0 &gt; /proc/sys/net/ipv4/conf/all/send_redirects
245echo 0 &gt; /proc/sys/net/ipv4/conf/default/send_redirects
246
247# Drop Spoofed Packets coming in on an interface, where responses
248# would result in the reply going out a different interface.
249echo 1 &gt; /proc/sys/net/ipv4/conf/all/rp_filter
250echo 1 &gt; /proc/sys/net/ipv4/conf/default/rp_filter
251
252# Log packets with impossible addresses.
253echo 1 &gt; /proc/sys/net/ipv4/conf/all/log_martians
254echo 1 &gt; /proc/sys/net/ipv4/conf/default/log_martians
255
256# be verbose on dynamic ip-addresses (not needed in case of static IP)
257echo 2 &gt; /proc/sys/net/ipv4/ip_dynaddr
258
259# disable Explicit Congestion Notification
260# too many routers are still ignorant
261echo 0 &gt; /proc/sys/net/ipv4/tcp_ecn
262
263# Set a known state
264iptables -P INPUT DROP
265iptables -P FORWARD DROP
266iptables -P OUTPUT DROP
267
268# These lines are here in case rules are already in place and the
269# script is ever rerun on the fly. We want to remove all rules and
270# pre-existing user defined chains before we implement new rules.
271iptables -F
272iptables -X
273iptables -Z
274
275iptables -t nat -F
276
277# Allow local-only connections
278iptables -A INPUT -i lo -j ACCEPT
279
280# Free output on any interface to any ip for any service
281# (equal to -P ACCEPT)
282iptables -A OUTPUT -j ACCEPT
283
284# Permit answers on already established connections
285# and permit new connections related to established ones
286# (e.g. port mode ftp)
287iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
288
289# Drop any incoming MULTICAST or BROADCAST packet before logging:
290# The box outputs several of them when using netbios or mDNS, and those
291# appear immediately as incoming, which clutters the log.
292iptables -A INPUT -m addrtype --dst-type BROADCAST,MULTICAST -j DROP
293
294# Log everything else.
295iptables -A INPUT -j LOG --log-prefix "FIREWALL:INPUT "
296
297# End $rc_base/rc.iptables</literal>
298EOF
299chmod 700 /etc/rc.d/rc.iptables</userinput></screen>
300
301<screen role="root" revision="systemd"><?dbfo keep-together="auto"?><userinput>install -v -dm755 /etc/systemd/scripts
302
303cat &gt; /etc/systemd/scripts/iptables &lt;&lt; "EOF"
304<literal>#!/bin/sh
305
306# Begin /etc/systemd/scripts/iptables
307
308# Insert connection-tracking modules
309# (not needed if built into the kernel)
310modprobe nf_conntrack
311modprobe xt_LOG
312
313# Enable broadcast echo Protection
314echo 1 &gt; /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
315
316# Disable Source Routed Packets
317echo 0 &gt; /proc/sys/net/ipv4/conf/all/accept_source_route
318echo 0 &gt; /proc/sys/net/ipv4/conf/default/accept_source_route
319
320# Enable TCP SYN Cookie Protection
321echo 1 &gt; /proc/sys/net/ipv4/tcp_syncookies
322
323# Disable ICMP Redirect Acceptance
324echo 0 &gt; /proc/sys/net/ipv4/conf/default/accept_redirects
325
326# Do not send Redirect Messages
327echo 0 &gt; /proc/sys/net/ipv4/conf/all/send_redirects
328echo 0 &gt; /proc/sys/net/ipv4/conf/default/send_redirects
329
330# Drop Spoofed Packets coming in on an interface, where responses
331# would result in the reply going out a different interface.
332echo 1 &gt; /proc/sys/net/ipv4/conf/all/rp_filter
333echo 1 &gt; /proc/sys/net/ipv4/conf/default/rp_filter
334
335# Log packets with impossible addresses.
336echo 1 &gt; /proc/sys/net/ipv4/conf/all/log_martians
337echo 1 &gt; /proc/sys/net/ipv4/conf/default/log_martians
338
339# be verbose on dynamic ip-addresses (not needed in case of static IP)
340echo 2 &gt; /proc/sys/net/ipv4/ip_dynaddr
341
342# disable Explicit Congestion Notification
343# too many routers are still ignorant
344echo 0 &gt; /proc/sys/net/ipv4/tcp_ecn
345
346# Set a known state
347iptables -P INPUT DROP
348iptables -P FORWARD DROP
349iptables -P OUTPUT DROP
350
351# These lines are here in case rules are already in place and the
352# script is ever rerun on the fly. We want to remove all rules and
353# pre-existing user defined chains before we implement new rules.
354iptables -F
355iptables -X
356iptables -Z
357
358iptables -t nat -F
359
360# Allow local-only connections
361iptables -A INPUT -i lo -j ACCEPT
362
363# Free output on any interface to any ip for any service
364# (equal to -P ACCEPT)
365iptables -A OUTPUT -j ACCEPT
366
367# Permit answers on already established connections
368# and permit new connections related to established ones
369# (e.g. port mode ftp)
370iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
371
372# Log everything else.
373iptables -A INPUT -j LOG --log-prefix "FIREWALL:INPUT "
374
375# End /etc/systemd/scripts/iptables</literal>
376EOF
377chmod 700 /etc/systemd/scripts/iptables</userinput></screen>
378
379 <para>
380 This script is quite simple, it drops all traffic coming
381 into your computer that wasn't initiated from your computer, but
382 as long as you are simply surfing the Internet you are unlikely
383 to exceed its limits.
384 </para>
385
386 <para>
387 If you frequently encounter certain delays at accessing
388 FTP servers, take a look at <xref linkend="fw-BB-4-ipt"/>.
389 </para>
390
391 <para>
392 Even if you have daemons or services running on your system,
393 these will be inaccessible everywhere but from your computer itself.
394 If you want to allow access to services on your machine, such as
395 <command>ssh</command> or <command>ping</command>, take a look at
396 <xref linkend="fw-busybox-ipt"/>.
397 </para>
398
399 </sect3>
400
401 <sect3 id="fw-masqRouter-ipt"
402 xreflabel="Creating a Masquerading Router With iptables">
403 <title>Masquerading Router</title>
404
405 <para>
406 A Network Firewall has two interfaces, one connected to an
407 intranet, in this example <emphasis role="strong">LAN1</emphasis>,
408 and one connected to the Internet, here <emphasis
409 role="strong">WAN1</emphasis>. To provide the maximum security
410 for the firewall itself, make sure that there are no unnecessary
411 servers running on it such as <application>X11</application>.
412 As a general principle, the firewall itself should not access
413 any untrusted service (think of a remote server giving answers that
414 makes a daemon on your system crash, or even worse, that implements
415 a worm via a buffer-overflow).
416 </para>
417
418<screen role="root" revision="sysv"><?dbfo keep-together="auto"?><userinput>cat &gt; /etc/rc.d/rc.iptables &lt;&lt; "EOF"
419<literal>#!/bin/sh
420
421# Begin rc.iptables
422
423echo
424echo "You're using the example configuration for a setup of a firewall"
425echo "from Beyond Linux From Scratch."
426echo "This example is far from being complete, it is only meant"
427echo "to be a reference."
428echo "Firewall security is a complex issue, that exceeds the scope"
429echo "of the configuration rules below."
430echo "You can find additional information"
431echo "about firewalls in Chapter 4 of the BLFS book."
432echo "https://www.&lfs-domainname;/blfs"
433echo
434
435# Insert iptables modules (not needed if built into the kernel).
436
437modprobe nf_conntrack
438modprobe nf_conntrack_ftp
439modprobe xt_conntrack
440modprobe xt_LOG
441modprobe xt_state
442
443# Enable broadcast echo Protection
444echo 1 &gt; /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
445
446# Disable Source Routed Packets
447echo 0 &gt; /proc/sys/net/ipv4/conf/all/accept_source_route
448
449# Enable TCP SYN Cookie Protection
450echo 1 &gt; /proc/sys/net/ipv4/tcp_syncookies
451
452# Disable ICMP Redirect Acceptance
453echo 0 &gt; /proc/sys/net/ipv4/conf/all/accept_redirects
454
455# Don't send Redirect Messages
456echo 0 &gt; /proc/sys/net/ipv4/conf/default/send_redirects
457
458# Drop Spoofed Packets coming in on an interface where responses
459# would result in the reply going out a different interface.
460echo 1 &gt; /proc/sys/net/ipv4/conf/default/rp_filter
461
462# Log packets with impossible addresses.
463echo 1 &gt; /proc/sys/net/ipv4/conf/all/log_martians
464
465# Be verbose on dynamic ip-addresses (not needed in case of static IP)
466echo 2 &gt; /proc/sys/net/ipv4/ip_dynaddr
467
468# Disable Explicit Congestion Notification
469# Too many routers are still ignorant
470echo 0 &gt; /proc/sys/net/ipv4/tcp_ecn
471
472# Set a known state
473iptables -P INPUT DROP
474iptables -P FORWARD DROP
475iptables -P OUTPUT DROP
476
477# These lines are here in case rules are already in place and the
478# script is ever rerun on the fly. We want to remove all rules and
479# pre-existing user defined chains before we implement new rules.
480iptables -F
481iptables -X
482iptables -Z
483
484iptables -t nat -F
485
486# Allow local connections
487iptables -A INPUT -i lo -j ACCEPT
488iptables -A OUTPUT -o lo -j ACCEPT
489
490# Allow forwarding if the initiated on the intranet
491iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
492iptables -A FORWARD ! -i WAN1 -m conntrack --ctstate NEW -j ACCEPT
493
494# Do masquerading
495# (not needed if intranet is not using private ip-addresses)
496iptables -t nat -A POSTROUTING -o WAN1 -j MASQUERADE
497
498# Log everything for debugging
499# (last of all rules, but before policy rules)
500iptables -A INPUT -j LOG --log-prefix "FIREWALL:INPUT "
501iptables -A FORWARD -j LOG --log-prefix "FIREWALL:FORWARD "
502iptables -A OUTPUT -j LOG --log-prefix "FIREWALL:OUTPUT "
503
504# Enable IP Forwarding
505echo 1 &gt; /proc/sys/net/ipv4/ip_forward</literal>
506EOF
507chmod 700 /etc/rc.d/rc.iptables</userinput></screen>
508
509<screen role="root" revision="systemd"><?dbfo keep-together="auto"?><userinput>install -v -dm755 /etc/systemd/scripts
510
511cat &gt; /etc/systemd/scripts/iptables &lt;&lt; "EOF"
512<literal>#!/bin/sh
513
514# Begin /etc/systemd/scripts/iptables
515
516echo
517echo "You're using the example configuration for a setup of a firewall"
518echo "from Beyond Linux From Scratch."
519echo "This example is far from being complete, it is only meant"
520echo "to be a reference."
521echo "Firewall security is a complex issue, that exceeds the scope"
522echo "of the configuration rules below."
523
524echo "You can find additional information"
525echo "about firewalls in Chapter 4 of the BLFS book."
526echo "https://www.&lfs-domainname;/blfs"
527echo
528
529# Insert iptables modules (not needed if built into the kernel).
530
531modprobe nf_conntrack
532modprobe nf_conntrack_ftp
533modprobe xt_conntrack
534modprobe xt_LOG
535modprobe xt_state
536
537# Enable broadcast echo Protection
538echo 1 &gt; /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
539
540# Disable Source Routed Packets
541echo 0 &gt; /proc/sys/net/ipv4/conf/all/accept_source_route
542
543# Enable TCP SYN Cookie Protection
544echo 1 &gt; /proc/sys/net/ipv4/tcp_syncookies
545
546# Disable ICMP Redirect Acceptance
547echo 0 &gt; /proc/sys/net/ipv4/conf/all/accept_redirects
548
549# Don't send Redirect Messages
550echo 0 &gt; /proc/sys/net/ipv4/conf/default/send_redirects
551
552# Drop Spoofed Packets coming in on an interface where responses
553# would result in the reply going out a different interface.
554echo 1 &gt; /proc/sys/net/ipv4/conf/default/rp_filter
555
556# Log packets with impossible addresses.
557echo 1 &gt; /proc/sys/net/ipv4/conf/all/log_martians
558
559# Be verbose on dynamic ip-addresses (not needed in case of static IP)
560echo 2 &gt; /proc/sys/net/ipv4/ip_dynaddr
561
562# Disable Explicit Congestion Notification
563# Too many routers are still ignorant
564echo 0 &gt; /proc/sys/net/ipv4/tcp_ecn
565
566# Set a known state
567iptables -P INPUT DROP
568iptables -P FORWARD DROP
569iptables -P OUTPUT DROP
570
571# These lines are here in case rules are already in place and the
572# script is ever rerun on the fly. We want to remove all rules and
573# pre-existing user defined chains before we implement new rules.
574iptables -F
575iptables -X
576iptables -Z
577
578iptables -t nat -F
579
580# Allow local connections
581iptables -A INPUT -i lo -j ACCEPT
582iptables -A OUTPUT -o lo -j ACCEPT
583
584# Allow forwarding if the initiated on the intranet
585iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
586iptables -A FORWARD ! -i WAN1 -m conntrack --ctstate NEW -j ACCEPT
587
588# Do masquerading
589# (not needed if intranet is not using private ip-addresses)
590iptables -t nat -A POSTROUTING -o WAN1 -j MASQUERADE
591
592# Log everything for debugging
593# (last of all rules, but before policy rules)
594iptables -A INPUT -j LOG --log-prefix "FIREWALL:INPUT "
595iptables -A FORWARD -j LOG --log-prefix "FIREWALL:FORWARD "
596iptables -A OUTPUT -j LOG --log-prefix "FIREWALL:OUTPUT "
597
598# Enable IP Forwarding
599echo 1 &gt; /proc/sys/net/ipv4/ip_forward
600
601# The following sections allow inbound packets for specific examples
602# Uncomment the example lines and adjust as necessary
603
604# Allow ping on the external interface
605#iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j ACCEPT
606#iptables -A OUTPUT -p icmp -m icmp --icmp-type echo-reply -j ACCEPT
607
608# Reject ident packets with TCP reset to avoid delays with FTP or IRC
609#iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset
610
611# Allow HTTP and HTTPS to 192.168.0.2
612#iptables -A PREROUTING -t nat -i WAN1 -p tcp --dport 80 -j DNAT --to 192.168.0.2
613#iptables -A PREROUTING -t nat -i WAN1 -p tcp --dport 443 -j DNAT --to 192.168.0.2
614#iptables -A FORWARD -p tcp -d 192.168.0.2 --dport 80 -j ACCEPT
615#iptables -A FORWARD -p tcp -d 192.168.0.2 --dport 443 -j ACCEPT
616
617# End /etc/systemd/scripts/iptables</literal>
618EOF
619chmod 700 /etc/systemd/scripts/iptables</userinput></screen>
620
621 <para>
622 With this script your intranet should be reasonably secure
623 against external attacks. No one should be able to setup a new
624 connection to any internal service and, if it's masqueraded,
625 makes your intranet invisible to the Internet. Furthermore, your
626 firewall should be relatively safe because there are no services
627 running that a cracker could attack.
628 </para>
629
630 </sect3>
631
632 <sect3 id="fw-busybox-ipt" xreflabel="Creating a BusyBox With iptables">
633 <title>BusyBox</title>
634
635 <para>
636 This scenario isn't too different from the <xref
637 linkend="fw-masqRouter-ipt"/>, but additionally offers some
638 services to your intranet. Examples of this can be when
639 you want to administer your firewall from another host on
640 your intranet or use it as a proxy or a name server.
641 </para>
642
643 <note>
644 <para>
645 Outlining specifically how to protect a server that
646 offers services on the Internet goes far beyond the scope of
647 this document. See the references in <xref linkend="fw-extra-info"/>
648 for more information.
649 </para>
650 </note>
651
652 <para>
653 Be cautious. Every service you have enabled makes your
654 setup more complex and your firewall less secure. You are
655 exposed to the risks of misconfigured services or running
656 a service with an exploitable bug. A firewall should generally
657 not run any extra services. See the introduction to the
658 <xref linkend="fw-masqRouter-ipt"/> for some more details.
659 </para>
660
661 <para>
662 If you want to add services such as internal Samba or
663 name servers that do not need to access the Internet themselves,
664 the additional statements are quite simple and should still be
665 acceptable from a security standpoint. Just add the following lines
666 into the script <emphasis>before</emphasis> the logging rules.
667 </para>
668
669<screen><literal>iptables -A INPUT -i ! WAN1 -j ACCEPT
670iptables -A OUTPUT -o ! WAN1 -j ACCEPT</literal></screen>
671
672 <para>
673 If daemons, such as squid, have to access the Internet
674 themselves, you could open OUTPUT generally and restrict
675 INPUT.
676 </para>
677
678<screen><literal>iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
679iptables -A OUTPUT -j ACCEPT</literal></screen>
680
681 <para>
682 However, it is generally not advisable to leave OUTPUT
683 unrestricted. You lose any control over trojans who would like
684 to "call home", and a bit of redundancy in case you've
685 (mis-)configured a service so that it broadcasts its existence
686 to the world.
687 </para>
688
689 <para>
690 To accomplish this, you should restrict INPUT and OUTPUT
691 on all ports except those that it's absolutely necessary to have
692 open. Which ports you have to open depends on your needs: mostly
693 you will find them by looking for failed accesses in your log
694 files.
695 </para>
696
697 <itemizedlist spacing="compact" role='iptables'>
698 <title>Have a Look at the Following Examples:</title>
699 <listitem>
700 <para>
701 Squid is caching the web:
702 </para>
703
704<screen><literal>iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
705iptables -A INPUT -p tcp --sport 80 -m conntrack --ctstate ESTABLISHED \
706 -j ACCEPT</literal></screen>
707
708 </listitem>
709 <listitem>
710 <para>
711 Your caching name server (e.g., named) does its lookups via UDP:
712 </para>
713
714<screen><literal>iptables -A OUTPUT -p udp --dport 53 -j ACCEPT</literal></screen>
715
716 </listitem>
717 <listitem>
718 <para>
719 You want to be able to ping your computer to ensure it's still
720 alive:
721 </para>
722
723<screen><literal>iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j ACCEPT
724iptables -A OUTPUT -p icmp -m icmp --icmp-type echo-reply -j ACCEPT</literal></screen>
725
726 </listitem>
727 <listitem>
728 <para id='fw-BB-4-ipt' xreflabel="BusyBox with iptables example number 4">
729 If you are frequently accessing FTP servers or enjoy chatting, you
730 might notice delays because some implementations of these daemons
731 query an identd daemon on your system to obtain usernames. Although
732 there's really little harm in this, having an identd running is not
733 recommended because many security experts feel the service gives
734 out too much additional information.
735 </para>
736
737 <para>
738 To avoid these delays you could reject the requests with a
739 'tcp-reset' response:
740 </para>
741
742<screen><literal>iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset</literal></screen>
743
744 </listitem>
745 <listitem>
746 <para>
747 To log and drop invalid packets (packets
748 that came in after netfilter's timeout or some types of
749 network scans) insert these rules at the top of the chain:
750 </para>
751
752<screen><literal>iptables -I INPUT 0 -p tcp -m conntrack --ctstate INVALID \
753 -j LOG --log-prefix "FIREWALL:INVALID "
754iptables -I INPUT 1 -p tcp -m conntrack --ctstate INVALID -j DROP</literal></screen>
755
756 </listitem>
757 <listitem>
758 <para>
759 Anything coming from the outside should not have a
760 private address, this is a common attack called IP-spoofing:
761 </para>
762
763<screen><literal>iptables -A INPUT -i WAN1 -s 10.0.0.0/8 -j DROP
764iptables -A INPUT -i WAN1 -s 172.16.0.0/12 -j DROP
765iptables -A INPUT -i WAN1 -s 192.168.0.0/16 -j DROP</literal></screen>
766
767 <para>
768 There are other addresses that you may also want to drop:
769 0.0.0.0/8, 127.0.0.0/8, 224.0.0.0/3 (multicast and
770 experimental), 169.254.0.0/16 (Link Local Networks), and
771 192.0.2.0/24 (IANA defined test network).
772 </para>
773 </listitem>
774 <listitem>
775 <para>
776 If your firewall is a DHCP client, you need to allow those packets:
777 </para>
778
779<screen><literal>iptables -A INPUT -i WAN1 -p udp -s 0.0.0.0 --sport 67 \
780 -d 255.255.255.255 --dport 68 -j ACCEPT</literal></screen>
781
782 </listitem>
783 <listitem>
784 <para>
785 To simplify debugging and be fair to anyone who'd like
786 to access a service you have disabled, purposely or by mistake,
787 you could REJECT those packets that are dropped.
788 </para>
789
790 <para>
791 Obviously this must be done directly after logging as the very
792 last lines before the packets are dropped by policy:
793 </para>
794
795<screen><literal>iptables -A INPUT -j REJECT</literal></screen>
796
797 </listitem>
798 </itemizedlist>
799
800 <para>
801 These are only examples to show you some of the capabilities
802 of the firewall code in Linux. Have a look at the man page of iptables.
803 There you will find much more information. The port numbers needed for
804 this can be found in <filename>/etc/services</filename>, in case you
805 didn't find them by trial and error in your log file.
806 </para>
807
808 </sect3>
809
810 <sect3 id="iptables-init">
811 <title><phrase revision="sysv">Boot Script</phrase>
812 <phrase revision="systemd">Systemd Unit</phrase></title>
813
814 <para revision="sysv">
815 To set up the iptables firewall at boot, install the
816 <filename>/etc/rc.d/init.d/iptables</filename> init script included
817 in the <xref linkend="bootscripts"/> package.
818 </para>
819
820 <para revision="systemd">
821 To set up the iptables firewall at boot, install the
822 <filename>iptables.service</filename> unit included in the
823 <xref linkend="systemd-units"/> package.
824 </para>
825
826 <indexterm zone="iptables iptables-init">
827 <primary sortas="f-iptables">iptables</primary>
828 </indexterm>
829
830<screen role="root"><userinput>make install-iptables</userinput></screen>
831
832 </sect3>
833
834 </sect2>
835
836 <sect2 role="content">
837 <title>Contents</title>
838
839 <segmentedlist>
840 <segtitle>Installed Programs</segtitle>
841 <segtitle>Installed Libraries</segtitle>
842 <segtitle>Installed Directories</segtitle>
843
844 <seglistitem>
845 <seg>
846 ip6tables,
847 ip6tables-apply,
848 ip6tables-legacy,
849 ip6tables-legacy-restore,
850 ip6tables-legacy-save,
851 ip6tables-restore,
852 ip6tables-save,
853 iptables,
854 iptables-apply,
855 iptables-legacy,
856 iptables-legacy-restore,
857 iptables-legacy-apply,
858 iptables-restore,
859 iptables-save,
860 iptables-xml,
861 nfsynproxy (optional),
862 and xtables-multi
863 </seg>
864 <seg>
865 libip4tc.so,
866 libip6tc.so,
867 libipq.so,
868 libiptc.so,
869 and libxtables.so
870 </seg>
871 <seg>
872 /lib/xtables and
873 /usr/include/libiptc
874 </seg>
875 </seglistitem>
876 </segmentedlist>
877
878 <variablelist>
879 <bridgehead renderas="sect3">Short Descriptions</bridgehead>
880 <?dbfo list-presentation="list"?>
881 <?dbhtml list-presentation="table"?>
882
883 <varlistentry id="iptables-prog">
884 <term><command>iptables</command></term>
885 <listitem>
886 <para>
887 is used to set up, maintain, and inspect the tables of
888 IP packet filter rules in the Linux kernel
889 </para>
890 <indexterm zone="iptables iptables-prog">
891 <primary sortas="b-iptables">iptables</primary>
892 </indexterm>
893 </listitem>
894 </varlistentry>
895
896 <varlistentry id="iptables-apply">
897 <term><command>iptables-apply</command></term>
898 <listitem>
899 <para>
900 is a safer way to update iptables remotely
901 </para>
902 <indexterm zone="iptables iptables-apply">
903 <primary sortas="b-iptables-apply">iptables-apply</primary>
904 </indexterm>
905 </listitem>
906 </varlistentry>
907
908 <varlistentry id="iptables-legacy">
909 <term><command>iptables-legacy</command></term>
910 <listitem>
911 <para>
912 is used to interact with iptables using the legacy command set
913 </para>
914 <indexterm zone="iptables iptables-legacy">
915 <primary sortas="b-iptables-legacy">iptables-legacy</primary>
916 </indexterm>
917 </listitem>
918 </varlistentry>
919
920 <varlistentry id="iptables-legacy-restore">
921 <term><command>iptables-legacy-restore</command></term>
922 <listitem>
923 <para>
924 is used to restore a set of legacy iptables rules
925 </para>
926 <indexterm zone="iptables iptables-legacy-restore">
927 <primary sortas="b-iptables-legacy-restore">iptables-legacy-restore</primary>
928 </indexterm>
929 </listitem>
930 </varlistentry>
931
932 <varlistentry id="iptables-legacy-save">
933 <term><command>iptables-legacy-save</command></term>
934 <listitem>
935 <para>
936 is used to save a set of legacy iptables rules
937 </para>
938 <indexterm zone="iptables iptables-legacy-save">
939 <primary sortas="b-iptables-legacy-save">iptables-legacy-save</primary>
940 </indexterm>
941 </listitem>
942 </varlistentry>
943
944 <varlistentry id="iptables-restore">
945 <term><command>iptables-restore</command></term>
946 <listitem>
947 <para>
948 is used to restore IP Tables from data specified on
949 STDIN. Use I/O redirection provided by your
950 shell to read from a file
951 </para>
952 <indexterm zone="iptables iptables-restore">
953 <primary sortas="b-iptables-restore">iptables-restore</primary>
954 </indexterm>
955 </listitem>
956 </varlistentry>
957
958 <varlistentry id="iptables-save">
959 <term><command>iptables-save</command></term>
960 <listitem>
961 <para>
962 is used to dump the contents of an IP Table in easily
963 parseable format to STDOUT. Use I/O-redirection
964 provided by your shell to write to a file
965 </para>
966 <indexterm zone="iptables iptables-save">
967 <primary sortas="b-iptables-save">iptables-save</primary>
968 </indexterm>
969 </listitem>
970 </varlistentry>
971
972 <varlistentry id="iptables-xml">
973 <term><command>iptables-xml</command></term>
974 <listitem>
975 <para>
976 is used to convert the output of
977 <command>iptables-save</command> to an XML format. Using the
978 <filename>iptables.xslt</filename> stylesheet converts the XML
979 back to the format of <command>iptables-restore</command>
980 </para>
981 <indexterm zone="iptables iptables-xml">
982 <primary sortas="b-iptables-xml">iptables-xml</primary>
983 </indexterm>
984 </listitem>
985 </varlistentry>
986
987 <varlistentry id="ip6tables">
988 <term><command>ip6tables*</command></term>
989 <listitem>
990 <para>
991 are a set of commands for IPV6 that parallel the iptables
992 commands above
993 </para>
994 <indexterm zone="iptables ip6tables">
995 <primary sortas="b-ip6tables">ip6tables</primary>
996 </indexterm>
997 </listitem>
998 </varlistentry>
999
1000 <varlistentry id="nfsynproxy">
1001 <term><command>nfsynproxy</command></term>
1002 <listitem>
1003 <para>
1004 (optional) configuration tool. SYNPROXY target makes handling of
1005 large SYN floods possible without the large performance penalties
1006 imposed by the connection tracking in such cases
1007 </para>
1008 <indexterm zone="iptables nfsynproxy">
1009 <primary sortas="b-nfsynproxy">nfsynproxy</primary>
1010 </indexterm>
1011 </listitem>
1012 </varlistentry>
1013
1014 <varlistentry id="xtables-multi">
1015 <term><command>xtables-multi</command></term>
1016 <listitem>
1017 <para>
1018 is a binary that behaves according to the name it is called by
1019 </para>
1020 <indexterm zone="iptables xtables-multi">
1021 <primary sortas="b-xtables-multi">xtables-multi</primary>
1022 </indexterm>
1023 </listitem>
1024 </varlistentry>
1025
1026 </variablelist>
1027
1028 </sect2>
1029
1030</sect1>
Note: See TracBrowser for help on using the repository browser.