个人工具

“UbuntuHelp:IptablesHowTo”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
(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...)
 
第12行: 第12行:
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables -L
 
# iptables -L
</nowiki></code>
+
</nowiki></pre>
 
lists your current rules in iptables. If you have just set up your server, you will have no rules, and you should see
 
lists your current rules in iptables. If you have just set up your server, you will have no rules, and you should see
 
<pre><nowiki>
 
<pre><nowiki>
第23行: 第23行:
 
Chain OUTPUT (policy ACCEPT)
 
Chain OUTPUT (policy ACCEPT)
 
target    prot opt source              destination
 
target    prot opt source              destination
</nowiki></code>
+
</nowiki></pre>
  
  
第30行: 第30行:
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
</nowiki></code>
+
</nowiki></pre>
  
 
=== Allowing Incoming Traffic on Specific Ports ===
 
=== Allowing Incoming Traffic on Specific Ports ===
第38行: 第38行:
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT
 
# iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT
</nowiki></code>
+
</nowiki></pre>
 
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.
 
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.
  
第48行: 第48行:
 
ACCEPT    all  --  anywhere            anywhere            state RELATED,ESTABLISHED
 
ACCEPT    all  --  anywhere            anywhere            state RELATED,ESTABLISHED
 
ACCEPT    tcp  --  anywhere            anywhere            tcp dpt:ssh
 
ACCEPT    tcp  --  anywhere            anywhere            tcp dpt:ssh
</nowiki></code>
+
</nowiki></pre>
  
 
Now, let's allow all web traffic
 
Now, let's allow all web traffic
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT
 
# iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT
</nowiki></code>
+
</nowiki></pre>
  
 
Checking our rules, we have
 
Checking our rules, we have
第63行: 第63行:
 
ACCEPT    tcp  --  anywhere            anywhere            tcp dpt:ssh
 
ACCEPT    tcp  --  anywhere            anywhere            tcp dpt:ssh
 
ACCEPT    tcp  --  anywhere            anywhere            tcp dpt:www
 
ACCEPT    tcp  --  anywhere            anywhere            tcp dpt:www
</nowiki></code>
+
</nowiki></pre>
  
 
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.
 
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.
第79行: 第79行:
 
ACCEPT    tcp  --  anywhere            anywhere            tcp dpt:www
 
ACCEPT    tcp  --  anywhere            anywhere            tcp dpt:www
 
DROP      all  --  anywhere            anywhere
 
DROP      all  --  anywhere            anywhere
</nowiki></code>
+
</nowiki></pre>
  
 
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.
 
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.
第97行: 第97行:
 
ACCEPT    all  --  anywhere            anywhere
 
ACCEPT    all  --  anywhere            anywhere
 
DROP      all  --  anywhere            anywhere
 
DROP      all  --  anywhere            anywhere
</nowiki></code>
+
</nowiki></pre>
  
 
The last two lines look nearly the same, so we will list iptables in greater detail.
 
The last two lines look nearly the same, so we will list iptables in greater detail.
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables -L -v
 
# iptables -L -v
</nowiki></code>
+
</nowiki></pre>
  
 
=== Logging ===
 
=== Logging ===
第108行: 第108行:
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
 
# iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
</nowiki></code>
+
</nowiki></pre>
 
See Tips section for more ideas on logging.
 
See Tips section for more ideas on logging.
  
第118行: 第118行:
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables-save > /etc/iptables.up.rules
 
# iptables-save > /etc/iptables.up.rules
</nowiki></code>
+
</nowiki></pre>
 
Then modify the ''/etc/network/interfaces'' script to apply the rules automatically (the bottom line is added)
 
Then modify the ''/etc/network/interfaces'' script to apply the rules automatically (the bottom line is added)
 
<pre><nowiki>
 
<pre><nowiki>
第124行: 第124行:
 
iface eth0 inet dhcp
 
iface eth0 inet dhcp
 
   pre-up iptables-restore < /etc/iptables.up.rules
 
   pre-up iptables-restore < /etc/iptables.up.rules
</nowiki></code>
+
</nowiki></pre>
  
 
You can also prepare a set of down rules and apply it automatically
 
You can also prepare a set of down rules and apply it automatically
第132行: 第132行:
 
   pre-up iptables-restore < /etc/iptables.up.rules
 
   pre-up iptables-restore < /etc/iptables.up.rules
 
   post-down iptables-restore < /etc/iptables.down.rules
 
   post-down iptables-restore < /etc/iptables.down.rules
</nowiki></code>
+
</nowiki></pre>
  
 
=== Tips ===
 
=== Tips ===
第140行: 第140行:
 
   pre-up iptables-restore < /etc/iptables.up.rules
 
   pre-up iptables-restore < /etc/iptables.up.rules
 
   post-down iptables-save > /etc/iptables.up.rules
 
   post-down iptables-save > /etc/iptables.up.rules
</nowiki></code>
+
</nowiki></pre>
 
The line "post-down iptables-save > /etc/iptables.up.rules" will save the rules to be used on the next boot.
 
The line "post-down iptables-save > /etc/iptables.up.rules" will save the rules to be used on the next boot.
  
第148行: 第148行:
 
# iptables-save > /etc/iptables.test.rules
 
# iptables-save > /etc/iptables.test.rules
 
# gedit /etc/iptables.test.rules
 
# gedit /etc/iptables.test.rules
</nowiki></code>
+
</nowiki></pre>
 
You will have a file that appears similiar to (following the example above):
 
You will have a file that appears similiar to (following the example above):
 
<pre><nowiki>
 
<pre><nowiki>
第164行: 第164行:
 
COMMIT
 
COMMIT
 
# Completed on Sun Apr 23 06:19:53 2006
 
# Completed on Sun Apr 23 06:19:53 2006
</nowiki></code>
+
</nowiki></pre>
 
Notice that these are iptables commands minus the <code><nowiki>iptable</nowiki></code> command.  Feel free to edit this to file and save when complete.  Then to test simply:
 
Notice that these are iptables commands minus the <code><nowiki>iptable</nowiki></code> command.  Feel free to edit this to file and save when complete.  Then to test simply:
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables-restore < /etc/iptables.test.rules
 
# iptables-restore < /etc/iptables.test.rules
</nowiki></code>
+
</nowiki></pre>
 
After testing, if you have not added the <code><nowiki>iptables-save</nowiki></code> command above to your <code><nowiki>/etc/network/interfaces</nowiki></code> remember not to lose your changes:
 
After testing, if you have not added the <code><nowiki>iptables-save</nowiki></code> command above to your <code><nowiki>/etc/network/interfaces</nowiki></code> remember not to lose your changes:
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables-save > /etc/iptables.up.rules
 
# iptables-save > /etc/iptables.up.rules
</nowiki></code>
+
</nowiki></pre>
  
 
==== More detailed Logging ====
 
==== More detailed Logging ====
第194行: 第194行:
 
COMMIT
 
COMMIT
 
# Completed on Sun Apr 23 05:32:09 2006
 
# Completed on Sun Apr 23 05:32:09 2006
</nowiki></code>
+
</nowiki></pre>
 
Note a new CHAIN called <code><nowiki>LOGNDROP</nowiki></code> at the top of the file.  Also, the standard <code><nowiki>DROP</nowiki></code> at the bottom of the INPUT chain is replaceed with <code><nowiki>LOGNDROP</nowiki></code> and add protocol descriptions so it makes sense looking at the log.  Lastly we drop the traffic at the end of the <code><nowiki>LOGNDROP</nowiki></code> chain.  The following gives some idea of what is happening:
 
Note a new CHAIN called <code><nowiki>LOGNDROP</nowiki></code> at the top of the file.  Also, the standard <code><nowiki>DROP</nowiki></code> at the bottom of the INPUT chain is replaceed with <code><nowiki>LOGNDROP</nowiki></code> and add protocol descriptions so it makes sense looking at the log.  Lastly we drop the traffic at the end of the <code><nowiki>LOGNDROP</nowiki></code> chain.  The following gives some idea of what is happening:
 
* <code><nowiki>--limit</nowiki></code> sets the number of times to log the same rule to syslog
 
* <code><nowiki>--limit</nowiki></code> sets the number of times to log the same rule to syslog
第205行: 第205行:
 
<pre><nowiki>
 
<pre><nowiki>
 
# iptables -F
 
# iptables -F
</nowiki></code>
+
</nowiki></pre>
  
 
=== Easy configuration via GUI ===
 
=== Easy configuration via GUI ===

2007年5月13日 (日) 12:32的版本


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

lists your current rules in iptables. If you have just set up your server, you will have no rules, and you should see

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


Allowing Established Sessions

We can allow established sessions to receive traffic:

# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

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.

# iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT

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)

# 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

Now, let's allow all web traffic

# iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT

Checking our rules, we have

# 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

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.

# 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

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.

# 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

The last two lines look nearly the same, so we will list iptables in greater detail.

# iptables -L -v

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:

# iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

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 iptables-save and iptables-restore.

Configuration on startup

Save your firewall rules to a file

# iptables-save > /etc/iptables.up.rules

Then modify the /etc/network/interfaces script to apply the rules automatically (the bottom line is added)

auto eth0
iface eth0 inet dhcp
  pre-up iptables-restore < /etc/iptables.up.rules

You can also prepare a set of down rules and apply it automatically

auto eth0
iface eth0 inet dhcp
  pre-up iptables-restore < /etc/iptables.up.rules
  post-down iptables-restore < /etc/iptables.down.rules

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 /etc/network/interfaces:

  pre-up iptables-restore < /etc/iptables.up.rules
  post-down iptables-save > /etc/iptables.up.rules

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 iptables-save and iptables-restore feature to edit and test your rules. To do this open the rules file in your favorite text editor (in this example gedit).

# iptables-save > /etc/iptables.test.rules
# gedit /etc/iptables.test.rules

You will have a file that appears similiar to (following the example above):

# 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

Notice that these are iptables commands minus the iptable command. Feel free to edit this to file and save when complete. Then to test simply:

# iptables-restore < /etc/iptables.test.rules

After testing, if you have not added the iptables-save command above to your /etc/network/interfaces remember not to lose your changes:

# iptables-save > /etc/iptables.up.rules

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:

# 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

Note a new CHAIN called LOGNDROP at the top of the file. Also, the standard DROP at the bottom of the INPUT chain is replaceed with LOGNDROP and add protocol descriptions so it makes sense looking at the log. Lastly we drop the traffic at the end of the LOGNDROP chain. The following gives some idea of what is happening:

  • --limit sets the number of times to log the same rule to syslog
  • --log-prefix "Denied..." adds a prefix to make finding in the syslog easier
  • --log-level 7 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

# iptables -F

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

Iptables Tutorial

Iptables How To

Netfilter and Iptables Multilingual Documentation

Easy Firewall Generator for IPTables

Credits

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


CategorySecurity CategoryCleanup