一、編寫腳本/root/bin/checkip.sh,每5分鐘檢查一次,如果發(fā)現(xiàn)通過(guò)ssh登錄失敗 次數(shù)超過(guò)10次,自動(dòng)將此遠(yuǎn)程IP放入Tcp Wrapper的黑名單中予以禁止防問(wèn)。
1、編寫腳本;
[root@localhost bin]# cat checkip.sh
#!/bin/bash
n=10
cat /var/log/secure |grep sshd|awk '/Failed/{print $(NF-3)}'|sort |uniq -c |sort >>ssherro.log
while read count ip;do
if [ ${count} > ${n} ];then
echo "sshd is from:${ip}" >>/etc/hosts.deny
fi
done < ssherro.log
附上獲取ssh登錄失敗次數(shù)統(tǒng)計(jì)步驟(用其它機(jī)器模擬ssh失敗登錄):
[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}'
192.168.1.111
192.168.1.111
192.168.1.111
[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}'
192.168.1.111
192.168.1.111
192.168.1.111
192.168.1.112
192.168.1.112
192.168.1.112
192.168.1.111
192.168.1.111
192.168.1.111
[root@localhost bin]# cat /var/log/secure | grep sshd | awk '/Failed/ {print $(NF-3)}' | sort | uniq -c | sort -r
6 192.168.1.111
3 192.168.1.112
2、寫入定時(shí)任務(wù);
[root@localhost bin]# crontab -l
*/5 * * * * root sh /root/bin/checkip.sh &>/dev/null
二、配置magedu用戶的sudo權(quán)限,允許magedu用戶擁有root權(quán)限。
[root@localhost ~]# visudo
## Allow root to run any commands anywhere
root ALL=(ALL) ALL ##在此處下面添加
magedu ALL=(ALL) ALL