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 
 

 

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