使用 Stunnel 加密 Windows 上的任何客戶端-服務器應用程序流量

Jacki

隧道該工具可用作代理服務,為本身不支持加密的客戶端-服務器網絡應用程序創建安全的 TLS 隧道。在某些情況下,最好使用此工具來保護對單個應用程序(服務)的遠程訪問,而不是實施全功能的 VPN 解決方案。

隧道服務可以在服務器或客戶端模式下運行。在客戶端模式下,Stunnel 接收來自客戶端應用程序的流量,對其進行加密,然後將其發送到服務器。流量被解密隧道服務器端然後發送到目標應用程序或服務。重要的是管理員不需要修改應用程序的客戶端或服務器部分證書可用於客戶端身份驗證。 Windows 和 Linux 均支持 Stunnel。

讓我們看看如何使用 stunnel 在客戶端和服務器之間創建安全訪問。在此示例中,服務器是一台 Windows 主機,帶有運行未加密 HTTP 站點的 IIS Web 服務器。任務是通過證書身份驗證限制客戶端訪問該網站並啟用流量加密。

從以下地址下載適用於 Windows 的 Stunnel 安裝程序https://www.stunnel.org/downloads.html並使用默認設置安裝它,包括openssl

下一步是為 CA、服務器和客戶端生成密鑰和證書。打開命令提示符並導航到目錄:

cd "c:Program Files (x86)stunnelbin"

生成 CA 密鑰:

openssl genpkey -algorithm RSA -out ca.key

閱讀更多:網絡監視器:捕獲和分析 Windows 上的網絡流量

在這種情況下,我們不使用密碼短語來保護證書私鑰。

創建CA證書:

openssl req -new -x509 -key ca.key -out ca.crt -subj "/O=woshubLTD/OU=IT/CN=CA_webserver1.com"

添加有關證書的信息主題字段以便於識別。

為服務器創建私鑰:

openssl genpkey -algorithm RSA -out server.key

創建證書籤名請求 (CSR):

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

使用根 CA 簽署服務器證書。

openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365 -subj "/O=woshubLTD/OU=IT/CN=server_webserver1.com"

現在,為客戶端創建私鑰:

openssl genpkey -algorithm RSA -out client.key

生成客戶端證書請求:

openssl req -key client.key -new -out client.csr

簽署客戶端證書:

openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 365 -subj "/O=woshubLTD/OU=IT/CN=client1_webserver1.com"

在網絡服務器上,複製ca.crt, 服務器.crt, 和服務器密鑰文件到C:Program Files (x86)stunnelconfig文件夾。

編輯stunnel.conf文件(可以清除默認設置)並添加以下配置:

; Write logs to stunnel.log
debug = info
output = stunnel.log
; Strong encryption settings. We assume that both devices have modern CPUs that support AES hardware acceleration. If such encryption settings cause server performance degradation under heavy traffic, you can simplify them.
options = CIPHER_SERVER_PREFERENCE
options = NO_SSLv2
options = NO_SSLv3
options = NO_TLSv1
sslVersion = TLSv1.2
sslVersion = TLSv1.3
ciphers = ECDHE-RSA-AES256-GCM-SHA384
; names (paths) to certificate files
cert = server.crt
key = server.key
CAfile = ca.crt
; This section contains the configuration of the service that the client will access via Stunnel.
[ITPoral]
; This is the IP address and port on which the Stunnel instance should listen for connections.
accept = 192.168.158.144:443
; or accept = 443
; The IP address and port of the service to which the connection should be redirected. In our case, this is a local HTTP site.
connect = 127.0.0.1:80
; or connect = 80
; Always check the remote computer's client certificate. Clients without a certificate will not be able to connect to the service.
verify=2

確保接受行包含未被其他 Windows 進程使用的端口號。

在 Windows Defender 防火牆中打開指定端口以允許傳入連接。您可以使用 PowerShell 創建防火牆規則。

New-NetFirewallRule -DisplayName "ITPoral_stunnel_443" -Direction Inbound -LocalPort 443 -Protocol TCP -Action Allow

在路由器或防火牆級別阻止到不安全 TCP 端口 80 的外部連接。

跑步Stunnel.exe並檢查 GUI 日誌以確保您的配置不包含錯誤。圖形界面允許快速重新讀取配置文件並立即查看錯誤日誌,這使得調試更加容易。

1

單擊菜單中的“終止”關閉 stunnel.exe GUI,然後將 stunnel 作為 Windows 服務啟動。運行命令:

"C:Program Files (x86)stunnelbinstunnel.exe" -install "C:Program Files (x86)stunnelconfigstunnel.conf"

Stunnel TLS 包裝器將創建服務。啟動它:

Start-Service wrapper

stunnel 進程啟動後會偵聽端口 443。

Windows 上的 Stunnel 客戶端配置示例

然後,在同一發行版上安裝 stunnel客戶端 Windows 設備。然後復制約crt,客戶端.crt, 和客戶端密鑰文件從服務器到C:Program Files (x86)stunnelconfig文件夾。

將以下內容添加到 stunnel.conf 配置文件中:

[ITPoral]
; Run Stunnel in the client mode.
client = yes
; Specify the IP address and TCP port through which your service will be accessible to your clients.
accept = localhost:8080
; the address of the stunnel server to redirect connections to
connect = 192.168.158.144:443
; certificate paths
CAfile = ca.crt
cert = client.crt
key = client.key
; Certificates must be checked explicitly when establishing a connection.
verify=2

保存配置文件。首先,手動運行 Stunnel 並檢查日誌是否有錯誤。現在,當您訪問該地址時localhost:8080從瀏覽器中,Stunnel 會將連接重定向到遠程服務器。

為了方便起見,您可以將證書和密鑰合併到一個文件中。例如:

Get-Content client.key, client.crt | Set-Content client.pem

在這種情況下,只需在 stunnel 配置文件中指定以下內容:

cert = client1.pem

如果一切正常,您可以在客戶端上將 stunnel 作為服務運行。

stunnel.exe -install

要撤銷證書(例如,受損的證書),請添加CRL路徑stunnel 服務器配置的選項。指定存儲已吊銷的 PEM 格式證書(證書吊銷列表)的文件夾路徑。

您還可以使用CA路徑選項指定包含允許的證書的文件夾的位置。