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

简介:

连接上互联网的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,如需转载请自行联系原作者



相关文章
|
6月前
|
安全 Shell Linux
ssh密码忘记了怎么办
通过上述措施,不仅能够有效应对SSH密码遗忘的挑战,还能全方位加固SSH连接的安全,确保数据传输的无忧。
386 2
|
8月前
|
安全 Shell 网络安全
告别繁琐密码,一键解锁GitHub高效秘籍!SSH配置大揭秘,让你的代码托管之旅飞起来!
【8月更文挑战第4天】在使用GitHub时,频繁输入账号密码颇为不便。采用SSH协议可提升安全性并简化流程。本文以问答形式指导你快速配置GitHub SSH:了解SSH优势、学会生成与添加SSH密钥及测试连接。通过简单的步骤,即可实现无缝代码推送与拉取,享受高效、安全的开发体验。记得保护好私钥并根据需要设置多个密钥对。
115 7
|
8月前
|
安全 Linux Shell
Linux系统之间实现免密码登录(SSH无密码登录
【8月更文挑战第21天】要在Linux系统间实现SSH免密码登录,需先在源机器生成SSH密钥对,然后将公钥复制到目标机器的`.ssh/authorized_keys`文件中。可通过`ssh-keygen`命令生成密钥,并使用`ssh-copy-id`命令传输公钥。最后测试SSH连接,确保能无密码登录。若目标机器缺少相关目录或文件,需手动创建并设置适当权限。完成这些步骤后,即可实现安全便捷的免密码登录。
348 0
|
10月前
|
安全 Linux Shell
SSH服务器拒绝密码登录的解决方法
SSH服务器拒绝密码登录的解决方法
2248 1
|
9月前
|
网络安全 数据安全/隐私保护
服务器密码登录出现了:SSH connection failed: connect ECONNREFUSEDxxxxxxxx:22 * Xshell提示 SSH connection fa
服务器密码登录出现了:SSH connection failed: connect ECONNREFUSEDxxxxxxxx:22 * Xshell提示 SSH connection fa
|
9月前
|
安全 Linux 网络安全
ssh中的密码登录和密钥登录
ssh中的密码登录和密钥登录
|
9月前
|
Linux 网络安全 数据安全/隐私保护
如何在Linux中设置SSH无密码登录
这样,你就设置了SSH无密码登录。但请注意,这种方式虽然方便,但如果你的私钥落入他人手中,他们就能访问你的服务器,所以要妥善保管你的私钥。
135 0
|
11月前
|
安全 网络协议 Linux
|
11月前
|
安全 Shell 网络安全
ssh配置无密码验证
ssh配置无密码验证要在SSH中配置无密码验证,您需要使用公钥验证【2月更文挑战第18天】
124 1
|
11月前
|
安全 网络协议 Shell
SSH连接密码问题:原因、表现与解决方案
SSH连接密码问题:原因、表现与解决方案
2909 3