1、背景

系统进行漏扫后输出如下信息,服务器为内网环境

漏洞名称

漏洞描述

等级

安全建议

SSH Weak Key Exchange Algorithms Enabled SSH 弱密钥交换算法已启用

远程 SSH 服务器配置为允许被认为是弱的密钥交换算法。这是基于 IETF 草案文档 Key Exchange (KEX) Method Updates and Recommendations for Secure Shell (SSH) draft-ietf-curdle-ssh-kex-sha2-20。第 4 节列出了关于不应和不得启用的密钥交换算法的指南。这包括:diffie-hellman-group-exchange-sha1、diffie-hellman-group1-sha1、gss-gex-sha1-、gss-group1-sha1-、gss-group14-sha1-*、rsa1024-sha1

低危

联系供应商或查阅产品文档以禁用弱算法。

2、处理

首先服务器是云服务器,目前只有ssh远程登陆,ssh的弱密钥交换算法的修复对ssh服务有一定的风险,保险期间。打开telnet服务处理该问题

2.1、开启telnet服务器

2.1.1、查看是否安装服务
# rpm -qa telnet-server
# rpm -qa xinetd
# systemctl status telnet
2.1.2、下载需要的安装包
# mkdir -p /root/tools/telnet
# cd /root/tools/telnet
# yum -y install telnet-server --downloadonly --downloaddir ./
# yum -y install xinetd --downloadonly --downloaddir ./
2.1.3、安装

讲下载的安装包拷贝至离线服务器进行安装

# rpm -ivh telnet-server-0.17-66.el7.x86_64.rpm
# rpm -ivh xinetd-2.3.15-14.el7.x86_64.rpm

Centos7打补丁 centos7 补丁_算法

2.1.4、 启动服务
# systemctl start telnet.socket
# systemctl start xinetd

Centos7打补丁 centos7 补丁_算法_02

2.1.5、查看启动状态
# systemctl status telnet.socket
# systemctl status xinetd

Centos7打补丁 centos7 补丁_perl_03

2.1.6、设置开机启动
# systemctl enable xinetd.service
# systemctl enable telnet.socket

Centos7打补丁 centos7 补丁_服务器_04

2.1.7、查看是否加入开机启动
# systemctl list-unit-files | grep enabled | grep telnet

Centos7打补丁 centos7 补丁_perl_05

2.1.7、开启端口

查看防火墙状态

# systemctl status firewalld

Centos7打补丁 centos7 补丁_算法_06

2.1.8、防火墙命令
# systemctl start firewalld		# 开启防火墙
# systemctl stop firewalld		# 关闭防火墙
# systemctl restart firewalld	# 重启防火墙

查看防火墙端口

# firewall-cmd --list-all

Centos7打补丁 centos7 补丁_perl_07


新增telnet端口

telnet服务默认使用23端口查看端口是否开放

# firewall-cmd --query-port=23/tcp

Centos7打补丁 centos7 补丁_Centos7打补丁_08


添加端口

# firewall-cmd --zone=public --add-port=23/tcp --permanent

Centos7打补丁 centos7 补丁_算法_09


重新加载防火墙

# firewall-cmd --complete-reload

重启后验证查看23端口

Centos7打补丁 centos7 补丁_算法_10


登陆

Centos7打补丁 centos7 补丁_perl_11


linux默认情况下root用户使用telnet是登录不了的,需要修改/etc/securetty文件

开启root账号telnet登陆

# vim /etc/pam.d/login
# auth requisite pam_securetty.so

安全考虑不应使用root账号直接登陆,需要使用普通账户登陆

Centos7打补丁 centos7 补丁_服务器_12


确认telnet登陆成功后,重启一下服务器,使用telnet登陆,登陆成功后开始第二部修复漏洞

2.2、修复SSH弱口令算法漏洞

本文通过升级openssh开修复漏洞

2.2.1、安装依赖包(离线情况下请自行挂载本地iso文件)
# yum install  -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel  pam-devel
# yum install  -y pam* zlib*
2.2.2、下载openssh包和openssl的包

以下安装涉及到多台服务器同时安装,本次使用ansible工具进行操作,ansible的使用可以参考《win10系统下ansible环境搭建》,在本文不多介绍ansible的基础知识,以使用为主。

下载openssh

在这里插入图片描述

https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/

Centos7打补丁 centos7 补丁_服务器_13

下载openssl
https://ftp.openssl.org/source/

Centos7打补丁 centos7 补丁_算法_14

2.2.3、 安装openssl

拷贝文件

# ansible jgxt1 -m copy -a 'src=openssl-3.0.0.tar.gz dest=/home/Downloads'
# ansible jgxt1 -m copy -a 'src=openssh-8.8p1.tar.gz dest=/home/Downloads'

解压文件

# ansible jgxt1 -a 'chdir=/home/Downloads tar xfz openssl-3.0.0.tar.gz'
# ansible jgxt1 -a 'chdir=/home/Downloads tar xfz openssh-8.8p1.tar.gz'

查看系统默认版本

# ansible jgxt1 -a 'openssl version'
# ansible jgxt1 -a 'ssh -V'

Centos7打补丁 centos7 补丁_perl_15

备份文件

# 查看文件
# ansible jgxt1 -a 'ls -all /usr/bin/openssl'
# 移动文件
# ansible jgxt1 -a 'mv /usr/bin/openssl /usr/bin/openssl_bak'

编译安装openssl
配置、编译、安装

# ansible执行
# ansible jgxt1 -m shell -a 'chdir=/home/Downloads/openssl-3.0.0 ./config shared --prefix=/opt/openssl && make && make install'

此处如果弹出
/usr/bin/env: perl: No such file or directory
需要安装一下perl,安装完毕后,重新执行安装

yum install perl -y

执行编译安装提示,perl安装完毕,执行还是报如下错误,

Can't locate CPAN.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

使用yum安装perl-CPAN模块

yum -y install perl-CPAN

是perl版本未安装Net::SNMP和CPAN.pm模块,使用Perl自带的模块——CPAN.pm模块。

perl -MCPAN -e shell
# 弹出提示框后回车或者输入yes自动安装

cpan[1]> install Net::SNMP

安装以上两个perl的模块后,重新执行以上编译安装命令。

验证安装

# ansible jgxt1 -a 'echo $?'

输出0表示没有问题

创建链接

# ansible jgxt1 -a 'ln -s /opt/openssl/bin/openssl /usr/bin/openssl'
# ansible jgxt1 -a 'ln -s /opt/openssl/include/openssl /usr/include/openssl'

查看

# ansible jgxt1 -a 'ls -all /usr/bin/openssl'
# ansible jgxt1 -a 'ls -all /usr/include/openssl'

加载配置

# ansible jgxt1 -m shell -a 'echo "/opt/openssl/lib64" >> /etc/ld.so.conf'

# ansible jgxt1 -m shell -a '/sbin/ldconfig'

Centos7打补丁 centos7 补丁_服务器_16

2.2.4、安装openssh

查看openssh版本

Centos7打补丁 centos7 补丁_服务器_17


删除原ssh配置文件和目录

# # ansible jgxt1 -m shell -a 'rm -rf /etc/ssh/*'

Centos7打补丁 centos7 补丁_perl_18

配置、编译、安装
切换到文件目录,配置编译

# ansible jgxt1 -m shell -a './configure --prefix=/opt/openssh --sysconfdir=/etc/ssh  --with-openssl-includes=/opt/openssl/include --with-ssl-dir=/opt/openssl   --with-zlib   --with-md5-passwords   --with-pam  && make && make install'

检查结果

Centos7打补丁 centos7 补丁_perl_19


修改配置文件当前连接异常

Centos7打补丁 centos7 补丁_服务器_20


修改配置文件两处

# vim /etc/ssh/sshd_config
PermitRootLogin yes
UseDNS no

配置文件拷贝

# cd /home/Downloads/openssh-8.8p1
# cp -a contrib/redhat/sshd.init /etc/init.d/sshd
# cp -a contrib/redhat/sshd.pam /etc/pam.d/sshd.pam

# chmod +x /etc/init.d/sshd 	# 赋权限
# chkconfig --add sshd			# 添加系统服务
# systemctl enable sshd 		# 设置开机启动

移除原服务文件

# mkdir -p /home/service
# mv /usr/lib/systemd/system/sshd.service /home/service/

设置开机启动

# chkconfig sshd on

重启服务

# /etc/init.d/sshd restart

Centos7打补丁 centos7 补丁_ssh_21


重启服务器

起来后无法连接使用,直接登陆服务器发现sshd没有启动,启动报错

排查问题

1、将源码安装包中 contrib/redhat/sshd.init 文件复制到 /etc/init.d/ 目录下并添加可执行权限

# cp contrib/redhat/sshd.init /etc/init.d/
# chmod +x /etc/init.d/sshd.init

2、启动服务

# /etc/init.d/sshd.init start

这时,/run/systemd/generator.late/ 目录下会产生一个名为 sshd.service的 systemd 服务配置文件。
3、将这个文件复制到 systemd 的服务配置文件目录下

# cp /run/systemd/generator.late/sshd.service  /usr/lib/systemd/system/sshd.service

重启防火墙

# firewall-cmd --reload 	# 重新加载防火墙

重启服务

# systemctl daemon-reload
# systemctl restart sshd.socket

目前使用的是 sshd.socket 服务,而想切换至 sshd.service 服务,可以执行如下命令:

# systemctl disable sshd.socket
# systemctl enable sshd.service
# systemctl stop sshd.socket; 
# systemctl start sshd.service

查看状态

Centos7打补丁 centos7 补丁_服务器_22


查看开机确认已经添加,

# systemctl enable sshd		# 设置自动启动
# systemctl list-unit-files| grep enabled | grep sshd	# 查询启动设置

重启服务器ssh登陆

登陆查看版本

Centos7打补丁 centos7 补丁_perl_23


确认没有问题,关闭telnet服务

# systemctl disable xinetd.service
# systemctl stop xinetd.service
# systemctl disable telnet.socket
# systemctl stop telnet.socket
# netstat -nltp

Centos7打补丁 centos7 补丁_Centos7打补丁_24


远程telnet登陆

Centos7打补丁 centos7 补丁_算法_25