Stunnelを使用してWindowsのクライアントサーバーアプリトラフィックを暗号化

スタンネルツールは、暗号化自体をサポートしていないクライアントサーバーネットワークアプリ向けの安全なTLSトンネルを作成するプロキシサービスとして使用できます。場合によっては、このツールを使用して、フル機能のVPNソリューションを実装するよりも、単一のアプリ(サービス)へのリモートアクセスを保護することが望ましいです。
スタンネル
サーバーモードまたはクライアントモードでサービスを実行できます。クライアントモードでは、Stunnelはクライアントアプリからトラフィックを受け取り、暗号化してからサーバーに送信します。トラフィックが復号化されます
スタンネルサーバー側
そして、ターゲットアプリまたはサービスに送信されます。重要なことは、管理者です
アプリのクライアントまたはサーバー部分を変更する必要はありません。
証明書
クライアント認証に使用できます。 Stunnelは、WindowsとLinuxの両方でサポートされています。
Stunnelを使用して、クライアントとサーバーの間に安全なアクセスを作成する方法を見てみましょう。この例では、サーバーは、暗号化されていないHTTPサイトを実行しているIIS Webサーバーを備えたWindowsホストです。タスクは、証明書認証を使用してクライアントからこのWebサイトへのアクセスを制限し、トラフィックの暗号化を有効にすることです。
WindowsでStunnel Serverの構成
https://www.stunnel.org/downloads.htmlからWindows用のスタンネルインストーラーをダウンロードし、「openssl」を含むデフォルト設定でインストールします。

次のステップは、CA、サーバー、クライアントのキーと証明書を生成することです。コマンドプロンプトを開き、ディレクトリに移動します。
cd" c:\プログラムファイル(x86)\ stunnel \ bin "
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 "
簡単に識別できるように、SUBJフィールドの証明書に関する情報を追加します。
サーバーの秘密鍵を作成します。
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-createserial-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-createSerial-out client.crt-days 365-subj"/o = woshubltd/ou = it/cn=client1\_webserver1.com ""
Webサーバーで、ca.crt、server.crt、およびserver.keyファイルを c:\ programファイル(x86)\ stunnel \ config folderにコピーします。

stunnel.confファイル(デフォルト設定をクリアできます)を編集し、次の構成を追加します。
; Write logs to stunnel.logdebug = infooutput = 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_PREFERENCEoptions = NO_SSLv2options = NO_SSLv3options = NO_TLSv1sslVersion = TLSv1.2sslVersion = TLSv1.3ciphers = ECDHE-RSA-AES256-GCM-SHA384; names (paths) to certificate filescert = server.crtkey = server.keyCAfile = 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 Defenderファイアウォールで指定されたポートを開き、着信接続を可能にします。 PowerShellでファイアウォールルールを作成できます。
new-netfirewallrule-displayname" itporal\_stunnel\_443 "-direction inbound-localport 443-protocol tcp-action avel
ルーターまたはファイアウォールレベルで、安全でないTCPポート80への外部接続をブロックします。
stunnel.exeを実行し、GUIログをチェックして、構成にエラーが含まれていないことを確認します。グラフィカルインターフェイスを使用すると、構成ファイルをすばやく再読み込み、すぐにエラーログを表示します。これにより、デバッグが簡単になります。
1 
メニューで終了してStunnel.exe GUIを閉じてから、Windowsサービスとしてスタンネルを開始します。コマンドを実行します:
" c:\ program files(x86)\ stunnel \ bin \ stunnel.exe "-install" c:\プログラムファイル(x86)\ stunnel \ config \ stunnel.conf "
スタンネルTLSラッパーサービスが作成されます。開始:
スタートサービスラッパー
スタンネルプロセスは、開始したらポート443に耳を傾けます。

Windowsのスタンネルクライアント構成の例
次に、クライアントWindowsデバイスに同じ分布からスタンネルをインストールします。次に、サーバーからc:\プログラムファイル(x86)\ stunnel \ config `フォルダーにca.crt、client.crt、およびclient.keyファイルをコピーします。
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 toconnect = 192.168.158.144:443; certificate pathsCAfile = ca.crtcert = client.crtkey = client.key; Certificates must be checked explicitly when establishing a connection.verify=2
構成ファイルを保存します。まず、スタンネルを手動で実行し、ログのエラーを確認します。これで、アドレス「localhost:8080」にアクセスすると、ブラウザから、スタンネルはリモートサーバーへの接続をリダイレクトします。

便宜上、証明書とキーを単一のファイルに組み合わせることができます。例えば:
get-content client.key、client.crt | Set-Content Client.pem
この場合、スタンネル構成ファイルで以下を指定します。
cert = client1.pem
すべてが適切に機能している場合は、サービスとしてクライアントでスタンネルを実行できます。
stunnel.exe-install

証明書を取り消すには(たとえば、妥協したもの)、Stunnel Server構成にCRLPATHオプションを追加します。 PEM形式で取り消された証明書(証明書の取り消しリスト)が保存されているフォルダーへのパスを指定します。
Capathオプションを使用して、許可された証明書を含むフォルダーの場所を指定することもできます。
*÷ソースリンク:
https://www.stunnel.org/downloads.html、PowerShellでファイアウォールルールを作成します。