TCP Wrapper

TCP Wrapper is a host-based Networking ACL system, used to controlling access to network services.
 TCP Wrapper Work Flow
Required Packages
[root@client1 Desktop]# rpm -qa tcp_wrappers*
tcp_wrappers-7.6-57.el6.x86_64
tcp_wrappers-libs-7.6-57.el6.x86_64
The Most Important library packages
/lib64/libwrap.so.0
/lib64/libwrap.so.0.7.6
TCP Wrappers Configuration Files
To determine if a client is allowed to connect to service, TCP Wrappers reference the following two files, which are commonly referred to as
“hosts access” files:
·         /etc/hosts.allow
·         /etc/hosts.deny
Help command
#man hosts_options
#man hosts_access
Note:
To determine if a network service binary is linked to “libwrap.so”, type the following command as the root user:
ldd | grep libwrap
Example
[root@client1 Desktop]# ldd /usr/sbin/sshd | grep libwrap
            libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f22184a9000)
[root@server1 Desktop]# ldd /usr/sbin/vsftpd | grep libwrap
            libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f906a243000)
Advantages of TCP Wrappers
TCP Wrappers provide the following advantages over other network service control techniques:
·         Transparency to both the client and the wrapped network service — Both the connecting client and the wrapped network service are unaware that TCP Wrappers are in use. Legitimate users are logged and connected to the requested service while connections from banned clients fail.
·         Centralized management of multiple protocols — TCP Wrappers operate separately from the network services they protect, allowing many server applications to share a common set of access control configuration files, making for simpler management.
Important points when using TCP Wrappers to protect network services:
1.       If access to a service is allowed in “hosts.allow”, a rule denying access to that same service in “hosts.deny” is ignored.
2.       The rules in each file are read from the top down and the first matching rule for a given service is the only one applied. The order of the rules is extremely important
3.       If no rules for the service are found in either file, or if neither file exists, access to the service is granted.
4.       TCP wrapped services do not cache the rules from the hosts access file, so any changes to hosts.allow or hosts.deny take effect immediately without restarting network services.
Default Log Files
The TCP Wrappers will do all its logging via syslog according to yout /etc/syslog.conf file. The following table lists the standard locations where messages from TCP Wrappers will appear:
1.       AIX                                                         –              /var/adm/messages
2.       HP-UX                                                   –              /usr/spool/mqueue/syslog
3.       Linux                                                     –              /var/log/messages
4.       FreeBSD, OpenBSD, NetBSD       –              /var/log/messages
5.       Mac OS X                                             –              /var/log/system.log
6.       Solaris                                                   –              /var/log/syslog
Formatting Access Rules
The format for both /etc/hosts.allow and /etc/hosts.deny is identical.
daemon_list : client_list : option : option …
daemon_list : client_list [ : shell_command ]
Daemon list:
                                A comma – separated list of process names (not service names) or the ALL wildcard.
Client list:
                                A comma – separated list of hostnames, host IP addresses, special patterns, or wildcards which identify the hosts affected by the rule.
Options:
                                An optional action or colon – separated list of actions performed when the rule is triggered. Option fields support expansions, launch shell commands, allow or deny access, and alter logging behaviour.
Wildcards
Wildcards allow TCP Wrappers to more easily match groups of daemons or hosts.
ALL                 Specifies all networks
LOCAL            Specifies the local network
EXCEPT          Excludes a particular user/client
KNOWN          Indicates all hosts that can be resolved by the system
UNKNOWN    Indicates all hosts that can’t be resolved by the system
PARANOID     Specifies that the forward and reverse lookup IP address don’t match
Examples
Server1.example.com –           192.168.1.100
Client1.example.com  –           192.168.1.101
Client2.example.com  –           192.168.1.102
Network                      –           192.168.1.0/24
1.      Configure server1.example.com does not ssh access client1.example.com except client2.example.com
Server1.example.com
[root@server1 ~]# vim /etc/hosts.allow
sshd : client1.example.com : deny
sshd : client2.example.com : allow
:wq!
or
vim /etc/hosts.allow
sshd : client2.example.com
vim /etc/hosts.deny
sshd : client1.example.com
or
vim /etc/hosts.allow
sshd : client1.example.com EXCEPT client2.example.com : deny
client1.example.com
[root@client1 Desktop]# ssh server1.example.com
ssh_exchange_identification: Connection closed by remote host
client2.example.com
[root@client2 Desktop]# ssh server1.example.com
root@server1.example.com’s password:
Last login: Sun Nov  4 16:32:51 2012 from client1.example.com
2.      Deny the all daemon network services in example.com except vsftpd daemon services.
#vim /etc/hosts.allow
ALL EXCEPT vsftpd : .example.com : deny
Or
#vim /etc/hosts.allow
Vsftpd : .example.com
#vim /etc/hosts.deny
ALL : .example.com
Or
#vim /etc/hosts.deny
ALL EXCEPT vsftpd : .example.com
Note:
ALL : .example.com
ALL : *.example.com
ALL : 192.168.1.
ALL : 192.168.1.0/24
ALL : 192.168.1.100
ALL : 192168.1.0/255.255.255.0
ALL : *.example.com EXCEPT my.org
ALL : ALL EXCEPT *.example.com : deny
3.      Allow all the daemon network services with in example.com only other all all network restricted.
#vim /etc/hosts.allow
ALL : ALL EXCEPT *.example.com : deny
Or
#vim /etc/hosts.deny
ALL : ALL EXCEPT *.example.com
4.      TCP Wrapper configure using shell commands example
Configure all daemon service running allowed details stored particular log file.
[root@server1 ~]# touch /var/log/tcp_wrappers.log
[root@server1 ~]# vim /etc/hosts.allow
ALL : *.example.com \ : spawn /bin/echo %d from %c user %u >> /var/log/tcp_wrappers.log \ : spawn /bin/date >> /var/log/tcp_wrappers.log : allow
[root@server1 ~]# cat /var/log/tcp_wrappers.log
sshd from client2.example.com user unknown
Sun Nov  4 22:53:03 IST 2012
vsftpd from client2.example.com user unknown
Sun Nov  4 22:54:03 IST 2012

Deny requests for a particular service

[root@myvm1 ~]# cat /etc/hosts.allow
sshd: .slashroot.in
[root@myvm1 ~]#

In the above shown example sshd service is only allowed from “slashroot.in” domain.

[root@myvm1 ~]# cat /etc/hosts.allow
vsftpd: .slashroot.in
[root@myvm1 ~]#

In the above shown example, vsftpd service is only allowed from slashroot.in domain.

Again keep the fact in mind that a conflicting entry in hosts.deny will be ignored, because hosts.allow is processed first and if a request pattern is allowed, it will never process hosts.deny file at all.

Also you can deny these same requests as shown in the above examples, by making the same entry in hosts.deny, but in that case your hosts.allow must be empty or else must not contain similar rule for allowing.

Let’s see another pattern for allowing and denying hosts.

[root@myvm1 ~]# cat /etc/hosts.allow
ALL: 172.16.
[root@myvm1 ~]#

In the above example all hosts with the ip address 172.16.*.* is allowed to make connections to all TCP wrapper based services on the hosts.

In the above example if you add ALL: 172.16.104.54, in the file hosts.deny will not be of any use, because you have already allowed all requests from 172.16.*.* in hosts.allow file.

You can also make the same entry with IP and subnet mask based style, as shown below.

[root@myvm1 ~]# cat /etc/hosts.allow
ALL: 172.16.0.0/255.255.0.0
[root@myvm1 ~]#

If you want to deny or allow a large number of hosts, then you can also do that by mentioning the list of ip/hostnames in another file and pointing to that file in /etc/hosts.allow.

[root@myvm1 ~]# cat /etc/hosts.allow
sshd: /etc/sshd.hosts
[root@myvm1 ~]#

In the above rule, an important point to note is that the rule starts with a “/”, mentioning the path for the file.

Previously we saw that you can allow/deny an entire domain, but what if you want to make exceptions to some hosts on that domain.

[root@myvm1 ~]# cat /etc/hosts.allow
ALL: .slashroot.in EXCEPT example.slashroot.in
[root@myvm1 ~]#

In the above example all hosts from slashroot.in domain will be allowed except example.slashroot.in.

In the exact similar manner, you can also deny one particular service, after allowing the rest to a group of hosts or domain, as shown below.

[root@myvm1 ~]# cat /etc/hosts.allow
ALL EXCEPT sshd: 172.16.0.0/255.255.0.0
[root@myvm1 ~]#

In the above shown method all hosts from 172.16.0.0 network are allowed for all the services except ssh.

The <options> field in the tcp wrapper entry can also be used to make all entry in one files itself(Yeah that’s correct, you can use a single file for accept and deny rules. This is the best method to avoid confusion.), the syntax for such entry should be made, by taking an extra care.

[root@myvm1 ~]# cat /etc/hosts.deny
vsftpd : example1.slashroot.in : allow
sshd : example1.slashroot.in : deny
sshd : example2.slahroot.in : allow
[root@myvm1 ~]#

In the above example, i have made the entry of both allowing and denying connections to service in hosts.deny file(i have kept my hosts.allow file empty). “allow” and “deny” are part of the options filed in the entry.

Another important fact that must be kept in mind is the length of the access rule that you are making in tcp wrapper files.

One rule per line is the way it must be made. Otherwise rules might get skipped without applying them while processing. There is a workaround for this problem, by including “/”,  for all those rules that are lengthy. An example is shown below.

[root@myvm1 ~]# cat /etc/hosts.allow
vsftpd : 172.16.103.150 \ : spawn /bin/echo ftp access prohibited>>/var/log/ftp.log \ : deny
[root@myvm1 ~]#

In the above example, we have used backslashes to denote that the rule is one line. Also we have spawned echo process to make a text redirect to ftp log file. This kind of actions can be taken with the help of options field as shown above.

Like we have used spawn to echo some text content in ftp log, this can be made very detailed log with the help of some options.

[root@myvm1 ~]# cat /etc/hosts.allow
vsftpd : 172.16.103.150 \ : spawn /bin/echo %c %h %p %u ftp access prohibited>>/var/log/ftp.log \ : deny
[root@myvm1 ~]#

In the above example, i have used

%c for complete client information like username and hostname

%h is used to determine client’s ip address

%p is used to log process id of the process

%u is used for username of the client who is requesting the service.

You can make much more interesting things to trigger on matching a rule, using the same spawn method and redirection.

A complete mannuel entry for TCP wrapper can be found by running the below command as shown below.

[root@myvm1 ~]# man hosts_options
[root@myvm1 ~]#

Hope this article was helpful in understanding the concept of TCP wrappers in Linux.

Leave a comment