source: postlfs/security/iptables.xml@ 0b8fb2f

12.0 12.1 kea ken/TL2024 ken/tuningfonts lazarus lxqt plabs/newcss python3.11 qt5new rahul/power-profiles-daemon renodr/vulkan-addition trunk xry111/llvm18 xry111/xf86-video-removal
Last change on this file since 0b8fb2f was c5b59ac, checked in by Ken Moffat <ken@…>, 15 months ago

ptables - clarify what libpcap is used for.

Thanks to Jamenson Espindula for reporting the 'nfsyyproxy' typo,
which caused me to look at this.

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