本文共 1840 字,大约阅读时间需要 6 分钟。
对于没有固定来源IP地址但有需要进行防护的场景来说,DenyHosts来防止暴力破解是一种非常有效的措施。
DenyHost是使用Python开发的,它通过监控系统日志文件(/var/log/secure),来分析是否存在对OpenSSH的暴力破解行为,如果发现暴力破解,则其从系统安全日志分析出来源IP地址,然后通过在/etc/hosts.deny文件中加入相应的条目来使TCP Warappers禁止该IP地址的后续连接尝试。
安装 DenyHosts
wget https://sourceforge.net/projects/denyhosts/files/latest/download -O denyhosts-2.3.tar.gz
tar xf denyhosts-2.3.tar.gz
cd DenyHosts-2.6/
python setup.py install
cd /usr/share/denyhosts/
cp denyhosts.cfg-dist denyhosts.cfg
cp daemon-control-dist daemon-controlln -s /usr/share/denyhosts/daemon-control /etc/init.d/
/etc/init.d/daemon-control start
注:如果启动有如下报错
#service denyhost start
starting DenyHosts: /usr/bin/env python /usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg python: can't open file '/usr/bin/denyhosts.py': [Errno 2] No such file or directory因为本机升级过Python所以之前的模块找不到了,我们可以通过find / -name denyhosts.py 找到 然后修改启动文件
vim /etc/init.d/daemon-control
15 DENYHOSTS_BIN = "/usr/local/python2.7/bin/denyhosts.py"
配置文件内容
vim /usr/share/denyhosts/denyhosts.cfg
12 SECURE_LOG = /var/log/secure
指定系统安全日志的位置,Centos和Redhat系统中设为/var/log/secure
34 HOSTS_DENY = /etc/hosts.deny
检测到暴力破解后,指定在哪个文件中添加相应的恶意IP并禁止Centos和Redhat系统中设为/etc/hosts.deny
58 PURGE_DENY = 1m
过多久后清除已阻止IP
77 PURGE_THRESHOLD = 3
定义了某一IP最多被解封多少次。某IP暴力破解SSH密码被阻止/解封达到了PURGE_THRESHOLD次,则会被永久禁止;
93 BLOCK_SERVICE = sshd
检测到暴力破解后,指定封停来源IP访问的哪些服务,可以指定sshd或者ALL(即封停来源IP访问任何使用了libwrap的服务程序)
108 DENY_THRESHOLD_INVALID = 5
对于在/etc/passwd不存在的用户名的暴力尝试,指定发现多少次以后封停
119 DENY_THRESHOLD_VALID = 5
对于在/etc/passwd存在的用户名(除root外)的暴力尝试,指定发现多少次以后封停
129 DENY_THRESHOLD_ROOT = 5
对于root账户的暴力尝试,指定发现多少次以后封停
153 WORK_DIR = /usr/share/denyhosts/data
将deny的host或ip纪录到Work_dir中
179 HOSTNAME_LOOKUP=NO
是否做域名反解
435 DAEMON_LOG = /var/log/denyhosts
DenyHosts日志位置
开机启动
vim /etc/rc.d/rc.local
/etc/init.d/daemon-control start
查看攻击IP
tail -f /etc/hosts.deny
转载地址:http://flepb.baihongyu.com/