linux服务篇-sshd服务管理与防止暴力破解

版权声明:本文为博主原创文章,如需转载请在显眼的地方注明源地址。 https://blog.csdn.net/Dorisnzy/article/details/75175249

sshd服务管理与防止暴力破解

  • sshd服务安装-ssh命令使用方法
  • sshd服务配置和管理
  • 防止sshd服务暴力破解的几种方法

(一)sshd服务安装-ssh命令使用方法

1.介绍

SSH 协议【安全外壳协议】为 Secure Shell 的缩写。SSH 为建立在应用层和传输层基础上的安全协议。sshd服务使用SSH协议可以用来进行远程控制, 或在计算机之间传送文件,相比较之前用telnet方式来传输文件要安全很多,因为telnet使用明文传输,是加密传输。

题外话:如果要远程操作linux服务器则sshd是一个不错的选择,因为在传输过程中对数据进行了加密处理,一句话:安全。

2.软件的安装

由于SSH服务c/s服务模式,所以需要安装以下几个软件包:

openssh-5.3p1-114.el6_7.x86_64:包含OpenSSH服务器及客户端需要的核心文件
openssh-clients-5.3p1-114.el6_7.x86_64:OpenSSH客户端软件包
openssh-server-5.3p1-114.el6_7.x86_64:OpenSSH服务器软件包
openssh-askpass-5.3p1-114.el6_7.x86_64:支持对话框窗口的显示,是一个基于X 系统的密码诊断工具

这四个软件包在我们的RHEL镜像软件安装包里有。

2.1查看我的光驱挂载的位置

[root@localhost ~]# df -Th
Filesystem     Type     Size  Used Avail Use% Mounted on
/dev/sda3      ext4     4.9G  2.0G  2.7G  43% /
tmpfs          tmpfs    491M     0  491M   0% /dev/shm
/dev/sda1      ext4     194M   29M  155M  16% /boot
/dev/sr0       iso9660  3.6G  3.6G     0 100% /mnt

2.2 可以查看到这个四个软件包都在光驱中,如下操作显示:

[root@localhost ~]# cd /mnt/Packages/
[root@localhost Packages]# ls openssh*
openssh-5.3p1-94.el6.x86_64.rpm          
openssh-clients-5.3p1-94.el6.x86_64.rpm
openssh-askpass-5.3p1-94.el6.x86_64.rpm  
openssh-server-5.3p1-94.el6.x86_64.rpm

找到软件包存放的位置之后,接下来我们来安装软件包,安装方式有两种,分别是:

  • 通过yum源的方式安装
  • 通过rpm方式安装

2.3下面我将通过yum的方式安装

[root@localhost Packages]# yum install openssh openssh-clients openssh-server openssh-askpass
Loaded plugins: product-id, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
………………中间部分省略………………
Installed:
  openssh-askpass.x86_64 0:5.3p1-122.el6                                                                                              

Updated:
  openssh.x86_64 0:5.3p1-122.el6         openssh-clients.x86_64 0:5.3p1-122.el6         openssh-server.x86_64 0:5.3p1-122.el6        

Complete!

OK,软件安装成功,接下来查看一下安装的软件的生成的文件:

[root@localhost Packages]# rpm -ql openssh
/etc/ssh
/etc/ssh/moduli
/usr/bin/ssh-keygen
/usr/libexec/openssh
/usr/libexec/openssh/ssh-keysign
………………后面部分省略………………

2.4 openssh配置文件的介绍

常用的配置文件主要有两个,存放位置位于/etc/ssh/ssh_config和/etc/sshd_config:

  • ssh_config为客户端配置文件
  • sshd_config为服务器端配置文件

2.5 服务的启动与关闭操作

格式:[root@localhost /]# service sshd restart/stop/start/status
sshd服务的启动、关闭、重启、查看服务状态命令如下:

  • [root@localhost /]# service sshd status # 查看启动状态
  • [root@localhost /]# service sshd restart # 重启服务
  • [root@localhost /]# service sshd start # 启动
  • [root@localhost /]# service sshd stop # 关闭

接下来开启sshd服务:

[root@localhost /]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

设置开机自启动

[root@localhost /]# chkconfig sshd on
[root@localhost /]# chkconfig --list sshd
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

以上可以很清楚的看到第三启动级别和第五启动级别是开机自启动状态,一般情况下我们在远程登录服务器时是第三启动级别。

切记:设置sshd开机自启动非常重要,一定设置开机自启动,原因就在于如果哪天一天机房的服务器断电了,然后重启,你要远程登录服务器如果没有设置开机自启动的话你就必须得跑到机房进行操作服务器了,嘿嘿,重要吧?

3. 如何远程连接服务器

远程连接服务器两种方式:

  • ssh [远程主机用户名] @[远程服务器主机名或IP地址]
  • ssh -l [远程主机用户名] [远程服务器主机名或IP 地址]

ssh命令的参数详解:
-1:强制使用ssh协议版本1;
-2:强制使用ssh协议版本2;
-4:强制使用IPv4地址;
-6:强制使用IPv6地址;
-A:开启认证代理连接转发功能;
-a:关闭认证代理连接转发功能;
-b:使用本机指定地址作为对应连接的源ip地址;
-F:指定ssh指令的配置文件;
-f:后台执行ssh指令;
-i:指定身份文件;
-l:指定连接远程服务器登录用户名;
-N:不执行远程指令;
-p:指定远程服务器上的端口;
-X:开启X11转发功能;
-y:开启信任X11转发功能。

来自: http://man.linuxde.net/ssh

3.1 ssh [远程主机用户名] @[远程服务器主机名或IP地址]

3.1.1 如果用root进程登录远程主机可以这样:
[root@localhost ~]# ssh 192.168.0.75
The authenticity of host '192.168.0.75 (192.168.0.75)' can't be established.
RSA key fingerprint is d7:2c:9a:b4:d5:6f:a8:6f:62:54:5f:0a:5f:cb:73:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.75' (RSA) to the list of known hosts.
root@192.168.0.75's password: 
Last login: Sat Jul 15 23:23:21 2017 from 192.168.0.2

第一次登录服务器时系统没有保存远程主机的信息,为了确认该主机身份会提示用户是否继续连接,输入yes 后登录,这时系统会将远程服务器信息写入用户主目录下的$HOME/.ssh/known_hosts文件中,下次再进行登录时因为保存有该主机信息就不会再提示了,可以查看一下这个文件内容如下:

[root@localhost ~]# cat ~/.ssh/known_hosts 
192.168.0.75 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAo4AZ3HlPwI8GU9GtggxuwN4eKkheTUV0wT7oPsVyJmKXsv65u083RJ9694QFAiu9jRkj4mVUoFGfeMwf/BDw36E2Nl1vGoYbiRC5z5gpuyb/GZIEnFhBg0omMJnAnGFmPsRf5SG+B53EkBQNVaj8ajCexri9hFlHVgjSzDopgNOM0HFNuTVGYprfkqthH6xq0iDKo8gnbocLxf+PteAXPfx72jACBK8uchHPEaDfLKBOtZU5w3z4nljMya7i3o9FoyGt9pudPhBFqgh5tSVlVWphEKUlwXDM+DUz+rbBufaFDx5dQY0GQx9NPfeq4sJAAcOcHLxVIoHfxXkvFveSHw==

说明:RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但是想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。

3.1.2 普通用于登录

首先先在服务器端创建一个doris用户并设置登录密码,命令如下:

[root@localhost /]# useradd doris
[root@localhost /]# echo "123456" | passwd --stdin doris
Changing password for user doris.
passwd: all authentication tokens updated successfully.

接下来我就在客户端服务器192.168.0.76上用doris账号登录服务器192.168.0.75,命令如下:

[root@localhost ~]# ssh [email protected]
doris@192.168.0.75's password: 
[doris@localhost ~]$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:68:FE:A3  
          inet addr:192.168.0.75  Bcast:192.168.0.255  
          ……后面省略…………

上面看到没有,我已经成功用doris账号登录到我的主服务器上了。第一种登录方式圆梦成功,接下来介绍第二种登录方式。

注:我的主服务器地址是:192.168.0.75,客户端服务器地址是:192.168.0.76

3.2 ssh -l [远程主机用户名] [远程服务器主机名或IP 地址]

指令如下,我先退出当前登录环境:

[doris@localhost ~]$ exit
logout
Connection to 192.168.0.75 closed.
[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:25:C9:84  
          inet addr:192.168.0.76  Bcast:192.168.0.255  
……后面省略…………

然后再登录系统

[root@localhost ~]# ssh -l doris 192.168.0.75
doris@192.168.0.75's password: 
Last login: Sun Jul 16 00:28:48 2017 from 192.168.0.76

以上的方式同样可以登录我的服务器,是吧?接下来讲一下sshd的服务配置和管理。

(二)sshd服务配置和管理

1. 介绍一下sshd配置和安全调优的地方

配置文件位于/etc/ssh/sshd_config,注:参数前面有#,表示是默认值。 当然#号也表示注示,如果#后面有空在加具体内容时是注释否则是默认值。当配置完配置文件后重启一下sshd服务就会生效了,OK,编辑配置文件设置相关参数。

  • 设置sshd监听端口号 :Port 22
  • 设置sshd服务器绑定的IP:
  • 选择的 SSH 协议版本:Protocol 2
  • 设置包含计算机私人密匙的文件:#HostKey /etc/ssh/ssh_host_key
  • -

1.1 设置监听端口:Port

将22端口改为222端口,重启服务,接着用客户端登录,命令如下:

[root@localhost /]# vim /etc/ssh/sshd_config 
  8 # The strategy used for options in the default sshd_config shipped with
  9 # OpenSSH is to specify options with their default value where
 10 # possible, but leave them commented.  Uncommented options change a
 11 # default value.
 12 
 13 #Port 22
 14 PORT 222

查看端口监听情况,命令如下:

[root@localhost /]# netstat -antup | grep sshd
tcp        0      0 0.0.0.0:222                 0.0.0.0:*                   LISTEN      1966/sshd                      
tcp        0      0 :::222                      :::*                        LISTEN      1966/sshd 

端口修改成功,在客户端登录服务试试看:

[doris@localhost ~]$ ssh doris@192.168.0.75
ssh: connect to host 192.168.0.75 port 22: Connection refused

无法登录,然后我们加上端口的参数登录一下试试:

[doris@localhost ~]$ ssh -p 222  doris@192.168.0.75
doris@192.168.0.75's password: 
Last login: Sun Jul 16 00:40:13 2017 from 192.168.0.76
[doris@localhost ~]$ 

OK,登录成功,那么我们为什么要修改端口呢?主要还是防止黑客攻击时让黑客猜不到我们的登录端口,当然这个也就只是一般防护而已,具体如何去防护下面一节将介绍。

1.2 设置sshd服务器绑定的IP 地址

设置sshd服务器绑定的IP 地址,0.0.0.0 表示侦听所有地址,这个值可以写成本地IP地址也可以写成所有地址

1.3 安全考虑,设置为最新的协议版本

选择的 SSH 协议版本,可以是 1 也可以是 2 ,CentOS 5.x 预设是仅支援 V2。安全考虑,设置为最新的协议版本

Protocol 2

1.4 设置包含计算机私人密匙的文件

#HostKey /etc/ssh/ssh_host_key

1.5 当有人使用 SSH 登入系统的时候,SSH 会记录信息,这个信息要记录的类型为AUTHPRIV

SyslogFacility AUTHPRIV

互动:登录系统的默认日志存放在哪?
例: 为什么sshd配置文件中没有指定日志,日志却存放在了 /var/log/secure ?

[root@localhost ssh]# vim /etc/rsyslog.conf  # 查看 
# The authpriv file has restricted access.
authpriv.*                                   /var/log/secure

原来是在开机自启动的时候就已经指定好了,OK,接下来,继续往下看

1.6 登录记录的等级,INFO级别以上

#LogLevel INFO

1.7登录延迟时间【安全调优重点】

当使用者连上 SSH server 之后,会出现输入密码的画面,在该画面中, 在多久时间内没有成功连上 SSH server 就强迫断线!若无单位则默认时间为秒!

可以根据实际情况来修改实际,默认是2秒

LoginGraceTime 2m       #grace 优雅

1.8 是否允许ROOT登录

是否允许 root 登入!预设是允许的,但是建议设定成 no ,真实的生产环境服务器,是不允许root账号登陆的!!!

# PermitRootLogin yes

1.9 是否需要密码验证

密码验证当然是需要的!所以这里写 yes,也可以设置为no,在真实的生产服务器上,根据不同安全级别要求,有的是设置不需要密码登陆的,通过认证的秘钥来登陆。

PasswordAuthentication yes

1.10 是否允许空密码登录

若PasswordAuthentication这一项如果设定为 yes 的话,这一项就最好设定为 no ,这个项目在是否允许以空的密码登入!当然不许啦!

# PermitEmptyPasswords no

1.11登录成功后显示的欢迎信息

登入后是否显示出一些信息呢?例如上次登入的时间、地点等等,预设是 yes, 亦即是打印出/etc/motd这个文档的内容。

# PrintMotd yes

例如:修改/etc/motd文件打印信息

[root@localhost ssh]# echo 'Warning! From now on, all of your operaton yes has been record!' > /etc/motd
[root@localhost ssh]# cat /etc/motd
Warning! From now on, all of your operaton yes has been record!

客户端登录试一下:

[doris@localhost ~]$ ssh -p 222  doris@192.168.0.75
doris@192.168.0.75's password: 
Last login: Sun Jul 16 01:28:10 2017 from 192.168.0.75
Warning! From now on, all of your operaton yes has been record!
[doris@localhost ~]$ 

看到了吗?有警告讯息哟

1.12 显示上次登录信息

显示上次登入的信息!预设也是 yes

# PrintLastLog yes
[doris@localhost ~]$ ssh -p 222  doris@192.168.0.75
doris@192.168.0.75's password: 
Last login: Sun Jul 16 01:28:10 2017 from 192.168.0.75 #就是这个咯

1.13 判断客户端来源合法

一般来说,为了要判断客户端来源是正常合法的,因此会使用 DNS 去反查客户端的主机名,不过如果是在内网互连,这项目设定为 no 会让联机速度比较快。

# UseDNS yes

总结:以上的配置选项已经讲解完毕,接下来讨论一下防止暴力破解的相关问题。

(三) 防止sshd服务暴力破解的几种方法

为了防止黑客恶意用写代码去不断的请求服务器,这样轻者则造成服务器负债大重者则服务器会被攻破。可想而知,我们得做一些防止被暴力破解可能性的方案,方案主要以下三种,当然你也可以使用其他防护措施。

  • 配置安全的sshd服务
  • 通过开源的防护软件来防护安全
  • 通过自定义的脚本来防护

1. 配置安全的sshd服务

1.1 可以从以下入手来防护:

  1. 密码足够的复杂,密码的长度要大于8位最好大于20位。密码的复杂度是密码要尽可能有数字、大小写字母和特殊符号混合组成,
  2. 修改默认端口号
  3. 不允许root账号直接登陆,添加普通账号,授予root的权限
  4. 不允许密码登陆,只能通过认证的秘钥来登陆系统,通过密钥认证实现sshd认证

1.2使用密钥认证登录实验操作

实验环境
服务端:192.168.0.75
客户端:192.168.0.76

客户端生成密钥对

[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
# 提示输入密匙文件的保存路径,选择默认继续哈~
Enter passphrase (empty for no passphrase): 
# 下面要求输入密码,这里的passphrase 密码是对生成的私匙文件(/root/.ssh/id_dsa)的保护口令,如果不设置可以回车。
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
da:2c:d8:53:92:6e:ff:4a:54:14:cd:23:28:b3:bb:3b root@xuegod64
The key's randomart image is:
+--[ RSA 2048]----+
|         .o+     |
|      o ... +    |
|       +  .. .   |
|      .. .       |
|      o.S        |
|     +.B         |
|    . B.+        |
|     .E=         |
|      .ooo.      |
+-----------------+
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub  known_hosts

发布公钥到服务端,此步骤需要输入doris的密码进行验证

[root@localhost .ssh]# ssh-copy-id -i id_rsa.pub "-p 222 [email protected]"
doris@192.168.0.75's password: 
Now try logging into the machine, with "ssh '-p 222 [email protected]'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

登录操作

[root@localhost .ssh]# ssh -p 222 doris@192.168.0.75
Last login: Sun Jul 16 02:05:39 2017 from 192.168.0.75
Warning! From now on, all of your operaton yes has been record!

以上可以看到,我没有输入密码,然后就直接登录了,但是只是解决了免密码登录,还没有解决如果被暴力破解该怎么办,以下将介绍用户第三方开源软件进行防护。

2. 通过开源的防护软件来防护安全

2.1 引言

最近公网网站一直被别人暴力破解sshd服务密码。虽然没有成功,但会导致系统负载很高,原因是在暴力破解的时候,系统会不断地认证用户,从而增加了系统资源额外开销,导致访问公司网站速度很慢。

2.2 解决办法

fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是防火墙),而且可以发送e-mail通知系统管理员,很好、很实用、很强大!

ban (bæn)禁令
简单来说其功能就是防止暴力破解。工作的原理是通过分析一定时间内的相关服务日志,将满足动作的相关IP利用iptables加入到dorp列表一定时间。

注:重启iptables服务的话,所有DORP将重置。

2.3下载软件安装包

地址:http://www.fail2ban.org/wiki/index.php/Downloads

2.4上传软件并安装

我已经上传了,在root家目录

[root@localhost ~]# ls
anaconda-ks.cfg  fail2ban-0.8.14.tar.gz  install.log  install.log.syslog

解压

[root@localhost ~]# tar xf fail2ban-0.8.14.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  fail2ban-0.8.14  fail2ban-0.8.14.tar.gz  install.log  install.log.syslog

这个陌生的软件如何安装呢?当然有方法啦,我们可以进去里面找到readme文件查看帮助

[root@localhost ~]# cd fail2ban-0.8.14
[root@localhost fail2ban-0.8.14]# ls
ChangeLog  config   doc              fail2ban-server         files        man        README.Solaris  setup.py   TODO
client     COPYING  fail2ban-client  fail2ban-testcases      FILTERS      MANIFEST   server          testcases
common     DEVELOP  fail2ban-regex   fail2ban-testcases-all  kill-server  README.md  setup.cfg       THANKS
[root@localhost fail2ban-0.8.14]# vim README.md
31 To install, just do:
 32 
 33     tar xvfj fail2ban-0.8.12.tar.bz2
 34     cd fail2ban-0.8.12
 35     python setup.py install
 36 

找到以上这几行可以看到,我们应该如何安装,这里面说,先解压fail2ban-0.8.12.tar.bz2,我们已经解压成功了,接下来是进到解压后的目录中运行python setup.py install

在看一下下面这句话,哟哟,这个要求python版本必须大于等于2.4

 20 **It is possible that Fail2ban is already packaged for your distribution.  In
 21 this case, you should use it instead.**
 22 
 23 Required:
 24 - [Python >= 2.4](http://www.python.org)

所以我们得查看一下我们现有系统的python版本,如果没有安装,可以使用yum方式安装以下,接下来查看一下:

[root@localhost fail2ban-0.8.14]# python -V
Python 2.6.6

非常好,可以发现我主机上的版本是2.6.6的,符合它的要求。接下来就安装吧!

[root@localhost fail2ban-0.8.14]# python setup.py install

安装成功!有必要说一下主要的文件:

/etc/fail2ban/action.d#动作文件夹,内含默认文件。iptables以及mail等动作配置
/etc/fail2ban/fail2ban.conf #定义了fai2ban日志级别、日志位置及sock文件位置
/etc/fail2ban/filter.d #条件文件夹,内含默认文件。过滤日志关键内容设置
/etc/fail2ban/jail.conf #主要配置文件,模块化。主要设置启用ban动作的服务及动作阀值

生成服务启动脚本,命令如下:

[root@localhost fail2ban-0.8.14]# pwd
/root/fail2ban-0.8.14
[root@localhost fail2ban-0.8.14]# cp files/redhat-initd /etc/init.d/fail2ban

互动: 你怎么知道要复制这个文件? 一个新的软件包,后期怎么可以知道哪个文件是启动脚本文件?这就要找服务器启动脚本文件中有什么特点,然后过滤出来对应的文件名。启动脚本里都包含chkconfig 字段,看到了吗?这个文件定位到了,它就是启动脚本
[root@localhost fail2ban-0.8.14]# grep chkconfig ./* -R --color
./files/redhat-initd:# chkconfig: - 92 08

添加到开机自启动,命令如下:

[root@localhost /]# chkconfig --add fail2ban
[root@localhost /]# chkconfig --list fail2ban #可以看到3,4,5启动级别可以开机自启动了
fail2ban        0:off   1:off   2:off   3:on    4:on    5:on    6:off

接下来实际操作一把!

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

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

编辑/etc/fail2ban/jail.conf文件,以下是配置项的说明

 #全局设置
[DEFAULT]              
ignoreip = 127.0.0.1/8       #忽略的IP列表,不受设置限制
bantime  = 600             #屏蔽时间,单位:秒
findtime  = 600             #这个时间段内超过规定次数会被ban掉
maxretry = 3                #最大尝试次数
backend = auto            #日志修改检测机制(gamin、polling和auto这三种)

#单个服务检查设置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置。
[ssh-iptables]               
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.c
om, sendername="Fail2Ban"]#触发报警的收件人
logpath  = /var/log/secure   #检测的系统的登陆日志文件。这里要写sshd服务日志文件。 默认为logpath  = /var/log/sshd.log
#5分钟内3次密码验证失败,禁止用户IP访问主机1小时。 配置如下
bantime  = 3600   #禁止用户IP访问主机1小时
findtime  = 300    #在5分钟内内出现规定次数就开始工作
maxretry = 3    #3次密码验证失败

启动服务

[root@localhost /]# > /var/log/secure # 清日志。 从现在开始
[root@localhost /]# service fail2ban restart
Stopping fail2ban:                                         [  OK  ]
Starting fail2ban:                                         [  OK  ]
[root@localhost /]# iptables -L -n
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@localhost .ssh]# ssh 192.168.0.75
root@192.168.0.75's password:       #1次
Permission denied, please try again.    #2次
root@192.168.0.75's password: 
Permission denied, please try again.    #3次
root@192.168.0.75's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@localhost .ssh]# ssh 192.168.0.75 #再次访问
ssh: connect to host 192.168.0.75 port 22: Connection refused   #已经拒绝访问了

具体看某一项的状态也可以看,如果显示被ban的ip和数目就表示成功了,如果都是0,说明没有成功。

[root@localhost /]# 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.0.76 #我的客户端IP被拒绝了
   `- Total banned: 1

查看fail2ban的日志能够看到相关的信息,以下显示这个IP被禁了,对不对,合情合理呀。

[root@localhost /]# tail /var/log/fail2ban.log 
2017-07-16 04:20:49,431 fail2ban.actions[3194]: WARNING [ssh-iptables] Ban 192.168.0.76

温馨提示:
1、如果做错了,想清空一下记录,还原:只需要把 > /var/log/secure 清空就可以了。service fail2ban restart
2、另外如果后期需要把iptables清空后或iptables重启后,也需要把fail2ban重启一下。
3、如果要修改ssh默认端口 。 同时也需要配置fail2ban来监控sshd服务,则可以这样处理,编辑配置文件/etc/fail2ban/jail.conf,port=指定的端口号:例如:

98 action = iptables[name=SSH, port=222, protocol=tcp]

配置位置

然后修改动作文件/etc/fail2ban/action.d/iptables.conf中的默认端口。即port=ssh改 为port=222

配置位置
重启服务即可
4、如果想要使用fail2ban发送告警邮件,请确保系统的邮件服务能够正常发送邮件!

3. 通过自定义脚本来防护

#!/bin/bash

cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2"="$1;}' > /root/satools/black.txt

DEFINE="10"

for i in `cat /root/satools/black.txt`
do
        IP=`echo $i | awk -F='{print $1}'`
        NUM=`echo $i | awk -F='{print $2}'`

        if [ $NUM -gt $DEFINE ];then
                grep $IP /etc/hosts.deny > /dev/null
                if [ $? -gt 0 ];then
                        echo "sshd:$IP" >> /etc/hosts.deny
                fi
        fi
done

猜你喜欢

转载自blog.csdn.net/Dorisnzy/article/details/75175249
今日推荐