Generate one-time SSH passwords with Vault
In a distributed cloud environment, tenant, and system is increasingly important part of online security. If an attacker gains access to your virtual machines, they can get control of most running applications, local data as well as its connected machines and systems.
The Vault SSH secrets engine provides secure authentication and authorization for access to machines via the SSH protocol. It supports signed SSH certificate and one-time SSH password modes. This tutorial demonstrates the one-time SSH password mode.
Personas
The end-to-end scenario described in this tutorial involves two personas:
operations
with privileged permissions to setup SSH secrets engineclient
trusted entity to request SSH OTP from Vault
Challenge
SSH servers provide a range of authentication methods, but most use password based authentication by default. If any user on the system has a fairly weak password, an attacker can more readily determine the password and create an unauthorized SSH connection.
Solution
Vault can create a one-time password (OTP) for SSH authentication on a network every time a client wants to SSH into a remote host using a helper command on the remote host to perform verification.
An authenticated client requests an OTP from the Vault server. If the client is authorized, Vault issues and returns an OTP. The client uses this OTP during the SSH authentication to connect to the desired target host.
When the client establishes an SSH connection, the OTP is received by the Vault helper which validates the OTP with the Vault server. The Vault server then deletes this OTP, ensuring that it is only used once.
Since the Vault server is contacted during SSH connection establishment, every login attempt and the correlating Vault lease information can be logged to an audit device.
Note
Vault SSH Helper version 0.1.6 and higher supports the Vault Enterprise namespaces feature.
Launch Terminal
This tutorial includes a free interactive command-line lab that lets you follow along on actual cloud infrastructure.
Prerequisites
This lab was tested on macOS using an x86_64 based processor. If you are running macOS on an Apple silicon-based processor, use a x86_64 based Linux virtual machine in your preferred cloud provider.
To perform the tasks described in this tutorial, you need to have:
- HCP or Vault Community Edition environment
- Vagrant installed with a configured provider such as VirtualBox
- jq installed
Policy requirements
Note
For the purpose of this tutorial, you can use root
token to work
with Vault. However, it is recommended that root tokens are only used for
initial setup or in emergencies. As a best practice, use tokens with
appropriate set of policies based on your role in the organization.
To perform all tasks demonstrated in this tutorial, your policy must include the following permissions:
If you are not familiar with policies, complete the policies tutorial.
Lab setup
Start Vault
Note
If you do not have access to an HCP Vault Dedicated cluster, visit the Create a Vault Cluster on HCP tutorial.
Launch the HCP Portal and login.
Click Vault in the left navigation pane.
In the Vault clusters pane, click vault-cluster.
Under Cluster URLs, click Public Cluster URL.
In a terminal, set the
VAULT_ADDR
environment variable to the copied address.Return to the Overview page and click Generate token.
Within a few moments, a new token will be generated.
Copy the Admin Token.
Return to the terminal and set the
VAULT_TOKEN
environment variable.Set the
VAULT_NAMESPACE
environment variable toadmin
.The
admin
namespace is the top-level namespace automatically created by HCP Vault. All CLI operations default to use the namespace defined in this environment variable.Type
vault status
to verify your connectivity to the Vault cluster.
The Vault Dedicated server is ready.
Start Ubuntu
Open another terminal session and initialize a new Ubuntu Vagrant box.
To successfully complete this tutorial, your Ubuntu box needs to be on the same network as your workstation. Edit the
Vagrantfile
to connect to your local network.Start the Ubuntu box.
When prompted, select the number that corresponds to your active network interface. For example, if you are connected to your local network via your Wi-Fi (Wireless) adapter, enter the number 1 and press enter.
Example output:
You are ready to proceed with the tutorial.
Setup the SSH secrets engine
(Persona: operations)
On the Vault server, you must enable the SSH secrets engine and create a role.
Return to the terminal session where you created the
VAULT_ADDR
environment variable.Enable the SSH secrets engine.
Create a role named
otp_key_role
withkey_type
set tootp
.This role defaults to creating credentials for the
vagrant
user and will allow all remote hosts whose IP addresses fit within this CIDR range.
Security: The tutorial uses a very permissive cidr_list
value of
0.0.0.0/0
. In production, we recommend that roles are defined with a range
that is granular to the range of remote hosts. We also recommend that you create
one role for each username to ensure isolation between usernames.
Setup the client authentication
(Persona: operations)
On the Vault server, you must create a policy to allow access to the SSH OTP role and then attach the policy to an authentication method.
Create a policy file named,
test.hcl
, that provides access to thessh/creds/otp_key_role
path.The path
ssh/creds/otp_key_role
is the path to the role created in the Setup the SSH secrets engine section.Create a policy named
test
with the policy defined intest.hcl
.Enable the
userpass
auth method.Create a user named
ubuntu
with the password "training" assigned thetest
policy.
Install vault-ssh-helper
(Persona: operations)
SELinux users
If you use SELinux in enforcing mode, you need to define and enable a module that allows vault-ssh-helper
to open and write log files, and communicate with the Vault server. Creating and enabling the SELinux module is beyond the scope of this tutorial. If you do not require SELinux enforcement, then operating SELinux in permissive mode also allows vault-ssh-helper
to function.
On each Remote host, you must:
- Install vault-ssh-helper
- Create a configuration file for the vault-ssh-helper
- Modify both the Pluggable Authentication Module (PAM) sshd configuration file and the sshd configuration file
- Restart the sshd service
Note
The example scenario in this tutorial uses the Ubuntu Linux distribution. If you use a different distribution, you will need to update paths and file locations to match your distribution.
Return to the terminal where you performed
vagrant up
and connect to Ubuntu.Download and install version
0.2.1
ofvault-ssh-helper
from releases.hashicorp.com.Unzip the binary from the archive into
/usr/local/bin
.Set the
vault-ssh-helper
binary's permissions to executable.Set the
vault-ssh-helper
binary's user and group toroot
.Create a directory to store the configuration file.
The Vault SSH Helper reads a configuration at the path
/etc/vault-ssh-helper.d/config.hcl
with this format:The
vault_addr
is the network address of the Vault server configured to generate the OTP.tls_skip_verify
enables or disables TLS verification.ca_cert
is the path to the PEM-encoded CA certificate files used to verify the Vault server's TLS certificate. Whenvault-ssh-helper
is run with the-dev
flag this is ignored.ssh_mount_point
is the Vault server path where the SSH secrets engine is enabled.namespace
is the namespace of the SSH mount point (Vault Enterprise and Vault Dedicated)allowed_roles
defines all*
or a comma-separated list of allowed roles defined in the SSH secrets engines.Refer to the documentation for the entire list of configuration properties.
Return to the terminal session where you created the
VAULT_ADDR
environment variable.Retrieve and copy the value for
VAULT_ADDR
.Return to the terminal connected to the Ubuntu box.
Create a variable named
VAULT_EXTERNAL_ADDR
that contains the Vault server's external network address copied in the previous step.Example:
Create a Vault SSH Helper configuration file
/etc/vault-ssh-helper.d/config.hcl
.Backup the original PAM sshd configuration configuration file.
Open the file in your preferred text editor.
The
common-auth
must be commented out or removed to disable the standard Unix authentication and replaced with authentication throughvault-ssh-helper
. Finally, a workaround for a bug that exists with some versions ofpam_exec.so
must also be included.Refer to the documentation for details about these parameter settings.
Example:
The
vault-ssh-helper
is configured to load the configuration fileconfig.hcl
with-config
parameter and while running it logs output to/var/log/vault-ssh.log
using the-log
parameter.Backup the original
sshd_config
configuration file.Modify the sshd configuration file.
Add or set the following:
This enables the keyboard-interactive authentication and PAM authentication modules. The password authentication is disabled.
Note
Older version of Ubuntu use
ChallengeResponseAuthentication
instead ofKbdInteractiveAuthentication
.Caution: You should ensure that there is another means of access to the server (such as a previously established SSH key for the root user) when disabling password authentication so that you are not prevented from logging in at all.
Restart the
sshd
service.Verify the configuration.
Note
These steps must be performed on all remote hosts.
Determine the IP address of the Ubuntu box.
Note
By following this tutorial, you should have two
eth
adapters. Theeth0
adapter represents the internal VirtualBox network and theeth1
adapter represents the bridged connection to your local network.Copy the IP address for
eth1
.Disconnect from Ubuntu.
Generate an OTP
(Persona: client)
A client configured to target a Vault server may now authenticate with the Vault server, generate an OTP and then use that OTP to establish an SSH connection.
Return to the terminal configured with the
VAULT_ADDR
environment variable.Create a variable named
REMOTE_HOST_IP
that stores the Ubuntu box's IP address.Example:
Authenticate with Vault using the
userpass
auth method with the usernameubuntu
and passwordtraining
. Store the token in an environment variable.Generate an OTP, through the
otp_key_role
, for remote host given its IP address.Example Output:
The output displays a
key
. Its value is the OTP to use during SSH authentication.
Initiate an SSH session
Connect to the Ubunut box as the
vagrant
user and the one time password provided by Vault.Note
To demonstrate one time passwords, include the
PubkeyAuthentication=no
parameter to ensure your native SSH client does include any cached SSH keys.When prompted enter
yes
to accept the fingerprint and for aPassword:
enter the OTP key from the previous step.Example:
You are now connected to the Ubuntu box using the one time password from Vault.
Cleanup
Disconnect from the Ubuntu box.
Switch to the terminal where you performed
vagrant up
and destroy the Ubuntu box.Stop the Vault dev mode server or delete your Vault Dedicated server.