IptablesHowTo

Differences between revisions 10 and 11
Revision 10 as of 2006-04-23 11:03:25
Size: 11373
Editor: c-69-253-244-226
Comment:
Revision 11 as of 2006-06-19 16:07:34
Size: 57
Editor: 127
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
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 regullar 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
{{{
# sudo 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
{{{
# sudo iptables -F
}}}

== Easy configuration via GUI ==
A newbie 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. Dialup 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.

CategoryDocumentation CategoryCleanup
#REFRESH 0 http://help.ubuntu.com/community/IptablesHowTo

IptablesHowTo (last edited 2008-08-06 16:25:16 by localhost)