Cha*_*haz 6 linux ssh key-authentication openwrt dropbear
我正在尝试通过 ssh 访问我的路由器。目前我只有 telnet 访问权限,我安装了 dropbear 并且正在运行(在连接到路由器的 USB 驱动器上使用 opkg)。
从一开始,我所做的是生成一个私钥并将其解密(因为 dropbear 尚不支持此功能)和公共密钥:
cd .ssh
openssl genrsa -des3 -out id_rsa
openssl rsa -in id_rsa -out id_rsa
ssh-keygen -y -f id_rsa > authorized_keys
Run Code Online (Sandbox Code Playgroud)
我将公钥 ( authorized_keys)上传到/root/.ssh. 我将文件放在 Apache 服务器上(在我的本地计算机中)并使用 wget 将其下载到路由器上(因此下载的文件以所有者/组的身份获取),然后将权限更改为 0600(与客户端相同,但与我的用户)。
当我尝试访问时,它给了我一个“权限被拒绝(公钥)”错误:
$ ssh -v -i ~/.ssh/id_rsa root@192.168.1.1
OpenSSH_7.4p1, OpenSSL 1.0.2j 26 Sep 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 192.168.1.1 [192.168.1.1] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/chazy/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/chazy/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version dropbear
debug1: no match: dropbear
debug1: Authenticating to 192.168.1.1:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha1 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha1 compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:1EFA75uwLp+4hBW0t3aaY05QjLzYd4jjDWoULAzF/8o
debug1: Host '192.168.1.1' is known and matches the RSA host key.
debug1: Found key in /home/chazy/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/chazy/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).
Run Code Online (Sandbox Code Playgroud)
除非我误读了文档(GitHub repo)所说的内容:
服务器公钥认证:
您可以像使用 OpenSSH 一样使用 ~/.ssh/authorized_keys,只需将密钥条目放在该文件中即可。它们应该是以下形式:
SSH-RSA AAAAB3NzaC1yc2EAAAABIwAAAIEAwVa6M6cGVmUcLl2cFzkxEoJd06Ub4bVDsYrWvXhvUV + ZAM9uGuewZBDoAqNKJxoIn0Hyd0Nk / yU99UVv6NWV / 5YSHtnf35LKds56j7cuzoQpFIdjNwdxAN0PCET / MG8qyskG / 2IE2DPNIaJ3Wy + Ws4IZEgdJgPlTYUBWWtCWOGc =有人@主机名
您必须确保 ~/.ssh 和密钥文件只能由用户写入。当心将密钥拆分为多行的编辑器。
Dropbear 支持authorized_keys 条目的一些选项,请参阅联机帮助页。
我做了它说的一切,所以我不知道问题出在哪里。
文档提到了另一种方式:
客户端公钥认证:
Dropbear 可以作为客户端进行公钥身份验证,但您必须将 OpenSSH 样式的密钥转换为 Dropbear 格式,或使用 dropbearkey 创建它们。
如果你有一个 OpenSSH 风格的私钥 ~/.ssh/id_rsa,你需要做:
dropbearconvert openssh dropbear ~/.ssh/id_rsa ~/.ssh/id_rsa.db dbclient -i ~/.ssh/id_rsa.db
Dropbear 不支持加密主机密钥,但可以连接到 ssh-agent。
所以这意味着如果我将私钥转换为 dropbear 私钥,我可以使用 dropbear 客户端连接到 dropbear 服务器:
dropbearconvert openssh dropbear id_rsa id_rsa.db
Run Code Online (Sandbox Code Playgroud)
我要试一试,看看它是否有效。但无论如何,服务器公钥身份验证应该可以工作。
简短回答:您可能正在运行 OpenWrt,并且您需要将您的公钥放入/etc/dropbear/authorized_keys而不是/root/.ssh/authorized_keys.
长答案:
你指向的GitHub repo是dropbear作者维护的;它说这是~/.ssh/authorized_keys有效的,根据 GitHub 的说法,它至少已经这样做了 14 年。查看svr-authpubkey.c中的代码,它添加/.ssh/authorized_keys到“pw_dir”中。
然而,我遇到了和你一样的问题,我发现 OpenWrt 18.06.1 中提供的二进制文件实际上正在打开/etc/dropbear/authorized_keys. 使用该文件对我有用。
此行为记录在OpenWrt 文档中。
那怎么来的?
鉴于上面的代码无法自行生成该文件名(.ssh缺少)并且.ssh任何地方都没有符号链接,我运行strings了二进制文件。这表明在 GitHub 代码中可以预期的/etc/dropbear/authorized_keys之前明确提到了%s/.ssh/authorized_keys这一点。我的结论是 OpenWrt 二进制文件不是从相同的来源编译的……事实上,OpenWrt 用这个补丁修补了上游代码。/etc/dropbear/authorized_keys当(且仅当)目标用户是 root 时,它会更改所使用的文件。
既然你提到了opkg,我想你也在使用 OpenWrt,这是你的问题。我在你的问题中添加了一个 OpenWrt 标签。
小智 6
我刚刚在寻找为什么通过 dropbear 连接到我的服务器突然停止工作的原因时遇到了这个问题(已经工作了几个月,但只是偶尔每隔几周使用一次)。
我最终找到的解决方案/解释是在debug1: send_pubkey_test: no mutual signature algorithm我的客户端 ssh 连接尝试中增加了详细信息,这使我找到了一篇bitbucket 故障排除文章。
该文章提到,由于各种安全漏洞[...],RSA 算法正在跨操作系统和 SSH 客户端快速弃用,并列出了可能的解决方法:
添加PubkeyAcceptedKeyTypes +ssh-rsa到客户端 cfg 文件(仅将此用作临时解决方法,因为它可能不安全!)
使用 ECDSA 或 ED25519 算法/密钥。现在,我的系统上存在 dropbear 版本,我只能使用ECDSA,因为 ED25519在 dropbears 端给了我未知的算法错误。
希望这可以帮助那些在这个问题上遇到困难的人,就像我所做的那样,这可能不是原始问题的解决方案,请。原谅。
男人放下熊钥匙:
NOTES
The program dropbearconvert(1) can be used to convert between Dropbear
and OpenSSH key formats.
Dropbear does not support encrypted keys.
EXAMPLE
generate a host-key:
# dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
extract a public key suitable for authorized_keys from private key:
# dropbearkey -y -f id_rsa | grep "^ssh-rsa " >> authorized_keys
Run Code Online (Sandbox Code Playgroud)