唯一可靠方式是显式声明强算法白名单并禁用脆弱组件;默认配置不安全,需手动设置protocol 2、kexalgorithms(如curve25519-sha26等)、ciphers和macs(含-etm@openssh.com),禁用cbc、sha-1、小模数dh组及dsa/弱rsa密钥。

直接在 /etc/ssh/sshd_config 中显式声明强算法白名单,并禁用所有已知脆弱组件,是唯一可靠的方式。默认配置不等于安全配置,尤其在旧系统或未更新环境中,仍可能协商出 CBC 模式、SHA-1 签名、小模数 DH 组等已被淘汰的算法。
禁用 SSHv1 并锁定协议版本
SSHv1 存在根本性设计缺陷,无法修复。即使客户端只发 v2 请求,服务端若未明确拒绝 v1,仍存在潜在降级风险(尤其在中间设备干扰场景):
- 在
sshd_config中添加或修改为:Protocol 2 - 删除或注释掉任何含
Protocol 1或Protocol 1,2的行 - OpenSSH 8.8+ 已彻底移除 v1 支持,但 8.7 及更早版本必须手动设置
替换密钥交换算法(KexAlgorithms)
这是握手阶段最易被扫描工具标记为“中危”的环节。弱 DH 组(如 group1-sha1、group14-sha1)可被预计算攻击破解:
- 推荐配置(一行写完,无空格、无换行):KexAlgorithms curve25519-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256
- 必须剔除:
diffie-hellman-group1-sha1、diffie-hellman-group14-sha1、diffie-hellman-group16-sha512(除非明确需兼容极老嵌入式设备) - 验证命令:
sshd -T | grep -w kexalgorithms和远程nmap --script ssh2-enum-algos -p22 目标IP
收紧对称加密与消息认证(Ciphers + MACs)
CBC 模式加密(如 aes256-cbc)存在填充预言攻击风险;非 ETM 认证(如 hmac-sha2-256)缺乏加密后认证,易被篡改:
- Ciphers 推荐:Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
- MACs 推荐:Macs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com
- 严禁出现:
*-cbc、3des-*、arcfour*、*-sha1、*-md5、以及任何不含-etm@openssh.com后缀的 MAC
约束主机密钥类型与签名方式
服务器身份认证依赖 HostKey,若使用 DSA 或短 RSA(ssh-rsa(SHA-1 签名):
- 确保
HostKey指向现代密钥文件,例如:HostKey /etc/ssh/ssh_host_ed25519_key 和 HostKey /etc/ssh/ssh_host_rsa_key(后者需为 3072 位以上) - 生成强 RSA 密钥:
ssh-keygen -t rsa -b 3072 -f /etc/ssh/ssh_host_rsa_key - 服务端无需在配置中写
HostKeyAlgorithms,但客户端可通过ssh -o HostKeyAlgorithms=+ssh-ed25519主动优选











