Paranoid SSH configuration -- 3FA
Daniel Nashed – 24 May 2020 08:04:01
This weekend I moved one of my servers to a new provider while upgrading it to current CentOS 8.1 release.
Beside disabling root access, implementing fail2ban and certificate based authentication, I checked additional multi-factor authentication.
In current SSH versions you can define how many authentication mechanisms need to succeed for a login.
Once that was setup I added time based authentication (TOTP) via Google Authenticator (see: https://en.wikipedia.org/wiki/Time-based_One-time_Password_algorithm).
For TOTP I used a YubiKey 5 NFC (https://www.yubico.com), which comes with an iPhone app protected by the key.
So in fact this adds another layer of security and you could see it as a 4th factor.
Beside setting up a key in .ssh/authorized_keys for your account you have to enable authentication methods and have to define which methods should be used in which order.
The required setting is not in the default /etc/ssh/sshd_config
AuthenticationMethods "publickey,password" "publickey,keyboard-interactive"
This line ensures that you are first authenticated with a certificate, before you have to use a password for your account.
In addition you have to make sure all the authentication options below are enabled:
# needed fur certificate based authentication
PubkeyAuthentication yes
# needed for password authentication
PasswordAuthentication yes
# needed for Google Authenticator 2FA / TOTP
ChallengeResponseAuthentication yes
# ensure PAM authentication is enabled -- also needed for Google Authenticator
UsePAM yes
Beside those settings I am disabling a couple of other options and move the SSH port to a none standard port, to ensure I have less login attempts
(and less logs, don't forget to change firewall rules for the port you chose).
# security best practices
Port 222
PermitRootLogin no
PermitEmptyPasswords no
GSSAPIAuthentication no
IgnoreRhosts yes
MaxAuthTries 4
MaxSessions 4
Beside those settings you have to install the Google Authenticator
yum install google-authenticator
And configure it as a PAM module in /etc/pam.d/sshd
auth required pam_google_authenticator.so
For reference here is the project page --> https://github.com/google/google-authenticator-libpam
You have to take care, not all options might already work in the version included in CentOS.
So once your configuration is done and you registered your use with the procedure below, you can login specifying 3 different factors.
In my case for TOTP I am using my
Example Login over SSH
login as: notes
Authenticating with public key "nsh@nashcom.loc" (#1 authentication with certificate)
Further authentication required
Keyboard-interactive authentication prompts from server:
Password: xxxx (#2 authentication with password)
Verification code: xxxx (#3 authentication with time based token -> in my case I use the key to protect my app #4 factor)
Example configuration for an individual user
google-authenticator
Do you want authentication tokens to be time-based (y/n) y
Warning: pasting the following URL into your browser exposes the OTP secret to Google:
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/nsh@centos8.nashcom.loc%3Fsecret%3D5DOMINOUZKRL4V7NOTES3JUYIXQ%26issuer%3Dcentos8.nashcom.loc
Failed to use libqrencode to show QR code visually for scanning.
Consider typing the OTP secret into your app manually.
Your new secret key is: 5DOMINOUZKRL4V7NOTES3JUYIXQ
Enter code from app (-1 to skip): 711236
Code confirmed
Your emergency scratch codes are:
11780937
21101653
12110617
57721107
55818118
Do you want me to update your "/home/nsh/.google_authenticator" file? (y/n) y
Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y
By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) y
If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) y
- Comments [0]