顯示具有 openssh 標籤的文章。 顯示所有文章
顯示具有 openssh 標籤的文章。 顯示所有文章

2026年5月13日 星期三

[Windows] 設置 OpenSSH 預設使用 PowerShell 7 環境 @ Windows 11

最近在處理 AI 自動化工作,開發了一些 claude skills ,發現最後需要在 Windows 11 運行 Claude Code CLI ,發現不同筆電的環境容易有問題,研究一陣子後,就決定統一都用 PowerShell 7 吧,也可以把 Windows 11 內建的 OpenSSH server 的 shell 也更換過去。

需要先理解 Windows 11 內建有 Powershell 5 ,但他不能拿來當 ssh 登入使用。這是預設採用 cmd 環境,改壞無法 ssh 登入 Windows 11 可以用這段還原,這是在 Powershell 運行:

New-ItemProperty `
  -Path "HKLM:\SOFTWARE\OpenSSH" `
  -Name DefaultShell `
  -Value "C:\Windows\System32\cmd.exe" `
  -PropertyType String `
  -Force

Restart-Service sshd

運行完,可用 Get-ItemProperty HKLM:\SOFTWARE\OpenSSH | Select-Object DefaultShell 查看預設的情況。

連續動作:

```
Windows PowerShell
著作權(C) Microsoft Corporation。保留擁有權利。

安裝最新的 PowerShell 以取得新功能和改進功能!https://aka.ms/PSWindows

PS C:\WINDOWS\system32> New-ItemProperty `
>>   -Path "HKLM:\SOFTWARE\OpenSSH" `
>>   -Name DefaultShell `
>>   -Value "C:\Windows\System32\cmd.exe" `
>>   -PropertyType String `
>>   -Force

DefaultShell : C:\Windows\System32\cmd.exe
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName  : OpenSSH
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.Core\Registry

PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> Restart-Service sshd
PS C:\WINDOWS\system32> Get-ItemProperty HKLM:\SOFTWARE\OpenSSH | Select-Object DefaultShell

DefaultShell
------------
C:\Windows\System32\cmd.exe
```

後續安裝完 Powershell 7 且更正確是要安裝 PowerShell 7 MSI 套件,直接在 MS Store 安裝會是另一個:

winget install --id Microsoft.PowerShell --installer-type wix

這時安裝完路徑會顯示在 `C:\Program Files\PowerShell\7\pwsh.exe` ,這版就可以當 ssh shell 來用了:

```
PS C:\WINDOWS\system32> Get-Command pwsh

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     pwsh.exe                                           7.6.1.0    C:\Program Files\PowerShell\7\pwsh.exe


PS C:\WINDOWS\system32> New-ItemProperty `
>>   -Path "HKLM:\SOFTWARE\OpenSSH" `
>>   -Name DefaultShell `
>>   -Value "C:\Program Files\PowerShell\7\pwsh.exe" `
>>   -PropertyType String `
>>   -Force

DefaultShell : C:\Program Files\PowerShell\7\pwsh.exe
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName  : OpenSSH
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.Core\Registry

PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> Restart-Service sshd
```

如此遠端到 Windows 11 時,登入後會看到版本資訊:

```
PowerShell 7.6.1
PS C:\Users\user> 
```

2024年7月2日 星期二

Linux 開發維護 - OpenSSH Server 資安漏洞補強,資安事件代號:CVE-2024-6387 @ Ubuntu 22.04

2024.07.02 一早就看到了 OpenSSH Server 漏洞,到處都在講,可參考 iThome 的文章:

iThome 2024.07.02 - OpenSSH含有可遠端攻陷伺服器的回歸漏洞

其中文章最後有提到:

OpenSSH團隊亦於本周一釋出了OpenSSH 9.8/9.8p1, 以修補CVE-2024-6387及另一個邏輯漏洞。該團隊亦說明,在實驗室環境下,要攻陷CVE-2024-6387需要不斷建立連結並持續6~8小時,直至達到伺服器極限,目前已於具備ASLR的32位元Linux系統上成功展現。

因此,除了修正外,也可以安裝一些阻擋 retry ssh 登入的行為,以前學生時代裝 denyhosts ,現在問 ChatGPT 得到 fail2ban 套件,在 Ubuntu 22.04 快速上手:

$ sudo apt-get update
$ sudo apt-get install fail2ban
$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
$ sudo systemctl restart fail2ban

建議還是要去看一下 /etc/fail2ban/jail.local 設定,像是 sshd retry 幾次會阻擋,以此評估是不是適合自己(常打錯密碼擋到自己也很無奈的)。裝完就可以用以下指令得知資訊:

$ sudo fail2ban-client status sshd 
Distributor ID: Ubuntu 
Description: Ubuntu 22.04.4 LTS 
Release: 22.04 
Codename: jammy 
Status for the jail: sshd 
|- Filter 
| |- Currently failed: 4 
| |- Total failed: 13 
| `- File list: /var/log/auth.log 
`- Actions 
|- Currently banned: 0 
|- Total banned: 0 
`- Banned IP list: 

不過,回到 CVE-2024-6387 資安事件,其實只要單純把 Ubuntu 系統更新,Ubuntu TLS 都給你傳便便:


Ubuntu 24.04
openssh-client - 1:9.6p1-3ubuntu13.3
openssh-server - 1:9.6p1-3ubuntu13.3

Ubuntu 23.10
openssh-client - 1:9.3p1-1ubuntu3.6
openssh-server - 1:9.3p1-1ubuntu3.6

Ubuntu 22.04
openssh-client - 1:8.9p1-3ubuntu0.10
openssh-server - 1:8.9p1-3ubuntu0.10

例如 Ubuntu 22.04 openssh 1:8.9p1-3ubuntu0.10 內容:


openssh 1:8.9p1-3ubuntu0.10 source package in Ubuntu
Changelog
openssh (1:8.9p1-3ubuntu0.10) jammy-security; urgency=medium

  * SECURITY UPDATE: remote code execution via signal handler race
    condition (LP: #2070497)
    - debian/patches/CVE-2024-6387.patch: don't log in sshsigdie() in log.c.
    - CVE-2024-6387

 -- Marc Deslauriers <email address hidden>  Wed, 26 Jun 2024 09:11:55 -0400

而自己的系統就只要查一下是否有更新至此版即可:

$ sudo apt upgrade && sudo apt upgrade && lsb_release -a && sudo apt policy openssh-server && ssh -V
...
Distributor ID: Ubuntu
Description: Ubuntu 22.04.4 LTS
Release: 22.04
Codename: jammy

openssh-server:
  Installed: 1:8.9p1-3ubuntu0.10
  Candidate: 1:8.9p1-3ubuntu0.10
  Version table:
 *** 1:8.9p1-3ubuntu0.10 500
        500 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages
        100 /var/lib/dpkg/status
     1:8.9p1-3 500
        500 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages

OpenSSH_8.9p1 Ubuntu-3ubuntu0.10, OpenSSL 3.0.2 15 Mar 2022

2023年8月31日 星期四

[macOS] 從 Macbook 遠端登入 Windows 筆電並使用 PowerShell 工作環境 @ macOS 13.2.1

由於想善用 Windows 筆電的資源(算力),先來試試看能不能打通 macOS command line 遠端登入進 Windows 11 筆電。查了一下資料,看來真的也有人有這種需求 XD

原理:
  1. Windows 11 安裝 OpenSSH Server
  2. macOS 安裝 PowerShell Core 資源 (非必要)
其中一還滿常見,二就有點怕怕,所幸資源算是微軟提供(?),有興趣可以裝 pkg 檔案,裝完在 macos command line 用 pwsh 指令就切換到 PowerShell 工作環境,目前只需做 (1) 就能完成需求,以下是在 Windows 11 筆電下運行指令,建議在上述微軟官方文件上複製使用,在此純做筆記:

PS C:\Users\user> Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

Name  : OpenSSH.Client~~~~0.0.1.0
State : Installed

Name  : OpenSSH.Server~~~~0.0.1.0
State : NotPresent

PS C:\Users\user> ssh -V
OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3

PS C:\Users\user> Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

Path          :
Online        : True
RestartNeeded : False

PS C:\Users\user> Start-Service sshd
PS C:\Users\user> Set-Service -Name sshd -StartupType 'Automatic'
PS C:\Users\user> if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
>>     Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
>>     New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
>> } else {
>>     Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
>> }
Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists.

PS C:\Users\user> ssh localhost
user@localhost's password:

如此筆電重啟後,也會自動啟動 OpenSSH Server 。而 macOS 要遠端進去一樣把它當成 linux server 即可,靠 ssh 加上帳密遠端登入。

另外,常會使用 ping 來查看機器健康情況,而 Windows 11 默認關閉(也是滿好的資安規劃),在此要人工開啟:

系統 -> 隱私權與安全性 -> Windows 安全性 -> 防火牆與網路保護 -> 進階設定 -> 輸入規則 ->  核心網路診斷 - ICMP 回應要求 -> 啟動


可以自行依照需求開啟全部 ICMP 回應或部分情境。 

最後,莫忘筆電的電源節源設計,預設應該是 5分鐘就會進入休眠,可以把它調整成插電就永不進入休眠,以及要衝刺算力也可以調整電源策略。

2021年6月4日 星期五

[Linux] 升級 Red Hat Enterprise Server 的 OpenSSL, OpenSSH 服務 @ Red Hat Enterprise Linux Server release 5.3

這是一台滿舊的機器,大概 10 年前吧。同事想要 git clone 時,出現了失敗問題,追蹤一下是機器的 openssl 過舊,由於機器有他的任務在,不好意思亂更新系統資源,怕爆!所以採用 tarball 的安裝方式。

openssl 太舊時,連用 wget/curl 去下載 https 來源時都會失敗的,只好靠 http 或是 scp 搬東西進去。

$ lsb_release -a
Distributor ID: RedHatEnterpriseServer
Description: Red Hat Enterprise Linux Server release 5.3 (Tikanga)
Release: 5.3
Codename: Tikanga

$ openssl version
OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008

只好挑個 /opt/tarball 安裝啦,沒想到要編譯 openssl 又會碰到 perl 版本不夠新:

$ wget http://www.cpan.org/src/5.0/perl-5.10.0.tar.gz
$ cd ~/perl-5.10.0
$ ./configure.gnu --prefix=/opt/tarball
$ make -j4 install

接著再安裝個 zlib ,採用 static 方式方便後續大家使用:

$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ cd ~/zlib-1.2.11
$ ./configure --prefix=/opt/tarball --static
$ make install

編譯 openssl:

$ wget http://www.openssl.org/source/openssl-1.1.1k.tar.gz 
$ cd ~/openssl-OpenSSL_1_1_1k 
$ PATH=/opt/tarball/bin:$PATH ./config --prefix=/opt/tarball
$ make -j4 install

編譯 openssh 工具包:https://github.com/openssh/openssh-portable/releases

$ cd ~/openssh-portable-V_8_6_P1
$ autoreconf
$ LD_LIBRARY_PATH=/opt/tarball/lib:$LD_LIBRARY_PATH ./configure --prefix=/opt/tarball/ --with-ssl-dir=/opt/tarball
$ LD_LIBRARY_PATH=/opt/tarball/lib:$LD_LIBRARY_PATH make -j4 install
$ file /opt/tarball/bin/ssh
/opt/tarball/bin/ssh: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, stripped

接著偷懶拉 code 方式:

$ GIT_SSH_COMMAND="LD_LIBRARY_PATH=/opt/tarball/lib:$LD_LIBRARY_PATH /opt/tarball/bin/ssh" git clone ...

或是在環境變數添加好:

LD_LIBRARY_PATH=/opt/tarball/lib:$LD_LIBRARY_PATH 
PATH=/opt/tarball/bin:$PATH

搞定!

2014年4月10日 星期四

[Linux] 更新 OpenSSH Server Key @ Ubuntu 12.04

由於熱門議題 OpenSSL Security Advisory [07 Apr 2014] / CVE-2014-0160 的影響,只好重新處理一下 OpenSSH Server key 了:

$ lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 12.04.4 LTS
Release: 12.04
Codename: precise
$ sudo rm /etc/ssh/ssh_host_*
$ sudo dpkg-reconfigure openssh-server