惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Help Net Security
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
Arctic Wolf
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 最新话题
Hacker News: Ask HN
Hacker News: Ask HN
The Last Watchdog
The Last Watchdog
Forbes - Security
Forbes - Security
SecWiki News
SecWiki News
H
Heimdal Security Blog
Latest news
Latest news
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Help Net Security
AWS News Blog
AWS News Blog
Cloudbric
Cloudbric
W
WeLiveSecurity
I
InfoQ
B
Blog RSS Feed
Webroot Blog
Webroot Blog
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
GbyAI
GbyAI
S
Security Affairs
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
T
Threatpost
罗磊的独立博客
L
LINUX DO - 热门话题
C
CXSECURITY Database RSS Feed - CXSecurity.com
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog

The Will Will Web

解密 macOS 視窗管理:從視窗縮放到虛擬空間的核心邏輯 - The Will Will Web Azure OpenAI Service API 設計演進:從部署導向到統一 v1 介面 - The Will Will Web 如何在 Windows 安裝和設定 Cloudflare Tunnel 服務 - The Will Will Web 如何使用 ssh-copy-id 快速設定 Linux 遠端主機的 SSH 免密碼登入 - The Will Will Web Windows 終端機入門操作手冊 - The Will Will Web 如何利用全新的 Trusted publishing 方法發佈 npm 套件 - The Will Will Web 如何正確對 WSL 2 的 ext4.vhdx 虛擬硬碟進行壓縮 - The Will Will Web 免費 GPU 就在 VS Code 裡 - Google Colab 官方擴充套件完整解析 - The Will Will Web 如何在 VS Code 搞定 .NET 主控台應用程式含執行參數的偵錯設定 - The Will Will Web 使用 Directory.Build.props 與 Directory.Build.targets 自動化 .NET 建置作業 - The Will Will Web uv 與 uvx 命令全攻略:Python 開發者的極速工具指南 - The Will Will Web 如何在 Ubuntu 22.04.5 LTS 更新 7-Zip 程式到最新版 - The Will Will Web 如何設定 Husky.Net 讓開發團隊確保一致的程式碼風格 - The Will Will Web 徹底重裝 Microsoft Teams 的方法 - The Will Will Web 開發 .NET 應用程式可利用 dotnet format 建立一致的程式碼風格 - The Will Will Web
如何透過批次檔批次將 PFX 憑證的私密金鑰匯出並重新打包新的 PFX 檔案 - The Will Will Web
Will Huang · 2025-09-18 · via The Will Will Web

今天有客戶希望我們重新提供給他們一次 PFX 憑證檔案,所以我寫了一支批次檔專門用來匯出先前的 PFX 金鑰,並且再重新將 TWCA 簽發的 server.cer 憑證搭配原始金鑰再匯出一份新的 PFX 金鑰與設定強式密碼給他們。這篇文章我就來分享今天撰寫的腳本。

Image

批次檔程式碼分享

以下兩個命令只要設定好密碼後,就可以一次執行到底,不用人工介入輸入密碼:

  1. 快速取得 PFX 金鑰檔中的私密金鑰

    @ECHO OFF
    
    if not exist "TEMP" mkdir "TEMP"
    cd TEMP
    
    COPY ..\example.com.pfx .
    
    REM 設定 PFX 的匯出密碼
    set PFX_PASS=VeryStrongPassword
    
    REM 將 example.com.pfx 解出 example.com-key.pem 這把私密金鑰
    openssl pkcs12 -in example.com.pfx -nocerts -out example.com-key.pem -nodes -passin env:PFX_PASS
    
    set PFX_PASS=
    
  2. 快速將私密金鑰與憑證合併成新的 PFX 金鑰檔

    @ECHO OFF
    
    if not exist "TEMP" mkdir "TEMP"
    cd TEMP
    
    REM https://delinea.com/resources/password-generator-it-tool
    set PFX_PASS=VeryStrongPassword
    
    if exist "example.com.pfx" del /f /q "example.com.pfx"
    if exist "example.com.cer" del /f /q "example.com.cer"
    
    REM 解壓縮 TWCA 提供給我們的壓縮檔(僅含伺服器憑證、中繼憑證、根憑證,不含私密金鑰)
    unzip -o ..\example.com-cert.zip
    
    REM TWCA 提供的金鑰檔案一律為 server.cer
    REN server.cer example.com.cer
    
    REM 匯出新的 PFX 金鑰檔
    openssl pkcs12 -export -inkey example.com-key.pem -in example.com.cer -out example.com.pfx -passout env:PFX_PASS
    
    REM 輸出一個包含 PFX 憑證匯出密碼的文字檔 (透過 powershell 是為了不要輸出斷行字元)
    powershell -NoProfile -Command "Set-Content -LiteralPath '憑證密碼.txt' -Value $env:PFX_PASS -NoNewline"
    
    
    REM 設定一組 7-zip 壓縮檔案時的「解壓密碼」
    set ZIP_PASS=AnotherVeryStrongPassword
    
    7z a -tzip "example.com-pfx-with-pw.zip" *.pfx *.txt -r -p%ZIP_PASS% -y
    
    set PFX_PASS=
    set ZIP_PASS=
    

然後,分別用不同的管道將「壓縮檔」與「解壓密碼」傳遞給客戶,避免郵件攔截攻擊:

  1. 僅包含強式加密的「壓縮檔」,內含 PFX 憑證與匯出密碼
  2. 僅包含強式加密的壓縮檔的「解壓密碼」

為什麼要分別寄出兩封分開的郵件給客戶?

我經常會看到銀行業客戶在寄送機密郵件時,會刻意的拆成兩封郵件寄出,一封是郵件本身並且包含一份加密的附件,另一封則是開啟的密碼,這兩封郵件經常是一起寄送的,我第一次看到時還會覺得有點奇怪,這樣真的有比較安全嗎?😅

其實,加密壓縮檔解壓縮密碼經常分兩封郵件送出,主要是基於資安考量,希望避免單一郵件被攔截後,攻擊者能夠同時取得壓縮檔與密碼。雖然這兩封郵件多半會寄給同一個收件人,但分開寄送是嘗試降低因單一管道漏洩所帶來的安全風險。

不過,如果兩封郵件都透過相同的電子郵件系統或路徑送出,一旦該郵件帳號或伺服器被入侵,攻擊者仍可能同時獲得壓縮檔和密碼,因此此作法並非絕對安全,而是提升了攻擊者的攔截難度。更安全的做法是利用第二管道 (例如電話、簡訊或即時通訊工具) 傳送密碼,或採用更完善的加密和傳輸管道保護!👍