SSH proxy and command logs
Cloudflare Zero Trust supports SSH proxying and command logging using Secure Web Gateway and the WARP client.
You can create network policies to manage and monitor SSH access to your applications. When a device connects to your origin server over SSH, a session log will be generated showing which user connected, the session duration, and optionally a full replay of all commands run during the session.
- Install the WARP client on end-user devices.
- Install the Cloudflare root certificate on end-user devices.
Cloudflare Gateway will take the identity from a token and, using short-lived certificates, authorize the user on the target infrastructure.
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:
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
:
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:
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
:
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:
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
:
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
:
Since this will put the security of your server entirely dependent on your Access configuration, make sure your Access policies are correctly configured.
Instead of traditional SSH keys, Gateway uses short-lived certificates to authenticate traffic between Cloudflare and your origin.
To generate a Gateway SSH proxy CA and get its public key:
-
Create an API token with the following permissions:
Type Item Permission Account Access: SSH Auditing Edit -
If you have not yet generated a Cloudflare SSH CA, make a
POST
request to the Cloudflare API: -
If you have already created a Cloudflare SSH CA or receive the error message
access.api.error.gateway_ca_already_exists
, make aGET
request instead: -
Copy the
public_key
value returned in the response.
-
Use the following command to change directories to the SSH configuration directory on the remote target machine:
-
Once there, you can use the following command to both generate the file and open a text editor to input/paste the public key.
-
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. -
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:
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/ssh
directory on the remote machine, open thesshd_config
file. -
Go to the row named
PubkeyAuthentication
. In most default configurations, the row will appear commented out as follows: -
Remove the
#
symbol to uncomment the line; keep the settingyes
enabled. -
Next, add a new line below
PubkeyAuthentication
as follows:Save the file and quit the editor. You might need to use the following command again to save and exit.
Cloudflare’s SSH proxy only works with servers running on the default port 22. Open the sshd_config
file and verify that no other Port
values are specified.
Once you have modified your SSHD configuration, restart the SSH service on the remote machine.
For older Debian/Ubuntu versions:
For newer Debian/Ubuntu versions:
For CentOS/RHEL 6 and older:
For CentOS/RHEL 7 and newer:
-
In Zero Trust ↗, go to Gateway > Firewall policies.
-
In the Network tab, create a new network policy.
-
Name the policy and specify the Destination IP for your origin server.
You can enter either a public or private IP. To use a private IP, refer to Connect private networks.
-
Add any other conditions to your policy. If a user does not meet the criteria, they will be blocked by default.
-
In the Action dropdown, select Audit SSH.
-
(Optional) Enable SSH Command Logging. If you have not already uploaded an SSH encryption public key, follow the steps in Configure SSH Command Logging.
-
Save the policy.
Users can use any SSH client to connect to the target resource, as long as they are logged into the WARP client on their device. Cloudflare Zero Trust will authenticate, proxy, and optionally encrypt and record all SSH traffic through Gateway.
Users must specify their desired username to connect with as part of the SSH command:
To log SSH commands, you will need to generate an HPKE key pair and upload the public key to Cloudflare.
-
Download ↗ the Cloudflare
ssh-log-cli
utility. -
Using the
ssh-log-cli
utility, generate a public and private key pair.This command outputs two files, an
sshkey.pub
public key and a matchingsshkey
private key. -
In Zero Trust ↗, go to Settings > Network.
-
In SSH encryption public key, paste the contents of
sshkey.pub
and select Save. Note that this a different public key from theca.pub
file you used to configure the SSH server.
All proxied SSH commands are immediately encrypted using this public key. The matching private key is required to view logs.
-
In Zero Trust ↗, go to Logs > Gateway > SSH.
-
If you enabled the SSH Command Logging feature, you can Download a session’s command log.
-
To decrypt the log, follow the instructions in the SSH Logging CLI repository ↗. In the following example,
sshkey
is the private key that matches the public key uploaded to Cloudflare.This command outputs a
sshlog-decrypted.zip
file with the decrypted logs.
SSH Command Logging does not support SFTP since it cannot be inspected and logged.