Last active
April 14, 2016 04:33
-
-
Save chris-x86-64/7087edbad2dc4f79badff901c307a8bd to your computer and use it in GitHub Desktop.
Login alert system using sendmail and Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add the following line to /etc/aliases | |
slack: |"/usr/bin/python /path/to/post-to-slack.py" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
( | |
echo "ALERT - Shell Access ($(hostname)) on: `date` `who`" | |
) | /usr/sbin/sendmail slack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import urllib | |
import urllib2 as urlrequest | |
import json | |
import mail | |
SLACK_POST_URL = "https://hooks.slack.com/services/[TOKEN]" | |
def build_attachment(): | |
b = email.message_from_string(sys.stdin.read()) | |
post_json = {"text": b.get_payload()} | |
return post_json | |
def post(payload): | |
payload_json = json.dumps(payload) | |
data = urllib.urlencode({"payload": payload_json}) | |
req = urlrequest.Request(SLACK_POST_URL) | |
response = urlrequest.build_opener(urlrequest.HTTPHandler()).open(req, data.encode('utf-8')).read() | |
return response.decode('utf-8') | |
post(build_attachment()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add the following line to /etc/pam.d/sshd | |
session optional pam_exec.so /bin/bash /path/to/login-hook.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment