使用 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

在这种情况下,我们不使用密码短语来保护证书私钥。

创建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"

了解更多:修复:Windows 无法连接到组策略客户端服务

在网络服务器上,复制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路径选项指定包含允许的证书的文件夹的位置。