source: postlfs/security/iptables.xml@ c7548853

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 c7548853 was c7548853, checked in by Thomas Trepl <thomas@…>, 4 years ago

Upgrade iptables-1.8.4

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

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