By Toradex秦海
1). 简介
嵌入式 Linux 由于运行平台通常资源受限同时对稳定性要求高,因此需要比较精简,那么针对 SSH 服务器/客户端应用,通常也不使用庞大的 OpenSSH,而是采用十分精简的 Dropbear SSH工具。Dropbear 是一个基于 MIT License 的开源软件,其一些基本信息可以参考如下软件发布页面:
Dropbear SSH
本文所演示的平台来自于Toradex Apalis iMX8 嵌入式平台,基于 NXP iMX8 系列 ARM 处理器,核心为 Cortex-A52/A53 。
2). 硬件准备
a). Apalis iMX8 ARM 核心版配合 Apalis Eva Board 载板,并连接调试串口和网口以便测试。
3). 具体配置说明
a). Apalis iMX8 模块标准 Ycoto Linux BSP 中已经包含 Dropbear 相关软件,不过由于默认配置为了开发测试方便,默认使能了 debug-tweaks 功能(比如这样可以允许 root 账户无密码登录),这样如下面 Ycoto Project/Openembedded 相关文件说明也就同时也使能了 weak ciphers
./ layers/meta-toradex-demos/recipes-core/dropbear/dropbear_%.bbappend
---------------------------------------
# THE Eclipse RSE system explorer uses a ssh client which cannot cope with the
# dropbear ssh server if weak ciphers are disabled.
# If debug-tweaks is set in IMAGE_FEATURES then enable also weak ciphers.
# With debug-tweaks we allow password less root access, enforcing strong
# ciphers is pointless anyway.
PACKAGECONFIG = "${@bb.utils.contains("IMAGE_FEATURES", "debug-tweaks", "", "disable-weak-ciphers",d)}"
---------------------------------------
b). 为了使 Dropbear SSH安全性更高,可以在 Ycoto 编译环境下参考如下 patch 文件修改关闭 debug-tweaks 和 weak ciphers。因为同时这样也关闭了 root 用户无密码登录,因此也需要给 root 用户配置默认密码。
./ local.conf 文件修改 patch
---------------------------------------
--- a/build/conf/local.conf 2023-05-30 12:16:33.780891419 +0800
+++ b/build/conf/local.conf 2023-05-31 10:55:36.841801362 +0800
@@ -277,3 +277,9 @@
include conf/machine/include/${MACHINE}.inc
# DO NOT SET THE MACHINE AFTER THE ABOVE INCLUDE
+# accept the Freescale EULA
+ACCEPT_FSL_EULA = "1"
+# add root password
+EXTRA_IMAGE_FEATURES = "allow-root-login package-management"
+INHERIT += "extrausers"
+EXTRA_USERS_PARAMS = "usermod -P Abcd1234 root"
---------------------------------------
./ 参考这里的说明将上述修改下重新编译生成的 Ycoto Linux Image 通过 Toradex Easy Installer 更新到 Apalis iMX8 模块,此时测试无论本地串口登录还是远程SSH登录 root 用户都需要输入预设的密码了,增强了安全性。
c). 为了进一步提高 SSH 安全性,可以创建普通 user 用户用于远程登录,而禁止 root 用户 SSH 远程登录。这样也可以通过限制 user 用户的权限来提高系统安全性。
./ 创建新的 user 用户
---------------------------------------
root@apalis-imx8-07308034:~# useradd testuser
root@apalis-imx8-07308034:~# passwd testuser
New password:
Retype new password:
passwd: password updated successfully
---------------------------------------
./ 禁止 root 用户 SSH 登录,参考如下 patch 修改 /etc/default/dropbear 文件
---------------------------------------
--- a/etc/default/dropbear
+++ b/etc/default/dropbear
@@ -1,2 +1,2 @@
# Disallow root logins by default
-DROPBEAR_EXTRA_ARGS=""
+DROPBEAR_EXTRA_ARGS=" -w"
---------------------------------------
./ 测试使用 testuser 用户远程 SSH 登录成功, root 用户登录失效
---------------------------------------
### root login ###
$ ssh [email protected]
[email protected]'s password:
Permission denied, please try again.
### testuser login ###
$ ssh [email protected]
[email protected]'s password:
mkdir: cannot create directory '/run/user/1000': Permission denied
chmod: cannot access '/run/user/1000': No such file or directory
apalis-imx8-07308034:~$
---------------------------------------
./ 另外,如果需要本地串口 testuser 或者 root 用户自动登录,可以参考如下 patch 修改
---------------------------------------
--- a/lib/systemd/system/[email protected]
+++ b/lib/systemd/system/[email protected]
@@ -30,7 +30,7 @@
[Service]
Environment="TERM=xterm"
-ExecStart=-/sbin/agetty -8 -L %I 115200 $TERM
+ExecStart=-/sbin/agetty -8 -a testuser -L %I 115200 $TERM
Type=idle
Restart=always
UtmpIdentifier=%I
---------------------------------------
d). 远程 SSH 除了默认的密码登录方式外,还可以开启安全等级更高的通过 public key 来无密码登录
./ 在需要远程登录 Apalis iMX8 设备的 PC 主机环境下通过 ssh-keygen 工具生成 SSH private key/public key pair
---------------------------------------
### generate 4096-bits key pair ###
$ ssh-keygen -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/simon/.ssh/id_rsa): /home/simon/local/tmp/ssh-test/id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/simon/local/tmp/ssh-test/id_rsa.
Your public key has been saved in /home/simon/local/tmp/ssh-test/id_rsa.pub.
The key fingerprint is:
SHA256:Pr5PQjzRuPMVS3Rrkdtq+7pDVOFMGumBLpFGkjGSEs0 simon@simon-Latitude-5300
The key's randomart image is:
+---[RSA 4096]----+
| .+..++.. o.++.|
| . E..o* o +Bo.|
| . + + +.+* |
| . + o =o .|
| S . o. . |
| o + . + |
| + o o . |
| . + o |
| oo. o=.|
+----[SHA256]-----+
---------------------------------------
./ 通过 SSH 远程命令将生成的 public key 写入到 Apalis iMX8 dropbear authorized_keys 文件
---------------------------------------
### create ssh folder on apalis iMX8 device ###
apalis-imx8-07308034:~$ mkdir /home/testuser/.ssh
### add public key to apalis iMX8 authorized_keys file from Host PC remotely ###
$ ssh [email protected] "tee -a /home/testuser/.ssh/authorized_keys" < /home/simon/local/tmp/ssh-test/id_rsa.pub
---------------------------------------
./ 参考如下 patch 修改 Apalis iMX8 dropbear 启动配置来使 public key 验证生效
---------------------------------------
--- a/lib/systemd/system/[email protected]
+++ b/lib/systemd/system/[email protected]
@@ -4,9 +4,9 @@
After=syslog.target dropbearkey.service
[Service]
-Environment="DROPBEAR_RSAKEY_DIR=/etc/dropbear"
+Environment="DROPBEAR_RSAKEY_DIR=/home/testuser/.ssh/"
EnvironmentFile=-/etc/default/dropbear
-ExecStart=-/usr/sbin/dropbear -i -r ${DROPBEAR_RSAKEY_DIR}/dropbear_rsa_host_key $DROPBEAR
_EXTRA_ARGS
+ExecStart=-/usr/sbin/dropbear -i $DROPBEAR_EXTRA_ARGS
ExecReload=/bin/kill -HUP $MAINPID
StandardInput=socket
KillMode=process
---------------------------------------
./ 重启 Apalis iMX8 使配置生效后,再次尝试远程 SSH 登录,可以实现无需密码而是采用 public key 验证登录
---------------------------------------
$ ssh -i /home/simon/local/tmp/ssh-test/id_rsa [email protected]
mkdir: cannot create directory '/run/user/1000': Permission denied
chmod: cannot access '/run/user/1000': No such file or directory
apalis-imx8-07308034:~$
---------------------------------------
e). 更多关于 dropbear 工具命令参数说明可以参考如下
https://manpages.ubuntu.com/manpages/bionic/man8/dropbear.8.html
4). 总结
本文基于嵌入式 Linux 简单演示了 轻量化 SSH 工具软件 Dropbear 的增强安全性配置供参考。