hokaido
Goto Top

SSH 2FA und RSA Key Ubuntu 20.04

Hallo,

ich wollte unter Ubuntu 20.04 neben dem RSA Key (funktioniert) eine 2FA einrichten.

Dazu habe ich diese Schritte durchgeführt
google-authenticator

-Do you want authentication tokens to be time-based (y/n) y

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

-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

nano /etc/pam.d/sshd
#@include common-auth

And all the way on the bottom add
@include common-password
auth required pam_google_authenticator.so

nano /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
UsePAM yes
AuthenticationMethods publickey,keyboard-interactive

service ssh restart

Wenn ich mich jetzt anmelden will, meckert Putty: No supported authentication methods available (server sent: keyboard-interactive)

Was hab ich falsch gemacht?


Christian
Danke

Content-Key: 662775

Url: https://administrator.de/contentid/662775

Printed on: April 20, 2024 at 13:04 o'clock

Mitglied: 147669
147669 Mar 15, 2021 updated at 17:23:55 (UTC)
Goto Top
Zitat von @hokaido:
nano /etc/pam.d/sshd
#@include common-auth
Falsch das bleibt dort drin.
And all the way on the bottom add
@include common-password
auth required pam_google_authenticator.so
Korrekt, nur die letzte Zeile hinzufügen die erste steht ja schon drin.
nano /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
OK.
UsePAM yes
OK
AuthenticationMethods publickey,keyboard-interactive
Das weg und nur
PublicKeyAuthentication yes
setzen
service ssh restart
OK
Wenn ich mich jetzt anmelden will, meckert Putty: No supported authentication methods available (server sent: keyboard-interactive)
Klappt hier nach obiger Methode einwandfrei.
Member: hokaido
hokaido Mar 15, 2021 at 21:18:32 (UTC)
Goto Top
Danke. Es geht aber mit dem gleichen Ergebnis. So ging es vorher auch schon. Es kommt nur die Abfrage des SSH Key Passwortes. Ich wollte aber SSH Key Passwort und 2FA.

Sorry, aber ich fuchse mich hier Schritt für Schritt durch die Tutorials und übe mit Linuxface-smile
Mitglied: 147669
147669 Mar 16, 2021 updated at 06:38:02 (UTC)
Goto Top
Zitat von @hokaido:

Danke. Es geht aber mit dem gleichen Ergebnis. So ging es vorher auch schon. Es kommt nur die Abfrage des SSH Key Passwortes.
Ich wollte aber SSH Key Passwort und 2FA.
Das ist normal,
Beides sind 2FA Methoden, bei der ersten ist das Passwort für den Key der zweite Schlüssel und bei letzterem das Token 😉.
Wenn du den privaten Schlüssel aus dem Verzeichnis raus nimmst fragt er dich dann automatisch nach Kennwort und auch nach dem Token.
Du hast in diesem Fall auch vergessen Passwort Authentication auf no zu setzen.
Member: hokaido
hokaido Mar 16, 2021 at 06:44:47 (UTC)
Goto Top
Danke. Vll. reden wir aneinander vorbei. Ich wollte - wenn es geht - dass beide Abfragen nacheinanderkommen. Erst die Eingabe des SSH Keys, dann die 2FA Abfrage.

Und das funktioniert eben so nicht. Vll. hab ich auch was falsch verstanden und es geht technisch nur ent- oder weder...
Mitglied: 147669
Solution 147669 Mar 16, 2021 updated at 07:00:43 (UTC)
Goto Top
Zitat von @hokaido:

Danke. Vll. reden wir aneinander vorbei. Ich wollte - wenn es geht - dass beide Abfragen nacheinanderkommen. Erst die Eingabe des SSH Keys, dann die 2FA Abfrage.

Und das funktioniert eben so nicht. Vll. hab ich auch was falsch verstanden und es geht technisch nur ent- oder weder...
Das geht nur entweder oder. Wenn du das so haben willst musst du den privaten Key ohne Passwort versehen dann kommt zusätzlich die Verification Code Abfrage.
Denn wie schon gesagt, beides sind 2FA Verfahren, das was du machen willst ist quasi 4FA.
Member: hokaido
hokaido Mar 16, 2021 at 07:00:30 (UTC)
Goto Top
Ok, danke. Dann hatte ich es tatsächlich falsch verstanden. Eigentlich reicht mir der Key. Das andere wäre nice to have gewesen. Danke!
Member: colinardo
colinardo Mar 16, 2021 updated at 11:01:20 (UTC)
Goto Top
Zitat von @147669:
Das geht nur entweder oder. Wenn du das so haben willst musst du den privaten Key ohne Passwort versehen dann kommt zusätzlich die Verification Code Abfrage.
ACHTUNG Falschinformation!
Das gewünschte Verhalten ist problemlos realisierbar, genau dafür ist ja PAM so flexibel konfigurierbar face-wink.
Halte dich exakt an die folgende Anleitung dann kommt das problemlos zum fliegen
https://www.digitalocean.com/community/tutorials/how-to-set-up-multi-fac ...
Die Anleitung ist zwar für Ubuntu 16.04, funktioniert aber exakt gleich auf einem 20.04 LTS! Wurde hier nochmal erfolgreich auf einer sauberen Ubuntu 20.04 LTS Server Version verifiziert. Das gleiche ist aber auch bswp. auf einem Debian und anderen Derivaten in ähnlicher Weise konfigurierbar, auch wenn man hier und da aufpassen muss wie die PAM Module verschachtelt sind.

screenshot

Hier noch als Referenz meine genutzten Configs

back-to-top/etc/pam.d/sshd

# PAM configuration for the Secure Shell service

# Standard Un*x authentication.
#@include common-auth

# Disallow non-root logins when /etc/nologin exists.
account    required     pam_nologin.so

# Uncomment and edit /etc/security/access.conf if you need to set complex
# access limits that are hard to express in sshd_config.
# account  required     pam_access.so

# Standard Un*x authorization.
@include common-account

# SELinux needs to be the first session rule.  This ensures that any
# lingering context has been cleared.  Without this it is possible that a
# module could execute code in the wrong domain.
session [success=ok ignore=ignore module_unknown=ignore default=bad]        pam_selinux.so close

# Set the loginuid process attribute.
session    required     pam_loginuid.so

# Create a new session keyring.
session    optional     pam_keyinit.so force revoke

# Standard Un*x session setup and teardown.
@include common-session

# Print the message of the day upon successful login.
# This includes a dynamically generated part from /run/motd.dynamic
# and a static (admin-editable) part from /etc/motd.
session    optional     pam_motd.so  motd=/run/motd.dynamic
session    optional     pam_motd.so noupdate

# Print the status of the user's mailbox upon successful login. 
session    optional     pam_mail.so standard noenv # [1]

# Set up user limits from /etc/security/limits.conf.
session    required     pam_limits.so

# Read environment variables from /etc/environment and
# /etc/security/pam_env.conf.
session    required     pam_env.so # [1]
# In Debian 4.0 (etch), locale-related environment variables were moved to
# /etc/default/locale, so read that as well.
session    required     pam_env.so user_readenv=1 envfile=/etc/default/locale

# SELinux needs to intervene at login time to ensure that the process starts
# in the proper default security context.  Only sessions which are intended
# to run in the user's context should be run after this. 
session [success=ok ignore=ignore module_unknown=ignore default=bad]        pam_selinux.so open

# Standard Un*x password updating.
@include common-password

auth    required        pam_google_authenticator.so

back-to-top/etc/ssh/sshd_config/sshd

#       $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile     .ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for 
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files 
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

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

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing, 
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password". 
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'. 
UsePAM yes
AuthenticationMethods publickey,keyboard-interactive

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem sftp  /usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server
#PasswordAuthentication yes

Grüße Uwe
Member: hokaido
hokaido Mar 19, 2021 at 08:51:57 (UTC)
Goto Top
Danke, schaue ich mir am Wochenende an
Member: hokaido
hokaido Mar 19, 2021 at 21:38:20 (UTC)
Goto Top
Also, leider funktioniert das nicht, habe exakt deinen Code reinkopiert

Lediglich den SSH Port habe ich auf einen anderen Port gelegt und bei der /etc/ssh/sshd_config/sshd habe ich am Anfang noch diesen Code
#Nur noch den user xyz zulassen. 
AllowUsers xyz
#root oder benutzer in der Gruppe root generell sperren
DenyUsers root
DenyGroups root

Sonst copy paste.

Fehlermeldung:
PuTTY Fatal Error
No supported authentication methods available (server sent: keyboard-interactive)
OK
Mitglied: 147669
147669 Mar 19, 2021 updated at 21:50:29 (UTC)
Goto Top
Also, leider funktioniert das nicht, habe exakt deinen Code reinkopiert
Also das es offensichtlich geht siehst du ja am Bild von @colinardo oben 🐒. Hab das hier gerade nochmal nachgestellt und es geht tatsächlich
einwandfrei, musst du wohl falsche Settings im PuTTY gesetzt haben, oder dein System ist verbastelt.
/etc/ssh/sshd_config/sshd
Naja wenn du das da rein kopiert hat wundert das ehrlich gesagt nicht, die gibbet nämlich ned🙃
Warte noch ein paar Minuten dann ist Freitag vorbei ...🐟
Member: hokaido
hokaido Mar 19, 2021 updated at 21:54:34 (UTC)
Goto Top
Ich habs hier reinkopiert:
/etc/ssh/sshd_config

Ich mach das nochmal kurz platt und probiere es morgen nochmal mit eingespielten Snapshot

Und ich wüüste nicht, welche Settings in Putty falsch sein sollten, denn SSH Public key Passwort abfrage alleine funktioniert ja einwandfrei. Nur die Kombi nicht.
Member: hokaido
hokaido Apr 01, 2021 at 08:05:40 (UTC)
Goto Top
Sorry für die späte Rückmeldung.
Ich habs hinbekommen.

Mein Fehler war, dass ich die 2FA natürlich unter root eingerichtet habe, den habe ich aber ja gesperrt.
Habe jetzt unter dem neuen user auch den 2FA Code eingerichtet und die Schritte befolgt, dann gehtsface-smile

Danke nochmalface-smile