source: postlfs/security/iptables.xml@ 6d6c19d

10.0 10.1 11.0 11.1 11.2 11.3 12.0 12.1 9.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 6d6c19d was 6d6c19d, checked in by DJ Lucas <dj@…>, 4 years ago

Update to blfs-bootscripts-20191203.
Update to firewalld-0.8.0. Fixes #12734.
Update to nftables-0.9.3. Fixes #12850.
Update to libnftnl-1.1.5. Fixes #12851.
Add missing dependency six to python-slip.

git-svn-id: svn://svn.linuxfromscratch.org/BLFS/trunk/BOOK@22424 af4574ff-66df-0310-9fd7-8a98e5e911e0

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