使用DenyHosts防御ssh密码暴力破解

mtain 2020年05月12日 434次浏览

简介

当Linux主机,被暴力破解时,会将日志记录到/var/log/secure中。

DenyHosts这个工具,可以将日志中恶意IP添加到系统的/etc/hosts.deny(系统IP黑名单),从而起到自动防御的作用。

登录系统时,如果显示There were 24 failed login attempts since the last successful login.,说明有人在暴力破解ssh的密码

Centos7安装过程

wget https://soft.itbulu.com/tools/DenyHosts-2.6.tar.gz
tar -zxvf DenyHosts-2.6.tar.gz
 
 
cd DenyHosts-2.6/
python setup.py install

# 先清空ssh日志
echo "" > /var/log/secure

cd /usr/share/denyhosts/
cp denyhosts.cfg-dist denyhosts.cfg
cp daemon-control-dist daemon-control
chown root daemon-control
chmod 700 daemon-control
vim /etc/denyhosts.conf
------------------------------------------------------------------------------------
SECURE_LOG = /var/log/secure   #ssh日志文件
# format is: i[dhwmy]
# Where i is an integer (eg. 7)
# m = minutes
# h = hours
# d = days
# w = weeks
# y = years
#
# never purge:
PURGE_DENY = 50m               #过多久后清除已阻止IP
HOSTS_DENY = /etc/hosts.deny   #将阻止IP写入到hosts.deny
BLOCK_SERVICE = sshd           #阻止服务名
PURGE_THRESHOLD =              #定义了某一IP最多被解封多少次。某IP暴力破解SSH密码被阻止/解封达到了PURGE_THRESHOLD次,则会被永久禁止;
DENY_THRESHOLD_INVALID = 1     #允许无效用户登录失败的次数
DENY_THRESHOLD_VALID = 10      #允许普通用户登录失败的次数
DENY_THRESHOLD_ROOT = 5        #允许root登录失败的次数
WORK_DIR = /usr/local/share/denyhosts/data #将deny的host或ip纪录到Work_dir中
DENY_THRESHOLD_RESTRICTED = 1 #设定 deny host 写入到该资料夹
LOCK_FILE = /var/lock/subsys/denyhosts #将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务。
HOSTNAME_LOOKUP=NO            #是否做域名反解
ADMIN_EMAIL =                 #设置管理员邮件地址
DAEMON_LOG = /var/log/denyhosts #DenyHosts日志位置
------------------------------------------------------------------------------------


# 启动,卸载时删除/etc/init.d/daemon-control
./daemon-control start
ln -s /usr/share/denyhosts/daemon-control /etc/init.d

# 加入开机启动
chkconfig daemon-control on

# 查看DenyHosts日志
tail -f /var/log/denyhosts

# 清除黑名单
echo > /etc/hosts.deny