コンテンツにスキップ

SSHプロキシとコマンドログ

Cloudflare Zero Trustは、Secure Web GatewayとWARPクライアントを使用してSSHプロキシとコマンドログをサポートしています。

ネットワークポリシーを作成して、アプリケーションへのSSHアクセスを管理および監視できます。デバイスがSSHを介してオリジンサーバーに接続すると、どのユーザーが接続したか、セッションの期間、およびオプションでセッション中に実行されたすべてのコマンドの完全なリプレイを示すセッションログが生成されます。

前提条件

1. Unixユーザー名がユーザーSSOアイデンティティと一致することを確認する

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:

Terminal window
sudo adduser jdoe

Advanced 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 nobody

This 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:

Terminal window
ssh johndoe@server

Username 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.txt

This 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:

jdoe
bwayne
robin

Username 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 nobody

This 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 nobody

Since this will put the security of your server entirely dependent on your Access configuration, make sure your Access policies are correctly configured.

2. Gateway SSHプロキシCAを生成する

従来のSSHキーの代わりに、GatewayはCloudflareとオリジン間のトラフィックを認証するために短命の証明書を使用します。

Gateway SSHプロキシCAを生成し、その公開鍵を取得するには:

  1. 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>"
  2. 成功レスポンスにはpublic_key値が含まれます。サーバーの設定用にキーを保存してください。

3. 公開鍵を保存する

  1. ステップ2でAPIリクエストから返されたpublic_key値をコピーします。
  1. Use the following command to change directories to the SSH configuration directory on the remote target machine:

    Terminal window
    cd /etc/ssh
  2. 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
  3. In the ca.pub file, paste the public key without any modifications.

    The ca.pub file can hold multiple keys, listed one per line. Empty lines and comments starting with # are also allowed.

  4. Save the ca.pub file. 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!

4. SSHD設定を変更する

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.

  1. While staying within the /etc/ssh directory on the remote machine, open the sshd_config file.

    Terminal window
    vim /etc/ssh/sshd_config
  2. Go to the row named PubkeyAuthentication. In most default configurations, the row will appear commented out as follows:

    # PubkeyAuthentication yes
  3. Remove the # symbol to uncomment the line; keep the setting yes enabled.

  4. Next, add a new line below PubkeyAuthentication as follows:

    TrustedUserCAKeys /etc/ssh/ca.pub

    Save the file and quit the editor. You might need to use the following command again to save and exit.

    :w !sudo tee %
    :q!

5. SSHポート番号を確認する

CloudflareのSSHプロキシは、デフォルトのポート22で実行されているサーバーでのみ機能します。sshd_configファイルを開き、他のPort値が指定されていないことを確認します。

Terminal window
cat /etc/ssh/sshd_config

6. SSHサーバーを再起動する

Once you have modified your SSHD configuration, restart the SSH service on the remote machine.

Debian/Ubuntu

For older Debian/Ubuntu versions:

Terminal window
sudo service ssh restart

For newer Debian/Ubuntu versions:

Terminal window
sudo systemctl restart ssh

CentOS/RHEL

For CentOS/RHEL 6 and older:

Terminal window
sudo service sshd restart

For CentOS/RHEL 7 and newer:

Terminal window
sudo systemctl restart sshd

7. 監査SSHポリシーを作成する

  1. Zero Trustに移動し、Gateway > Firewall Policiesに進みます。

  2. Networkタブで、新しいネットワークポリシーを作成します。

  3. ポリシーに名前を付け、オリジンサーバーのDestination IPを指定します。

    公開IPまたはプライベートIPのいずれかを入力できます。プライベートIPを使用するには、プライベートネットワークを接続するを参照してください。

  4. ポリシーに他の条件を追加します。ユーザーが基準を満たさない場合、デフォルトでブロックされます。

  5. Actionドロップダウンで、_Audit SSH_を選択します。

  6. (オプション)SSHコマンドログ記録を有効にします。SSH暗号化公開鍵をまだアップロードしていない場合は、SSHコマンドログ記録の設定の手順に従ってください。

  7. ポリシーを保存します。

8. ユーザーとして接続する

ユーザーは、デバイス上でWARPクライアントにログインしている限り、任意のSSHクライアントを使用してターゲットリソースに接続できます。Cloudflare Zero Trustは、すべてのSSHトラフィックをGatewayを通じて認証、プロキシ、およびオプションで暗号化および記録します。

ユーザーはSSHコマンドの一部として接続するために希望するユーザー名を指定する必要があります:

Terminal window
ssh <username>@<hostname>

(オプション)SSHコマンドログ記録を設定する

監査SSHポリシーSSHコマンドログ記録を有効にした場合、HPKEキーのペアを生成し、公開鍵をダッシュボードにアップロードする必要があります。

  1. Cloudflareのssh-log-cliユーティリティをダウンロードします。

  2. ssh-log-cliユーティリティを使用して、公開鍵と秘密鍵のペアを生成します。

    Terminal window
    ./ssh-log-cli generate-key-pair -o sshkey
    ls
    README.md ssh-log-cli sshkey sshkey.pub

    このコマンドは、sshkey.pubという公開鍵と対応するsshkeyという秘密鍵の2つのファイルを出力します。

  3. Zero Trustに移動し、Settings > Networkに進みます。

  4. SSH暗号化公開鍵フィールドにsshkey.pubの内容を貼り付け、保存を選択します。これは、オリジンサーバーの設定に使用したca.pubファイルとは異なる公開鍵です。

すべてのプロキシされたSSHコマンドは、この公開鍵を使用して即座に暗号化されます。対応する秘密鍵は、ログを表示するために必要です。

SSHログを表示する

  1. Zero Trustで、Logs > Gateway > SSHに移動します。

  2. SSHコマンドログ記録機能を有効にしている場合、セッションのコマンドログをダウンロードできます。

  3. ログを復号化するには、SSH Logging CLIリポジトリの指示に従ってください。以下の例では、SSHコマンドログ記録の設定で生成された秘密鍵を使用します:

    Terminal window
    ./ssh-log-cli decrypt -i sshlog -k sshkey

    このコマンドは、復号化されたログを含むsshlog-decrypted.zipファイルを出力します。

制限事項

SSHコマンドログ記録は、SFTPをサポートしていません。SFTPは検査およびログ記録できないためです。