Using TCP Wrappers hosts.allow and hosts.deny to secure RedHat / CentOS

RedHat / Centos and other similar Linux distros have 3 layers of protection that can restrict access to services (Iptables, TCP Wrappers (hosts.deny, hosts.allow) and SeLinux). When a machine on a network requests access to a specific service it first goes through Iptables, when access is granted it then goes through TCP Wrappers and then SeLinux is the last layer of protection. The best part about TCP wrappers is that it is English like and is very easy to understand. Here are some examples.

/etc/hosts.deny
vsftpd : ALL EXCEPT 192.168.1.
mysqld : ALL EXCEPT .example.com
sshd : ALL EXCEPT 192.168.1.2
ALL : 192.168.1.2
ALL : .example.com EXCEPT server.example.com
ALL : 192.168.
ALL : *.example.com
ALL : 64.55.34.22, 203.92.87.12, 12.14.34.43
ALL : 124.57.23.123/16, 64.55.34.22/24
ALL : 124.57.23.123/255.255.254.0

Denies access and logs a failed attempt to /var/log/connections.log
sshd : 192.168.11.217 \
: spawn (/bin/echo %a from %h attempted to access %d >> \
/var/log/connections.log) \
: deny

Example from the above rule:
[root@192.168.11.217 ~]# ssh 192.168.11.216
ssh_exchange_identification: Connection closed by remote host
[root@nfs ~]# cat /var/log/connections.log
192.168.11.217 from 192.168.11.217 attempted to access sshd

Additional info:
TCP Wrappers will not work with httpd or squid, use iptables instead.
All of these rules can be placed in hosts.allow or hosts.deny
192.168. – Anything in the 192.168.x.x subnet will be allowed/denied access
192.168.1. – Anything in the 192.168.1.x subnet is allowed/denied access
.example.com – Anything in the example.com domain is denied/allowed access
*.example.com – Anything in the example.com domain is denied/allowed access
Changes to the hosts.allow hosts.deny take effect immediately.
Only one rule is allowed per service in hosts.allow and hosts.deny.
Each rule has to be on a separate line.
You need to use both Iptables and TCP Wrappers for maximum protection.
Best way to test tcp wrappers is to stop Iptables and adjust the above rules.

SPONSOR:

This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *