Chuyển đến nội dung chính
  1. Bài viết/

Nhận thông báo Slack mỗi khi có người SSH vào máy chủ của bạn

· loading · loading ·
Nhân Tài Đức
Tác giả
Nhân Tài Đức
Người dẫn dắt đội ngũ và kỹ sư phần mềm, sống tại Seoul, Hàn Quốc

Mục đích
#

  • Với SSH, bạn có thể cho chạy lệnh mỗi khi có người thử đăng nhập. Bài này hướng dẫn cách kết nối với Slack và điều khiển thứ tự thực thi của ssh

Webhook trên Slack API
#

  • Xem hướng dẫn tạo app tại đây.

  • Bạn cần tạo một Slack app, bật webhook rồi tạo một incoming webhook để Slack nhận được thông báo từ SSH.

Viết script cho lần đăng nhập thất bại
#

  • Bạn có thể lưu tệp dưới đây ở bất cứ đâu trên hệ thống, tôi thì để ở /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

Với các sự kiện SSH khác
#

  • Với các sự kiện khác, chẳng hạn đăng nhập thành công, tôi khuyên bạn nên tạo hai tệp riêng.
  • Mỗi tệp chứa nội dung JSON tương ứng để gửi lên Slack.
  • Bạn sẽ cần sửa dòng fallback\": \"SSH login: $PAM_USER connected to \$host`"`
  • Ngay bên dưới là chỗ Openssh gọi các script này

Cấu hình ssh
#

Tôi đã comment dòng gửi thông báo khi đăng nhập thành công; muốn dùng thì bạn bỏ comment đi để script chạy và báo lên Slack. Dòng này:# auth optional pam_exec.so /var/opt/notify-login.sh

[success=2] nghĩa là nếu đăng nhập ssh thành công thì sẽ nhảy qua hai dòng, tức là bỏ qua phần thông báo đăng nhập thất bại.

Tệp: /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

Cảm ơn bạn!
#

  • Nếu có câu hỏi hay góp ý gì, bạn cứ liên hệ trực tiếp với tôi nhé