From b15afcf7ca20238a71d5235531d47fe8d67eedaa Mon Sep 17 00:00:00 2001 From: Brice Waegeneire Date: Wed, 16 Aug 2023 13:42:22 +0200 Subject: [PATCH] ssh: Ajout documentation limitations des commandes --- HowtoOpenSSH.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/HowtoOpenSSH.md b/HowtoOpenSSH.md index ee59e302..aad761cb 100644 --- a/HowtoOpenSSH.md +++ b/HowtoOpenSSH.md @@ -467,6 +467,53 @@ On peut mettre des restrictions d'accès via le fichier `.ssh/authorized_keys`, no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa XXXXX commentaires ~~~ +#### Limitations des commandes autorisées + +Il est possible de limiter une clée à une commande spécifique en le déclarant dans le fichier `.ssh/authorized_keys` : + +~~~ +command="cat /etc/os-release" ssh-ed25519 XXXXX commentaires +~~~ + +C'est aussi faisable dans la configuration du serveur `sshd_config` avec la directive `ForceCommand`. + +Dans l'exemple précédent on est limité à une seule commande par clée SSH, ce qui dans la plus part des cas est assez limitant. Pour autoriser plusieurs commandes pour une seule clée, il suffit d'écrire un script qui vérifie la variable d'environement `SSH_ORIGINAL_COMMAND` qui contiens la commande spécifiée par le client SSH. Ci-dessous se trouve un exemple pour ce type de script ainsi que sa configuration ; sans garantie aucune de sécurité : + +~~~ +$ cat /usr/local/libexec/ssh-authorized-commands +#!/bin/sh +# +# Execute file present in directory $1 without support for arguments. + +set -e + +authorized_command_dir=$(realpath $1) + +# When no command is specified by the client +test -z "${SSH_ORIGINAL_COMMAND}" && exit 1 + +command=$(realpath "${authorized_command_dir}/${SSH_ORIGINAL_COMMAND}") + +# Trying to avoid path traversal attack +test $(dirname "$command") != "$authorized_command_dir" && exit 1 + +if [ -x "$command" ]; then + "$command" +else + echo "$(basename $0): ${SSH_ORIGINAL_COMMAND}: command not found" >&2 && exit 127 +fi +$ grep ssh-authorized-commands ~/.ssh/authorized_keys +command="/usr/local/libexec/ssh-authorized-commands /etc/ssh/authorized_command.d" ssh-ed25519 XXXXX commentaires +$ head -n-0 authorized_command.d/* +==> authorized_command.d/machine-id <== +#!/bin/sh +cat /etc/machine-id + +==> authorized_command.d/os-release <== +#!/bin/sh +cat /etc/os-release +~~~ + ### Agent SSH Il est important de protéger une clé SSH utilisée par un humain par une passphrase.