SSHプロキシとコマンドログ
Cloudflare Zero Trustは、Secure Web GatewayとWARPクライアントを使用してSSHプロキシとコマンドログをサポートしています。
ネットワークポリシーを作成して、アプリケーションへのSSHアクセスを管理および監視できます。デバイスがSSHを介してオリジンサーバーに接続すると、どのユーザーが接続したか、セッションの期間、およびオプションでセッション中に実行されたすべてのコマンドの完全なリプレイを示すセッションログが生成されます。
- エンドユーザーのデバイスにWARPクライアントをインストールします。
- エンドユーザーのデバイスにCloudflareルート証明書をインストールします。
Cloudflare Gatewayはトークンからアイデンティティを取得し、短命の証明書を使用してターゲットインフラストラクチャ上のユーザーを認証します。
The simplest setup is one where a user’s Unix username matches their email address prefix. Issued short-lived certificates will be valid for the user’s email address prefix. For example, if a user in your Okta or GSuite organization is registered as jdoe@example.com, they would log in to the SSH server as jdoe.
For testing purposes, you can run the following command to generate a Unix user on the machine:
sudo adduser jdoeAdvanced setup: Differing usernames
SSH certificates include one or more principals in their signature which indicate the Unix usernames the certificate is allowed to log in as. Cloudflare Access will always set the principal to the user’s email address prefix. For example, when jdoe@example.com tries to connect, Access issues a short-lived certificate authorized for the principal jdoe.
By default, SSH servers authenticate the Unix username against the principals listed in the user’s certificate. You can configure your SSH server to accept principals that do not match the Unix username.
Username matches a different email
To allow jdoe@example.com to log in as the user johndoe, add the following to the server’s /etc/ssh/sshd_config:
Match user johndoe AuthorizedPrincipalsCommand /bin/echo 'jdoe' AuthorizedPrincipalsCommandUser nobodyThis tells the SSH server that, when someone tries to authenticate as the user johndoe, check their certificate for the principal jdoe. This would allow the user jdoe@example.com to sign into the server with a command such as:
ssh johndoe@serverUsername matches multiple emails
To allow multiple email addresses to log in as vmuser, add the following to the server’s /etc/ssh/sshd_config:
Match user vmuser AuthorizedPrincipalsFile /etc/ssh/vmusers-list.txtThis tells the SSH server to load a list of principles from a file. Then, in /etc/ssh/vmusers-list.txt, list the email prefixes that can log in as vmuser, one per line:
jdoebwaynerobinUsername matches all users
To allow any Access user to log in as vmuser, add the following command to the server’s /etc/ssh/sshd_config:
Match user vmuser AuthorizedPrincipalsCommand /bin/bash -c "echo '%t %k' | ssh-keygen -L -f - | grep -A1 Principals" AuthorizedPrincipalsCommandUser nobodyThis command takes the certificate presented by the user and authorizes whatever principal is listed on it.
Allow all users
To allow any Access user to log in with any username, add the following to the server’s /etc/ssh/sshd_config:
AuthorizedPrincipalsCommand /bin/bash -c "echo '%t %k' | ssh-keygen -L -f - | grep -A1 Principals"AuthorizedPrincipalsCommandUser nobodySince this will put the security of your server entirely dependent on your Access configuration, make sure your Access policies are correctly configured.
従来のSSHキーの代わりに、GatewayはCloudflareとオリジン間のトラフィックを認証するために短命の証明書を使用します。
Gateway SSHプロキシCAを生成し、その公開鍵を取得するには:
-
Cloudflare APIに対して、メールアドレスとAPIキーをリクエストヘッダーとして含む
POSTリクエストを行います。Terminal window curl --request POST \"https://api.cloudflare.com/client/v4/accounts/{account_id}/access/gateway_ca" \--header "X-Auth-Email: <EMAIL>" \--header "X-Auth-Key: <API_KEY>" -
成功レスポンスには
public_key値が含まれます。サーバーの設定用にキーを保存してください。
- ステップ2でAPIリクエストから返された
public_key値をコピーします。
-
Use the following command to change directories to the SSH configuration directory on the remote target machine:
Terminal window cd /etc/ssh -
Once there, you can use the following command to both generate the file and open a text editor to input/paste the public key.
Terminal window vim ca.pub -
In the
ca.pubfile, paste the public key without any modifications.The
ca.pubfile can hold multiple keys, listed one per line. Empty lines and comments starting with#are also allowed. -
Save the
ca.pubfile. In some systems, you may need to use the following command to force the file to save depending on your permissions:Terminal window :w !sudo tee %:q!
The following procedure makes two changes to the sshd_config file on the remote target machine. The first change requires that you uncomment a field already set in most default configurations; the second change adds a new field.
-
While staying within the
/etc/sshdirectory on the remote machine, open thesshd_configfile.Terminal window vim /etc/ssh/sshd_config -
Go to the row named
PubkeyAuthentication. In most default configurations, the row will appear commented out as follows:# PubkeyAuthentication yes -
Remove the
#symbol to uncomment the line; keep the settingyesenabled. -
Next, add a new line below
PubkeyAuthenticationas follows:TrustedUserCAKeys /etc/ssh/ca.pubSave the file and quit the editor. You might need to use the following command again to save and exit.
:w !sudo tee %:q!
CloudflareのSSHプロキシは、デフォルトのポート22で実行されているサーバーでのみ機能します。sshd_configファイルを開き、他のPort値が指定されていないことを確認します。
cat /etc/ssh/sshd_configOnce you have modified your SSHD configuration, restart the SSH service on the remote machine.
For older Debian/Ubuntu versions:
sudo service ssh restartFor newer Debian/Ubuntu versions:
sudo systemctl restart sshFor CentOS/RHEL 6 and older:
sudo service sshd restartFor CentOS/RHEL 7 and newer:
sudo systemctl restart sshd-
Zero Trust ↗に移動し、Gateway > Firewall Policiesに進みます。
-
Networkタブで、新しいネットワークポリシーを作成します。
-
ポリシーに名前を付け、オリジンサーバーのDestination IPを指定します。
公開IPまたはプライベートIPのいずれかを入力できます。プライベートIPを使用するには、プライベートネットワークを接続するを参照してください。
-
ポリシーに他の条件を追加します。ユーザーが基準を満たさない場合、デフォルトでブロックされます。
-
Actionドロップダウンで、_Audit SSH_を選択します。
-
(オプション)SSHコマンドログ記録を有効にします。SSH暗号化公開鍵をまだアップロードしていない場合は、SSHコマンドログ記録の設定の手順に従ってください。
-
ポリシーを保存します。
ユーザーは、デバイス上でWARPクライアントにログインしている限り、任意のSSHクライアントを使用してターゲットリソースに接続できます。Cloudflare Zero Trustは、すべてのSSHトラフィックをGatewayを通じて認証、プロキシ、およびオプションで暗号化および記録します。
ユーザーはSSHコマンドの一部として接続するために希望するユーザー名を指定する必要があります:
ssh <username>@<hostname>監査SSHポリシーでSSHコマンドログ記録を有効にした場合、HPKEキーのペアを生成し、公開鍵をダッシュボードにアップロードする必要があります。
-
Cloudflareの
ssh-log-cliユーティリティをダウンロード ↗します。 -
ssh-log-cliユーティリティを使用して、公開鍵と秘密鍵のペアを生成します。Terminal window ./ssh-log-cli generate-key-pair -o sshkeylsREADME.md ssh-log-cli sshkey sshkey.pubこのコマンドは、
sshkey.pubという公開鍵と対応するsshkeyという秘密鍵の2つのファイルを出力します。 -
Zero Trust ↗に移動し、Settings > Networkに進みます。
-
SSH暗号化公開鍵フィールドに
sshkey.pubの内容を貼り付け、保存を選択します。これは、オリジンサーバーの設定に使用したca.pubファイルとは異なる公開鍵です。
すべてのプロキシされたSSHコマンドは、この公開鍵を使用して即座に暗号化されます。対応する秘密鍵は、ログを表示するために必要です。
-
Zero Trustで、Logs > Gateway > SSHに移動します。
-
SSHコマンドログ記録機能を有効にしている場合、セッションのコマンドログをダウンロードできます。
-
ログを復号化するには、SSH Logging CLIリポジトリ ↗の指示に従ってください。以下の例では、SSHコマンドログ記録の設定で生成された秘密鍵を使用します:
Terminal window ./ssh-log-cli decrypt -i sshlog -k sshkeyこのコマンドは、復号化されたログを含む
sshlog-decrypted.zipファイルを出力します。
SSHコマンドログ記録は、SFTPをサポートしていません。SFTPは検査およびログ記録できないためです。