工控網(wǎng)首頁
>

應(yīng)用設(shè)計

>

嵌入式Linux下Dropbear SSH配置優(yōu)化

嵌入式Linux下Dropbear SSH配置優(yōu)化

1). 簡介

嵌入式 Linux  由于運(yùn)行平臺通常資源受限同時對穩(wěn)定性要求高,因此需要比較精簡,那么針對 SSH 服務(wù)器/客戶端應(yīng)用,通常也不使用龐大的 OpenSSH,而是采用十分精簡的 Dropbear SSH工具。Dropbear 是一個基于 MIT License 的開源軟件,其一些基本信息可以參考如下軟件發(fā)布頁面:

https://matt.ucc.asn.au/dropbear/dropbear.html

 

本文所演示的平臺來自于Toradex Apalis iMX8 嵌入式平臺,基于 NXP iMX8 系列 ARM 處理器,核心為 Cortex-A52/A53 

 

 

2)硬件準(zhǔn)備

a).  Apalis iMX8 ARM 核心版配合 Apalis Eva Board 載板,并連接調(diào)試串口和網(wǎng)口以便測試。

 

 

3). 具體配置說明

a). Apalis iMX8 模塊標(biāo)準(zhǔn) Ycoto Linux BSP 中已經(jīng)包含 Dropbear 相關(guān)軟件,不過由于默認(rèn)配置為了開發(fā)測試方便,默認(rèn)使能了 debug-tweaks 功能(比如這樣可以允許 root 賬戶無密碼登錄),這樣如下面 Ycoto Project/Openembedded 相關(guān)文件說明也就同時也使能了 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 編譯環(huán)境下參考如下 patch 文件修改關(guān)閉 debug-tweaks 和 weak ciphers。因為同時這樣也關(guān)閉了 root 用戶無密碼登錄,因此也需要給 root 用戶配置默認(rèn)密碼。

./ 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 模塊,此時測試無論本地串口登錄還是遠(yuǎn)程SSH登錄 root 用戶都需要輸入預(yù)設(shè)的密碼了,增強(qiáng)了安全性。

 

c). 為了進(jìn)一步提高 SSH 安全性,可以創(chuàng)建普通 user 用戶用于遠(yuǎn)程登錄,而禁止 root 用戶 SSH 遠(yuǎn)程登錄。這樣也可以通過限制 user 用戶的權(quán)限來提高系統(tǒng)安全性。

./ 創(chuàng)建新的 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 用戶遠(yuǎn)程 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). 遠(yuǎn)程 SSH 除了默認(rèn)的密碼登錄方式外,還可以開啟安全等級更高的通過 public key 來無密碼登錄

./ 在需要遠(yuǎn)程登錄 Apalis iMX8 設(shè)備的 PC 主機(jī)環(huán)境下通過 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 遠(yuǎn)程命令將生成的 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 使配置生效后,再次嘗試遠(yuǎn)程 SSH 登錄,可以實現(xiàn)無需密碼而是采用 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). 更多關(guān)于 dropbear 工具命令參數(shù)說明可以參考如下

https://manpages.ubuntu.com/manpages/bionic/man8/dropbear.8.html

 

 

4). 總結(jié)

本文基于嵌入式 Linux 簡單演示了 輕量化 SSH 工具軟件 Dropbear 的增強(qiáng)安全性配置供參考。

審核編輯(
王靜
)
投訴建議

提交

查看更多評論
其他資訊

查看更多

Verdin AM62 LVGL 移植

基于 NXP iMX8MM 測試 Secure Boot 功能

隆重推出 Aquila - 新一代 Toradex 計算機(jī)模塊

Verdin iMX8MP 調(diào)試串口更改

NXP iMX8MM Cortex-M4 核心 GPT Capture 測試