十一、TCP_Wrappers及sudo

一、TCP_Wrappers介绍

  • 工作在第四层(传输层)的TCP协议

  • 对有状态连接的特定服务进行安全检测并实现访问控制

  • 以库文件形式实现

  • 某进程是否接受libwrap的控制,取决于发起此进程的程序在编译时是否调用libwrap

  • 判断服务程序是否能够由tcp_wrapper进行访问控制的方法:
    ldd /PATH/TO/PROGRAM|grep libwrap.so
    strings PATH/TO/PROGRAM|grep libwrap.so

1.1 TCP_Wrappers使用

配置文件:/etc/hosts.allow, /etc/hosts.deny
帮助参考:man 5 hosts_access,man 5 hosts_options
检查顺序:hosts.allow,hosts.deny(如无内容则表示默认允许)
注:一旦前面规则匹配,直接生效,将不再继续
基本语法:
daemon_list@host: client_list [ :options :option… ]

daemon_list@host格式:

  • 单个应用程序的二进制文件名,而非服务名,例如vsftpd
  • 以逗号或空格分隔的应用程序文件名列表,如:sshd,vsftpd
  • ALL表示所有接受tcp_wrapper控制的服务程序
  • 主机有多个IP,可用@hostIP来实现控制
    例如:[email protected]

客户端Client_list格式

  • 以逗号或空格分隔的客户端列表
  • 基于IP地址:192.168.10.1 192.168.1.
  • 基于主机名:www.magedu.com .magedu.com 较少用
  • 基于网络/掩码:192.168.0.0/255.255.255.0
  • 基于net/prefixlen: 192.168.1.0/24(CentOS7)
  • 基于网络组(NIS 域):@mynetwork
  • 内置ACL:ALL,LOCAL,KNOWN,UNKNOWN,PARANOID

EXCEPT用法:
示例:拒绝172.16.0.0/16网段连接vsftpd进程,除了172.16.100.0/24,但172.16.100.1例外
vsftpd: 172.16. EXCEPT 172.16.100.0/24 EXCEPT 172.16.100.1

常见用法示例:编写脚本/root/bin/checkip.sh,每5分钟检查一次,如果发现通过ssh登录失败次数超过10次,自动将此远程IP放入Tcp Wrapper的黑名单中予以禁止防问

cat checkip.sh 
#!/bin/bash
lastb|awk '!/^btmp|^$/{ip[$3]++}END{for(i in ip){if(ip[i]>=10){system("echo sshd: "i" >> /etc/hosts.deny")}}}'

crontab -l
*/5 * * * * /root/checkip.sh


cat /etc/hosts.deny
#
# hosts.deny    This file contains access rules which are used to
#               deny connections to network services that either use
#               the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               The rules in this file can also be set up in
#               /etc/hosts.allow with a 'deny' option instead.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
#
sshd: 192.168.75.131

二、sudo权限

等价于:su 切换身份 su -l username -c ‘command’

  • 来自sudo包,man 5 sudoers
  • sudo能够授权指定用户在指定主机上运行某些命令。如果未授权用户尝试使用 sudo,会提示联系管理员
  • sudo可以提供日志,记录每个用户使用sudo操作
  • sudo为系统管理员提供配置文件,允许系统管理员集中地管理用户的使用权限和使用的主机
  • sudo使用时间戳文件来完成类似“检票”的系统,默认存活期为5分钟的“入场券”
  • 通过visudo命令编辑配置文件,具有语法检查功能
    visudo -c 检查语法
    visudo -f /etc/sudoers.d/test

2.1 sudo使用

配置文件:/etc/sudoers, /etc/sudoers.d/
时间戳文件:/var/db/sudo
日志文件:/var/log/secure
配置文件支持使用通配符glob:
? 任意单一字符
* 匹配任意长度字符
[wxc] 匹配其中一个字符
[!wxc] 除了这三个字符的其它字符
\x 转义
[[alpha]] 字母 示例: /bin/ls [[alpha]]*

配置文件规则
1、别名定义:不是必须的
2、授权规则:必须的
授权规则格式:
用户 登入主机=(代表用户) 命令
user host=(runas) command
示例:
root ALL=(ALL) ALL

  • 格式说明:
    user: 运行命令者的身份
    host: 通过哪些主机
    (runas):以哪个用户的身份
    command: 运行哪些命令

配置示例:配置magedu用户的sudo权限,允许magedu用户拥有root权限

visudo  ##在第100行下方添加如下内容:
magedu  ALL=(ALL)       ALL

保存退出:wq

验证结果:

su - magedu
cat /etc/shadow
cat: /etc/shadow: Permission denied

sudo cat /etc/shadow

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for magedu: 
root:$6$xYGbjjf5gHfWfU74$evxYxJ0LdnpmAH0mXY7RbYGo7W4ngrmZRZJe85wuH4u.xthGn/jNz6l3ievX3Hp8CCEnxBvvqjELBI5jvvpqz.::0:99999:7:::

你可能感兴趣的:(十一、TCP_Wrappers及sudo)