SSH 支持弱加密算法【漏洞修复】

问题背景

项目平台服务器执行安全扫描,发现 SSH 弱加密算法相关漏洞,我们需要对这个漏洞进行修复。漏洞名称为:
SSH 服务支持弱加密算法【原理扫描】

系统信息

[root@m01 ~]# cat /etc/redhat-release 
Rocky Linux release 8.10 (Green Obsidian)
[root@m01 ~]# ssh -V 
OpenSSH_8.0p1, OpenSSL 1.1.1k  FIPS 25 Mar 2021

问题核心

我们不能通过直接修改 sshd_config 配置文件,通过增强算法这个思路来修复这个漏洞,因为在 RHEL / Rocky Linux 8+ 系统中,引入了一个叫做 系统级加密策略(System-wide Crypto Policies) 的机制。它的作用是统一控制系统中所有支持该机制的服务(包括 OpenSSH、OpenSSL、GnuTLS 等)所允许使用的加密算法。
当你运行下面的命令,你可以查看当前的加密策略:

[root@m01 ~]# update-crypto-policies --show
DEFAULT

默认情况下,系统会将 /etc/crypto-policies/config 设置为 DEFAULT,并生成对应的策略文件(如 /etc/crypto-policies/back-ends/opensshserver.config),OpenSSH 服务在启动时会优先读取这个策略文件中的算法列表,而不是完全依赖 sshd_config 中手动指定的 Ciphers / MACs / KexAlgorithms 等字段。

📌 关键点:即使你在 /etc/ssh/sshd_config 中明确指定了 Ciphers、MACs、KexAlgorithms,如果系统启用了 crypto-policies(默认启用),那么 OpenSSH 实际使用的算法是你配置的算法与系统策略允许的算法的交集。

  • 如果,你在 sshd_config 中写了一些弱算法(比如 aes128-cbc),但策略是 FUTURE(禁止弱算法),那么这些弱算法会被自动过滤掉 。

  • 但反过来,如果你在 sshd_config 中只写了强算法,而策略是 DEFAULT 或 LEGACY,那可能仍然会暴露一些你不希望的算法。

  • 更重要的是:如果你在 sshd_config 中没有显式指定某些算法,OpenSSH 会使用策略允许的默认算法列表。

如果,我们使用常规方式去直接去修改 sshd_config 配置文件并重启服务后,你可以执行下面的命令表达式获取算法配置项

[root@m01 ~]# sshd -T | grep cip 
ciphers [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]

sshd -T 是 解析配置文件后输出当前生效的内部配置值,但它 不会模拟 crypto-policies 的运行时行为。换句话说,它显示的是“如果忽略策略,仅按配置文件应使用的算法”,不是实际运行时协商可用的算法列表。

[root@m01 ~]# sshd -T | grep ciphers
ciphers [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
root@m01 back-ends]# pwd
/etc/crypto-policies/back-ends
[root@m01 back-ends]# cat opensshserver.config  # 为了方便对比,只保留了算法配置
CRYPTO_POLICY='[email protected],[email protected],aes256-ctr,aes256-cbc,[email protected],aes128-ctr,aes128-cbc 

那怎么获取实际对外暴露的算法呢?我们可以通过客户端扫描发现真实对外暴露的算法列表,下面我们展示详细结果:

[root@master01 ~]# nmap -p 22 --script ssh2-enum-algos -Pn 192.168.2.117 
Starting Nmap 7.92 ( https://nmap.org ) at 2025-12-06 14:38 CST
Nmap scan report for 192.168.2.117
Host is up (0.00037s latency).

PORT   STATE SERVICE
22/tcp open  ssh
| ssh2-enum-algos: 
|   kex_algorithms: (12)
|       curve25519-sha256
|       [email protected]
|       ecdh-sha2-nistp256
|       ecdh-sha2-nistp384
|       ecdh-sha2-nistp521
|       diffie-hellman-group-exchange-sha256
|       diffie-hellman-group14-sha256
|       diffie-hellman-group16-sha512
|       diffie-hellman-group18-sha512
|       diffie-hellman-group-exchange-sha1
|       diffie-hellman-group14-sha1
|       [email protected]
|   server_host_key_algorithms: (5)
|       rsa-sha2-512
|       rsa-sha2-256
|       ssh-rsa
|       ecdsa-sha2-nistp256
|       ssh-ed25519
|   encryption_algorithms: (7)
|       [email protected]
|       [email protected]
|       aes256-ctr
|       aes256-cbc
|       [email protected]
|       aes128-ctr
|       aes128-cbc
|   mac_algorithms: (8)
|       [email protected]
|       [email protected]
|       [email protected]
|       [email protected]
|       hmac-sha2-256
|       hmac-sha1
|       [email protected]
|       hmac-sha2-512
|   compression_algorithms: (2)
|       none
|_      [email protected]
MAC Address: 00:0C:29:48:69:DA (VMware)

Nmap done: 1 IP address (1 host up) scanned in 0.38 seconds

通过扫描结果,我们可以看到两者得出的算法是不一致的,而 nmap --script ssh2-enum-algos 是通过 真实连接 SSH 服务并进行算法协商探测,反映的是 实际对外暴露的算法,这会受到 crypto-policies 的限制或覆盖。所以你看到两者不一致,正是因为 sshd -T 没有考虑 crypto-policies 的运行时影响。

回到漏洞的问题上吧,我们通过扫描结果发现了默认的策略及配置文件中包含部分弱加密算法,比如 CBC 模式加密算法,这些模式存在漏洞问题:

原因:

  • CBC模式容易受到选择明文攻击(CPA)

  • 可能受到填充预言攻击(Padding Oracle Attack)

  • SSH协议中CBC模式存在时间侧信道漏洞

  • 许多安全标准(如NIST、PCI DSS)已不推荐CBC模式用于新系统

  • 128位密钥长度(安全性较低)

修复方案

方式一:继续使用 crypto-policies(推荐)

这是 Red Hat 官方推荐的做法,安全且统一。

# 查看当前策略
update-crypto-policies --show

# 设置更强的策略(如 FUTURE)
update-crypto-policies --set=FUTURE

# 重启 sshd 使策略生效
systemctl restart sshd

我们通过修改策略的方式即可修复漏洞,且无需手动维护算法列表,系统自动更新安全基线。

Crypto-Policies 主要策略场景

  • DEFAULT(默认策略)
    适用场景:大多数生产环境和日常使用
    特点:平衡安全性与兼容性
    包含算法:禁用已知弱算法(如 SSH CBC 模式),保留主流安全算法
    典型用户:不需要特殊合规要求的普通服务器和工作站

  • LEGACY(传统兼容策略)
    适用场景:连接老旧系统或设备的特殊环境
    特点:最大兼容性,包含已不推荐的弱算法
    包含算法:支持较旧的加密算法(包括CBC模式、SHA1等)
    典型用户:需要连接传统设备、遗留系统的环境(临时使用)

  • FUTURE(未来安全策略)
    适用场景:高安全要求环境、安全敏感系统
    特点:超前安全标准,移除有风险的算法
    包含算法:只保留目前认为长期安全的算法(如禁用128位、只保留256位+)
    典型用户:金融系统、政府机构、需要前瞻性安全防护的环境

方式二:禁用 crypto-policies 对 OpenSSH 的控制

如果你坚持要手动控制算法(例如需要兼容特定设备),可以通过 systemd 覆盖环境变量,要让 OpenSSH 忽略系统 crypto-policies,必须在 sshd 的 systemd 服务中设置环境变量 CRYPTO_POLICY=(空值),而不是写进 sshd_config

步骤:

修改 /usr/lib/systemd/system/sshd.service 配置文件,注释掉策略配置文件的引用

[root@m01 back-ends]# cat  /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target

[Service]
Type=notify
#EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config # 注释掉这行
Environment=CRYPTO_POLICY=  # 新增环境变量
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

编辑 vim /etc/ssh/sshd_config 增加算法配置列表

[root@m01 back-ends]# cat /etc/ssh/sshd_config | grep Ciphers
# Ciphers and keying
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr
# crypto properties (Ciphers, MACs, ...) will not have any effect here.

重启 sshd 服务,扫描验证结果

[root@m01 back-ends]# systemctl daemon-reload 
[root@m01 back-ends]# systemctl restart sshd 

# 扫描验证
[root@master01 ~]# nmap -p 22 --script ssh2-enum-algos -Pn 192.168.2.117 
Starting Nmap 7.92 ( https://nmap.org ) at 2025-12-06 15:13 CST
Nmap scan report for 192.168.2.117
Host is up (0.00027s latency).

PORT   STATE SERVICE
22/tcp open  ssh
| ssh2-enum-algos: 
|   kex_algorithms: (11)
|       curve25519-sha256
|       [email protected]
|       ecdh-sha2-nistp256
|       ecdh-sha2-nistp384
|       ecdh-sha2-nistp521
|       diffie-hellman-group-exchange-sha256
|       diffie-hellman-group16-sha512
|       diffie-hellman-group18-sha512
|       diffie-hellman-group14-sha256
|       diffie-hellman-group14-sha1
|       [email protected]
|   server_host_key_algorithms: (5)
|       rsa-sha2-512
|       rsa-sha2-256
|       ssh-rsa
|       ecdsa-sha2-nistp256
|       ssh-ed25519
|   encryption_algorithms: (5)
|       [email protected]
|       [email protected]
|       [email protected]
|       aes256-ctr
|       aes192-ctr
|   mac_algorithms: (10)
|       [email protected]
|       [email protected]
|       [email protected]
|       [email protected]
|       [email protected]
|       [email protected]
|       [email protected]
|       hmac-sha2-256
|       hmac-sha2-512
|       hmac-sha1
|   compression_algorithms: (2)
|       none
|_      [email protected]
MAC Address: 00:0C:29:48:69:DA (VMware)

Nmap done: 1 IP address (1 host up) scanned in 0.30 seconds

总结

上面的算法列表仅供参考,您可以按照您的需求修改,推荐使用修改策略的方式来修复这个漏洞问题。在 Rocky Linux 8 + 版本及红帽 8+ 版本中,仅修改 /etc/ssh/sshd_config 中的加密算法配置是无效的,因为系统默认启用了 Crypto Policies(系统级加密策略),它会覆盖或限制 OpenSSH 实际使用的算法。

参考文献

crypto-policies

用一杯咖啡支持我们,我们的每一篇[文档]都经过实际操作和精心打磨,而不是简单地从网上复制粘贴。期间投入了大量心血,只为能够真正帮助到您。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇