網頁

2021年11月16日 星期二

透過 private key 免帳密直接登入 SSH

 步驟:

  1. ssh-keygen -t rsa 產生 id_rsa & id_rsa.pub
  2. mv | cat | rename ~/.ssh/id_rsa.pub ~/.ssh/authorized_key
    • 此情境是在伺服器端處理,也可在客戶端產生後將 public key 資訊傳到伺服器端
    • 若有多筆公鑰內文可以附加到 authorized_key 檔案
    • 若 SSH 伺服器是 Window OpenSSH 且登入的帳號是屬於管理者身份。那就要將 id_rsa.pub 內文附加到 C:\ProgramData\ssh\administrators_authorized_keys 檔案內。
  3. id_rsa 傳給客戶端。客戶端將該檔放在 USERPROFILE\.ssh\* 就可以直接透過 SSH 免帳密直接登入使用 (e.g. ssh sftp@192.168.0.2)
  4. id_rsa 透過 puttygen (Tools bar: Conversions item) 轉換成 id_rsa.ppk
    • puttygen Tools Bar : Conversions item
    • Load id_rsa files and choice "Save private key" item
  5. 使用方式
    • 將 id_rsa.ppk 轉給 client 使用。如透過 filezilla client,登入形式採用金鑰,金鑰指向 id_rsa.ppk
    •  id_rsa (不用轉換 ppk ) 直接放到 windows 的使用者目錄內 (如,C:\Users\user_account\.ssh)後,再直接輸入 ssh user_account@IP 即可直接登入遠方主機
  6. 指定使用者產生供使用的 private key
    runas /user:account cmd,開啟視窗後,輸入密碼。以該身份執行以上的程序即可(authorized_keys 檔案放在該登入帳號的 .ssh 目錄內)。

備忘:公鑰與私鑰的製作可以在使用者端或是伺服器端。將公鑰放到伺服器端且將私鑰傳給要連線的客戶端。

相關參考:

 

2021年11月11日 星期四

filezilla server 1.1.0 安裝備忘

新版本 v 1.1.0 舊版本 v 0.9.60_b 的介面差異很大。 

在舊版本已存在的狀態下直接做新版本的安裝會自動透過 filezilla-server-config-converter.exe 自動轉換成新版本所採用的設定檔。轉換上去後,在 passive mode settings 的部份沒有順利轉換上去,需要自行重新設定,其它的部份看來都有正確的轉換。

安裝完畢後進入介面的 configure 項目做以下設定調整:

FTP Server Listeners :
Protocol 的部份全部改成  Require explicit FTP over TLS (port 990 可以關閉)

Passive mode:
需要自行重新設定

Security:
Provide a X.509 certificate,憑證的產生方式已沒有內建,可利用 openssl 自行產生。或者直接採用 filezilla server 所提供的憑證 “Use a self-signed X.509 certificate” 來做使用。先前舊版本的憑證可以透過 filezilla server 自已提供的功能產生,但它的做法會將 RSA PRIVATE KEY、CERTIFICATE 放在同一個檔案,如果要照舊那就產生的 key and crt 的內容合併在一起即可。

設定檔備份:
新版本設定檔的部份調整放到 C:\Windows\System32\config\systemprofile\AppData\Local\filezilla-server

Daemon 執行身份調整 (e.g. account_ftp user):預設安裝時會將設定檔放到上面所指定的位置。於系統服務將該 daemon 切換執行身份後會在新設定的身份的  C:\New_User\AppData\Local\filezilla-server\* 新增 filezilla server 的設定檔案。將 \filezilla-server\* 目錄賦予相關使用者寫入的權限後再將目錄內已存在的相關設定檔直接刪除再由原來已設定完成(C:\Windows\.....)的設定檔案複製過來。重新啟動 filezilla server 服務。這不確定有沒有其它更簡便的切換方式,爾後再確認。

版本更新方式:下載新版本後(e.g. 1.5.0 version),直接點選執行檔。重新按介面提示重新安裝一次。但更新後它的 daemon 於系統服務內的登入身份會轉換到執行更新時的帳號身份。若採不同身份執行特定 deamon 的做法時,記得將身份切換回所要指定的身份。

Logging:
先新增 ftp_log.txt 後再到介面內將 File path 指向這個檔案;log output 選用 To file.

目前使用狀況:
使用起來都OK。但 client 連線使用完畢做切斷的動作會出現以下訊息。這在舊版本沒有出現。
GnuTLS error -110 in gnutls_record_recv: The TLS connection was non-properly terminated.
Client did not properly shut down TLS connection
Control channel closed with error from source 0. Reason: ECONNABORTED - Connection aborted.
Session 1 ended with error from source 0. Reason: ECONNABORTED - Connection aborted.

note : 此問題已於 1.4 版本修正

FTPstats GUI-based FileZilla log parser : 待找
 

2021年11月5日 星期五

PHP Curl SSL certificate problem

 

CA files 

PHP.ini

curl.cainfo="C:\php\extras\ssl\cacert.pem"

PHP code

//Tell cURL where our certificate bundle is located.
$certificate = "C:\php\extras\ssl\cacert.pem";
curl_setopt($ch, CURLOPT_CAINFO, $certificate);
curl_setopt($ch, CURLOPT_CAPATH, $certificate);

 

相關資料:

2021年11月2日 星期二

批次壓縮檔案與建立目錄內檔案清單

 zip.bat

rem 使用方式:檔案拖拉到此批次檔案即可
rem 此機制會自動在檔案名稱後加入副檔名名稱, 如 .pdf.zip。再利用批次重新命名修改即可
@echo off
set "a=%ProgramFiles%\7-zip\7z.exe"
for %%a in (%*) do if exist "%%~a\" (
        "%a%" a -tzip "%%~a.zip" "%%~a\*" -mx7
        ) else (
        "%a%" a -tzip "%%~a.zip" "%%~a" -mx7
        )

 list.bat 

rem 列出檔案名稱並依照檔名排序
rem 使用方式:檔案拖拉到此批次檔案即可
dir /b /on > list.txt

2021年7月21日 星期三

Apache CGI

httpd.conf example

<Directory "C:/Apache2/cgi-bin">
    #AllowOverride None
    #Options None
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
    Require all granted
</Directory>

cgi or perl 程式需要放到指定的 cgi-bin 目錄內執行。 很多 cgi 或 perl  都是採用 #!/usr/bin/perl 的宣告 。所以避免修改原有程式可以將 active perl 安裝於 C:\usr\ 目錄內,這樣就不需要修改現有程式。若是按 perl 預設安裝路徑那就需要修改為 #!C:\Perl64\bin\perl.exe。

相關參考:

2021年3月9日 星期二

SFTP 安裝@ Windows

步驟 

@windows

應用程式內新增 OpenSSH伺服器

@powershell

Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'

Get-NetFirewallRule -Name *ssh* //檢視規則

New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH SSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 //調整規則

調整 ssh_config 檔案 (%programdata%\ssh) & restart sshd

Port 22
ForceCommand internal-sftp //強制僅使用 sftp connections
Subsystem    sftp    sftp-server.exe -d "C:\Data_SFTP\"
ChrootDirectory C:\Data_SFTP //指定連線到特定目錄

AllowUsers sftpuser //加強管理
AllowGroups sshusers //加強管理

Logging //Windows OpenSSH 預設會記錄在事件檢視器。若要調整為記錄於檔案內需做以下調整。

SyslogFacility AUTH | LOCAL0 //AUTH 記錄在事件檢視器;LOCAL0 記錄在檔案
LogLevel INFO | VERBOSE | Debug3  (視需求)

//針對特定的 group 限制(系統內新增 sftponly group,並設定指定受限帳號)
Match Group sftponly
       ChrootDirectory C:\Apache2\htdocs
       ForceCommand internal-sftp
       X11Forwarding no
       AllowTcpForwarding no
 
//針對指定帳號登到指定目錄
Match User sftp_user
       ChrootDirectory C:\Apache2\htdocs\sftp_user
       ForceCommand internal-sftp
       X11Forwarding no
       AllowTcpForwarding no


參考資料