source: postlfs/security/iptables.xml@ 7481fd4

12.0 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 7481fd4 was 9cc0aba5, checked in by Xi Ruoyao <xry111@…>, 11 months ago

postlfs: Convert the remaining kernel configuration to new rendering

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