source: postlfs/security/iptables.xml@ 14891a90

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

treewide: More "User Notes" clean up

Remove links to pages w/o real contents.

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