嵌入式Linux下Dropbear SSH配置優化
By Toradex秦海
1). 簡介
嵌入式 Linux 由于運行平臺通常資源受限同時對穩定性要求高,因此需要比較精簡,那么針對 SSH 服務器/客戶端應用,通常也不使用龐大的 OpenSSH,而是采用十分精簡的 Dropbear SSH工具。Dropbear 是一個基于 MIT License 的開源軟件,其一些基本信息可以參考如下軟件發布頁面:
https://matt.ucc.asn.au/dropbear/dropbear.html
本文所演示的平臺來自于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 root@10.20.1.168
root@10.20.1.168's password:
Permission denied, please try again.
### testuser login ###
$ ssh testuser@10.20.1.168
testuser@10.20.1.168'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/serial-getty@.service
+++ b/lib/systemd/system/serial-getty@.service
@@ -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 testuser@10.20.1.168 "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/dropbear@.service
+++ b/lib/systemd/system/dropbear@.service
@@ -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 testuser@10.20.1.168
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 的增強安全性配置供參考。
*博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。