TCP_Wrappers介绍
工作在第四层(传输层)的TCP协议
对有状态连接的特定服务进行安全检测并实现访问控制
以库文件形式实现
某进程是否接受libwrap的控制取决于发起此进程的程序在编译时是否针对libwrap进行编译的
判断服务程序是否能够由tcp_wrapper进行访问控制的方法:
ldd /PATH/TO/PROGRAM|grep libwrap.so
strings PATH/TO/PROGRAM|grep libwrap.so
#举例 查看sshd依赖的库
[root@localhost ~]# ldd /usr/sbin/sshd|grep libwrap
libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f376400a000)
TCP_Wrappers的使用
配置文件:/etc/hosts.allow, /etc/hosts.deny
帮助参考:man 5 hosts_access,man 5 hosts_options
检查顺序:hosts.allow,hosts.deny(默认允许)
注意:一旦前面规则匹配,直接生效,将不再继续
基本语法:
[sshd@ip](:sshd@ip)
#服务名|程序名@ip|远程主机列表
#IP表示:通过哪一个ip来访问这个服务(程序)的时候,才通过tcp_wrappers来控制它;
#如果没有写,及所有的IP。
#:远程主机列表
#Daemon_list
#单个应用程序的二进制文件名,而非服务名,例如vsftpd
#以逗号或空格分隔的应用程序文件名列表,如:sshd,vsftpd
ALL表示所有接受tcp_wrapper控制的服务程序
主机有多个IP,可用@hostIP来实现控制
举例演示:编写脚本/root/bin/checkip.sh,每5分钟检查一次,如果发现通过ssh登录失败,次数超过10次,自动将此远程IP放TcpWrapper的黑名单中予以禁止防问
法1:后台执行脚本
[root@C7-37-100-destop scripts]# vim checkip.sh
#!/bin/bash
#
sleeptime=5m
#睡5分钟
num=10
#定义超过次数
file=/etc/hosts.deny
#定义黑名单文件
while true;do
lastb|grep ssh|awk '{print $3}'|uniq -c|while read conn
ip;do
#循环读取次数,和ip
if [ "$conn" -ge "$num" ];then
#判断次数超过10次
grep -q ^ssh.*$ip $file
#静默模式过滤 去文件中的重复记录
#以ssh开头的后跟任意字符加ip的在/etc/hosts.deny里是否存在
[ $? -ne 0 ] && echo "ssh:$ip" >>$file
#不等于0说明不存在,就添加到/etc/hosts.deny里
fi
done
sleep $sleeptime
done
[root@C7-37-100-destop scripts]# bash checkip.sh &
[1] 28092
#将其后台运行
[root@C7-37-100-destop scripts]# lastb|grep ssh|awk '{print $3}'|uniq -c
9 192.168.37.101
5 192.168.37.103
7 192.168.37.128
3 192.168.37.100
12 172.16.0.128
2 192.168.37.101
1 192.168.37.1
#测试最后应该在/etc/hosts.deny里有如下内容
[root@C7-37-100-destop scripts]# tail /etc/hosts.deny
#
# 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
#
#脚本成功
ssh:172.16.0.128
法2:脚本实现,加入定时任务中
#!/bin/bash
#
sleep 10
file=/etc/hosts.deny
num=10
lastb|awk '/ssh/{print $3}'|uniq -c|while read conn ip ;do
if [ "$conn" -ge "$num" ];then
grep -q ^ssh.*:$ip $file
[ $? -ne 0 ] && echo "ssh:$ip" >>$file
fi
done
#测试crontab
[root@C7-37-100-destop scripts]# crontab -e
no crontab for root - using an empty one
*/1 * * * * /bin/bash /data/scripts/checkip2.sh
#演示每1分钟执行一次,测试,实际用时改成5分钟就好
"/tmp/crontab.LmLKs5" 1L, 48C written
crontab: installing new crontab
[root@C7-37-100-destop scripts]# crontab -l
*/5 * * * * /bin/bash /data/scripts/checkip2.sh
#可以用tail -f来监控hosts.deny文件
[root@C7-37-100-destop scripts]# tail -f /etc/hosts.deny
#
# 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
#
ssh:172.16.0.128
sudo介绍
• sudo能够授权指定用户在指定主机上运行某些命令。如果未授权用户尝试使
用 sudo,会提示联系管理员
• sudo可以提供日志,记录每个用户使用sudo操作
• sudo为系统管理员提供配置文件,允许系统管理员集中地管理用户的使用权
限和使用的主机
• sudo使用时间戳文件来完成类似“检票”的系统,默认存活期为5分钟的“入
场券”
su的缺点,暴露root密码,sudo不用暴露root密码。
sudo的基本使用
- 通过visudo命令编辑配置文件,具有语法检查功能
举例演示:配置magedu用户的sudo权限,允许magedu用户拥有root权限
我们知道配置sudo权限,可以直接编辑配置文件爱你/etc/sudoers,但是其不具有语法检查功能;当然配置好以后可以通过命令visudo -c来检查语法;实际使用中,我们通过visudo命令编辑配置文件,具有语法检查功能 ;
[root@c6-37-128-desktop ~]# visudo
magedu ALL=(ALL) ALL
#表示magedu用户通过本机所有IP登陆时,可以使用所有身份运行所有命令。
#root用户可以使用所有身份。
#也可以这样写
magedu ALL=(root) ALL
#测试
#1 先检查是否有此用户,没有添加之
[root@c6-37-128-desktop ~]# tail -5 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zouyongbing:x:500:500:centos6:/home/zouyongbing:/bin/bash
wang:x:501:501::/home/wang:/bin/bash
mage:x:502:502::/home/mage:/bin/bash
[root@c6-37-128-desktop ~]# id magedu
id: magedu: No such user
[root@c6-37-128-desktop ~]# useradd magedu
[root@c6-37-128-desktop ~]# passwd magedu
Changing password for user magedu.
New password:
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
#visudo添加
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
#一般写在默认root的下面
magedu ALL=(ALL) ALL
#切换用户测试之
[root@c6-37-128-desktop ~]# su - magedu
[magedu@c6-37-128-desktop ~]$ useradd baba
#不加sudo提示无权限
-bash: /usr/sbin/useradd: Permission denied
[magedu@c6-37-128-desktop ~]$ sudo useradd baba
#输入magedu的秘密即可
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:
[magedu@c6-37-128-desktop ~]$ id baba
#创建成功
uid=504(baba) gid=504(baba) groups=504(baba)
当然我们也可以不修改配置文件/etc/sudoers来完成,此操作,直接在/etc/sudoers.d/目录下创建文件来完成sudo授权。
#visudo里删除root ALL=(ALL) ALL
[root@localhost ~]# cd /etc/sudoers.d/
[root@localhost sudoers.d]# vim test
magedu ALL=(root) ALL
[root@c6-37-128-desktop ~]# cat /etc/sudoers.d/test
magedu ALL=(root) ALL
#测试
[root@c6-37-128-desktop ~]# cat /etc/sudoers.d/test
magedu ALL=(root) ALL
[root@c6-37-128-desktop ~]# su - magedu
[magedu@c6-37-128-desktop ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[magedu@c6-37-128-desktop ~]$ sudo cat /etc/shadow
[sudo] password for magedu:
root:$1$kZlilHfn$ofRQglMH3PsEcXVasvcZe.:18388:0:99999:7:::
bin:*:17246:0:99999:7:::
daemon:*:17246:0:99999:7:::
adm:*:17246:0:99999:7:::
lp:*:17246:0:99999:7:::
sync:*:17246:0:99999:7:::
shutdown:*:17246:0:99999:7:::
halt:*:17246:0:99999:7:::
mail:*:17246:0:99999:7:::
uucp:*:17246:0:99999:7:::
operator:*:17246:0:99999:7:::
games:*:17246:0:99999:7:::
gopher:*:17246:0:99999:7:::
ftp:*:17246:0:99999:7:::
nobody:*:17246:0:99999:7:::
dbus:!!:18388::::::
usbmuxd:!!:18388::::::
rtkit:!!:18388::::::
avahi-autoipd:!!:18388::::::
vcsa:!!:18388::::::
abrt:!!:18388::::::
haldaemon:!!:18388::::::
ntp:!!:18388::::::
apache:!!:18388::::::
saslauth:!!:18388::::::
postfix:!!:18388::::::
gdm:!!:18388::::::
pulse:!!:18388::::::
sshd:!!:18388::::::
tcpdump:!!:18388::::::
zouyongbing:$1$kZlilHfn$ofRQglMH3PsEcXVasvcZe.:18388:0:99999:7:::
wang:$1$iJmct2C2$SPHEv7ymmWAGkMTS2iJj4/:18390:0:99999:7:::
mage:$1$votN2a/l$p0GAgf88PoeWUV7v3iRIZ0:18390:0:99999:7:::
magedu:$1$T64n9ifM$Uv3hxXj00u1x/IzmGKv240:18395:0:99999:7:::
baba:!!:18395:0:99999:7:::
也可以授权组,在/etc/sudoers文件里,默认有一个wheel组,只要将用户加入其组,即可以具有root权限。
#先删除test文件
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
#默认的可以将用户加入wheel用户组,就可以拥有root权限,将其启用,然后将magedu添加到此组中也可。
[root@c6-37-128-desktop sudoers.d]# groupmems -g wheel -a magedu
[root@c6-37-128-desktop sudoers.d]# id magedu
uid=503(magedu) gid=503(magedu) groups=503(magedu),10(wheel)
#测试
[magedu@c6-37-128-desktop ~]$ sudo cat /etc/shadow
[sudo] password for magedu:
root:$1$kZlilHfn$ofRQglMH3PsEcXVasvcZe.:18388:0:99999:7:::
bin:*:17246:0:99999:7:::
daemon:*:17246:0:99999:7:::
adm:*:17246:0:99999:7:::
lp:*:17246:0:99999:7:::
sync:*:17246:0:99999:7:::
shutdown:*:17246:0:99999:7:::
halt:*:17246:0:99999:7:::
mail:*:17246:0:99999:7:::
uucp:*:17246:0:99999:7:::
operator:*:17246:0:99999:7:::
games:*:17246:0:99999:7:::
gopher:*:17246:0:99999:7:::
ftp:*:17246:0:99999:7:::
nobody:*:17246:0:99999:7:::
dbus:!!:18388::::::
usbmuxd:!!:18388::::::
rtkit:!!:18388::::::
avahi-autoipd:!!:18388::::::
vcsa:!!:18388::::::
abrt:!!:18388::::::
haldaemon:!!:18388::::::
ntp:!!:18388::::::
apache:!!:18388::::::
saslauth:!!:18388::::::
postfix:!!:18388::::::
gdm:!!:18388::::::
pulse:!!:18388::::::
sshd:!!:18388::::::
tcpdump:!!:18388::::::
zouyongbing:$1$kZlilHfn$ofRQglMH3PsEcXVasvcZe.:18388:0:99999:7:::
wang:$1$iJmct2C2$SPHEv7ymmWAGkMTS2iJj4/:18390:0:99999:7:::
mage:$1$votN2a/l$p0GAgf88PoeWUV7v3iRIZ0:18390:0:99999:7:::
magedu:$1$T64n9ifM$Uv3hxXj00u1x/IzmGKv240:18395:0:99999:7:::
baba:!!:18395:0:99999:7:::