1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > SSHD 服务防止暴力破解

SSHD 服务防止暴力破解

时间:2019-12-27 03:38:45

相关推荐

SSHD 服务防止暴力破解

SSHD 服务防止暴力破解

目录

1、 配置安全的 SSHD 服务(方法一)

2、通过开源的防护软件来防护安全(方法二)

1、配置安全的 SSHD 服务

1.1、 服务器安全策略

密码足够复杂:

密码的长度要大于 8 位不大于 20 位。密码的复杂度是密码要尽可能有数字、大小写字母和特殊符号混合组成。

修改默认端口号:

修改掉默认ssh登陆的22端口,改成其它未被使用的端口号。

禁止 root 登录:

不允许 root 账号直接登陆到系统,添加普通账号,使用普通账号登录系统,授予 root的权限,必要时再从普通用户切换到 root 用户。

不允许密码登陆,只能通过认证的密钥来登陆系统:

这条视情况而定。

1.2、通过密钥认证实现 SSHD 认证。

1.2.1、实验环境:

服务端:Bob-zabbix129 IP:192.168.231.129

客户端:Bob130 IP:192.168.231.130

客户端生成密钥对,然后把公钥传输到服务端

1.2.2、生存密钥对

[root@Bob130 ~]# ssh-keygen

Generating public/private rsa key pair.

#生成公共/私有 rsa 密钥对

Enter file in which to save the key (/home/centos/.ssh/id_rsa):

#输入保存密钥的文件(/root/.ssh/id\u rsa)

Created directory ‘/home/centos/.ssh’.

Enter passphrase (empty for no passphrase):

#输入密码短语(无密码短语为空):

Enter same passphrase again:

#再次输入相同的密码短语:

Your identification has been saved in /home/centos/.ssh/id_rsa.

#您的标识已保存在/root/.ssh/id_rsa 中。

Your public key has been saved in /home/centos/.ssh/id_rsa.pub.

#您的公钥已保存在/root/.ssh/id_rsa.pub 中。

The key fingerprint is:

#关键指纹是:

SHA256:zIHUVIyXp26601rYLbmy8xsvW5VirrJsBBYLfodNh8o centos@Bob130

The key’s randomart image is:

#钥匙的随机图像是:

±–[RSA 2048]----+

| .o.=… |

| …= = . |

| . o.B.o o |

| . Eoo… . |

| o oS. o o |

| .oo= o |

| …+* + |

| .B.oB |

| .@+. |

±—[SHA256]-----+

[root@Bob130 ~]# cd /root/.ssh/

[root@Bob130 .ssh]# ls

id_rsa id_rsa.pub known_hosts

密钥对已经生成好了。

1.2.3、发布公钥到服务端。

使用 ssh-copy-id 命令将客户端生成的公钥发布到远程服务器 192.168.231.129 Bob-zabbix129。

[root@Bob130 .ssh]# ssh-copy-id 192.168.231.129

/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”

The authenticity of host ‘192.168.231.129 (192.168.231.129)’ can’t be established.

ECDSA key fingerprint is SHA256:tNiTdSuK8FKPhaGE9JKCfvfgt0tBOgBvPcQL0pxhK1s.

ECDSA key fingerprint is MD5:51:17:96:fe:82:e2:81:98🆎20:ec:e2:0c:c6:f0:8f.

Are you sure you want to continue connecting (yes/no)? yes

#输入 yes

/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/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.231.129’s password:

#输入 192.168.231.129 主机登录密码。

Number of key(s) added: 1

#此处说明已经添加一个密钥

Now try logging into the machine, with: “ssh ‘192.168.231.129’”

and check to make sure that only the key(s) you wanted were added.

#这个时候可以通过 ssh 无密钥直接登陆主机

[root@Bob130 .ssh]#

1.2.4、测试登陆129

[root@Bob130 .ssh]# ssh 192.168.231.129

Last login: Fri Jan 28 14:53:45

Bobo && Qiqi

[root@Bob-zabbix129 ~]#

登陆成功

注意:如果服务器不是监听 22 端口,则需要指定端口传输密钥:

[root@Bob-zabbix129 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 222 192.168.231.130

#如果端口是222 用这种方法,-p+端口号

/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”

/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/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.231.130’s password:

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘192.168.231.130’”

and check to make sure that only the key(s) you wanted were added.

[root@Bob-zabbix129 ~]# ssh 192.168.231.130

Last login: Fri Jan 28 15:29:00 from bob-

Warning! From now on, all of your operation has been record!

[root@Bob130 ~]#

登陆成功

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

2.1、实战背景:

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

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

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

优点:

使用简单、灵活、功能强大

源码下载软件包

官方地址:

以下 fail2ban 的源码包下载地址,实验使用 yum 安装

2.2、需要Python开发环境,且版本大于2.4

检查Python环境:

[root@Bob-zabbix129 ~]# python -V

Python 2.7.5

2.3、使用 yum 安装 fail2ban

[root@Bob-zabbix129 ~]# yum -y install epel-release

安装epel源

[root@Bob-zabbix129 ~]# yum -y install fail2ban

安装fail2ban

[root@Bob-zabbix129 ~]# ls /etc/fail2ban/

action.d fail2ban.d jail.conf paths-common.conf

fail2ban.conf filter.d jail.d paths-fedora.conf

安装完成

2.4、应用实例说明

设置条件:

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

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

2.5、jail.conf 文件说明

[sshd]

#mode = normal

port = ssh

logpath = %(sshd_log)s

backend = %(sshd_backend)s

#单个服务检查设置,如设置 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@, sender=fail2ban@, sendername=“Fail2Ban”] 、

#触发报警的收件人。

logpath = /var/log/secure

#检测的系统的登陆日志文件。这里要写 sshd 服务日志文件。

默认为 logpath = /var/log/sshd.log(在 centos8 当中需要删除默认的 ssh 区域 logpath 选项) 。

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

bantime = 3600

#禁止用户 IP 访问主机 1 小时。

findtime = 300

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

maxretry = 3

#3 次密码验证失败。

2.6、启动服务测试验证

2.6.1启动服务

[root@Bob-zabbix129 ~]# systemctl start fail2ban

开机自启动

[root@Bob-zabbix129 ~]# systemctl enable fail2ban

2.6.2测试

清空日志文件

[root@Bob-zabbix129 ~]# > /var/log/secure

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

[root@Bob130 .ssh]# ssh 192.168.231.129

root@192.168.231.129’s password:

Permission denied, please try again.

root@192.168.231.129’s password:

Permission denied, please try again.

root@192.168.231.129’s password:

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

三次输错后,再次尝试登录,直接被拒绝了。

[root@Bob130 .ssh]# ssh 192.168.231.129

ssh: connect to host 192.168.231.129 port 22: Connection refused

查看failban2状态

#配置好之后我们检测下 fail2ban是否工作

[root@Bob-zabbix129 .ssh]# fail2ban-client status

Status

|- Number of jail: 1

`- Jail list: sshd

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

查看被拦截并且禁止登陆的信息

[root@Bob-zabbix129 .ssh]# fail2ban-client status sshd

Status for the jail: sshd

|- Filter

| |- Currently failed: 0

| |- Total failed: 3

|- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd- Actions

|- Currently banned: 1

|- Total banned: 1

`- Banned IP list: 192.168.231.130

[root@Bob-zabbix129 .ssh]#

查看 fail2ban 的日志也能够看到相关的信息

[root@Bob-zabbix129 .ssh]# tail /var/log/fail2ban.log

-01-28 16:32:21,736 fail2ban.filtersystemd [12754]: INFO [sshd] Added journal match for: ‘_SYSTEMD_UNIT=sshd.service + _COMM=sshd’

-01-28 16:32:21,736 fail2ban.filter [12754]: INFO maxRetry: 3

-01-28 16:32:21,736 fail2ban.filter [12754]: INFO encoding: UTF-8

-01-28 16:32:21,737 fail2ban.filter [12754]: INFO findtime: 300

-01-28 16:32:21,737 fail2ban.actions [12754]: INFO banTime: 3600

-01-28 16:32:21,738 fail2ban.jail [12754]: INFO Jail ‘sshd’ started

-01-28 16:39:34,541 fail2ban.filter [12754]: INFO [sshd] Found 192.168.231.130 - -01-28 16:39:23

-01-28 16:39:34,541 fail2ban.filter [12754]: INFO [sshd] Found 192.168.231.130 - -01-28 16:39:26

-01-28 16:39:34,542 fail2ban.filter [12754]: INFO [sshd] Found 192.168.231.130 - -01-28 16:39:28

-01-28 16:39:34,585 fail2ban.actions [12754]: NOTICE [sshd] Ban 192.168.231.130

需要注意的 2 点:

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

如果修改 ssh 默认端口 22 为 后,配置 fail2ban 来监控 SSHD 服务需要修改配置文件

例:

[root@Bob-zabbix129 .ssh]# vim /etc/ssh/sshd_config

改 #Port 22

为 Port

[root@Bob-zabbix129 .ssh]# vim /etc/fail2ban/jail.conf

#修改 iptables 动作中的端口号,默认为 SSH,如图 1-11 所示。

改:port=ssh

为:port=

重启服务

[root@Bob-zabbix129 .ssh]# systemctl restart fail2ban

2.6.3、fail2ban 从黑名单中移除 IP 的方法:

移除命令:

[root@Bob-zabbix129 .ssh]# fail2ban-client set sshd unbanip 192.168.231.130

1

查看状态

[root@Bob-zabbix129 .ssh]# fail2ban-client status sshd

Status for the jail: sshd

|- Filter

| |- Currently failed: 0

| |- Total failed: 0

|- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd- Actions

|- Currently banned: 0

|- Total banned: 1

`- Banned IP list:

此时Banned IP list列表已经没有之前拦截的地址了。

验证:

[root@Bob130 .ssh]# ssh 192.168.231.129

root@192.168.231.129’s password:

Last failed login: Fri Jan 28 16:39:28 CST from on ssh:notty

There were 3 failed login attempts since the last successful login.

Last login: Fri Jan 28 16:39:13 from

Bobo && Qiqi

[root@Bob-zabbix129 ~]#

登陆成功。

再次查看日志

root@Bob-zabbix129 .ssh]# cat /var/log/fail2ban.log

022-01-28 16:39:34,585 fail2ban.actions [12754]: NOTICE [sshd]Ban 192.168.231.130

被禁用了

-01-28 16:52:50,856 fail2ban.actions [12754]: NOTICE [sshd] Flush ticket(s) with iptables-SSH

-01-28 16:52:50,864 fail2ban.actions [12754]: NOTICE [sshd] Unban 192.168.231.130

-01-28 16:53:01,394 fail2ban.actions [14393]: NOTICE [sshd] Restore Ban 192.168.231.130

#恢复禁用状态

-01-28 16:53:48,428 fail2ban.actions [14393]: NOTICE [sshd]Unban 192.168.231.130

正常登陆

学习笔记–完

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。