Bootstrap

使用/etc/hosts.deny拒绝ssh暴力破解

一、背景
云上的服务器,容易被恶意暴力ssh破解,本文介绍使用/etc/hosts.deny增加黑名单的方式来阻止。

二、介绍

基于白名单机制,/etc/hosts.allow 优先于 /etc/hosts.deny。

如果在allow和deny文件中都有某个ip,那么这个ip是被允许ssh。

如果在allow中有,deny没有,那么这个ip是被允许ssh。

如果allow中没有,deny有,那么这个ip是被禁止ssh。

还可以在 /etc/hosts.deny文件里追加一个额外配置,让其读取另外一个ip列表。这样可以使你以后维护需要禁止的ip更方便

三、 /etc/hosts.deny的使用方法
1、方法1:
     直接在 /etc/hosts.deny文件里写拒绝规则

echo "sshd: 192.168.118.109" >> /etc/hosts.deny

2、方法2:
     在 /etc/hosts.deny文件写一个额外读取ip列表的规则

echo "sshd: /etc/sshd.deny.hostguard"   >> /etc/hosts.deny
echo "192.168.118.109" >> /etc/sshd.deny.hostguard


注:以后就往sshd.deny.hostguard里面写恶意ip的地址就行了。保持一行一个ip。

四、(附)看哪些ip在恶意访问

tail -f /var/log/secure
tail -n 100 /var/log/secure |grep 'Failed password' |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'
lastb -n 500 |awk '{print $3}' |sort |uniq |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'

;