2024年4月3日发(作者:)

fail2ban阻止SSH和VSFTP暴力破解密码

最近deepvps发现自己的VPS每天都被不停的探测SSH和VSFTP的密码。虽然已经做了一

些简单的防护,但是总觉的不保险。为了彻底解决这个问题。deepvps发现了fail2ban这款

软件不错,可以有效阻止这样的

攻击

,下面是基本的安装配置方法:

一、下载安装

#wget

...2

#2

#cdfail2ban-0.8.4

#all

#cdfiles

#cp./redhat-initd/etc/init.d/fail2ban

#chkconfig–addfail2ban

#servicefail2banstart

注意:如果重起iptables记的一定还要重起fail2ban,不然他就不能生效,fail2ban的过滤表

是在iptables

启动

后在加入的。

二、配置

1、fail2ban本身配置

默认里面就三个参数,而且都有注释。

#默认日志的级别

loglevel=3

#日志的存放路径

logtarget=/var/log/

#socket的位置

socket=/tmp/

2、fail2ban防护配置

全局设置

#vi/etc/fail2ban/

#忽悠IP范围如果有二组以上用空白做为间隔

ignoreip=127.0.0.1

#设定IP被封锁的时间(秒),如果值为-1,代表永远封锁

bantime=86400

#设定在多少时间内达到maxretry的次数就封锁

findtime=600

#设定在多少时间内达到maxretry的次数就封锁

maxretry=3

#允许尝试的次数

分类设置

#针对

ssh

d暴力入侵防护

[ssh-iptables]

enabled=true

filter=sshd

action=iptables[name=SSH,port=ssh,protocol=tcp]

send

mail

-whois[name=SSH,dest=you@,sender=fail2ban@]

logpath=/var/log/secure

#如果有个别的次数设定就设在这里

maxretry=3

#针对v

sftp

d暴力入侵防护

[vsftpd-iptables]

enabled=true

filter=vsftpd

action=iptables[name=VSFTPD,port=ftp,protocol=tcp]

sendmail-whois[name=VSFTPD,dest=you@]

logpath=/var/log/secure

maxretry=3

建议设置成maxretry为3表示3次错误就封锁,另外logpath(Centos5和Rhel5中)要改成

/var/log/secure。

然后我们设置启动服务:

#chkconfig–level345fail2banon

#servicefail2banstart

三、测试

查看iptables的规则多出了iptables-ssh的规则

iptables-L

ChainINPUT(policyACCEPT)

targetprotoptsourcedestination

fail2ban-SSHtcp—anywhereanywheretcpdpt:ssh

fail2ban-SSHtcp—anywhereanywheretcpdpt:ssh

Chainfail2ban-SSH(2references)

targetprotoptsourcedestination

DROPall—122.102.64.54anywhere

#这有一个被阻止的IP拒绝时间根据在你的配置文件设置时间有关我设置的是一天

RETURNall—anywhereanywhere

在其他的机器上ssh192.168.1.2

连续三次输错密码

使用

命令

fail2ban-clientstatusssh-iptables

查看阻止状态

或者

fail2ban-clientstatus

Status

|-Numberofjail:1

`-Jaillist:ssh-iptables

测试结果:

#fail2ban-clientstatusssh-iptables

Statusforthejail:ssh-iptables

|-filter

||-

File

list:/var/log/secure

||-Currentlyfailed:0

|`-Totalfailed:3

`-action

|-Currentlybanned:1

|`-IPlist:192.168.1.1

`-Totalbanned:1

我们在来看看fail2ban的日志记录

2010-05-1716:57:23,s:WARNING[ssh-iptables]Ban192.168.1.1

2010-05-1721:35:00,s:WARNING[ssh-iptables]Ban218.108.85.244

2010-05-1803:56:34,s:WARNING[ssh-iptables]Ban59.39.66.30

记录了被阻止的IP,成功阻止了ssh密码猜测

这里只是只介绍了保护SSH和VPSFTP方法,配置文件中还有其他的服务配置,有时间了

大家可以自己研究一下。