Windows SSH 连接失败 Corrupted MAC on input 解决方案

1. 问题现象

在 Windows 上使用 PowerShell 或 CMD 通过 OpenSSH 客户端连接远程 Linux/网络设备时,出现以下错误并断开连接:

1
2
3
ssh admin@192.168.1.45
Corrupted MAC on input.
ssh_dispatch_run_fatal: Connection to 192.168.1.45 port 22: message authentication code incorrect

服务端提示连接已建立,但客户端在密钥交换/加密通信阶段校验失败,整个会话立即被强制关闭,无法正常登录。


2. 根因分析

通过开启 SSH 详细日志(ssh -vvv)可以观察到失败时协商的算法组合:

项目 协商结果
Cipher aes128-ctr
MAC umac-128-etm@openssh.com
状态 Corrupted MAC on input → 断开

将算法组合改为以下两种后,连接恢复正常:

项目 替换后配置
Cipher aes256-ctr
MAC hmac-sha2-256

2.1 结论

不是网络链路问题,也不是 SSH 服务端故障,而是 umac-128-etm@openssh.com 这一 MAC 算法组合在两端实现上存在兼容性问题。

可能原因:

  1. 服务端 OpenSSH / OpenSSL 实现问题

    • UMAC-ETM 属于较新的 Encrypt-then-MAC 模式。
    • 某些定制系统虽然声明支持该算法,但底层 libcrypto 实际实现存在 bug。
  2. Windows OpenSSH 9.x 默认优先级变化

    • OpenSSH 9.x 客户端默认将 umac-128-etm@openssh.com 排在较高优先级。
    • 因此 Windows 客户端会优先选中它并触发问题。
  3. 硬件 offload / NIC 驱动(概率较低)

    • 部分网卡驱动对加解密 offload 处理不当会放大该问题。
    • 换用 hmac-sha2-256 后立即正常,基本可排除该项。

3. 解决方案

3.1 临时方案:命令行指定算法

仅作一次性测试使用,不修改任何配置文件:

1
ssh -c aes256-ctr -o MACs=hmac-sha2-256 admin@192.168.1.45

3.2 永久方案:写入 SSH 客户端配置(推荐)

编辑 Windows 用户目录下的 SSH 配置文件:

1
C:\Users\<用户名>\.ssh\config

Windows OpenSSH 默认不会自动创建该文件,需手动创建。

步骤 1:确认 .ssh 目录

1
ls $env:USERPROFILE\.ssh

如不存在则创建:

1
mkdir $env:USERPROFILE\.ssh

步骤 2:创建/编辑 config 文件

1
notepad $env:USERPROFILE\.ssh\config

写入以下内容(针对单台设备):

1
2
3
Host 192.168.1.45
Ciphers aes256-ctr
MACs hmac-sha2-256

⚠️ 注意:文件名必须为 config不要保存为 config.txt

步骤 3:验证文件已创建

1
dir $env:USERPROFILE\.ssh

正常情况下应看到:

1
2
config
known_hosts

4. 验证配置是否生效

4.1 查看 SSH 实际生效的配置

1
ssh -G admin@192.168.1.45 | findstr /i "cipher macs"

期望输出:

1
2
ciphers aes256-ctr
macs hmac-sha2-256

4.2 重新发起连接

1
ssh admin@192.168.1.45

应能正常登录,不再出现 Corrupted MAC on input 错误。


5. 批量设备配置(可选)

如果存在多台相似设备(如 BMC、交换机、服务器),可使用全局配置:

1
2
3
Host *
MACs hmac-sha2-256,hmac-sha2-512
Ciphers aes256-ctr,aes128-ctr

⚠️ 不建议全局禁用 UMAC,除非确认所有目标设备均存在相同问题,否则可能在某些设备上反而无法连接。


6. 排查参考

6.1 对比两端支持的 MAC 算法

Linux 服务端:

1
ssh -Q mac

Windows 客户端:

1
ssh -Q mac

比较输出,可判断是否因服务端实现问题导致协商到不兼容的算法。

6.2 典型案例记录

Windows OpenSSH 9.5p1 连接 OpenSSH 8.6:使用 umac-128-etm@openssh.com 时认证失败,强制指定 hmac-sha2-256 后解决。

对于测试环境,可作为兼容性 workaround 长期保留。


7. 小结

  • 现象Corrupted MAC on input + message authentication code incorrect
  • 根因umac-128-etm@openssh.com 算法协商失败
  • 临时解决ssh -c aes256-ctr -o MACs=hmac-sha2-256 user@host
  • 永久解决:在 ~/.ssh/config 中固定 CiphersMACs
  • 风险:仅影响算法协商,不涉及认证信息泄露