Would it not be easier to switch off ssh forwarding on the ssh server? Just change AllowTcpForwarding
from yes to no in your /etc/ssh/sshd_config. If this doesn't suit, you could try something along the lines of
iptables -A OUTPUT -o eth1 -p tcp --cmd-owner "sshd" -j DROP
With IPTables rules, order matters. The rules are added, and applied, in order. Moreover, when adding rules manually they get applied immediately. Thus, in your example, any packets going through the INPUT and OUTPUT chains start getting dropped as soon as the default policy is set. This is also, incidentally, why you received the error
message you did. What is happening is this:
- The default DROP policy get applied
- IPTables receives a hostname as a destination
- IPTables attempts a DNS lookup on 'serverfault.com'
- The DNS lookup is blocked by the DROP action
While the source/destination options will accept hostnames, it is strongly discouraged. To quote the man page,
Hostnames will be resolved once
only, before the rule is submitted to
the kernel. Please note that
specifying any name to be resolved
with a remote query such as DNS is a
really bad idea.
Slillibri hit the nail on the head which his answer, you missed the DNS ACCEPT rule. In your case it won't matter, but generally I would set the default policy later on the process. The last thing you want is to be working remotely and allow SSH after turning on a default deny.
Also, depending on your distribution, you should be able to save your firewall rules such that they will be automatically applied at start time.
Knowing all that, and rearranging your script, here is what I would recommend.
# Allow loopback
iptables -I INPUT 1 -i lo -j ACCEPT
# Allow DNS
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
# Now, allow connection to website serverfault.com on port 80
iptables -A OUTPUT -p tcp -d serverfault.com --dport 80 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# Drop everything
iptables -P INPUT DROP
iptables -P OUTPUT DROP
Best Answer
If this is a standalone host, I suggest you solve this issue within your mailserver, not a firewall. Gmail has every right to change their IP addresses as they see fit at any time and you'll end up having a broken system. Configure your mailserver to accept mails to addresses you allow and possibly from addresses you allow. Alternatively use something like fetchmail to download your mails from Gmail instead of using a forward.