码迷,mamicode.com
首页 > 移动开发 > 详细

tcp_wrappers应用详解

时间:2016-04-23 15:11:00      阅读:1004      评论:0      收藏:0      [点我收藏+]

标签:系统管理员   应用程序   防火墙   第三方   客户端   tcp_wrappers   

简介
    TCP wrappers 通常被称为 wrappers,它是由 Wieste Venema 编写,已经出现几年了。其背后的思想很简单,其要旨是可以在您的 AIX (UNIX/Linux) 主机上快速轻松地锁定流行的通过 TCP 访问的客户端。
    Wrappers 允许系统管理员控制 wrappers 支持的基于 TCP 的服务或守护进程的访问。Tcpd 控制从 /etc/inetd.conf 中运行的 TCP 守护进程。不过,很多基于 TCP 的应用程序已加入 wrappers 支持(一般使用 libwrap 库),并且能感知 wrapper;不通过 /etc/inetd.conf 进行控制也没有关系。可以在 wrappers 下进行访问控制的通常有 telnet、ssh、sendmail、ftp 包、pop3 和 vsftpd。

Wrappers 提供对基于 UDP 的连接的有限控制,不过我建议使用内置或第三方防火墙进行基于 UDP 的访问。


1.首先我得了解程序是否支持tcp_wrapper,使用strings命令和ldd命令获取hosts_access或libwrap

    (1)并非所有服务均能由tcp_wrapper

    (2)判断某服务程序是否能够由tcp_wrapper
        动态编译:

 ldd 命令检测其是否链接至libwrap库上即可
 # ldd `which sshd` | grep libwrap
 libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f6788fe8000)

        静态编译:

 strings /path/to/program
  # which rpcbind
  /sbin/rpcbind
  # strings /sbin/rpcbind | grep hosts_access
  hosts_access

2.tcp_wrapper配置文件语法格式:
        daemon_list: client_list
        daemon_list:
            应用程序名称;
            应用程序列表:如果有多个,用逗号分隔即可;
            ALL:匹配所有进程
        
        client_list:
            IP地址:172.16.100.100
            主机名:www.opensamlee.com
            网络地址/子网掩码:掩码仅允许使用长格式,不允许使用CIDR格式
            172.16. 表示 172.16.0.0/255.255.0.0
            
        tcp_wrapper有几个内置的Macro     
            用于client_list的有:ALL, KNOWN, UNKNOWN, PARANOID
            用于daemon_list的有:ALL
        
        EXCEPT: 可以用于client或daemon之中,起到排除功能
        
        [:options]
            在hosts.allow文件使用deny选项: 在hosts.allow文件定义拒绝规则
            在hosts.deny文件使用allow选项:在hosts.deny文件定义放行规则
        
        spawn命令调用:
            echo
                %c: user@host
                %s: server@host
                %h: 客户端主机名
                %p: 服务器上的进程的PID
        
        #man 5 hosts_access:获取其完整帮助信息    
        注意:echo的信息无需加引号,否则,命令替换可能不会进行       

 in.telnetd:     ALL : spawn echo `date` login attempt from %c to %s >> /var/log/tcp_wrapper.log

3.tcp_wrapper匹配机制:
1、先检查/etc/hosts.allow,如果被允许,则直接放行;
2、如果/etc/hosts.allow没有匹配项,则检查/etc/hosts.deny,如果被拒绝,则禁止访问;
3、如果二者均无匹配,则放行


tcp_wrapper实践应用案例:

  1. 本机ip为:172.16.100.7,

  (1)仅放行172.16网段访问本机telnet服务:

  (2)仅放行172.16网段访问本机telnet服务,但不允许172.16.100.8访问

安装telnet服务:

# yum -y install telnet-server

查看telnet程序依赖的工作链

# ldd `which in.telnetd` 
linux-vdso.so.1 =>  (0x00007fff450c1000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f8055a31000)
libc.so.6 => /lib64/libc.so.6 (0x00007f805569d000)
/lib64/ld-linux-x86-64.so.2 (0x0000003541000000)
# ldd `which xinetd`
libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fe109c7e000)

启动telnet服务:

# chkconfig telnet on
# service xinetd start
Starting xinetd:                                           [  OK  ]
# ss -tnl |grep :23
LISTEN     0      64                       :::23                      :::*

定义tcp_wrapper规则文件---仅放行172.16网段访问本机telnet服务:

# vim /etc/hosts.allow
in.telnetd:    192.168.0.
# vim /etc/hosts.deny 
in.telnetd:    ALL

定义tcp_wrapper规则文件---仅放行172.16网段访问本机telnet服务,但不允许172.16.100.8访问

# vim /etc/hosts.allow
in.telnetd:    172.16. EXCEPT 172.16.100.8
# vim /etc/hosts.deny 
in.telnetd:    ALL

使用172.16.100.8主机访问:
# telnet 172.16.100.7
Trying 172.16.100.7...
Connected to 172.16.100.7.
Escape character is ‘^]‘.
Connection closed by foreign host.


2.本机ip:172.16.100.7

(1)sshd仅允许172.16.0.0/16网段访问:

方法:
1、/etc/hosts.allow
sshd: 172.16.
2、/etc/hosts.deny
sshd: ALL

3.本机ip:172.16.100.7

telnet服务不允许172.16.0.0/16,但允许172.16.100.200访问;其他客户端不做控制。

方法1:
1、/etc/hosts.allow
in.telnetd: 172.16.100.200
2、/etc/hosts.deny
in.telnetd: 172.16.
方法2:
/etc/hosts.deny
in.telnetd: 172.16. EXCEPT 172.16.100.200
方法3:
/etc/hosts.allow
in.telnetd: ALL EXCEPT 172.16. EXCEPT 172.16.100.200
/etc/hosts.deny
in.telnetd: ALL

3.本机ip:172.16.100.7

使用spawn命令调用记录用户访问控制记录信息:

/etc/hosts.allow
记录登录用户日志:
##记录允许172.16.主机访问记录
in.telnetd: 172.16. : spawn echo "somebody entered,`date`" >> /var/log/tcpwrapper.log
##记录允许172.16.主机 且排除 172.16.100.8主机的访问sshd记录
sshd: 172.16. EXCEPT 172.16.100.8 :spawn echo "somebody entered,`date`" >> /var/log/tcpwrapper.log

/etc/hosts.deny
in.telnetd: ALL
sshd: ALL

4.本机ip:172.16.100.7
(1)仅允许172.16.0.0/16网络内的主机访问vsftpd;但不允许172.16.200.8访问

方法:
/etc/hosts.allow
vsftpd: 172.16. EXCEPT 172.16.200.8
/etc/hosts.deny
vsftpd: ALL


5.本机ip:172.16.100.7
(1)仅允许172.16.0.0/16网络内的主机访问rsync;但不允许172.16.56.8访问

方法:
/etc/hosts.allow
rsync: 172.16. EXCEPT 172.16.200.8
/etc/hosts.deny
rsync: ALL

6.使用tcp_wrapper控制vsftp服务仅允许172.16.0.0网络中的主机访问,但要拒绝172.16.200.中的所有主机,对所有的拒绝访问尝试使用记录日志:

方法:
/etc/hosts.allow
vsftpd: 172.16. 
/etc/hosts.deny
vsftpd: 172.16.200. spawn echo `date` login attempt from %c to %s >> /var/log/tcp_wrapper_vsftpd.log

以上tcp_wrappers应用详解所有内容。

本文出自 “Opensamlee” 博客,请务必保留此出处http://gzsamlee.blog.51cto.com/9976612/1766976

tcp_wrappers应用详解

标签:系统管理员   应用程序   防火墙   第三方   客户端   tcp_wrappers   

原文地址:http://gzsamlee.blog.51cto.com/9976612/1766976

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!