服务的访问规则
说明:linux下的进程分为独立进程和依赖于超级守护进程(xinetd)的进程。独立进程链接到libwrap.so库中,访问独立进程的时候需要被tcpd wrapper定义的规则控制,而非独立进程的访问需要被xinetd的默认控制规则和自身的访问规则所控制。当然,xinetd本身也是被链接到libwrap.so库中的。
e.g:访问telnet(非独立进程)的时候,访问的查看规则是:tepd wrapper—》xinetd—》telnet
注释:以下地址中192.168.0.46为服务器的地址,192.168.0.45为客户端的地址。
一.独立进程的访问规则
独立进程是链接到libwrap.so库中的,所以独立进程的访问规则受tcpd wrapper的规则限制,下面以vsftp服务作为演示:
1.     vim /etc/host.allow,在其中添加只能让192.168.0.48主机访问,vim /etc/host.deny,在其中添加拒绝所有的主机
# 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.
#
vsftpd : 192.168.0.48
# 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!
vsftpd:  ALL
然后用192.168.0.45主机登陆进行测试,结果为:
[root@station45 ~]# ftp 192.168.0.46
Connected to 192.168.0.46.
421 Service not available.
ftp>
登录失败。
2.     我们在/etc/hosts.allow中添加如下内容,此内容的目的是为了一旦有人登陆到vsftp时就会记录日志:
(1).#
# 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.
#
vsftpd : ALL : spawn echo "login attempt from %c to %s " >> /var/log/test.log
(2).touch /var/log/test.log
(3).登陆ftp服务器:ftp 192.168.0.46
(4).查看/var/log/test.log中是否有记录:
[root@station46 ~]# cat /var/log/test.log
login attempt from 192.168.0.45 to vsftpd@192.168.0.46
[root@station46 ~]#
可以看到结果是正确的
3.我们在/etc/hosts.allow中设置一下登录到ftp服务器上时出现的欢迎信息,在/etc/hosts.allow中添加如下内容:
vsftpd: ALL : banners /var/banners/
(2).然后在/var/banners/目录下编辑vsftpd文件:vim /var/banners/vsftpd
(3).登陆可以看到,在登录界面会出现我们在/var/banners/vsftpd中定义的欢迎信息:
[root@station45 ~]# ftp 192.168.0.46
Connected to 192.168.0.46.
how are you?
ftp>
二.守护进程的访问规则
守护进程是受超级守护进程(xinetd)控制的,访问守护进程的服务时需要先经过xinetd的默认访问规则,然后再经过自身访问规则的控制,下面以telnet服务为例:
1.     编辑/etc/xinetd.d文件设置默认规则,在其中所设定的规则是对所有依赖于守护进程的服务都有效的
2.     cd到/etc/xinetd.d目录中,编辑telnet文件,设定telnet服务自身的规则,在其中的设定如下:
service telnet
{
        disable = no
        flags           = REUSE
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/sbin/in.telnetd
        log_on_failure  += USERID
        instances = 2
        per_source  = 1
        bind = 192.168.0.46
}
绑定到192.168.0.46主机上
3.     然后编辑/etc/hosts.allow文件,设定对telnet服务的访问规则:
in.telnetd: 192.168.0.49
在/etc/hosts.deny中编辑如下:
in.telnetd:  ALL
4.     然后用用192.168.0.45主机登陆进行验证,结果如下:
[root@station45 ~]# telnet 192.168.0.46
Trying 192.168.0.46...
Connected to station46.example.com (192.168.0.46).
Escape character is '^]'.
Connection closed by foreign host.
[root@station45 ~]#
5.     我们改一下/etc/hosts.deny文件:
#in.telnetd:  ALL
然后登陆验证:
[root@station45 ~]# telnet 192.168.0.46
Trying 192.168.0.46...
Connected to station46.example.com (192.168.0.46).
Escape character is '^]'.
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Kernel 2.6.18-164.el5 on an i686
login:
可以看到登陆成功。
6.     我们去修改一下telnet自己的规则配置文件:vim /etc/xinetd.d/telnet
instances = 2
  per_source  = 1
在此的配置的作用是一个ip只能连接一个进程
7.     我们用192.168.0.45登录验证
[root@station45 ~]# telnet 192.168.0.46
Trying 192.168.0.46...
Connected to station46.example.com (192.168.0.46).
Escape character is '^]'.
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Kernel 2.6.18-164.el5 on an i686
login: redhat
Password:
Last login: Tue Feb 23 12:11:27 from station45
[redhat@station46 ~]$
上面显示的是用一个终端登录的结果,然后我们在开启一个终端,显示的结果如下:
[root@station45 ~]# telnet 192.168.0.46
Trying 192.168.0.46...
Connected to station46.example.com (192.168.0.46).
Escape character is '^]'.
Connection closed by foreign host.
[root@station45 ~]#
显示登录失败。
好了,就先这样了~~~~