nixos-config/common/services/sshd.nix

90 lines
2.5 KiB
Nix
Raw Normal View History

2024-04-24 12:34:43 +02:00
{pkgs, ...}: {
2024-04-22 04:21:07 +02:00
# Hardened OpenSSH server
# Resources:
2024-04-23 16:19:33 +02:00
# https://cyber.gouv.fr/en/publications/openssh-secure-use-recommendations (2015)
2024-04-24 15:34:26 +02:00
# ...more soon...
2024-04-22 04:21:07 +02:00
services.openssh = {
enable = true;
allowSFTP = false;
settings = {
PermitRootLogin = "no";
2024-04-24 15:17:34 +02:00
AllowUsers = ["user" "tasia"];
2024-04-24 12:34:43 +02:00
2024-04-22 04:21:07 +02:00
# Public key authentiation only
PasswordAuthentication = false;
2024-04-24 15:17:34 +02:00
ChallengeResponseAuthentication = true;
2024-04-24 12:34:43 +02:00
KbdInteractiveAuthentication = true;
2024-04-24 15:17:34 +02:00
AuthenticationMethods = "publickey,keyboard-interactive";
2024-04-22 04:21:07 +02:00
};
extraConfig = ''
# Only allow SSH v2
Protocol 2
# Check file modes in /etc/ssh
StrictModes yes
UsePrivilegeSeparation sandbox
PrintLastLog yes
2024-04-26 15:06:55 +02:00
# Don't allow clients to mess with environment variables
2024-04-22 04:21:07 +02:00
PermitUserEnvironment no
# AcceptEnv
AllowTcpForwarding no
# wip
2024-04-26 15:26:18 +02:00
# AllowTcpForwarding yes
2024-04-22 04:21:07 +02:00
X11Forwarding no
AllowAgentForwarding no
AllowStreamLocalForwarding no
2024-10-05 10:39:16 +02:00
# Yubikey
PubkeyAuthOptions verify-required
2024-04-22 04:21:07 +02:00
'';
};
2024-04-24 12:34:43 +02:00
2024-04-26 15:06:55 +02:00
# needed for 2fa
2024-04-24 15:17:34 +02:00
security.pam.services = {
sshd.text = ''
2024-04-26 15:06:55 +02:00
# Check for the client's public key
2024-04-24 15:17:34 +02:00
account required pam_unix.so # unix (order 10900)
2024-04-26 15:06:55 +02:00
# Actually check for the 2FA code.
# "nullok": accept session if .google_authenticator doesn't exist
# "no_increment_hotp": make sure the counter isn't incremented for failed attempts.
2024-04-24 15:17:34 +02:00
auth required ${pkgs.google-authenticator}/lib/security/pam_google_authenticator.so nullok no_increment_hotp # google_authenticator (order 12500)
2024-04-26 15:06:55 +02:00
# If .google_authenticator isn't present, you can still let them through
2024-04-24 15:34:26 +02:00
auth sufficient pam_permit.so
2024-04-24 15:17:34 +02:00
2024-04-26 15:06:55 +02:00
# Load the environment variables for the new ssh session
2024-04-24 15:17:34 +02:00
session required pam_env.so conffile=/etc/pam/environment readenv=0 # env (order 10100)
2024-04-26 15:06:55 +02:00
# Logs when a user logins or leave the system.
2024-04-24 15:17:34 +02:00
session required pam_unix.so # unix (order 10200)
2024-04-26 15:06:55 +02:00
# Record user's login uid to the process attribute
2024-04-24 15:17:34 +02:00
session required pam_loginuid.so # loginuid (order 10300)
2024-04-26 15:06:55 +02:00
# Register user sessions in the systemd login manager
2024-04-24 15:17:34 +02:00
session optional ${pkgs.systemd}/lib/security/pam_systemd.so # systemd (order 12000)
'';
2024-04-24 12:34:43 +02:00
};
2024-04-24 15:17:34 +02:00
2024-04-29 14:31:33 +02:00
# CLI tools
2024-04-24 12:34:43 +02:00
environment.systemPackages = with pkgs; [
google-authenticator
2024-04-29 14:31:33 +02:00
ssh-audit
2024-04-24 12:34:43 +02:00
];
2024-04-29 14:31:33 +02:00
# Check whether this is actually doing anything
services.fail2ban = {
enable = true;
ignoreIP = [
#
];
};
2024-04-22 04:21:07 +02:00
}
# ssh R6: StrictHostKeyChecking ask
2024-04-26 15:06:55 +02:00