转载tcp_wrappers

来源:互联网 发布:报表数据分析 编辑:程序博客网 时间:2024/06/06 01:39

inux访问控制的流程:

----------iptables----------tcp_wrappers----------服务本身的访问控制。

Iptables:          基于原IP,目的IP,原端口,目的端口来进行控制的。

Tcp_wrappers:对服务的本身来进行控制。

Service:           对行为做控制,更细致的控制。

 

今天我们来讨论第二层防火墙tcp_wrappers。

Tcp_wrappwes的访问控制主要是两个文件,

/etc/hosts.allow  

/etc/hosts.deny 

在/etc/hosts.allow是用来定义允许的访问,

在/etc/hosts.deny是用来定义拒绝的访问。

 

现在我们来了解一下tcp_wrappers的访问控制判断顺序,

首先查看/etc/hosts.allow,如果匹配到一个条目,将不在读取下面的。

如果在/etc/hosts.allow没有匹配到条目,则读取/etc/hosts.deny,如果匹配,则拒绝,如果不匹配,则默认允许所有。

举例说明:
allow     192.168.1.0/24

Deny     192.168.1.1

如果是这样的呢,192.168.1.1可以访问吗?

是可以的,在allow里面定义了允许192.168.1.0/24,这个网段里面就包含了192.168.1.1。它已经明确了指定了允许192.168.1.0/24的网络,那么192.168.1.1匹配到这个条目,就不在往下面读。

 

关于tcp_wrappers的语法

Daemon_list : client_list

还可以这样写,指定在那台主机上面的服务,

daemon@host :client_list

 

关于客户机的说明

IP地址   192.168.0.1

域名      .example.com

网段      192.168.0.0/255.255.255.0  192.168.0.

这里的表示方法不可以用/24。

 

现在我们来做个试验,

默认情况下,我们的tcp_wrappers防火墙的默认允许所有的,是因为在/etc/hosts.allow和/etc/hosts.deny这两个文件里面默认是任何策略也没有的。

现在我们在192.168.0.10这台主机上面去ssh到192.168.0.254。

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.254

root@192.168.0.254's password:

Last login: Thu Mar 11 21:56:54 2010 from 192.168.0.254

[root@localhost ~]#

OK,可以看到,我们没有做任何的策略,192.168.0.10的确是可以ssh到192.168.0.254这台主机上面去的。

下面我们开始做策略,在192.168.0.254这台主机上面拒绝192.168.0.10来ssh。

编辑/etc/hosts.deny文件,

Vim  /etc/hosts.deny

#

# hosts.deny    This file describes the names of the hosts which are

#               *not* allowed to use the local INET services, as decided

#               by the '/usr/sbin/tcpd' server.

#

# The portmap line is redundant, but it is left to remind you that

# the new secure portmap uses hosts.deny and hosts.allow.  In particular

# you should know that NFS uses portmap!

sshd: 192.168.0.10

~

/etc/hosts.deny文件就定义完成了,现在来测试下,

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.254

ssh_exchange_identification: Connection closed by remote host

[root@localhost ~]#

可以看到,现在连接就被直接拒绝了。

当然,后面匹配的客户端的写法可以是192.168.0.0/255.255.255.0,也可以是192.168.0.这个也可以。用域的方式来表示也没有问题可以写成.example.com。但是不可以用192.168.0.0/24来表示一个网段。

 

关于主机的宏定义表示方式

宏定义表示某一种类型的主机

LOCAL             主机中不含.的主机(通常是指自己)

KNOWN           所有在DNS中可以解析到的主机

UNKNOWN     所有在DNS不可以解析到的主机

PARANOID      所有在DNS中正向解析与反向解析不匹配的主机

ALL                  代表匹配所有(这个主机和服务都可以定义)

还有一个非常有用的定义方式:

EXCEPT

这个就代表反向选择,

现在做一个试验来理解这个参数的意义,

先在192.168.0.254这台主机上面定义./etc/hosts.deny这个文件,

#

# hosts.deny    This file describes the names of the hosts which are

#               *not* allowed to use the local INET services, as decided

#               by the '/usr/sbin/tcpd' server.

#

# The portmap line is redundant, but it is left to remind you that

# the new secure portmap uses hosts.deny and hosts.allow.  In particular

# you should know that NFS uses portmap!

sshd: 192.168.0.0/255.255.255.0 EXCEPT 192.168.0.10

/etc/hosts.deny文件定义完成了,来测试下。

~

现在我们在192.168.0.10上面去ssh到192.168.0.254上,看下能否成功。

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.254

root@192.168.0.254's password:

Last login: Thu Mar 11 22:25:04 2010 from 192.168.0.10

[root@localhost ~]#

OK,192.168.0.10是可以ssh到192.168.0.254上面去的。

现在我们在来利用192.168.0.20来ssh到192.168.0.254,看下能否成功。

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.254

ssh_exchange_identification: Connection closed by remote host

[root@localhost ~]#

可以看到,现在连接就被拒绝了。

为什么192.168.0.10可以ssh到192.168.0.254上面,而192.168.0.20不可以ssh到192.168.0.254上面去呢,这个就是刚才EXCEPT的作用。我们在/etc/hosts.deny文件里面定义了拒绝192.168.0.0/255.255.255.0这个网络中的所有主机,但是,除了192.168.0.10以外。就是这个原因。

我们刚才是将EXCEPT写在了/etc/hosts.deny文件中,如果写在/etc/hosts.allow里面呢,那么就和写在deny里面就又不一样了。

现在将hosts.deny给清空掉,然后编辑hosts.allow文件。

#

# hosts.allow   This file describes the names of the hosts which are

#               allowed to use the local INET services, as decided

#               by the '/usr/sbin/tcpd' server.

#

sshd: 192.168.0.0/255.255.255.0 EXCEPT 192.168.0.10

~

/etc/hosts.allow文件就编辑完成了,这个时候,192.168.0.10可以ssh到192.168.0.254吗,测试下。

[root@localhost ~]#

[root@localhost ~]# ssh 192.168.0.254

root@192.168.0.254's password:

Last login: Thu Mar 11 22:31:42 2010 from 192.168.0.20

[root@localhost ~]#

可以看到,是允许的,这个又是为什么呢,当192.168.0.10想要去ssh到192.168.0.254这台主机的时候,先读取/etc/hosts.allow文件,在hosts.allow文件中定义了允许192.168.0.0/255.255.255.0这个网络,除了192.168.0.10这台主机,但是并没有明确给192.168.0.10这台主机一个允许或拒绝的定义,是先读取hosts.allow文件,而这个文件只是将192.168.0.10这台主机排除在外,最终还是要看hosts.deny文件中的定义是怎么样的。

 

查询那些服务是支持tcp_wrappers

可以先查询这个服务的脚本在哪里,用which命令,

[root@localhost ~]#

[root@localhost ~]# which sshd

/usr/sbin/sshd

[root@localhost ~]#

在使用ldd命令来查询

查询这个脚本在允许的时候调用了那些动态链接库文件,

[root@localhost ~]#

[root@localhost ~]# ldd /usr/sbin/sshd |grep libwrap

        libwrap.so.0 => /lib/libwrap.so.0 (0x00b92000)

[root@localhost ~]#

如果服务调用了libwrap.so这个动态链接库文件就代表这个服务支持tcp_wrappers。

当然也可以这样来直接查询,

[root@localhost ~]#

[root@localhost ~]# ldd `which vsftpd` |grep libwrap

        libwrap.so.0 => /lib/libwrap.so.0 (0x00825000)

[root@localhost ~]#

可以看到,vsftpd也是可以支持tcp_wrappers的。

还有一种方式也可以来查询那些服务可以支持tcp_wrappers。

[root@localhost ~]#

[root@localhost ~]# strings `which portmap` |grep hosts

hosts_access_verbose

hosts_allow_table

hosts_deny_table

/etc/hosts.allow

/etc/hosts.deny

[root@localhost ~]#

只要查询出来有/etc/hosts.allow和/etc/hosts.deny这两个文件就代表这个服务支持tcp_wrappers。

那么我们的xinetd服务是否支持tcp_wrappers呢?

[root@localhost ~]#

[root@localhost ~]# ldd `which xinetd` |grep libwrap

        libwrap.so.0 => /lib/libwrap.so.0 (0x0035a000)

[root@localhost ~]#

可以看到,xinetd服务是支持tcp_wrappers的,也就是说由xinetd管理的所有服务都是支持tcp_wrapers的。

使用ldd和strings工具来查询,只要满足其中一种方式就可以支持tcp_wrappers。

当然,也有很多服务是不支持tcp_wrappers的,

比如说named,httpd等等。

这也是为什么还要有iptables防火墙,它会更加的强大的

0 0