If you use local firewall rules and tcp wrappers on a remote host where you might get locked out, with no easy way to get logged in again, here is a quick howto on playing it safe. The trick is to setup a couple cron jobs to undo whatever you stuffed up.
I scheduled two 10 minute recurring jobs. Gives you 10 minute windows of configuring/testing before security resets.
Paranoid hint: Make sure you stay logged into the target host with an extra terminal somewhere else as well.
I could also have done /etc/init.d/iptables restart or service iptables restart to reset rules from cron. That would prevent you from having a wide open machine after the flush. But the downside of that is if you save rules that were broken, a restart will load your saved (broken) rules.
Set two cron jobs:
[root@uhz002192 dev]# crontab -l/10 * * * * cp /root/hosts.deny /etc/hosts.deny/10 * * * * /sbin/iptables --flushTcp wrappers:
I made a copy of /etc/hosts.deny file in /root and then waited for the next cron run to test if the copy is really working as expected.
It looked good after cron ran.
# cat /etc/hosts.deny#...#ALL: ALLNow uncomment the ALL: ALL line in the real /etc/hosts.deny and start testing /etc/hosts.allow rules.
# more /etc/hosts.allow...# Host allowed to SSHsshd: xx.xx.xx.xxTest from non allowed and allowed host.
Feb 24 05:32:56 uhz002192 sshd[12346]: pam_unix(sshd:session): session opened for user rrosso by (uid=0)Feb 24 05:33:43 uhz002192 sshd[12380]: refused connect from host.domain.com (::ffff:xx.xx.xx.xx)
Feb 24 05:34:34 uhz002192 sshd[12386]: Accepted password for rrosso from xx.xx.xx.xx port 37415 ssh2Feb 24 05:34:34 uhz002192 sshd[12386]: pam_unix(sshd:session): session opened for user rrosso by (uid=0)Now lets go tune the firewall rules…
List rules:
# iptables --listChain INPUT (policy ACCEPT)target prot opt source destinationACCEPT all -- anywhere anywhereACCEPT icmp -- anywhere anywhere icmp anyACCEPT all -- anywhere anywhere state RELATED,ESTABLISHEDACCEPT tcp -- anywhere anywhere state NEW tcp dpt:webcacheACCEPT tcp -- anywhere anywhere state NEW tcp dpt:etlservicemgrACCEPT tcp -- anywhere anywhere state NEW tcp dpt:mysqlACCEPT tcp -- anywhere anywhere state NEW tcp dpt:redwood-brokerACCEPT tcp -- anywhere anywhere state NEW tcp dpt:sshACCEPT tcp -- anywhere anywhere state NEW tcp dpt:smtpACCEPT tcp -- anywhere anywhere state NEW tcp dpt:httpACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftpACCEPT tcp -- anywhere anywhere state NEW tcp dpt:httpsREJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)target prot opt source destination
Chain OUTPUT (policy ACCEPT)target prot opt source destinationSaved rules in this file:
# cat /etc/sysconfig/iptables# Firewall configuration written by system-config-securitylevel# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -i lo -j ACCEPT-A INPUT -p icmp --icmp-type any -j ACCEPT-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 9001 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3001 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibitedCOMMITDelete unneeded rules:
# iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT# iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT# iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT# iptables -D INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPTCheck (and test using something like nmap):
# iptables --listChain INPUT (policy ACCEPT)target prot opt source destinationACCEPT all -- anywhere anywhereACCEPT icmp -- anywhere anywhere icmp anyACCEPT all -- anywhere anywhere state RELATED,ESTABLISHEDACCEPT tcp -- anywhere anywhere state NEW tcp dpt:etlservicemgrACCEPT tcp -- anywhere anywhere state NEW tcp dpt:redwood-brokerACCEPT tcp -- anywhere anywhere state NEW tcp dpt:sshACCEPT tcp -- anywhere anywhere state NEW tcp dpt:httpACCEPT tcp -- anywhere anywhere state NEW tcp dpt:httpsREJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)target prot opt source destination
Chain OUTPUT (policy ACCEPT)target prot opt source destinationSave the rules:
# service iptables saveSaving firewall rules to /etc/sysconfig/iptables: [ OK ]Check stored rules:
# cat /etc/sysconfig/iptables# Generated by iptables-save v1.3.5 on Fri Feb 24 05:48:21 2012*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [734:96465]-A INPUT -i lo -j ACCEPT-A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 9001 -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 3001 -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibitedCOMMIT# Completed on Fri Feb 24 05:48:21 2012Check running rules:
# iptables --listChain INPUT (policy ACCEPT)target prot opt source destinationACCEPT all -- anywhere anywhereACCEPT icmp -- anywhere anywhere icmp anyACCEPT all -- anywhere anywhere state RELATED,ESTABLISHEDACCEPT tcp -- anywhere anywhere state NEW tcp dpt:etlservicemgrACCEPT tcp -- anywhere anywhere state NEW tcp dpt:redwood-brokerACCEPT tcp -- anywhere anywhere state NEW tcp dpt:sshACCEPT tcp -- anywhere anywhere state NEW tcp dpt:httpACCEPT tcp -- anywhere anywhere state NEW tcp dpt:httpsREJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)target prot opt source destination
Chain OUTPUT (policy ACCEPT)target prot opt source destinationDelete the cron job(s) when working!