cmc/cleberg.net

My personal web garden & blog.

clone: git clone https://gitbay.org/cmc/cleberg.net.git

main: content/blog/2022-03-26-ssh-mfa.org · raw

  1#+date:        [2022-03-26 Sat 00:00:00]
  2#+title:       SSH MFA: TOTP on Top of Keys
  3#+description: Adding TOTP two-factor authentication on top of SSH key auth.
  4#+slug:        ssh-mfa
  5#+filetags:    :security:
  6
  7* Why Do I Need Multi-Factor Authentication (MFA) for SSH (Secure Shell Protocol)?
  8
  9If you are a sysadmin of a server anywhere (that includes at home!), you may
 10want an added layer of protection against intruders. This is not a replacement
 11for other security measures, such as:
 12
 13- Disable root SSH
 14- Disable SSH password authentication
 15- Allow only certain users to login via SSH
 16- Allow SSH only from certain internet protocol (IP) addresses
 17
 18However, MFA can be added as an additional security measure to ensure that your
 19server is protected. This is especially important if you need to allow password
 20authentication for SSH.
 21
 22For more guidance on server security measures, see my other post: [[https://cleberg.net/blog/server-hardening.html][Step-by-Step
 23Guide to Securing Your Home Server with Firewalls, SSH, and VLANs]].
 24
 25* Install MFA PAM Module
 26
 27PAM (Pluggable Authentication Module) is an authentication infrastructure used
 28on Linux systems to authenticate a user. In order to use this technology, let's
 29install the =libpam-google-authenticator= package:
 30
 31#+begin_src sh
 32sudo apt-get update
 33#+end_src
 34
 35#+begin_src sh
 36sudo apt-get install libpam-google-authenticator
 37#+end_src
 38
 39* Initialize the PAM Module
 40
 41** Interactive Method
 42
 43Once the package is installed, initialize it and following the interactive
 44prompts to generate your OTP (One-Time Password) or TOTP (Time-based One-Time
 45Password):
 46
 47#+begin_src sh
 48google-authenticator
 49#+end_src
 50
 51If you are not sure how to answer, read the prompts carefully and think about
 52having to how each situation would affect your normal login attempts. If you are
 53still not sure, use my default responses below.
 54
 55#+begin_src txt
 56OUTPUT
 57
 58Do you want authentication tokens to be time-based (y/n) y
 59#+end_src
 60
 61At this point, use an authenticator app somewhere one of your devices to scan
 62the QR (quick-response) code. Any future login attempts after our upcoming
 63configuration changes will require that TOTP.
 64
 65#+begin_src txt
 66OUTPUT
 67
 68Do you want me to update your "/home/user/.google_authenticator" file? (y/n) y
 69#+end_src
 70
 71#+begin_src txt
 72OUTPUT
 73
 74Do you want to disallow multiple uses of the same authentication
 75token? This restricts you to one login about every 30s, but it increases
 76your chances to notice or even prevent man-in-the-middle attacks (y/n) y
 77#+end_src
 78
 79#+begin_src txt
 80OUTPUT
 81
 82By default, a new token is generated every 30 seconds by the mobile app.
 83In order to compensate for possible time-skew between the client and the server,
 84we allow an extra token before and after the current time. This allows for a
 85time skew of up to 30 seconds between authentication server and client. If you
 86experience problems with poor time synchronization, you can increase the window
 87from its default size of 3 permitted codes (one previous code, the current
 88code, the next code) to 17 permitted codes (the 8 previous codes, the current
 89code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
 90between client and server.
 91Do you want to do so? (y/n) n
 92#+end_src
 93
 94#+begin_src txt
 95OUTPUT
 96
 97If the computer that you are logging into isn't hardened against brute-force
 98login attempts, you can enable rate-limiting for the authentication module.
 99By default, this limits attackers to no more than 3 login attempts every 30s.
100Do you want to enable rate-limiting? (y/n) y
101#+end_src
102
103** Non-Interactive Method
104
105If you need to do this quickly, know your responses to the prompts, or
106are setting this up for numerous users, the non-interactive method can
107be much faster:
108
109#+begin_src sh
110google-authenticator -t -d -f -r 3 -R 30 -w 3
111#+end_src
112
113The options referenced above are as follows:
114
115#+begin_src txt
116google-authenticator [<options>]
117 -h, --help                     Print this message
118 -c, --counter-based            Set up counter-based (HOTP) verification
119 -t, --time-based               Set up time-based (TOTP) verification
120 -d, --disallow-reuse           Disallow reuse of previously used TOTP tokens
121 -D, --allow-reuse              Allow reuse of previously used TOTP tokens
122 -f, --force                    Write file without first confirming with user
123 -l, --label=<label>            Override the default label in "otpauth://" URL
124 -i, --issuer=<issuer>          Override the default issuer in "otpauth://" URL
125 -q, --quiet                    Quiet mode
126 -Q, --qr-mode={NONE,ANSI,UTF8} QRCode output mode
127 -r, --rate-limit=N             Limit logins to N per every M seconds
128 -R, --rate-time=M              Limit logins to N per every M seconds
129 -u, --no-rate-limit            Disable rate-limiting
130 -s, --secret=<file>            Specify a non-standard file location
131 -S, --step-size=S              Set interval between token refreshes
132 -w, --window-size=W            Set window of concurrently valid codes
133 -W, --minimal-window           Disable window of concurrently valid codes
134 -e, --emergency-codes=N        Number of emergency codes to generate
135#+end_src
136
137This fully configures the authenticator, saves it to a file, and then outputs
138the secret key, QR code, and recovery codes. (If you add the flag =-q=, then
139there won't be any output). If you use this command in an automated fashion,
140make sure your script captures the secret key and/or recovery codes and makes
141them available to the user.
142
143* PAM Configuration Settings
144
145Once you've enabled MFA and have it saved to an MFA app on your phone or other
146device, open the PAM =sshd= file:
147
148#+begin_src sh
149sudo nano /etc/pam.d/sshd
150#+end_src
151
152You need to do two things in this file. First, add the following lines to the
153bottom of the file:
154
155#+begin_src config
156auth required pam_google_authenticator.so nullok
157auth required pam_permit.so
158#+end_src
159
160Second, comment-out the following line near the top of the file.
161
162If you leave this line uncommented, every SSH login attempt will ask for the
163following three authentication factors:
164
1651. Publickey
1662. Password
1673. T/OTP code
168
169#+begin_src config
170#@include common-auth
171#+end_src
172
173* SSH Configuration Settings
174
175Finally, edit the =sshd_config= file again:
176
177#+begin_src sh
178sudo nano /etc/ssh/sshd_config
179#+end_src
180
181You'll need to change =ChallengeResponseAuthentication= to yes and add the
182=AuthenticationMethods= line to the bottom of the file.
183
184#+begin_src config
185ChallengeResponseAuthentication yes
186AuthenticationMethods publickey,password publickey,keyboard-interactive
187#+end_src
188
189Finally, restart the =ssh= service:
190
191#+begin_src sh
192sudo systemctl restart sshd.service
193#+end_src
194
195The next time you log in, you should be greeted with a verification code
196request!