Ubuntu 22.04 SSH-RSA登录失败
最近用户反馈我们的WebShell在使用ssh密钥登陆Ubuntu系统时报认证错误,为此调查了下。
SSH2模块下报错如下
Client: publickey auth failed
Error: All configured authentication methods failed
排查
- 公私钥密码使用的RSA加密算法
- 尝试本地终端SSH登陆,终端App为Mac自带Terminal
ssh -v -i ~/test.pem ubuntu@1.221.54.8
登陆发现可以成功,而WebShell报错,因此WebShell大概率有问题
注意
- SSH如果报错Permissions 0644 for ‘test.pem’ are too open.即需要确保私钥文件权限合适,执行
chmod 400 test.pem
解决文件权限 - SSH登陆成功后,想退出直接输入
exit
即可
分析
- 本地终端Terminal或iTerm2均可登陆成功,WebShell不可以,WebShell中用到了node模块ssh2。同使用该模块的shell产品,如tabby/electerm,均登陆失败,因此可确定跟ssh2模块实现有关。
- ubuntu22.04是2022年4月发布的新系统,使用老版系统是发现没有该问题,因此也跟系统升级有关,查询发现新系统
默认禁用SSH--RSA签名
综上可以确定新系统关闭RSA验证引发了该问题,而本地终端可以应该该情况支持,但node下ssh2模块明显没支持。
解决方案
确定了现象/问题,解决方案有以下几种
方案1-服务器配置修改
sudo vi /etc/ssh/sshd_config
增加如下配置PubkeyAcceptedKeyTypes +ssh-rsa
重启sshd服务sudo service sshd restart
方案2-公私钥算法调整
重新创建公私钥,生成算法选其它的,比如ed25519
方案3-客户端SSH2升级
实际处理逻辑应该是本地终端一样做了处理。社区大佬已有PR,支持,在没有合并发布新SSH2包之前可以临时调整版本到关联如下commit来解决。
1 | "ssh2": "git@github.com:Eugeny/ssh2.git#22735cecf1d9c118b2b8af1c2f80fe5b04996fe1", |
测试以上三种方案,均可解决
不提倡的ssh-rsa
摘自OpenSSH官网的一段介绍
Future deprecation notice
It is now possible[1] to perform chosen-prefix attacks against the SHA-1 hash algorithm for less than USD$50K. For this reason, we will be disabling the
ssh-rsa
public key signature algorithm that depends on SHA-1 by default in a near-future release.This algorithm is unfortunately still used widely despite the existence of better alternatives, being the only remaining public key signature algorithm specified by the original SSH RFCs.
客户端SSH2实现
Ubuntu22.04虽然默认关闭了ssh-rsa但支持rsa-sha2-512/256,而这两个摘要算法与ssh-rsa都是采用的RSA算法只是摘要算法选择的SHA2而非SHA1。因此只要客户端在签名时将ssh-rsa公钥的签名算法设置为sha2-512/256即可。
之所以登录失败是因为当前客户端在做签名时走的SHA1,而服务端已不支持
具体 change见https://github.com/Eugeny/ssh2/commit/22735cec
写在最后
综上RSA安全性目前还是有保证的,仅是SHA1摘要算法安全性较低,因此面对已经生成的RSA公钥并不需要过多担心,更多是注意不要使用SHA1即可