oathtool을 사용하는 2단계 SSH: IP withelist(액세스 규칙)를 설정하는 방법은 무엇입니까?

oathtool을 사용하는 2단계 SSH: IP withelist(액세스 규칙)를 설정하는 방법은 무엇입니까?

내 Debian 9 VPS에서는 다음 절차를 사용하여 "oathtool을 사용하는 2단계 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. "users-oath" 보안: "users.oath":
    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. SSH를 다시로드하십시오. (절차 종료)

지금특정 IP 주소의 특정 사용자를 2FA에서 제외하고 싶습니다., 그래서 "/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]

이제 2FA의 특정 IP 주소에서 특정 사용자를 제외하기 위해 "/etc/security/login_token.conf"에 사용자/IP 커플을 추가할 수 있습니다.

관련 정보