OpenSSL 使用

這個真的很常使用,寫下來做紀錄

指令
openssl <= 進入互動模式
openssl version <= 列出 openssl 版本
openssl version -a <= 列出 openssl 版本及更多資訊
openssl ciphers -help <= 列出 ciphers 指令的可用參數
openssl ciphers -v <= 列出具有協定版本加密方式
openssl ciphers -V <= 列出具有協定版本加密方式(含代碼)
openssl ciphers -v -tls1 <= 僅列出 tls1 協定版本的加密方式
openssl ciphers -v 'ALL' <= 列出所有支援的加密方式
openssl ciphers -v 'HIGH' <= 列出高強度加密方式
openssl ciphers -v 'ADH' <= 僅列出ADH加密方式
openssl ciphers -v 'HIGH:!ADH' <= 列出高強度加密方式但不含 ADH 加密方式
openssl ciphers -v 'ALL:!ADH:@STRENGTH' <= 列出所有密碼(除了 NULL 和 ADH),並按強度排序
openssl ciphers -v '3DES:+RSA' => 僅包含3DES密碼,然後將RSA密碼放在最後

在很多的 APP 中(如SSH、RDP、Apache 等…)都有支援加密方式,但常因為加密方式被破解而在弱點掃瞄中被視為有風險,這時就必需關閉特定的加密法,以 xrdp 為例,在 xrdp.ini 檔中在啟動 tls 加密後,有一段設定可以限制可以使用哪些加密法

tls_ciphers=HIGH:!ADH:!AECDH <= 使用高強度(> 128 bits 長度的加密法)但不含 ADH 及 AECDH
註:HIGH(> 128 bits 長度的加密方式)、MEDIUM(加入部分已有部解法的 > 128 bits 長度的加密方式)、LOW(64、56 bits 長度的加密方式,OpenSSL 1.0.2g 已不在支援)

參考資料:
OpenSSL 官網 -- OpenSSL commands
https://www.openssl.org/docs/man1.0.2/man1/
https://www.openssl.org/docs/man1.1.1/man1/

補充1:關於 windows ciphers 的設定

群組原則
gpedit.msc => 本機電腦 原則 => 電腦設定 => 系統管理範本 => 網路 => SSL 組態設定 => SSL 加密套件順序
在這個空格中加入需要的加密套件,設定完成後下 gpupdate /force 重新套用所有原則

官方文件
Cipher Suites in TLS/SSL -- 文件中有說明關於 Cipher 名稱規則及各版本所預設支援的 Ciphers
http://go.microsoft.com/fwlink/?LinkId=517265

------------------------------------------------------------------------

補充2:關於 apache ciphers 的設定

ssh.conf 檔中以下三個設定值
SSLCipherSuite HIGH:!aNULL => 支援所有高強度加密,aNULL 除外。所有 ciphers 上的調寫在這裡,可以使用 +、- 及 !
SSLProtocol -ALL +TLSv1.2 => 不支援所有協定,但加上 TLSv1.2
SSLHonorCipherOrder on => 開啟 ciphers 排序

------------------------------------------------------------------------

補充3:關於 ssh ciphers 的設定

sshd_config 檔
Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128

註:CentOS8 Cipher 修改路徑
/etc/crypto-policies/back-ends/opensshserver.config

------------------------------------------------------------------------

補充4:關於 xrdp ciphers 的設定

/etc/xrdp/xrdp.ini 設定檔中
tls_ciphers=HIGH:!ADH:!AECDH 


使用 openssl 建立公私鑰

建立私鑰
openssl genrsa -aes128 -out ./private.key
建立使用 aes
註:這個方式會要求 pass phrase
openssl genrsa -out ./private.key 2048

建立公鑰
openssl rsa -in private.key -out public2.key -pubout -outform PEM

參考資料:mbzhong 大文章
https://www.gushiciku.cn/pl/pjap/zh-tw  


使用 openssl 產生 csr 檔

openssl req -new -key ./server.key -out ./server.csr


使用 openssl 查看憑證請求檔(csr)

openssl req -in server.csr -noout -text

參考資料:
ssorc大大文件
https://ssorc.tw/42/openssl-%E6%8C%87%E4%BB%A4/  


查看公鑰內容

openssl rsa -inform PEM -pubin -in public2.key -noout -text
查看公鑰的值(非經過 base64 加密過的資訊)


檢查私鑰

openssl rsa -in privateKey.key -check
註:這個方式可以檢查私鑰的格式是否正確,是否可用,與 cat 查看不同

參考資料:
SSLShopper 官方說明 -- 文中說明了產生、檢查、除錯、轉換四個常用指令方式
https://www.sslshopper.com/article-most-common-openssl-commands.html 


查看憑證內容

openssl x509 -in [憑證檔] -text -noout


使用 openssl 檢查公開網站憑證資訊

echo | openssl s_client -showcerts -servername test.tw -connect test.tw:443 2>/dev/null | openssl x509 -inform pem -noout -text

參考資料:

serverfault 討論 -- Pedro Perez 回應
https://serverfault.com/questions/661978/displaying-a-remote-ssl-certificate-details-using-cli-tools

某大大文章
https://officeguide.cc/linux-openssl-find-check-tls-ssl-certificate-expiry-date-tutorial-examples/ 


使用 openssl 檢查網站對 TLS 的支援情形

openssl s_client -connect www.google.com.tw:443 -tls1_2

參考資料:
ryan 口述 


openssl encode

echo -ne '\000username\000password' | openssl base64

參考資料:
Postfix 官網
http://www.postfix.org/SASL_README.html  


從 csr 檔中,將公鑰取出

CSR 檔中已包含公鑰資訊,那是否可以透過 CSR 檔取出公鑰?可以,用以下指令就可以取出公鑰
openssl req -in csr.txt -noout -pubkey -out publickey.pem

參考資料:
superuser 討論 -- Liam Dennehy 大回應
https://superuser.com/questions/1233571/how-to-export-public-key-from-certificate-signing-request  


公鑰與私鑰比對

當有多個私鑰時,有可能會發生公私鑰拿錯,造成無法解密,如果要比對公私鑰,可以用以下方式

方式一、使用 openssl 比對憑證中的公鑰及私鑰產生的公鑰檔 MD5 值是否相同
openssl x509 -noout -modulus -in <public.crt> | openssl md5 > /tmp/crt.pub
openssl rsa -noout -modulus -in <private.key> | openssl md5 > /tmp/key.pub
diff /tmp/crt.pub /tmp/key.pub

方式二、使用 ssh-keygen 產生公鑰檔並與公鑰檔進行比對,Michuelnik 大大的方式
PRIVKEY=id_rsa
TESTKEY=id_rsa.pub
diff <( ssh-keygen -y -e -f "$PRIVKEY" ) <( ssh-keygen -y -e -f "$TESTKEY" )

參考資料:
SUSE 官方說明
https://www.suse.com/support/kb/doc/?id=000018423

Michuelnik 大大回應
https://serverfault.com/questions/426394/how-to-check-if-an-rsa-public-private-key-pair-match

comodo 網站說明
https://support.comodo.com/index.php?/Knowledgebase/Article/View/684/17/how-do-i-verify-that-a-private-key-matches-a-certificate-openssl


檢查憑證內容

openssl s_client -connect 192.168.65.99:1234 -starttls ftp

這個會列出該 port 所綁的憑證資訊

資料來源:rudy 口述

留言

這個網誌中的熱門文章

zimbra 安裝、管理、設定

Fortigate 100D -- 管理、設定

IT 隨手記6