目的#
- SSHは誰かがログインを試みたときにコマンドを実行できます。この文書では、Slackに接続してSSHの実行順序を制御する方法を説明します。
Slack APIでのWebhook#
アプリの作成について学ぶにはこちらをご覧ください。
Slackアプリを作成し、Webhookを有効にし、SSHから通知を受信するためのIncoming Webhookを作成する必要があります。
失敗した試行のためのスクリプトの作成#
- 以下のファイルはシステムのどこにでも保存できます。私は
/var/opt/notify-attempt.shにファイルを配置しました。
#!/bin/bash
if [ "$PAM_TYPE" != "close_session" ]; then
url="<YOUR SLACK WEBHOOK>"
channel="#channel"
host="$(hostname)"
content="\"attachments\": [ { \"mrkdwn_in\": [\"text\", \"fallback\"], \"fallback\": \"SSH login: $PAM_USER connected to \`$host\`\", \"text\": \"SSH login to \`$host\`\", \"fields\": [ { \"title\": \"User\", \"value\": \"$PAM_USER\", \"short\": true }, { \"title\": \"IP Address\", \"value\": \"$PAM_RHOST\", \"short\": true } ], \"color\": \"#F35A00\" } ]"
curl -X POST --data-urlencode "payload={\"channel\": \"$channel\", \"mrkdwn\": true, \"username\": \"SSH Notifications\", $content, \"icon_emoji\": \":inbox-tray:\"}" "$url" &
fi
exit他のSSHイベントについて#
- 成功したログインなどの他のイベントについては、2つのファイルを作成することをお勧めします。
- 各ファイルには、Slackに送信される正しいJSONコンテンツが含まれます。
fallback\": \"SSH login: $PAM_USER connected to \$host`"` という行を編集する必要があります。- 以下に、Opensshがこれらのスクリプトをどのように使用するかを示します。
sshの設定#
成功した試行の通知をコメントアウトしています。コメントを解除してスクリプトを実行し、Slackに通知できます。
この行:# auth optional pam_exec.so /var/opt/notify-login.sh
[success=2] は、sshログインの試行が成功した場合、2行ジャンプすることを意味し、失敗した試行の通知をスキップします。
ファイル:/etc/pam.d/common-auth
# /etc/pam.d/common-auth - authentication settings common to all services
#
# This file is included from other service-specific PAM config files,
# and should contain a list of the authentication modules that define
# the central authentication scheme for use on the system
# (e.g., /etc/shadow, LDAP, Kerberos, etc.). The default is to use the
# traditional Unix authentication mechanisms.
#
# As of pam 1.0.1-6, this file is managed by pam-auth-update by default.
# To take advantage of this, it is recommended that you configure any
# local modules either before or after the default block, and use
# pam-auth-update to manage selection of other modules. See
# pam-auth-update(8) for details.
# here are the per-package modules (the "Primary" block)
auth [success=2 default=ignore] pam_unix.so nullok
auth optional pam_exec.so /var/opt/notify-attempt.sh
# here's the fallback if no module succeeds
auth requisite pam_deny.so
# prime the stack with a positive return value if there isn't one already;
# this avoids us returning an error just because nothing sets a success code
# since the modules above will each just jump around
# For successful login, uncomment the below line, ensure the file exists.
# auth optional pam_exec.so /var/opt/notify-login.sh
# auth required pam_permit.so
# and here are more per-package modules (the "Additional" block)
auth optional pam_cap.so
# end of pam-auth-update configありがとうございます!#
- ご質問やコメントがございましたら、直接お問い合わせください

