Posted by:dillinja
Egress filtering for a healthier Internet.
By Brian Hatch.
The Slammer/Sapphire/etc worm really ruined my Internet experience for a few days. Yes, patches were available. Yes, people should have blocked inbound access to their windows boxes when not needed. These and many other explanations about what could have been done were discussed all over. However one thing that seems to have been overlooked in many sources is that it was the outbound packets that were killing us, and we shouldn't have been subject to them at all.
We are used to thinking of security as a defensive battle: keep the bad guys out of our systems. We set up our firewalls to block known attacks, block any inbound packets that don't go to permitted ports, restrict inbound packets from untrusted sources, keep spam out by blocking connections from machines on DNSBLs[1]. These are all defensive barriers we erect in front of our servers to keep the bad guys out.
The problem is that sooner or later there will be a breach of those barriers. Perhaps a bug exists in one of the services that you do need to permit. Perhaps your firewall rules were too weak. The problem is that once you are cracked, the cracker is on the inside of the firewall, and is uninhibited by your inbound rulesets.
Blocking inappropriate inbound access is a very good and necessary thing. However people need to start blocking inappropriate outbound access too. If your machine is just a mail server, then it needs to be able to send out packets from and to port 25, but it doesn't need to be able to make an HTTP connection. If you run a DNS server, it doesn't need to be able to support outbound FTP. And for goodness sake, if your network is w.x.y.z, packets from other addresses should never leave your boarders.
When your computer is compromised, you are no longer the innocent party trying to defend yourself, to other machines you have become the attacker. You owe it to others to make outbound attacks more difficult to the cracker or worms that have managed to get onto your machine.
Outbound packet security is called egress filtering. It's no harder to implement than the ingress filtering rules you should already have. Had the Slammer hosts been behind firewalls with proper egress filters, the outbound attacks would never have been dropped at their firewall/router.
As always, the best rule when creating firewalls/access lists is to list those packets which should be allowed and deny the rest. Be very restrictive! Better to break a few applications during testing than to leave holes open. If you have a completely new network, it is easy to block everything and open ports as needed. if you have an existing network, you should run a sniffer, logging the type of network activity you experience currently, and then create your rules to match them.[2]
So, let's take an example of the following server. We'll create both ingress and egress filters such that it does exactly what it needs, and no more.
Our sample machine runs Apache, Postfix, and allows inbound SSH access from the local network only. It needs to be able to look up DNS entries for it's Apache logfiles and to support a few DNSBL lists for preventing inbound spam. Here are the iptables rules you'd create on this host to support this configuration:




