chrootをさせるときなど、OpenSSHではアクセスしてきたクライアントに対し、matchに記述した条件に応じてその後の処理を変えることができる。 これを利用することで、たとえば特定のネットワークアドレスからのアクセスの場合はchrootを、そうでない場合は普通にsshで接続させるようにするなどの指定が行える。

設定は「/etc/ssh/sshd_config」で、以下のような内容を追記してやる。 (ちなみに、調べた内容はCentOS 7の「openssh-server-6.6.1p1-33.el7_3.x86_64」なので、バージョンによっては差異があるかもしれない)

/etc/ssh/sshd_config
Match 条件1 条件合致後の設定 ... Match 条件2 条件合致後の設定 ...

Matchで指定できる条件には以下がある。 複数の条件に合致した場合を指定する際、スペース区切りで複数条件が指定可能だ。 各条件ごとに複数の値を指定する場合、「,」区切りで指定できる。 (この時、「User *,!root」みたいな感じで記述してあげると、root以外の全ユーザを対象にできる。一応、後の例で記述。)

  • User … アクセスしてきているユーザ名
  • Group … アクセスしてきているユーザのグループ
  • Host … アクセス元のホスト名(まぁ、あまり使わないだろう…)
  • LocalAddress … アクセスされているローカルアドレス(IPアドレスが複数ある場合などに使える)
  • LocalPort … アクセスされているローカルポート
  • Address … sshクライアントのIPアドレス。ワイルドカードでの指定や、CIDR形式での指定もできる。

条件合致後に指定できる設定は以下(manから抽出)。

  • AcceptEnv
  • AllowAgentForwarding
  • AllowGroups
  • AllowTcpForwarding
  • AllowUsers
  • AuthenticationMethods
  • AuthorizedKeysCommand
  • AuthorizedKeysCommandUser
  • AuthorizedKeysFile
  • AuthorizedPrincipalsFile
  • Banner
  • ChrootDirectory
  • DenyGroups
  • DenyUsers
  • ForceCommand
  • GatewayPorts
  • GSSAPIAuthentication
  • HostbasedAuthentication
  • HostbasedUsesNameFromPacketOnly
  • KbdInteractiveAuthentication
  • KerberosAuthentication
  • KerberosUseKuserok
  • MaxAuthTries
  • MaxSessions
  • PasswordAuthentication
  • PermitEmptyPasswords
  • PermitOpen
  • PermitRootLogin
  • PermitTTY
  • PermitTunnel
  • PubkeyAuthentication
  • RekeyLimit
  • RhostsRSAAuthentication
  • RSAAuthentication
  • X11DisplayOffset
  • X11MaxDisplays
  • X11Forwarding
  • X11UseLocalHost

ちなみに、この設定「上から順に条件に一致しているか調べる」ため、例えば1個目の条件にmatchしたとしてもそこで終了にはならず、2個め以降の条件にもmatchしているかどうかを確認しに行くことになる。 一応、一番最初にmatchした設定が優先される(2個め以降で同じ設定項目が出てきても、それは有効にならない)のだが、そこは注意かも。 以下の設定では、"test1"ユーザであればbannerで「test1」という表記が、"root以外のユーザ(test1ユーザも該当する)"+LocalAddressが"172.28.0.118"であれば「ABCDEFG」という表記+パスワード認証が無効化されるのだが、実際に実行してみると…。

Match User test1
    Banner /etc/test1

Match User *,!root LocalAddress 172.28.0.118
    Banner /etc/test2
    PasswordAuthentication no

blacknon@BS-PUB-DEVELOP:~$ ssh test1@172.28.0.118
test1
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
blacknon@BS-PUB-DEVELOP:~$ ssh test2@172.28.0.118
ABCDEFG
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
blacknon@BS-PUB-DEVELOP:~$ ssh test1@172.20.100.118
test1
test1@172.20.100.118's password:
Last login: Thu Mar 30 10:44:55 2017 from bs-pub-develop
[test1@BS-PUB-CENT7-01 ~]$ exit
ログアウト
Connection to 172.20.100.118 closed.
blacknon@BS-PUB-DEVELOP:~$ ssh test2@172.20.100.118
test2@172.20.100.118's password:
Last login: Thu Mar 30 10:29:02 2017 from bs-pub-develop
[test2@BS-PUB-CENT7-01 ~]$

このように、2個目にMatchする条件についても適用されるので注意。 (設定項目がかぶってるのは1個目の条件が適用されるので、状況次第かとは思うが)

この他、条件指定時に%uで対象のユーザを、%hでユーザのホームディレクトリを指定できたりする。

設定例.アクセスのあったネットワークに応じてChrootDirectoryを切り替える(管理者用のユーザだけ除外)

設定例として、こんな感じに記述することで、アクセスのあったネットワークに応じてChrootDirectoryするディレクトリを切り替えさせることができる。 ※この時、特定のユーザ(admin)だけはChrootDirectoryさせないようにする。

Match User *,!admin Address 172.28.0.*
    ChrootDirectory /opt/jail/master

Match User *,!admin Address 192.168.0.*
    ChrootDirectory /opt/jail/sub

設定例2.特定のユーザ+アクセス元ネットワークのみパスワード認証を許可

特定のユーザとアクセス元のネットワークの組み合わせのときのみ、パスワード認証を許可する場合。

PasswordAuthentication no

Match User admin Address 172.28.0.*
    PasswordAuthentication yes

色々なことに応用できそうだ。うまいこと使いこなしていきたい。

参考:manの該当箇所

Match

 Introduces a conditional block.  If all of the criteria on the Match line are satisfied, the keywords on the following lines override those set in the global section of the config file, until either another Match line or the end of the file.  If a keyword appears in multiple Match blocks that are satisified, only the first instance of the keyword is applied.

 The arguments to Match are one or more criteria-pattern pairs or the single token All which matches all criteria.  The available criteria are User, Group, Host, LocalAddress, LocalPort, and Address.  The match patterns may consist of single entries or comma-separated lists and may use the wildcard and negation operators described in the PATTERNS section of ssh_config(5).

 The patterns in an Address criteria may additionally contain addresses to match in CIDR address/masklen format, e.g. “192.0.2.0/24” or “3ffe:ffff::/32”.  Note that the mask length provided must be consistent with the address - it is an error to specify a mask length that is too long for the address or one with bits set in this host portion of the address.  For example, “192.0.2.0/33” and “192.0.2.0/8” respectively.

 Only a subset of keywords may be used on the lines following a Match keyword.  Available keywords are AcceptEnv, AllowAgentForwarding, AllowGroups, AllowTcpForwarding, AllowUsers, AuthenticationMethods,AuthorizedKeysCommand, AuthorizedKeysCommandUser, AuthorizedKeysFile, AuthorizedPrincipalsFile, Banner, ChrootDirectory, DenyGroups, DenyUsers, ForceCommand, GatewayPorts, GSSAPIAuthentication, HostbasedAuthentication,HostbasedUsesNameFromPacketOnly, KbdInteractiveAuthentication, KerberosAuthentication, KerberosUseKuserok, MaxAuthTries, MaxSessions, PasswordAuthentication, PermitEmptyPasswords, PermitOpen, PermitRootLogin,PermitTTY, PermitTunnel, PubkeyAuthentication, RekeyLimit, RhostsRSAAuthentication, RSAAuthentication, X11DisplayOffset, X11MaxDisplays, X11Forwarding and X11UseLocalHost.

参考