목적#
- SSH는 누군가가 로그인을 시도할 때 명령을 실행할 수 있습니다. 다음 문서에서는 Slack에 연결하고 SSH 실행 순서를 제어하는 방법을 보여줍니다.
Slack API의 웹훅#
앱 만들기에 대해 알아보려면 여기를 방문하세요.
Slack 앱을 만들고, 웹훅을 활성화하고, SSH로부터 알림을 받기 위한 수신 웹훅을 만들어야 합니다.
실패한 시도를 위한 스크립트 생성#
- 다음 파일은 시스템의 어디에나 저장할 수 있습니다. 저는
/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 이벤트에 대해#
- 성공적인 로그인과 같은 다른 이벤트의 경우 두 개의 파일을 만드는 것이 좋습니다.
- 각 파일에는 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 로그인 시도가 성공하면 두 줄을 건너뛰므로 실패한 시도 알림을 건너뛴다는 의미입니다.
파일: /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감사합니다!#
- 질문이나 의견이 있으시면 직접 연락주세요

