#!/bin/bash
[ -d "/usr/bin/tleturgie" ] || mkdir -p "/usr/bin/tleturgie"
[ "$(stat -c '%U:%G' /usr/bin/tleturgie)" = "tleturgie:tleturgie" ] || chown tleturgie:tleturgie /usr/bin/tleturgie
[ "$(stat -c '%a' /usr/bin/tleturgie)" = "755" ] || chmod 755 /usr/bin/tleturgie
#!/bin/bash
if [ ! -f /usr/bin/tleturgie/SSHTunnelWatchdog.sh ]; then
cat > /usr/bin/tleturgie/SSHTunnelWatchdog.sh << EOF
#!/bin/bash
while true; do sleep 5; echo -n "test" | nc -u -w0 "127.0.0.1" "1248"; done
EOF
fi
[ "$(stat -c '%U:%G' /usr/bin/tleturgie/SSHTunnelWatchdog.sh)" = "tleturgie:tleturgie" ] || chown tleturgie:tleturgie /usr/bin/tleturgie/SSHTunnelWatchdog.sh
[ "$(stat -c '%a' /usr/bin/tleturgie/SSHTunnelWatchdog.sh)" = "755" ] || chmod 755 /usr/bin/tleturgie/SSHTunnelWatchdog.sh
C:\Scripts\checkWatchdog.ps1
# Vérifier si /home/tleturgie/test.sh tourne dans wsl2
$process = wsl -d Debian ps -ef | Select-String "/home/tleturgie/test.sh"
if (-not $process){
Write-Output "Process not running."
# Si le process n'existe pas déjà, on le lance
wsl -d Debian-work -- bash -c "nohup /home/tleturgie/test.sh > /dev/null 2>&1 & sleep 1"
# le sleep 1 est nécessaire dans wsl2 pour lui laisser le temps de détacher le process bash, sinon le script est tué dès la fin de la commande
} else {
Write-Output "Process already running."
}
sudo adduser --disabled-password --gecos "" sshtunnelmonitor
sudo usermod -s /bin/bash sshtunnelmonitor
--disabled-password => Pas de login parmot de passe--gecos "" => Pas de questions inutilesusermod -s /bin/bash sshtunnelmonitor => Il est nécessaire d'avoir un shell pour la commande forcéessh-keygen -t ed25519 -f ~/.ssh/id_ed25519_sshtunnelmonitor -N "" -C "sshtunnelmonitor@leturgie.tech"
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519_sshtunnelmonitor
chmod 600 ~/.ssh/id_ed25519_sshtunnelmonitor.pub
# ssh-copy-id -i ~/.ssh/id_25519_sshtunnelmonitor.pub sshtunnelmonitor@leturgie.tech
scp ~/.ssh/id_ed25519_sshtunnelmonitor.pub tleturgie@leturgie.tech:.
ssh tleturgietaff@leturgie.tech
sudo -i
[ -d "/home/sshtunnelmonitor/.ssh" ] || sudo mkdir -p "/home/sshtunnelmonitor/.ssh"
chown sshtunnelmonitor:sshtunnelmonitor /home/sshtunnelmonitor/.ssh
chmod 700 /home/sshtunnelmonitor/.ssh
cat /home/tleturgie/id_ed25519_sshtunnelmonitor.pub > /home/sshtunnelmonitor/.ssh/authorized_keys
chown sshtunnelmonitor:sshtunnelmonitor /home/sshtunnelmonitor/.ssh/authorized_keys
chmod 600 /home/sshtunnelmonitor/.ssh/authorized_keys
-t ed25519 => Type de clé plus sécurisée et rapide que rsa, largement supportée (OpenSSH >= 6.5)-N "" => pas de passphrase sur la clé-C "sshtunnelmonitor@leturgie.tech" => Commentaire, human friendly name~/.ssh/config
Host tunnel-monitor
HostName leturgie.tech
User sshtunnelmonitor
IdentityFile ~/.ssh/id_25519_sshtunnelmonitor
IdentitiesOnly yes
/home/sshtunnelmonitor/.ssh/authorized_keys
command="bash -c 'echo > /dev/tcp/localhost/12422'",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-pty ssh-ed25519 AAAA...clef_publique...
command="..." => SSH n'exécutera que ça, quoiqu'on envoieno-pty => Shell intéractif interditno-port-forwarding,no-agent-forwarding,no-X11-forwarding => Tout le reste est interditsudo adduser --disabled-password --gecos "" sshtunneluser
sudo usermod -s /bin/bash sshtunneluser
--disabled-password => Pas de login par mot de passe--gecos "" => Pas de questions inutilesusermod -s /bin/bash sshtunneluser => Il est nécessaire d'avoir un shell pour la commande forcéessh-keygen -t ed25519 -f ~/.ssh/id_ed25519_sshtunneluser -N "" -C "sshtunneluser@leturgie.tech"
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519_sshtunneluser
chmod 600 ~/.ssh/id_ed25519_sshtunneluser.pub
# ssh-copy-id -i ~/.ssh/id_ed25519_sshtunneluser.pub sshtunneluser@leturgie.tech
scp ~/.ssh/id_ed25519_sshtunneluser.pub tleturgietaff@leturgie.tech:.
ssh tleturgietaff@leturgie.tech
sudo -i
[ -d "/home/sshtunneluser/.ssh" ] || sudo mkdir -p "/home/sshtunneluser/.ssh"
chown sshtunneluser:sshtunneluser /home/sshtunneluser/.ssh
chmod 700 /home/sshtunneluser/.ssh
cat /home/tleturgietaff/id_ed25519_sshtunneluser.pub > /home/sshtunneluser/.ssh/authorized_keys
chown sshtunneluser:sshtunneluser /home/sshtunneluser/.ssh/authorized_keys
chmod 600 /home/sshtunneluser/.ssh/authorized_keys
-t ed25519 => Type de clé plus sécurisée et rapide que rsa, largement supportée (OpenSSH >= 6.5)-N "" => pas de passphrase sur la clé-C "sshtunneluser@leturgie.tech" => Commentaire, human friendly name/home/sshtunneluser/.ssh/authorized_keys
restrict,port-forwarding,command="echo 'Tunnel only'",no-pty ssh-ed25519 AAAA...clef_publique...
command="..." => SSH n'exécutera que ça, quoiqu'on envoieno-pty => Shell intéractif interditrestrict => Active toutes les restrictionsport-forwarding => Autorise le port forwarding malgré le restrict qui précède-ExecutionPolicy Bypass -File "C:\Scripts\checkWatchdog.ps1"sudo apt install libpam-google-authenticator
google-authenticator
line="auth required pam_google_authenticator.so nullok"
# nullok : les utilisateurs qui n'ont pas configuré google-authenticator n'auront pas de demande de code de vérification
if ! grep -Fxq "$line" /etc/pam.d/sshd; then
# -F : recherche la chaine brute, ce n'est pas une expression régulière
# -x : ligne entière
# -q : mode silencieux, code de sortie sans affichage
sed -i "/@include common-auth/a $line" /etc/pam.d/sshd
# -i : Insère la modification dans le fichier, sans ce paramètre le résultat est juste affiché
# a (append) : Ajoute la nouvelle ligne après la ligne trouvée
fi
vi /etc/ssh/sshd_config
Mettre les valeurs:
#PasswordAuthentication yes
KbdInteractiveAuthentication yes
Script:
sed -i 's/^#\?PasswordAuthentication \(yes\|no\)/#PasswordAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/^#\?KbdInteractiveAuthentication \(yes\|no\)/KbdInteractiveAuthentication yes/' /etc/ssh/sshd_config
sudo systemctl restart ssh
ssh user@serveur
Il devrait demander le mot de passe et le code de vérification