serveur ssh, authentification par clé

installation debian

si nécessaire

apt install openssh-server

les clés du serveur sont générées automatiquement à l'installation du serveur

configuration

editor /etc/ssh/sshd_config

par défaut:

modification:

pour faire simple, ajout bloc suivant en fin de fichier :

    # modifications ultérieures éventuelles facilitées
 #LogLevel DEBUG
 #AuthorizedKeysFile  .ssh/authorized_keys

    # standard répété
ChallengeResponseAuthentication no
PermitEmptyPasswords no
PubkeyAuthentication yes

    # changement port d'écoute
Port 5555
    # suppression accès par mot de passe
PasswordAuthentication no
    # même pas avec une clef !
PermitRootLogin no

    # facultatif ipv4
AddressFamily inet

    # facultatif, restreindre l'accès à certains
AllowUsers <user>

relancer le service: systemctl restart sshd

connexion client

le client a une clé autorisée sur le serveur. pour la génération et la publication, voir https://kyodev.frama.io/kyopages/trucs/ssh/ssh-client/

ssh <user>@SRV

le mot de passe de la clé n'est pas demandé et la connexion se fait correctement

ça marche

ça marche pas

config serveur rapide

    # modifications ultérieures éventuelles facilitées
 #LogLevel DEBUG
 #AuthorizedKeysFile  .ssh/authorized_keys

    # standard
ChallengeResponseAuthentication no
PermitEmptyPasswords no
PubkeyAuthentication yes

    # modifications
Port 5555
PasswordAuthentication no
PermitRootLogin no

de manière plus ou moins aléatoire?, la connexion inactive provoque une erreur genre:

packet_write_wait: Connection to <ip server> port <port>: Broken pipe

selon cette page:

ClientAliveInterval 300
ClientAliveCountMax 2

fail2ban

su
apt install fail2ban
systemctl enable fail2ban.service
systemctl start fail2ban.service

echo [sshd]               > /etc/fail2ban/jail.d/90sdeb.local
echo "enabled = true"    >> /etc/fail2ban/jail.d/90sdeb.local
echo "filter  = sshd"    >> /etc/fail2ban/jail.d/90sdeb.local
echo "#port = ssh, sftp, 5555"    >> /etc/fail2ban/jail.d/90sdeb.local
echo "mode = extra"      >> /etc/fail2ban/jail.d/90sdeb.local
echo "maxretry = 3"      >> /etc/fail2ban/jail.d/90sdeb.local
echo "findtime = 3600"   >> /etc/fail2ban/jail.d/90sdeb.local
echo "bantime = 86400"   >> /etc/fail2ban/jail.d/90sdeb.local

fail2ban-client reload

vérifier:

systemctl status fail2ban.service

fail2ban-client -d

su

iptables -S | grep f2b
    -N f2b-sshd
    -A INPUT -p tcp -m multiport --dports 22 -j f2b-sshd
    -A f2b-sshd -s <xyz> -j REJECT --reject-with icmp-port-unreachable
    -A f2b-sshd -j RETURN

iptables -L
    target     prot opt source               destination 
    REJECT     all  --  <xyz>                  anywhere             reject-with icmp-port-unreachable

suivre les logs:

tail -f /var/log/fail2ban.log

unban:

fail2ban-client status sshd
    Status for the jail: sshd
    |- Filter
    |  |- Currently failed: 0
    |  |- Total failed: 0
    |  `- File list:    /var/log/auth.log
    `- Actions
       |- Currently banned: 1
       |- Total banned: 1
       `- Banned IP list:   <xyz>

fail2ban-client unban <IP>

mais ne résiste pas à un restart du service fail2ban

liens divers