手机版
你好,游客 登录 注册
背景:
阅读新闻

SSH防止暴力破解及fail2ban的使用方法

[日期:2017-06-18] 来源:Linux社区  作者:jasmine-Jobs [字体:大 中 小]

三、fail2ban如何防止暴力破解

实例:

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

因为动作文件(action.d/iptables.conf)以及日志匹配条件文件(filter.d/sshd.conf )安装后是默认存在的。基本不用做任何修改。所有主要需要设置的就只有jail.conf文件。启用sshd服务的日志分析,指定动作阀值即可。实例文件/etc/fail2ban/jail.conf及说明如下:

fail2ban自身设置

1、fail2ban本身配置

[root@xiaolyu76 ~]# vim /etc/fail2ban/fail2ban.conf

默认fail2ban.conf里面就三个参数,而且都有注释。
loglevel = 3                                            #默认日志的级别
logtarget = /var/log/fail2ban.log              #日志的存放路径
socket = /var/run/fail2ban/fail2ban.sock   #socket的位置

2、jail.conf

vim /etc/fail2ban/jail.conf

[DEFAULT]

#
"ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
# ban a host
which matches an address in this list. Several addresses can be
# defined using space separator.
ignoreip
= 127.0.0.1/8

# External command that will take an tagged arguments to ignore, e.g.
<ip>,
# and return
true if the IP is to be ignored. False otherwise.
#
# ignorecommand
= /path/to/command <ip>
ignorecommand
=

#
"bantime" is the number of seconds that a host is banned.
bantime 
= 600

# A host is banned
if it has generated "maxretry" during the last "findtime"
# seconds.
findtime 
= 600

#
"maxretry" is the number of failures before a host get banned.
maxretry
= 3

#
"backend" specifies the backend used to get files modification.
# Available options are
"pyinotify", "gamin", "polling" and "auto".
# This option can be overridden
in each jail as well.
#
# pyinotify: requires pyinotify (a
file alteration monitor) to be installed.
#              If pyinotify is not installed, Fail2ban will use auto.
# gamin:    requires Gamin (a
file alteration monitor) to be installed.
#              If Gamin is not installed, Fail2ban will use auto.
#
# pyinotify: requires pyinotify (a
file alteration monitor) to be installed.
#              If pyinotify is not installed, Fail2ban will use auto.
# gamin:    requires Gamin (a
file alteration monitor) to be installed. # If Gamin is not installed, Fail2ban will use auto. # polling: uses a polling algorithm which does not require external libraries.
# auto:      will try to use the following backends,
in order:
#              pyinotify, gamin, polling.
backend
= auto

#
"usedns" specifies if jails should trust hostnames in logs,
#  warn when DNS lookups are performed, or ignore all hostnames
in logs
#
# yes: 
if a hostname is encountered, a DNS lookup will be performed.
# warn: 
if a hostname is encountered, a DNS lookup will be performed,
#        but it will be logged as a warning.
# no:   
if a hostname is encountered, will not be used for banning,
#        but it will be logged as
info.
usedns
= warn


# This jail corresponds to the standard configuration
in Fail2ban.
# The mail
-whois action send a notification e-mail with a whois request
#
in the body.

 

[DEFAULT]              #全局设置

ignoreip = 127.0.0.1/8    10.10.10.0/24  #忽略的IP列表,不受设置限制  如果有二组以上用空白做为间隔

bantime  = 600            #屏蔽时间,单位:秒(设置IP被封锁的时间)

findtime  = 600            #这个时间段内超过规定次数会被ban掉(设定多长时间内达到最大次数就解锁。)

maxretry = 3                #最大尝试次数

backend = auto            #日志修改检测机制(gamin、polling和auto这三种)

 

[ssh-iptables]

#enabled 
= false
enabled 
= true
filter 
= sshd
action 
= iptables[name=SSH, port=ssh, protocol=tcp]
          sendmail
-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"]
#logpath 
= /var/log/sshd.log
logpath 
= /var/log/secure
#maxretry
= 5
maxretry
= 3

 

[ssh-iptables]                  #单个服务检查设置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置。

enabled  = true            #是否激活此项(true/false)修改成 true

filter       = sshd             #过滤规则filter的名字,对应filter.d目录下的sshd.conf

action  = iptables[name=SSH, port=ssh, protocol=tcp]            #动作的相关参数,对应action.d/iptables.conf文件

sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"] 

#触发报警的收件人

 dest=收件者电邮地址, sender=寄件者电邮地址

logpath  = /var/log/secure  #检测的系统的登陆日志文件。这里要写sshd服务日志文件。 默认为logpath  = /var/log/sshd.log

#5分钟内3次密码验证失败,禁止用户IP访问主机1小时。 配置如下

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

findtime  = 300    #在5分钟内内出现规定次数就开始工作

maxretry = 3    #3次密码验证失败

 

注意:在做这个实验之前要先做如下准备工作:

1. 将/etc/passwd文件中用户mk和root的相关修改恢复过来。#因为用root用户便于做实验。

2. 将/etc/ssh/sshd_config文件中,将禁止root用户登录修改为允许。#说明生产上是要禁止root登录,这里为了方便做实验。

3. iptables防火墙需要配置。

    因为我在前面说了fail2ban的工作的原理就是通过分析一定时间内的相关服务日志,将满足动作的相关IP利用iptables加入到dorp列表一定时间。 

所以首先要启动防火墙iptables的相关配置:

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH  --update --seconds 1800 --hitcount 5 -j DROP
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH --set -j ACCEPT

 特别注意的是如果这个地方dport 端口设置为22,那么/etc/ssh/sshd_config中的Port端口也要设置为22.总之一句话,两个设置端口要一致。

4. sshd服务器的端口,要和iptables 中dport端口要一致,我这里讲两个都设置为22了。

[root@xiaolyu76 ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH  --update --seconds 1800 --hitcount 5 -j DROP
[root@xiaolyu76 ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH --set -
j ACCEPT
[root@xiaolyu76 ~]# service iptables -
L  #该命令如果返回防火墙规则,说明防火墙起作用了,否则不起作用。
Usage: iptables {start|stop|reload|restart|condrestart|status|panic|
save}
[root@xiaolyu76 ~]#

 启动服务:

[root@xiaolyu76 ~]# > /var/log/secure  #清空该日志,从现在开始重新写
[root@xiaolyu76 ~]# cat /var/log/secure
[root@xiaolyu76 ~]# service fail2ban restart #重启fail2ban
Stopping fail2ban:                                         [  OK  ]
Starting fail2ban:                                         [  OK  ]
[root@xiaolyu76 ~]# iptables -L -n   #生成关于fail2ban的规则链
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
fail2ban-SSH  tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain fail2ban-SSH (1 references)
target     prot opt source               destination         
RETURN     all  --  0.0.0.0/0            0.0.0.0/0 

 

测试:故意输入错误密码3次,再进行登录时,会拒绝登录

[root@xiaolyu77 ~]# ssh xiaolyu76
root@xiaolyu76
's password:
Permission denied, please try again.
root@xiaolyu76
's password:
Permission denied, please try again.
root@xiaolyu76
's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@xiaolyu77
~]# ssh xiaolyu76
ssh: connect to host xiaolyu76 port 22: Connection refused

如何查看登录被拒绝的相关信息呢?

1. 我们可以用iptables查看连接被屏蔽的情况:iptables -L 

[root@xiaolyu76 ~]# iptables -L |tail -4

2. 还可以通过fail2ban-client status来查看被屏蔽的情况:

说明: 如果fail2ban-client status ssh-iptables  显示被ban的ip和数目就表示成功了,如果都是0,说明没有成功

3. 也可以通过查看fail2ban的日志看到相关的信息

[root@xiaolyu76 ~]# tail /var/log/fail2ban.log

如何解除被禁止的IP ?

1. 直接重启fail2ban服务即马上解除所有IP 

  因为重新启动fail2ban,iptables中的规则就会全部被清空。

2. 到了fail2ban规定时间之后自动解除

那如何查看发送的邮件呢?

四、ssh服务器免秘钥登录:

1. 在客户端(我这里是xiaolyu77主机)执行ssh-keygen生成秘钥对

[root@xiaolyu77 ~]# ssh-keygen

2. 将该秘钥对复制到服务器上:

[root@xiaolyu77 .ssh]# ssh-copy-id  -i   /root/.ssh/id_rsa.pub  xiaolyu76

3. 现在,服务器xiaolyu76的主机上ssh服务的公钥是客户端xiaolyu77的主机上ssh服务的私钥,那么可否在xiaolyu76(服务器端)来免密码登录

xiaolyu77(客户端)呢?

可以看到,显然不可以,这是为什么呢?因为xiaolyu76的私钥和xiaolyu77的公钥之间没有任何关系。因为是私钥加密,公钥解密。

说通俗一点就是如果私钥是钥匙,那么公钥就是锁。xiaolyu76的私钥和xiaolyu77的公钥没有什么关系,就是钥匙打不开锁,所以不能够免密码登录。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-06/144911.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款