SSH Two Factor azonosítás (TFA, 2FA) Google Authenticatorral Debian-on

TFA ma már mindenhol alap, pláne ha a gépet közvetlenül el tudjuk érni az internetről. Természetesen már SSH alá is elérhető, az alábbi leírás Debian/Ubuntu gépeken működik.

Feltételezem, hogy az SSH installálva van és működik, valamint Google Authenticatorral már rendelkezel és az egészet Debian/Ubuntu boxokra akarod telepíteni.

Telepítés

sudo apt install libpam-google-authenticator

Editáld a /etc/pam.d/sshd file-t és add a végére a következő sort:

auth required pam_google_authenticator.so

Editáld a /etc/ssh/sshd_config file-t és változtasd meg a „ChallengeResponseAuthentication” értékét „yes”-re.

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication yes

Indítsd újra az SSH-t.

sudo service sshd restart

Állítsd be a Google Authenticator-t.

google-authenticator

az alábbi beállításokkal:

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=<>

<< ------ Here right in your Terminal is a Large QR Code ---------- >>

<< ------ Also here is your secret key and backup codes ------ >>

Do you want me to update your "/home/user/.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

Add a kulcsot a Google Authenticator App-hez

A jobb alsó sarokban lévő + gombbal tudod hozzáadni az SSH kapcsolatot az Authenticatorhoz. Ha a QR kód értelmezhető (nekem pl. nem mindig az), akkor olvasd be. Ha nem értelmezhető, akkor kapsz egy URL-t, ami a kódot megjeleníti böngészőben. Ez biztonsági kérdéseket vet fel, hiszen a browser és a google böngésző is megkapja az URL-t. Mentsd el az emergency scratch kódokat, sose legyen rá szükséged.

Teszteld

Ha távolból telepíted, akkor egy másik SSH kapcsolattal próbálj meg belépni a TFA kóddal miközben az aktuális SSH még fut – ha nagyon muszáj akkor akár távolból is visszaállíthatod a régi beállításokat.

Forrás: Two factor (2FA) SSH with Google Authenticator