SSHD服务防止暴力破解

防止暴力破解的3种方法:
1配置安全的sshd服务
2使用密钥进行认证登录
3使用开源的软件进行保护

1配置安全的sshd服务

1足够复杂的密码
2修改默认的端口号
3不允许root直接登录
4不允许密码登录
前3个很简单,有些是修改配置文件/etc/ssh/sshd_config就可以了
最后一个,是使用密钥登录sshd服务,以下介绍关于密钥登录sshd服务的详细过程。

2使用密钥远程登录sshd服务

实验环境:
服务器:192.168.1.63
客户端:192.168.1.64
客户端生成密钥对,然后把公钥传输到服务端
ssh-copy-id -i root@192.168.1.63
或者是:ssh-copy-id -i 192.168.1.63
操作步骤
客户端:

1输入ssh-keygen生成密钥对
[root@break ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
##输入私人密钥的保存路径,选择默认路径继续
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
密钥的保护密码
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.
2将公钥传输给服务器。
cd /root/.ssh
ls -al
ssh-copy-id -i 192.168.1.63
ssh root@192.168.1.63
##演示
[root@break ~]# cd /root/.ssh/
[root@break .ssh]# ls -la
总用量 12
drwx------. 2 root root   57 3月  21 21:48 .
dr-xr-x---. 6 root root  252 3月  21 21:45 ..
-rw-------  1 root root 1679 3月  21 21:46 id_rsa
-rw-r--r--  1 root root  395 3月  21 21:46 id_rsa.pub
-rw-r--r--. 1 root root  174 3月  20 21:28 known_hosts
[root@break .ssh]# ssh-copy-id -i 192.168.1.63
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.63's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.1.63'"
and check to make sure that only the key(s) you wanted were added.

服务器端:

cat /root/.ssh/authorized_keys与客户端的/root/.ssh/id_rsa.pub相同,
注意事项:
也可以进行复制粘贴,但是需要注意权限问题600

3fail2ban防止暴力破解软件的安装和使用

软件的地址:
链接:https://pan.baidu.com/s/1SJlOBCVhSJhCg3CENUAyMg
提取码:bp0j

详细步骤

1将下载好的fail2ban的安装包上传到我们的操作系统上去。使用xshell的rz命令进行上传。

[root@break ~]# ls fail2ban-0.8.14.tar.gz 
fail2ban-0.8.14.tar.gz

2将其解压缩到任意的文件夹中,我这里指定的是/usr/local/下,然后切换到该文件夹下,然后进行安装,一般开源软件安装的过程是:下载——上传——解压——查看README.md文档。根据README.md文档进行详细的安装。首先是安装python的安装,其次是脚本文件的查找(一般的脚本中存在chkconfig这样的语句,可以使用grep进行查找),查找到脚本文件后,将其放入系统的启动目录。-------然后设置开机启动。

安装的命令详解——仅仅是安装成功。

#将其解压到指定的目录
tar -xf fail2ban-0.8.14.tar.gz -C /usr/local/
#进入解压的目录
cd /usr/local/fail2ban-0.8.14
#查看README.md文档
vim README.md
#安装python
python setup.py install 
#查看python的版本
python -V
#查看脚本文件
grep chkconfig ./* -R --color
#将脚本文件复制到默认的系统的启动目录下
cp redhat-initd /etc/rc.d/init.d/fail2ban

[root@break ~]# tar -xf fail2ban-0.8.14.tar.gz -C /usr/local/  
[root@break local]# cd /usr/local/                  
[root@break local]# ls fail2ban-0.8.14/
build      doc                     FILTERS         setup.cfg
ChangeLog  fail2ban-client         kill-server     setup.py
client     fail2ban-regex          man             testcases
common     fail2ban-server         MANIFEST        THANKS
config     fail2ban-testcases      README.md       TODO
COPYING    fail2ban-testcases-all  README.Solaris
DEVELOP    files                   server
[root@break local]# cd fail2ban-0.8.14/
[root@break fail2ban-0.8.14]# vim README.md
[root@break fail2ban-0.8.14]# python setup.py install
running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
changing mode of /usr/bin/fail2ban-client to 755
changing mode of /usr/bin/fail2ban-server to 755
changing mode of /usr/bin/fail2ban-regex to 755
running install_data
copying README.md -> /usr/share/doc/fail2ban
running install_egg_info
Removing /usr/share/fail2ban/fail2ban-0.8.14-py2.7.egg-info
Writing /usr/share/fail2ban/fail2ban-0.8.14-py2.7.egg-info

Please do not forget to update your configuration files.
They are in /etc/fail2ban/.————————————显示安装后的路径
[root@break fail2ban-0.8.14]# grep chkconfig ./* -R --color
./files/redhat-initd:# chkconfig: - 92 08
[root@break files]# cp redhat-initd /etc/rc.d/init.d/fail2ban 
[root@break files]# systemctl enable fail2ban

使用readme.md文档查看如何安装以及安装的注意事项。如图所示,本软件所需要的是python脚本的版本大于等于2.4.以及安装的简易命令。
在这里插入图片描述
在这里插入图片描述

应用实例:

主目录下文件的介绍

[root@break fail2ban-0.8.14]# cd /etc/fail2ban/
[root@break fail2ban]# ls -al
总用量 64
drwxr-xr-x    6 root root   124 3月  22 13:47 .
drwxr-xr-x. 138 root root  8192 3月  21 22:41 ..
drwxr-xr-x    2 root root  4096 3月  21 18:04 action.d
-rw-rw-r--    1 root root  1525 8月  20 2014 fail2ban.conf
drwxr-xr-x    2 root root     6 3月  21 17:14 fail2ban.d
drwxr-xr-x    2 root root  4096 3月  21 17:14 filter.d
-rw-rw-r--    1 root root 19345 3月  21 17:46 jail.conf
drwxr-xr-x    2 root root     6 3月  21 17:14 jail.d

/etc/fail2ban的重要文件以及文件夹的介绍

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

条件:设置条件: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这三种)

[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"]   
 #触发报警的收件人
logpath  = /var/log/secure   #检测的系统的登陆日志文件。这里要写sshd服务日志文件。 
默认为logpath  = /var/log/sshd.log

/etc/fail2ban/jail.conf修改内容如下:

#5分钟内3次密码验证失败,禁止用户IP访问主机1小时。 配置如下
96行的:enabled = true
bantime  = 3600   #禁止用户IP访问主机1小时
findtime  = 300    #在5分钟内内出现规定次数就开始工作
maxretry = 3    #3次密码验证失败

配置好之后记得重启服务和fail2ban

systemctl restart sshd
systemctl restart fail2ban

使用192.168.1.64(客户端)主机来进行测试。(3次密码输入错误就会1小时之内无法登陆)

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

使用服务器对其进行查看

fail2ban-client status
fail2ban-client status ssh-iptables
[root@break fail2ban]# fail2ban-client status 
Status
|- Number of jail:	1
`- Jail list:		ssh-iptables
[root@break fail2ban]# 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.64 
   `- Total banned:	1
[root@break fail2ban]# 

注意事项

1、如果做错了,想清空一下记录,还原:
只需要把 >  /var/log/secure   清空就可以了。
systemctl restart fail2ban

2、另外如果后期需要把iptables清空后或iptables重启后,也需要把fail2ban重启一下。

3.初学者对于一些错误可能不熟悉,我建议出现了无法解决的问题,可以使用虚拟机的快照,重新开始你的配置。

3、如果修改ssh默认端口22为2015后 。 配置fail2ban来监控sshd服务

需要修改配置文件:
jail.conf的98行中的内容
[root@break fail2ban]# vim jail.conf
在这里插入图片描述
改:port=ssh 为 port=2015

#修改iptables动作中的端口号。 默认为ssh。 61行的内容
[root@break fail2ban]# cd action.d/
[root@break action.d]# vim iptables.conf
改: port=ssh 为port=2015
在这里插入图片描述

4使用一个脚本对sshd服务进行防护安全(了解——为以后做准备)
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值