カテゴリー: ssh

  • SSHの設定を再確認

    Ubuntu 22.04でsshサーバの設定 /etc/ssh/sshd_config を行っていたところ,PasswordAuthentication no を設定していたにもかかわらず,パスワード認証が無効になっていなかった.

    原因としては/etc/ssh/sshd_config.d/50-cloud-init.confPasswordAuthentication yesの記述があったため,こちらの設定が優先されていた.

    加えて,ChallengeResponseAuthentication または
    KbdInteractiveAuthenticationyes かつ UsePAMyes の場合,pam設定がデフォルトの場合もパスワードによる認証のみでログインできてしまう.これはpamによる2段階認証などを提供する仕組みである.

    これを機にSSHの設定の見直しを行った.

    sshd_config の先頭に
    Include /etc/ssh/sshd_config.d/*.conf があり,sshd_config.d内の.confをファイル名順に参照し,先勝ちでパラメータが設定されているようである.(システムによっては上書きになるものもあるのでややこしい)

    公開鍵認証

    /etc/ssh/sshd_config.d/00-publkey-auth.conf を作成

    PermitRootLogin no
    PubkeyAuthentication yes
    PasswordAuthentication no

    50-cloud-init.confは必要ないのであれば削除しても良いだろう.

    2段階認証

    パスワード認証 + Google Authenticatorによる認証を行う.公開鍵認証とは別に動作することに注意.

    パッケージのインストール

    # apt install libpam-google-authenticator libqrencode4

    /etc/pam.d/sshdを編集します.

    auth required pam_google_authenticator.so

    /etc/ssh/sshd_config.d/100-google-authenticator.confを作成します.

    KbdInteractiveAuthentication yes
    UsePAM yes

    sshdを再起動します.

    # systemctl restart ssh

    ユーザーの設定 google-authenticatorコマンドを実行

    $ google-authenticator
    Do you want authentication tokens to be time-based (y/n) y
    Your new secret key is: **************************
    Enter code from app (-1 to skip): 

    QRコードを読み取るか,Secret keyを設定してコードを取得します.

    ワンタイムコードを入力するか-1を入力すると以下のように設定項目に関する質問があります.デフォルトで問題ないので全てyを選択します.

    Your emergency scratch codes are:
      ********
      ********
      ********
      ********
      ********
    
    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

    sftp

    ファイルサーバなどでsftpやscpのみに制限する.

    /etc/ssh/sshd_config.d/50-sftp.conf を作成する

    AllowTcpForwarding no
    x11Forwarding no
    ForceCommand internal-sftp

    ForceCommand internal-sftpを指定することにより,sshすると
    This service allows sftp connections only. とメッセージが表示されconnectionがcloseされる.