
このAnsibleロールは、セキュリティ関連のssh設定を多数提供し、全体的な基本保護を実現します。
注意: このロールは hardening-collection に移行されました:
問題やプルリクエストはそちらでお願いします!
network_ipv6_enable
ssh_listen_to も IPv6 アドレス (例: [::]) をリッスンするように設定する必要があります。ssh_server_ports
ssh_client_port
ssh_listen_to
ssh_host_key_files
ssh_host_key_algorithms
ssh_client_alive_interval
ssh_client_alive_count
ssh_permit_tunnel
ssh_remote_hosts
defaults/main.yml を参照してください。ssh_permit_root_login
without-password または yes に設定します。ssh_allow_tcp_forwarding
'no'。TCP フォワーディングを許可するには 'yes' に設定します。OpenSSH >= 6.2 バージョンを使用している場合は、'yes'、'no'、'all'、または 'local' を指定できます。 'yes' および 'no' の値は引用符で囲んで渡す必要があります。ssh_gateway_ports
falsefalse。ワイルドカードアドレスへのバインドを強制するには true に設定します。クライアントがバインドするアドレスを指定できるようにするには clientspecified に設定します。ssh_allow_agent_forwarding
ssh_x11_forwarding
ssh_pam_support
ssh_use_pam
ssh_gssapi_support
ssh_kerberos_support
ssh_deny_users
ssh_allow_users
ssh_deny_groups
ssh_allow_groups
ssh_authorized_keys_file
ssh_trusted_user_ca_keys_file
ssh_trusted_user_ca_keys
ssh_trusted_user_ca_keys_file が設定されている場合のみ使用されます。ssh_authorized_principals_file
ssh_trusted_user_ca_keys_file が設定されている場合のみ使用されます。ssh_authorized_principals
default_custom.yml を参照してください。ssh_authorized_principals_file が設定されている場合のみ使用されます。ssh_print_motd
ssh_print_pam_motd
ssh_print_last_log
sftp_enabled
sftp_umask
sftp_chroot
sftp_chroot_dir
ssh_client_roaming
sshd_moduli_file
sshd_moduli_minimum
ssh_challengeresponseauthentication
ssh_client_password_login
truessh_server_password_login
truessh_banner
falsetruessh_banner_path
ssh_client_hardening
truefalsessh_client_port
'22'ssh_client_compression
falsessh_compression
falsessh_login_grace_time
30sssh_max_auth_retries
2ssh_max_sessions
10ssh_print_debian_banner
falsetruessh_server_enabled
truefalsessh_server_hardening
truefalsessh_server_match_address
ssh_server_match_group
ssh_server_match_user
ssh_server_match_local_port
ssh_server_permit_environment_vars
noyes。openssh バージョン 7.8 以降では、グローバルな "yes" または "no" 設定に加えて、環境変数名のホワイトリストを指定できます。ssh_server_accept_env_vars
ssh_use_dns
falsessh_server_revoked_keys
ssh_max_startups
ssh_macs
defaults/main.yml にあります。ssh_kex
defaults/main.yml にあります。ssh_ciphers
defaults/main.yml にあります。ssh_custom_options
sshd_custom_options
sshd_syslog_facility
sshd_log_level
sshd_strict_modes
sshd_authenticationmethods
publickeydefaults/main.yml にあります。上記にリストされていない ssh オプションを設定する場合は、ssh_custom_options (/etc/ssh/ssh_config 用) または sshd_custom_options (/etc/ssh/sshd_config 用) を使用して設定できます。これらのオプションはファイルの先頭に設定されるため、ファイルの後半でオプションを上書きできます。
プレイブックの例:
- hosts: localhost
roles:
- dev-sec.ssh-hardening
vars:
ssh_custom_options:
- "Include /etc/ssh/ssh_config.d/*"
sshd_custom_options:
- "AcceptEnv LANG"
このロールは、デフォルトポート 22 またはインベントリで設定されたポートを使用してサーバーに接続します。デフォルトの ssh ポートが ssh_server_ports で変更された場合、ssh サーバーが再起動されると、以前のポートを使用して接続を試み続けます。同じサーバーでこのロールを再度実行するには、新しい ssh ポートを使用するようにインベントリを更新する必要があります。
冪等性が重要な場合は、このロールのラッパーであり、設定されたポートに到達できない場合にポート 22 にフォールバックする ssh-hardening-fallback ロールの使用を検討してください。
- hosts: localhost
roles:
- dev-sec.ssh-hardening
ロールをローカルでテストする推奨方法は Docker を使用することです。システムに Docker をインストールする必要があります。システムに適した Docker パッケージについては Get started を参照してください。
vagrant と Virtualbox または VMWare を使用してローカルでテストを実行することもできます。システムに Virtualbox と Vagrant をインストールする必要があります。システムに適した vagrant パッケージについては Vagrant Downloads を参照してください。すべてのテストで test-kitchen を使用します。test-kitchen に慣れていない場合は、そのガイド を参照してください。
次に test-kitchen をインストールします:
# 依存関係のインストール
gem install bundler
bundle install
# 1 台のマシンでの高速テスト
bundle exec kitchen test ssh-ubuntu1804-ansible-latest
# すべてのマシンでのテスト
bundle exec kitchen test
# 開発用
bundle exec kitchen create ssh-ubuntu1804-ansible-latest
bundle exec kitchen converge ssh-ubuntu1804-ansible-latest
bundle exec kitchen verify ssh-ubuntu1804-ansible-latest
# クリーンアップ
bundle exec kitchen destroy ssh-ubuntu1804-ansible-latest
# 1 台のマシンでの高速テスト
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen test ssh-ubuntu-1804
# すべてのマシンでのテスト
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen test
# 開発用
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen create ssh-ubuntu-1804
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen converge ssh-ubuntu-1804
詳細については test-kitchen を参照してください。
アカウントにログインできません。クライアントキーを登録しましたが、それでもログインできません。
一般的な問題 (ファイアウォール、ネットワーク、キー不足、キー間違い、アカウント無効化など) をすべて確認した場合、アカウントがロックされている可能性があります。最も早い確認方法は、ユーザーのパスワードハッシュを確認することです:
sudo grep myuser /etc/shadow
ハッシュに ! が含まれている場合、アカウントはロックされています:
myuser:!:16280:7:60:7:::
これを解決する正しい方法は、アカウントのロックを解除することです (passwd -u myuser)。ユーザーにパスワードがない場合は、次の方法でロックを解除できます:
usermod -p "*" myuser
または、PAM を使用する予定がある場合は、ssh_use_pam: true で有効にしました。PAM はロックされたユーザーがキーでログインすることを許可します。
アプリケーションが SSH 経由で接続できなくなりました。なぜですか?
まず常にログファイルを確認し、可能であれば接続時に完了するクライアントとサーバー間のネゴシエーションを確認してください。
python および ruby ベースのアプリケーションで、古い暗号セットを使用しているために発生する問題をいくつか確認しています。これは、暗号、メッセージ認証コード (MAC)、および鍵交換 (KEX) アルゴリズムのリストをより安全な選択に減らしたこのハードニングモジュールと衝突します。
ロールを使用した後、Ansible の template/copy/file モジュールが機能しなくなりました!
このロールはデフォルトで SFTP を無効にします。Ansible はデフォルトで SFTP を使用してファイルをリモートホストに転送します。ansible.cfg で scp_if_ssh = True を設定する必要があります。これにより、Ansible は SCP を使用してファイルをコピーします。または、sftp_enabled を true に設定して SFTP を再度有効にすることもできます。
権限不足のため sshd サービスを再起動できません
ハンドラー "restart sshd" の実行時に次のエラーが発生した場合
Unable to restart service ssh: Failed to restart ssh.service: Access denied
または
failure 1 running systemctl show for 'ssh': Failed to connect to bus: No such file or directory
プレイブックを root として実行するか (プレイブックレベルで become: yes を使用せずに)、ハンドラーに become: yes を追加してください。
これは Ansible のバグです: 詳細については こちら と こちら を参照してください。
コントリビューターガイドライン を参照してください。
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.