【SSH】最近触らなくなった?SSH の設定ファイルを見直してみる

概要

最近ではレンタルサーバを借りても自分で SSH 周りの設定をすることはありません。

今回、たまたまセキュリティがざるなサーバ達を扱う機会があったので、SSH 周りで直しておいた方がいい設定をまとめました。

 

やる事リスト

他のサーバから SSH 接続される時の設定ファイル ( /etc/ssh/sshd_config ) を修正します。

  • SSH の認証方式を公開鍵認証に切り替える
  • SSH のポート番号を 22 番から変更する
  • root ユーザーの SSH ログインを禁止する
  • SSH のプロトコルを Version 2 に制限する

 

はじめに

公開鍵認証で SSH するためのキーペア(公開鍵と秘密鍵)をローカル PC で作成します。

既存のものがある場合には上書きしないように注意して下さい。

$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/kyamanak/.ssh/id_rsa): # Enter 押す
Enter passphrase (empty for no passphrase): # パスワード入力
Enter same passphrase again: # パスワード再入力

公開鍵は後ほどサーバにアップロードします。

 

対応手順

1. サーバーログイン

この時の SSH 接続は最後まで絶対に切らないで下さい!

設定を間違えると新規で SSH 接続出来なくなるので、その時に設定をロールバックするためです。

出来るだけみんなでログインした方がいいと思います。

2. 公開鍵をサーバーに登録

何かしらかの方法でサーバにアップロードしてください。最悪コピペでもいいです。

# 追加上書きをします
$ cat id_rsa.pub >> ~/.ssh/authorized_keys
# 権限は 600 が正しいです
$ chmod 600 ~/.ssh/authorized_keys

3. /etc/ssh/sshd_config の修正

参考:sshd の設定(sshd_config)

設定をミスった時のためにバックアップを用意しておきます。

$ cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bk
3.1. 認証方式を変更

公開鍵認証以外の認証方式を禁止します。

# パスワード認証禁止
PasswordAuthentication no
# チャレンジレスポンス認証禁止
ChallengeResponseAuthentication no
# Rhosts 認証禁止
RhostsRSAAuthentication no
IgnoreRhosts yes

パスワード無しユーザーのログインも禁止します。

PermitEmptyPasswords no

公開鍵認証を許可します。

RSAAuthentication yes
PubkeyAuthentication yes
3.2. 認証時の閾値変更

ついでにユーザがログインに成功するまでの制限時間を設定します。

デフォルトは 120 秒ですが、公開鍵認証であれば一瞬だと思うので 20 秒に変更しました。

ログインに失敗した時のリトライ回数も公開鍵認証であれば失敗しないと思うのデフォルトの 6 回から 3 回に変更します。

LoginGraceTime 20
MaxAuthTries 3
3.3. ポート番号を変更

SSH で利用しているポート番号は 22 番で、そのことは誰でも知っています。

そのため 22 番ポートは比較的狙われやすいポート番号となっています。

Port 22222

0 ~ 1023番までのウェルノウン・ポート以外で使っていないポート番号を指定してください。

今回は適当に 22222 番としました。

3.4. root ユーザーのログインを禁止

root ユーザーもほとんどのサーバで存在することがわかっているため標的とされやすいです。

root ユーザーを乗っ取られてしまうと何でも出来てしまうので、まず root ユーザーでログインを出来ないようにして乗っ取りのリスクを下げます。

PermitRootLogin no
3.5. プロトコルを Version 2 に制限

SSH のプロトコル Version1 には複数の脆弱性が見つかっている様なので、Version 2 を使う様に制限します。

Protocol 2

5. 再起動して設定反映

# 構文チェック
$ sshd -t
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
# 再起動
$ service sshd restart
# 22222 番ポートが LISTEN になっている事を確認
$ sudo lsof -i:22222
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 13492 root 3u IPv4 40777 0t0 TCP *:22222 (LISTEN)
sshd 13492 root 4u IPv6 40786 0t0 TCP *:22222 (LISTEN)

6. 各自 .ssh/config を追加

これはローカル PC 側の作業です。

今までの設定を SSH 接続する際にオプションで毎回指定するのは面倒だと思うので .ssh/config に設定を記載します。

$ cat .ssh/config
Host <エイリアス>
    HostName <サーバー名 or IP アドレス>
    User <ユーザー名>
    Port 22222
    Protocol 2
    IdentityFile ~/.ssh/id_rsa

7. 新規 SSH 接続で動作確認

再起動したら他のターミナルから新規で SSH 接続が出来るか確認して下さい。

SSH 接続できなければ設定をロールバックする必要があります。

ここまで確認して問題なければ、初めて最初の SSH 接続を切断していいです。

 

最後に

最後に自分が使っている AWS の Ubuntu のサーバの /etc/ssh/sshd_config と比べて見ます。

AWS のサーバはデフォルトで今回の設定がほとんど入っていました。

多くの利用者がいるレンタルサーバの /etc/ssh/sshd_config を真似するのも一つの手かもしれませんね。

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22222
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 20
MaxAuthTries 3
PermitRootLogin no
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile	%h/.ssh/authorized_keys

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

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

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

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no

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

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

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

Subsystem sftp /usr/lib/openssh/sftp-server

# 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

ClientAliveInterval 180

Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se