source: postlfs/security/iptables.xml@ eede1a3

11.0 11.1 11.2 11.3 12.0 12.1 kea ken/TL2024 ken/inkscape-core-mods ken/tuningfonts lazarus lxqt plabs/newcss plabs/python-mods python3.11 qt5new rahul/power-profiles-daemon renodr/vulkan-addition trunk upgradedb xry111/intltool xry111/llvm18 xry111/soup3 xry111/test-20220226 xry111/xf86-video-removal
Last change on this file since eede1a3 was f2a586e, checked in by Ken Moffat <ken@…>, 3 years ago

iptables: remove instructions for using /sbin and lib.

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