服务器被暴力破解的解决办法(二)

服务器被暴力破解的解决办法(二)

解决方案goocz2025-07-01 17:07:262A+A-

上一次,我们说到小王公司服务器遭遇暴力破解,拿到解决方案回公司就开始部署。部署完成后的确起到了一定的效果,不过接下来的一个问题让他很头疼,原来黑客虽然攻入不进系统,但是依旧不依不饶的进行暴力破解。

大家都知道在被暴力破解的时候,系统会不断地认证用户,从而增加了系统资源额外开销,导致访问公司网站速度很慢。

基于此我又给到他一个方案,使用工具fail2ban防止暴力破解

关于fail2ban百度百科里的解释 是:

fail2ban是一款实用软件,可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作

fail2ban可以支持大量服务,例如sshd,apache,qmail,proftpd,sasl等

当然也支持多种动作,如iptables,tcp-wrapper,shorewall(iptables第三方工具),mail notifications(邮件通知)等。

在这里笔者针对sshd服务和iptables来给小王做了下演示:

模拟场景:使用fail2ban 设置ssh远程登录5分钟内3次密码验证失败,禁止用户IP访问主机1小时,1小时该限制自动解除,此IP可以重新登录。

下载fail2ban:(鉴于小王用虚的拟机,所以这里演示下载windows下载,然后上传到linux虚拟机里的情况)

官方下载网址及截图:

官方地址:

http://www.fail2ban.org

安装:fail2ban

[root@xinsz08 ~]# tar -zxvf fail2ban-0.9.3.tar.gz

[root@xinsz08 fail2ban-0.9.14]# cd fail2ban-0.9.3

[root@xinsz08 fail2ban-0.9.3]# vim README.md #查看以下内容

从文件里看以看到,安装fail2ban要求python大于2.6版本,安装方法也已经很清晰了,直接使用python setup.py install 就可以了。

首先我们查看一下python版本看看是否符合要求

[root@xinsz fail2ban-0.9.3]# python

Python 2.6.6 (r266:84292, Sep 4 2013, 07:46:00)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> exit()

安装:

[root@xinsz fail2ban-0.9.3]# python setup.py install

将fail2ban的启动脚本复制到 /etc/init.d/fail2ban

[root@xinsz fail2ban-0.9.3]# cp files/redhat-initd /etc/init.d/fail2ban

设置开机启动

[root@xinsz fail2ban-0.9.3]# chkconfig --add fail2ban

[root@xinsz fail2ban-0.9.3]# chkconfig --list fail2ban

fail2ban 0:off 1:off 2:off 3:on 4:on 5:on 6:off

具体配置:

[root@xinsz fail2ban-0.9.3]# ls /etc/fail2ban/

action.d fail2ban.d jail.conf paths-common.conf paths-fedora.conf paths-osx.conf

fail2ban.conf filter.d jail.d paths-debian.conf paths-freebsd.conf

[root@xinsz fail2ban-0.9.3]# vim /etc/fail2ban/jail.conf

[DEFAULT]

bantime = 3600 禁止此用户IP访问主机1小时

findtime = 300 在5分钟内内出现规定次数就实施动作,默认时间单位:秒

maxretry = 3 密码验证失败次数最大值为3

filter = sshd

protocol = tcp 协议为TCP

action = iptables[name=SSH, port=ssh, protocol=tcp] 所采用的工作,按照名字可在action.d目录下找到

[sshd]

port = ssh

logpath = /var/log/secure # 检测的系统的登陆日志文件,这里我们sshd服务日志文件的路径

最后保存设置

启动fail2ban服务

/etc/init.d/fail2ban start

[root@xinsz jt]# service fail2ban status

fail2ban-server (pid 5491) is running...

验证:

故意输错密码三次,看看效果

从192.168.1.63 ssh到192.168.1.62

[root@localhost jt]# ssh root@192.168.1.62

The authenticity of host '192.168.1.62 (192.168.1.62)' can't be established.

RSA key fingerprint is 39:51:1b:f7:be:07:dd:e8:61:32:6f:bd:83:88:14:72.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.1.62' (RSA) to the list of known hosts.

root@192.168.1.62's password:

Permission denied, please try again.

root@192.168.1.62's password:

Permission denied, please try again.

root@192.168.1.62's password:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

三次失败后直接限制登陆,连输入密码的机会都没有了,很神奇有没有

由此看来,我们的设置是完全可以实现当黑客进行暴力破解,输错三次密码,就限制这个IP一小时之内不能登陆

配置文件里还有很多值得玩味的地方,比如设置邮件等,有兴趣的话,小王也会自己去探索。小王的问题暴力破解问题解决了。

我是辛舒展,专注云计算大数据方案,希望我的文字能给你带来不一样的声音和视角。

你可以关注下我的头条号和微信公众号 :每天我都会发布一篇技术文章 ,这些文章总会有一篇是你想要的

微信公众号:xinsz08

点击这里复制本文地址 以上内容由goocz整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

果子教程网 © All Rights Reserved.  蜀ICP备2024111239号-5