На моем собственном Debian 9 VPS я включил «двухфакторную аутентификацию SSH с помощью oathtool» и PUBLICKEY (пароль отключен), выполнив следующую процедуру:
- установить через apt "liboath0 и libpam-oath oathtool"
export HEX_SECRET=$(head -15 /dev/urandom | sha1sum | cut -b 1-30)
oathtool --verbose --totp $HEX_SECRET --digits=8
- обеспечить безопасность "users-oath": "users.oath":
touch /etc/users.oath
chmod 0600 /etc/users.oath
- отредактируйте /etc/users.oath:
/*Option User Prefix Seed*/
HOTP/T30/6 myuser - 12345668048373737372828
- Удалена переменная «HEX_SECRET»:
unset HEX_SECRET
Настроил правила доступа, отредактировав /etc/security/login_token.conf
# Do not require two-factor from here: + : dennis : 1.1.1.0/24 + : myuser : 1.123.123.44/32 # lolnope don't need two-factor at all + : lolnope : ALL # Demand two-factor from everywhere and everyone else - : ALL : ALL
отредактировал /etc/ssh/sshd_config:
UsePAM yes
AuthenticationMethods publickey,keyboard-interactive
PasswordAuthentication no
ChallengeResponseAuthentication yes
- перезагрузить sshd. (Завершить процедуру)
СейчасЯ хочу исключить из 2FA определенного пользователя с определенного IP-адреса, поэтому я отредактировал "/etc/security/login_token.conf" и добавил:
+ : myuser : 1.123.123.44/32
И перезагрузил SSH.
Здесь "/etc/pam.d/sshd":
# PAM configuration for the Secure Shell service
# Standard Un*x authentication.
# @include common-auth
# Exceptions from two-factor
auth [success=1 default=ignore] pam_access.so accessfile=/etc/security/login_token.conf
# Two-factor
auth requisite pam_oath.so usersfile=/etc/users.oath
# Exceptions from two-factor and publickey
auth required pam_sepermit.so
# 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
В результате этот пользователь не может войти на целевой SSH-сервер, используя «myuser» с «myip». Вот локальный журнал ошибок:
ssh -v [email protected] -p 12345
OpenSSH_7.4p1 Debian-10+deb9u6, OpenSSL 1.0.2u 20 Dec 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to server2.mydomain [78.6.82.99] port 12345.
debug1: Connection established.
debug1: identity file /home/myuser/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/myuser/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/myuser/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/myuser/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/myuser/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/myuser/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/myuser/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/myuser/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4p1 Debian-10+deb9u6
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to server2.mydomain:12345 as 'myuser'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:hgw8r3487y9ty2c405utv04uy0356uv0c08t4
debug1: Host '[server2.mydomain]:12345' is known and matches the ECDSA host key.
debug1: Found key in /home/myuser/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected]>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/myuser/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
Authenticated with partial success.
debug1: Authentications that can continue: keyboard-interactive
debug1: Next authentication method: keyboard-interactive
debug1: Authentications that can continue: keyboard-interactive
debug1: No more authentication methods to try.
Permission denied (keyboard-interactive). ```
решение1
Нашел проблему.
ПеречитываяСинтаксис файла PAM в LinuxЯ понял, что ценность"1":
[success=1 default=ignore]
на "/etc/pam.d/sshd" кажется неправильным, и вместо этого правильный синтаксис должен быть"сделанный":
[success=done default=ignore]
Теперь можно добавить пару пользователь/IP в "/etc/security/login_token.conf", чтобы исключить определенных пользователей с определенных IP-адресов из 2FA.