利用fail2ban阻止ssh暴力破解密码

本文介绍如何通过Fail2Ban工具配置防火墙规则来阻止针对SSH服务的暴力破解攻击。通过设置合理的参数,Fail2Ban能够有效地识别并屏蔽恶意IP地址。

连接上互联网的linux主机不免有人探测你的ssh的密码

最近发现我的机器被来自各个国家的黑客不停测在暴力猜测密码,经不起这样的折腾!

后来发现了一款软件可以阻止这样的攻击,很奏效的!

下面是我的配置:

下载源码包安装
地址:http://www.fail2ban.org/wiki/index.php/Main_Page

上传到服务器上

安装
#tar xvfj fail2ban-0.8.4.tar.bz2
#cd fail2ban-0.8.4
#python setup.py install
安装完成后服务会自动设置成启动的

在之前需要将系统的IPTABLES启动
配置:
# vi /etc/fail2ban/jail.conf 

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

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

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

# 允许尝试的次数     
maxretry = 3 
 
[ssh-iptables]

# 开启ssh阻止规则

enabled  = true 
filter   = sshd
action   = iptables[name=SSH, port=22, protocol=tcp] 

#不发送邮件
#sendmail-whois[name=SSH, dest=you@mail.com, sender=fail2ban@mail.com]


#ssh 失败日志路径

logpath  = /var/log/secure

启动fail2ban
service fail2ban start


启动fail2ban

Fail2ban-client start

关闭fail2ban

Fail2ban-client stop


 fail2ban-client status

 

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

iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
fail2ban-SSH  tcp  --  anywhere             anywhere            tcp dpt:ssh 
fail2ban-SSH  tcp  --  anywhere             anywhere            tcp dpt:ssh

Chain fail2ban-SSH (2 references)
target     prot opt source               destination         
DROP       all  --  122.102.64.54        anywhere      

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


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

在其他的机器上ssh 192.168.1.2
连续三次输错密码

使用命令fail2ban-client status ssh-iptables
查看阻止状态

或者

fail2ban-client status
Status
|- Number of jail:      1
`- Jail list:           ssh-iptables

 
测试结果:
#fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- filter
|  |- File list:        /var/log/secure 
|  |- Currently failed: 0
|  `- Total failed:     3
`- action
   |- Currently banned: 1
   |  `- IP list:       192.168.1.1 
   `- Total banned:     1

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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

2010-05-17 16:57:23,964 fail2ban.actions: WARNING [ssh-iptables] Ban 192.168.1.1
2010-05-17 21:35:00,449 fail2ban.actions: WARNING [ssh-iptables] Ban 218.108.85.244
2010-05-18 03:56:34,986 fail2ban.actions: WARNING [ssh-iptables] Ban 59.39.66.30

记录了被阻止的IP


成功阻止了ssh 密码猜测
 

这只是阻止其中的一种ssh攻击,配置文件中还有其他的服务配置,等有时间了我慢慢的贴上来!



     本文转自andylhz 51CTO博客,原文链接:http://blog.51cto.com/andylhz2009/317395,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值