Linux下的TCP Wrapper机制

inetd程序在系统中是作为一个服务进程出现,它监听许多端口并且在得到客户请求时启动这个端口的服务程序。
早期系统中使用的inetd被称作超级服务器,其实现控制对主机网络连接。当一个请求到达由inetd管理的服务端口,inetd将该请求转发给名为 tcpd的程序。tcpd根据配置文件host.{allow,deny}来判断是否允许服务该请求,如果请求被允许刚相应的服务器程序(如:ftpd、 telnet)将被启动。这个机制也被称为TCP_Wrapper
xinetd(eXended Internet services Daemon)提供类似于inetd+tcp_wrapper的功能,但是更加强大和安全。已经逐渐取代了inetd,并且提供了访问控制、加强的日志和资源管理功能,成了Linux系统的Internet标准超级守护进程。很多系统服务都用到了xinetd如:FTP、IMAP、POP和telnet等。/etc/services中所有的服务通过他们的端口来访问服务器的时候,先由xinetd来处理,在唤起服务请求之前,xinetd先检验请求者是否满足配置文件中指定的访问控制规则,当前的访问是否超过了指定的同时访问数目,还有配置文件中指定的其他规则等,检查通过,xinetd将这个请求交付到相应的服务去处理,自己就进入sleep状态,等待下一个请求的处理。
 
以telnet为例,每当有telnet的连接请求时,tcpd即会截获请求,先读取系统管理员所设置的访问控制文件,合乎要求,则会把这次连接原封不动的转给真正的telnet进程,由telnet完成后续工作;如果这次连接发起的ip不符合访问控制文件中的设置,则会中断连接请求,拒绝提供telnet服务。
#ldd $(which Command) | grep wrap "查看是否支持TCP Wrapper的服务"
[root@rhel6 ~]# ldd `which vsftpd` | grep wrap
        libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f58c2cdd000)    "有返回值则表示支持TCP_Wrapper"
[root@rhel6 ~]# ldd `which sshd` | grep wrap  
        libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fdbbc848000)
 
·    配置文件
TCP_Wrappers的主要配置文件为:/etc/hosts.allow/etc/hosts.deny
/usr/sbin/tcpd进程首先检查/etc/hosts.allow,如果请求访问的主机名或IP包含在此文件中,则允许访问。
如果请求访问的主机名或IP不包含在/etc/hosts.allow那么tcpd进程就检查/etc/hosts.deny。如果请求访问的主机名或IP包含在hosts.deny文件中则访问就被拒绝;
如果都没有,默认许可
·    访问控制规则的格式
访问控制需要加在/etc/hosts.allow/etc/hosts.deny里,规则格式如下:
       <daemon list>:<client list>[:<option>:<option>:...]
     daemon list        服务进程名列表,如telnet的服务进程名为in.telnetd
     client list            访问控制的客户端列表,可以写域名、主机名或网段,如.example.com或者192.168.1.
     option               可选选项,这里可以是某些命令,也可以是指定的日志文件
 
  1. [root@rhel6 ~]# cat /etc/hosts.allow  
  2. # hosts.allow   This file contains access rules which are used to 
  3. #               allow or deny connections to network services that 
  4. #               either use the tcp_wrappers library or that have been 
  5. #               started through a tcp_wrappers-enabled xinetd. 
  6. #               See 'man 5 hosts_options' and 'man 5 hosts_access' 
  7. #               for information on rule syntax. 
  8. #               See 'man tcpd' for information on tcp_wrappers 
  9. in.telnetd:.xfcy.org 
  10. vsftpd:192.168.0. 
  11. sshd:192.168.0.0/255.255.255.0 
  12.  
  13. [root@rhel6 ~]# cat /etc/hosts.deny  
  14. # hosts.deny    This file contains access rules which are used to 
  15. #               deny connections to network services that either use 
  16. #               the tcp_wrappers library or that have been 
  17. #               started through a tcp_wrappers-enabled xinetd. 
  18. #               The rules in this file can also be set up in 
  19. #               /etc/hosts.allow with a 'deny' option instead
  20. #               See 'man 5 hosts_options' and 'man 5 hosts_access' 
  21. #               for information on rule syntax. 
  22. #               See 'man tcpd' for information on tcp_wrappers 
  23. ALL:ALL 
  24.  
  25. /etc/hosts.deny里的ALLALL表示,除非在/etc/hosts.allow里明确允许访问,否则一律拒绝 
  26. /etc/hosts.allow里第一行in.telnetd:.xfcy.org表示,只有xfcy.org这个域里的主机允许访问telnet服务,注意xfcy.org前面的那个点(.) 
  27. /etc/hosts.allow里第二行表示,只有192.168.0这个网段的用户允许访问FTP服务,注意0后面的点(.) 
  28. /etc/hosts.allow里第三行表示,只有192.168.0这个网段的用户允许访问SSH服务,注意这里不能写为192.168.0.0/24 
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章