个人工具

UbuntuHelp:IptablesHowTo

来自Ubuntu中文

Oneleaf讨论 | 贡献2007年5月13日 (日) 11:22的版本 (New page: {{From|https://help.ubuntu.com/community/IptablesHowTo}} {{Languages|php5}} THIS IS NOT COMPLETE AND SHOULD BE COMPLETED BY SOMEONE WHO KNOWS MORE THAN ME! THANKS == Basic Iptables How to...)

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索


THIS IS NOT COMPLETE AND SHOULD BE COMPLETED BY SOMEONE WHO KNOWS MORE THAN ME! THANKS

Basic Iptables How to for Ubuntu Server Edition

Iptables is a firewall, installed by default on the Ubuntu Server. On regular Ubuntu install, iptables is installed but allows all traffic (thus firewall is ineffective / inactive)

There is a wealth of information available about iptables, but much of it is fairly complex, and if you want to do a few basic things, this How To is for you.

Basic Commands

Typing

# iptables -L
</code>
lists your current rules in iptables. If you have just set up your server, you will have no rules, and you should see
<pre>
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
</code>


=== Allowing Established Sessions ===
We can allow established sessions to receive traffic:
<pre>
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
</code>

=== Allowing Incoming Traffic on Specific Ports ===
You could start by blocking traffic, but you might be working over SSH, where you would need to allow SSH before blocking everything else.

To allow incoming traffic on port 22 (traditionally used by SSH), you could tell iptables to allow  all TCP traffic on port 22 of your network adapter.
<pre>
# iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT
</code>
Specifically, this appends (-A) to the table INPUT the rule that any traffic to the interface (-i) eth0 on the destination port for ssh that iptables should jump (-j), or perform the action, ACCEPT.

Lets check the rules: (only the first few lines shown, you will see more)
<pre>
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
</code>

Now, let's allow all web traffic
<pre>
# iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT
</code>

Checking our rules, we have
<pre>
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:www
</code>

We have specifically allowed tcp traffic to the ssh and web ports, but as we have not blocked anything, all traffic can still come in.

=== Blocking Traffic ===
Once a decision is made about a packet, no more rules affect it. As our rules allowing ssh and web traffic come first, as long as our rule to block all traffic comes after them, we can still accept the traffic we want. All we need to do is put the rule to block all traffic at the end. The -A command tells iptables to append the rule at the end, so we'll use that again.

<pre>
# iptables -A INPUT -j DROP
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:www
DROP       all  --  anywhere             anywhere
</code>

Because we didn't specify an interface or a protocol, any traffic for any port on any interface is blocked, except for web and ssh.

=== Editing iptables ===

The only problem with our setup so far is that even the loopback port is blocked. We could have written the drop rule for just eth0 by specifying -i eth0, but we could also add a rule for the loopback. If we append this rule, it will come too late - after all the traffic has been dropped. We need to insert this rule onto the fourth line.

<pre>
# iptables -I INPUT 4 -i lo -j ACCEPT
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:www
ACCEPT     all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
</code>

The last two lines look nearly the same, so we will list iptables in greater detail.
<pre>
# iptables -L -v
</code>

=== Logging ===
In the above examples none of the traffic will be logged.  If you would like to log dropped packets to syslog, this would be the quickest way:
<pre>
# iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
</code>
See Tips section for more ideas on logging.

=== Saving iptables ===
If you were to reboot your machine right now, your iptables configuration would disapear. Rather than type this each time you reboot, however, you can save the configuration, and have it start up automatically. To save the configuration, you can use <code>iptables-save</code> and <code>iptables-restore</code>.

=== Configuration on startup ===
Save your firewall rules to a file
<pre>
# iptables-save > /etc/iptables.up.rules
</code>
Then modify the ''/etc/network/interfaces'' script to apply the rules automatically (the bottom line is added)
<pre>
auto eth0
iface eth0 inet dhcp
  pre-up iptables-restore < /etc/iptables.up.rules
</code>

You can also prepare a set of down rules and apply it automatically
<pre>
auto eth0
iface eth0 inet dhcp
  pre-up iptables-restore < /etc/iptables.up.rules
  post-down iptables-restore < /etc/iptables.down.rules
</code>

=== Tips ===
==== If you manually edit iptables on a regular basis ====
The above steps go over how to setup your firewall rules and presume they will be relatively static (and for most people they should be).  But if you do a lot of development work, you may want to have your iptables saved everytime you reboot.  You could add a line like this one in <code>/etc/network/interfaces</code>:
<pre>
  pre-up iptables-restore < /etc/iptables.up.rules
  post-down iptables-save > /etc/iptables.up.rules
</code>
The line "post-down iptables-save > /etc/iptables.up.rules" will save the rules to be used on the next boot.

==== Using iptables-save/restore to test rules ====
If you edit your iptables beyond this tutorial, you may want to use the <code>iptables-save</code> and <code>iptables-restore</code> feature to edit and test your rules.  To do this open the rules file in your favorite text editor (in this example gedit).
<pre>
# iptables-save > /etc/iptables.test.rules
# gedit /etc/iptables.test.rules
</code>
You will have a file that appears similiar to (following the example above):
<pre>
# Generated by iptables-save v1.3.1 on Sun Apr 23 06:19:53 2006
*filter
:INPUT ACCEPT [368:102354]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [92952:20764374]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
-A INPUT -j DROP
COMMIT
# Completed on Sun Apr 23 06:19:53 2006
</code>
Notice that these are iptables commands minus the <code>iptable</code> command.  Feel free to edit this to file and save when complete.  Then to test simply:
<pre>
# iptables-restore < /etc/iptables.test.rules
</code>
After testing, if you have not added the <code>iptables-save</code> command above to your <code>/etc/network/interfaces</code> remember not to lose your changes:
<pre>
# iptables-save > /etc/iptables.up.rules
</code>

==== More detailed Logging ====
For further detail in your syslog you may want create an additional Chain.  This will be a very brief example of my /etc/iptables.up.rules showing how I setup my iptables to log to syslog:
<pre>
# Generated by iptables-save v1.3.1 on Sun Apr 23 05:32:09 2006
*filter
:INPUT ACCEPT [273:55355]
:FORWARD ACCEPT [0:0]
:LOGNDROP - [0:0]
:OUTPUT ACCEPT [92376:20668252]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -j LOGNDROP
-A LOGNDROP -p tcp -m limit --limit 5/min -j LOG --log-prefix "Denied TCP: " --log-level 7
-A LOGNDROP -p udp -m limit --limit 5/min -j LOG --log-prefix "Denied UDP: " --log-level 7
-A LOGNDROP -p icmp -m limit --limit 5/min -j LOG --log-prefix "Denied ICMP: " --log-level 7
-A LOGNDROP -j DROP
COMMIT
# Completed on Sun Apr 23 05:32:09 2006
</code>
Note a new CHAIN called <code>LOGNDROP</code> at the top of the file.  Also, the standard <code>DROP</code> at the bottom of the INPUT chain is replaceed with <code>LOGNDROP</code> and add protocol descriptions so it makes sense looking at the log.  Lastly we drop the traffic at the end of the <code>LOGNDROP</code> chain.  The following gives some idea of what is happening:
* <code>--limit</code> sets the number of times to log the same rule to syslog
* <code>--log-prefix "Denied..."</code> adds a prefix to make finding in the syslog easier
* <code>--log-level 7</code> sets the syslog level to informational (see man syslog for more detail, but you can probably leave this)

==== Disabling the firewall ====

If you need to disable the firewall temporarily, you can flush all the rules using
<pre>
# iptables -F
</code>

=== Easy configuration via GUI ===
A new user can use Firestarter (a gui), available in repositories (Synaptic or apt-get) to configure her/his iptable rules, without needing the command line knowledge. Please see the tutorial though... Configuration is easy, but may not be enough for the advanced user. However, it should be enough for the most home users... The (read:my) suggested outbound configuration is "restrictive", with whitelisting each connection type whenever you need it (port 80 for http, 443 for secure http -https-, 1863 for msn chat etc) from the "policy" tab within firestarter. You can also use it to see active connections from and to your computer... The firewall stays up once it is configured using the wizard. Dial-up users will have to specify it to start automatically on dial up in the wizard.

Homepage for firestarter: http://www.fs-security.com/ (again, available in repositories, no compiling required)
Tutorial: http://www.fs-security.com/docs/tutorial.php

Personal note: Unfortunately, it does not have the option to block (or ask the user about) connections of specific applications/programs... Thus, my understanding is that once you enable port 80 (i.e. for web access), any program that uses port 80 can connect to any server and do anything it pleases...

=== Further Information ===
[http://iptables-tutorial.frozentux.net/iptables-tutorial.html Iptables Tutorial]

[http://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO.html Iptables How To]

[http://www.netfilter.org/documentation/ Netfilter and Iptables Multilingual Documentation]

[http://easyfwgen.morizot.net/gen/ Easy Firewall Generator for IPTables]

=== Credits ===
Thanks to Rusty Russell and his How-To, as much of this is based off that.
----
CategorySecurity CategoryCleanup

[[category:UbuntuHelp]]