| 1 |
.. _cedar-securingssh: |
| 2 |
|
| 3 |
Securing Password-less SSH Connections |
| 4 |
====================================== |
| 5 |
|
| 6 |
Cedar Backup relies on password-less public key SSH connections to make |
| 7 |
various parts of its backup process work. Password-less ``scp`` is used |
| 8 |
to stage files from remote clients to the master, and password-less |
| 9 |
``ssh`` is used to execute actions on managed clients. |
| 10 |
|
| 11 |
Normally, it is a good idea to avoid password-less SSH connections in |
| 12 |
favor of using an SSH agent. The SSH agent manages your SSH connections |
| 13 |
so that you don't need to type your passphrase over and over. You get |
| 14 |
most of the benefits of a password-less connection without the risk. |
| 15 |
Unfortunately, because Cedar Backup has to execute without human |
| 16 |
involvement (through a cron job), use of an agent really isn't feasable. |
| 17 |
We have to rely on true password-less public keys to give the master |
| 18 |
access to the client peers. |
| 19 |
|
| 20 |
Traditionally, Cedar Backup has relied on a “segmenting” strategy to |
| 21 |
minimize the risk. Although the backup typically runs as root --- so |
| 22 |
that all parts of the filesystem can be backed up --- we don't use the |
| 23 |
root user for network connections. Instead, we use a dedicated backup |
| 24 |
user on the master to initiate network connections, and dedicated users |
| 25 |
on each of the remote peers to accept network connections. |
| 26 |
|
| 27 |
With this strategy in place, an attacker with access to the backup user |
| 28 |
on the master (or even root access, really) can at best only get access |
| 29 |
to the backup user on the remote peers. We still concede a local attack |
| 30 |
vector, but at least that vector is restricted to an unprivileged user. |
| 31 |
|
| 32 |
Some Cedar Backup users may not be comfortable with this risk, and |
| 33 |
others may not be able to implement the segmentation strategy --- they |
| 34 |
simply may not have a way to create a login which is only used for |
| 35 |
backups. |
| 36 |
|
| 37 |
Fortunately, there is a solution. The SSH authorized keys file supports |
| 38 |
a way to put a “filter” in place on an SSH connection. This excerpt is |
| 39 |
from the AUTHORIZED_KEYS FILE FORMAT section of man 8 sshd: |
| 40 |
|
| 41 |
:: |
| 42 |
|
| 43 |
command="command" |
| 44 |
Specifies that the command is executed whenever this key is used for |
| 45 |
authentication. The command supplied by the user (if any) is ignored. The |
| 46 |
command is run on a pty if the client requests a pty; otherwise it is run |
| 47 |
without a tty. If an 8-bit clean channel is required, one must not request |
| 48 |
a pty or should specify no-pty. A quote may be included in the command by |
| 49 |
quoting it with a backslash. This option might be useful to restrict |
| 50 |
certain public keys to perform just a specific operation. An example might |
| 51 |
be a key that permits remote backups but nothing else. Note that the client |
| 52 |
may specify TCP and/or X11 forwarding unless they are explicitly prohibited. |
| 53 |
Note that this option applies to shell, command or subsystem execution. |
| 54 |
|
| 55 |
|
| 56 |
Essentially, this gives us a way to authenticate the commands that are |
| 57 |
being executed. We can either accept or reject commands, and we can even |
| 58 |
provide a readable error message for commands we reject. The filter is |
| 59 |
applied on the remote peer, to the key that provides the master access |
| 60 |
to the remote peer. |
| 61 |
|
| 62 |
So, let's imagine that we have two hosts: master “mickey”, and peer |
| 63 |
“minnie”. Here is the original ``~/.ssh/authorized_keys`` file for the |
| 64 |
backup user on minnie (remember, this is all on one line in the file): |
| 65 |
|
| 66 |
:: |
| 67 |
|
| 68 |
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAxw7EnqVULBFgPcut3WYp3MsSpVB9q9iZ+awek120391k;mm0c221=3=km |
| 69 |
=m=askdalkS82mlF7SusBTcXiCk1BGsg7axZ2sclgK+FfWV1Jm0/I9yo9FtAZ9U+MmpL901231asdkl;ai1-923ma9s=9= |
| 70 |
1-2341=-a0sd=-sa0=1z= backup@mickey |
| 71 |
|
| 72 |
|
| 73 |
This line is the public key that minnie can use to identify the backup |
| 74 |
user on mickey. Assuming that there is no passphrase on the private key |
| 75 |
back on mickey, the backup user on mickey can get direct access to |
| 76 |
minnie. |
| 77 |
|
| 78 |
To put the filter in place, we add a command option to the key, like |
| 79 |
this: |
| 80 |
|
| 81 |
:: |
| 82 |
|
| 83 |
command="/opt/backup/validate-backup" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAxw7EnqVULBFgPcut3WYp |
| 84 |
3MsSpVB9q9iZ+awek120391k;mm0c221=3=km=m=askdalkS82mlF7SusBTcXiCk1BGsg7axZ2sclgK+FfWV1Jm0/I9yo9F |
| 85 |
tAZ9U+MmpL901231asdkl;ai1-923ma9s=9=1-2341=-a0sd=-sa0=1z= backup@mickey |
| 86 |
|
| 87 |
|
| 88 |
Basically, the command option says that whenever this key is used to |
| 89 |
successfully initiate a connection, the ``/opt/backup/validate-backup`` |
| 90 |
command will be run *instead of* the real command that came over the SSH |
| 91 |
connection. Fortunately, the interface gives the command access to |
| 92 |
shell variables that can be used to invoke the original command |
| 93 |
if you want to. |
| 94 |
|
| 95 |
A very basic ``validate-backup`` script might look something like this: |
| 96 |
|
| 97 |
:: |
| 98 |
|
| 99 |
#!/bin/bash |
| 100 |
if [[ "${SSH_ORIGINAL_COMMAND}" == "ls -l" ]] ; then |
| 101 |
${SSH_ORIGINAL_COMMAND} |
| 102 |
else |
| 103 |
echo "Security policy does not allow command [${SSH_ORIGINAL_COMMAND}]." |
| 104 |
exit 1 |
| 105 |
fi |
| 106 |
|
| 107 |
|
| 108 |
This script allows exactly ``ls -l`` and nothing else. If the user attempts |
| 109 |
some other command, they get a nice error message telling them that their |
| 110 |
command has been disallowed. For remote commands executed over ``ssh``, the |
| 111 |
original command is exactly what the caller attempted to invoke. |
| 112 |
|
| 113 |
For remote copies using the legacy SCP protocol, the commands are either ``scp |
| 114 |
-f file`` (copy *from* the peer to the master) or ``scp -t file`` (copy *to* |
| 115 |
the peer from the master). When using the SFTP protocol, which is the default |
| 116 |
in Debian starting with bookworm, the command is simply ``/usr/lib/openssh/sftp-server``. |
| 117 |
As a result, this mechanism is really only useful if you force the legacy SCP |
| 118 |
protocol using ``scp -O``. |
| 119 |
|
| 120 |
If you want, you can see what command SSH thinks it is executing by |
| 121 |
using ``ssh -v`` or ``scp -O -v``. The command will be right at the top, |
| 122 |
something like this: |
| 123 |
|
| 124 |
:: |
| 125 |
|
| 126 |
Executing: program /usr/bin/ssh host mickey, user (unspecified), command scp -v -f .profile |
| 127 |
OpenSSH_4.3p2 Debian-9, OpenSSL 0.9.8c 05 Sep 2006 |
| 128 |
debug1: Reading configuration data /home/backup/.ssh/config |
| 129 |
debug1: Applying options for minnie |
| 130 |
debug1: Reading configuration data /etc/ssh/ssh_config |
| 131 |
debug1: Applying options for * |
| 132 |
debug2: ssh_connect: needpriv 0 |
| 133 |
|
| 134 |
|
| 135 |
Omit the ``-v`` and you have your command: ``scp -f .profile``. |
| 136 |
|
| 137 |
For a normal, non-managed setup, you need to allow the following |
| 138 |
commands, where ``/path/to/collect/`` is replaced with the real path to |
| 139 |
the collect directory on the remote peer: |
| 140 |
|
| 141 |
:: |
| 142 |
|
| 143 |
scp -f /path/to/collect/cback.collect |
| 144 |
scp -f /path/to/collect/* |
| 145 |
scp -t /path/to/collect/cback.stage |
| 146 |
|
| 147 |
|
| 148 |
If you are configuring a managed client, then you also need to list the |
| 149 |
exact command lines that the master will be invoking on the managed |
| 150 |
client. You are guaranteed that the master will invoke one action at a |
| 151 |
time, so if you list two lines per action (full and non-full) you should |
| 152 |
be fine. Here's an example for the collect action: |
| 153 |
|
| 154 |
:: |
| 155 |
|
| 156 |
/usr/bin/cback3 --full collect |
| 157 |
/usr/bin/cback3 collect |
| 158 |
|
| 159 |
Of course, you would have to list the actual path to the ``cback3`` |
| 160 |
executable --- exactly the one listed in the ``<cback_command>`` |
| 161 |
configuration option for your managed peer. |
| 162 |
|
| 163 |
Below is the script that I use for my own backups, to allow the |
| 164 |
master to stage files from each client. This is stored as |
| 165 |
``~/.ssh/validate-backup`` and is referenced in ``~/.ssh/authorized-keys`` |
| 166 |
as described above. |
| 167 |
|
| 168 |
:: |
| 169 |
|
| 170 |
# Since this script is specified as the command in ~/.ssh/authorized_keys, it |
| 171 |
# acts as a "filter" and prevents the backup user from doing anything except |
| 172 |
# specific Cedar Backup actions (stage, in this case, via scp). |
| 173 |
|
| 174 |
# See the AUTHORIZED_KEYS FILE FORMAT section in sshd(8) for more information. |
| 175 |
|
| 176 |
# As of Debian bookworm, the sshd server implements SCP over SFTP. That breaks |
| 177 |
# this filter, because the SSH command is just "/usr/lib/openssh/sftp-server". |
| 178 |
# The workaround is to use `scp -O` to force the old protocol. |
| 179 |
|
| 180 |
typeset -x COLLECTDIR=/data/backup/collect |
| 181 |
|
| 182 |
typeset -x CMD1="scp -f ${COLLECTDIR}/cback.collect" # check collect indicator |
| 183 |
typeset -x CMD2="scp -f ${COLLECTDIR}/*" # stage all files |
| 184 |
typeset -x CMD3="scp -t ${COLLECTDIR}/cback.stage" # write the stage indicator |
| 185 |
|
| 186 |
if [[ "${SSH_ORIGINAL_COMMAND}" == "${CMD1}" ]] ; then |
| 187 |
${SSH_ORIGINAL_COMMAND} |
| 188 |
elif [[ "${SSH_ORIGINAL_COMMAND}" == "${CMD2}" ]]; then |
| 189 |
${SSH_ORIGINAL_COMMAND} |
| 190 |
elif [[ "${SSH_ORIGINAL_COMMAND}" == "${CMD3}" ]]; then |
| 191 |
${SSH_ORIGINAL_COMMAND} |
| 192 |
else |
| 193 |
echo "Security policy does not allow command [${SSH_ORIGINAL_COMMAND}]." |
| 194 |
exit 1 |
| 195 |
fi |
| 196 |
|
| 197 |
---------- |
| 198 |
|
| 199 |
*Previous*: :doc:`recovering` • *Next*: :doc:`copyright` |
| 200 |
|
| 201 |
.. |note| image:: images/note.png |
| 202 |
.. |tip| image:: images/tip.png |
| 203 |
.. |warning| image:: images/warning.png |