使用 oathtool 的双因素 SSH:如何设置 IP withelist(访问规则)?

使用 oathtool 的双因素 SSH:如何设置 IP withelist(访问规则)?

在我自己的 Debian 9 VPS 上,我使用以下步骤启用了“使用 oathtool 的双因素 SSH”和 PUBLICKEY(禁用密码):

  1. 通过 apt“liboath0 和 libpam-oath oathtool”安装
  2. export HEX_SECRET=$(head -15 /dev/urandom | sha1sum | cut -b 1-30)
  3. oathtool --verbose --totp $HEX_SECRET --digits=8
  4. 安全化“用户誓言”:“用户誓言”:
    touch /etc/users.oath
    chmod 0600 /etc/users.oath
  1. 编辑/etc/users.oath:
    /*Option User Prefix Seed*/
    HOTP/T30/6      myuser    -       12345668048373737372828
  1. 删除变量“HEX_SECRET”:
    unset HEX_SECRET
  1. 配置访问规则,编辑/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
    
  2. 编辑/etc/ssh/sshd_config:

    UsePAM yes
    AuthenticationMethods publickey,keyboard-interactive
    PasswordAuthentication no
    ChallengeResponseAuthentication yes
  1. 重新加载 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

结果是该用户无法从“myip”使用“myuser”登录到目标 SSH 服务器,这里是本地错误日志:

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

发现问题了。

重读Linux PAM 文件语法我意识到价值“1”

[success=1 default=ignore]

在“/etc/pam.d/sshd”上似乎不正确,正确的语法应该是“完毕”

[success=done default=ignore]

现在可以将用户/IP 组合添加到“/etc/security/login_token.conf”,以将特定 IP 地址的特定用户从 2FA 中排除

相关内容