1、編寫腳本/root/bin/checkip.sh,每5分鐘檢查一次扫沼,如果發(fā)現(xiàn)通過ssh登錄失敗 次數(shù)超過10次崇呵,自動將此遠程IP放入Tcp Wrapper的黑名單中予以禁止防問
#!/bin/bash
[ $UID -ne 0 ]&&echo "this script must root run it " && exit 1
if [[ ! -e "/ip_monitor" ]]; then
mkdir /ip_monitor
fi
cmd=`cat /var/log/secure|grep "Failed password for qiuhom"|awk '{print $(NF-3)}'|sort|uniq -c|awk '{print $2"==>"$1}'`
echo "$cmd">/ip_monitor/ip.txt
for i in `cat /ip_monitor/ip.txt`
do
ip=`echo "$i"|awk -F "==>" '{print $1}'`
count=`echo "$i"|awk -F "==>" '{print $2}'`
if [ $count -gt 10 ];then
xx=`grep $ip /etc/hosts.deny |wc -l`
if [ $xx == 0 ];then
echo "ALL:$ip" >> /etc/hosts.deny
fi
fi
done
2缤剧、配置magedu用戶的sudo權限,允許magedu用戶擁有root權限
root@CentOS7[13:50:32]: echo 'magedu ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/magedu
# 切換用戶進行驗證
root@CentOS7[13:50:34]: su magedu
magedu@CentOS7[13:53:00]:~# sudo su
root@CentOS7[13:53:09]:/home/magedu#