forked from DanaEpp/PortScanHoneypot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks.py
154 lines (129 loc) · 5.46 KB
/
webhooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
from enum import IntEnum
import logging
import requests
import json
import pymsteams
import os
import sys
import socket
from email.mime.text import MIMEText
import email.utils
from subprocess import Popen, PIPE
import smtplib, ssl
username="honeypot"
class WebHookType(IntEnum):
NONE = 0
GENERIC = 1
SLACK = 2
TEAMS = 3
DISCORD = 4
EMAIL_ONLY = 5
class WebHook:
DEFAULT_HEADERS = {'Content-Type': 'application/json'}
def __init__(self, url, hooktype=WebHookType.GENERIC, email="", sendmail = False, smarthost = "", mail_port = 25, mail_user = "", mail_pass = "", use_ssl = False):
self.url = url
self.hooktype = hooktype
self.email = email
self.sendmail = sendmail
self.smarthost = smarthost
self.mail_user = mail_user
self.mail_pass = mail_pass
self.mail_port = mail_port
self.use_ssl = use_ssl
def notify(self, message):
if message:
# Why oh why can't python support switch/case??? >:(
if self.hooktype == WebHookType.GENERIC:
self.__send_to_generic_webhook(message)
elif self.hooktype == WebHookType.SLACK:
self.__send_to_slack(message)
elif self.hooktype == WebHookType.TEAMS:
self.__send_to_teams(message)
elif self.hooktype == WebHookType.DISCORD:
self.__send_to_discord(message)
if self.sendmail:
self.__send_to_email(message)
def __send_to_email(self, message):
logging.debug( "[SENDMAIL] {0}".format(message))
try:
hn = socket.gethostname()
msg = MIMEText(message)
msg["Subject"] = f"[{hn}] Honeypot Triggered!"
msg["To"] = self.email
if len(self.mail_user) == 0:
self.mail_user = f"{username}@{hn}"
msg["From"] = self.mail_user
msg["Sender"] = self.mail_user
msg["Date"] = email.utils.formatdate(localtime=True)
# use specified server
if len(self.smarthost):
if self.use_ssl:
context = ssl.create_default_context()
with smtplib.SMTP_SSL(self.smarthost, self.mail_port, context = context) as server:
if len(self.mail_pass):
server.login(self.mail_user, self.mail_pass)
server.sendmail(self.mail_user, self.email, msg.as_bytes())
else:
with smtplib.SMTP(self.smarthost, self.mail_port) as server:
if len(self.mail_pass):
server.login(self.mail_user, self.mail_pass)
server.sendmail(self.mail_user, self.email, msg.as_bytes())
# use MX
else:
#p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE) # for plain sendmail mta
p = Popen(["/usr/sbin/sendmail", self.email], stdin=PIPE) # for exim
p.communicate(msg.as_bytes())
if not p.returncode == 0:
logging.debug( "Failed to send notification via sendmail. Return code: {0}".format(p.returncode))
except Exception as e:
logging.exception(e)
def __send_to_generic_webhook(self, message):
logging.debug( "[WEBHOOK] {0}".format(message))
data = {
'content': message,
'username': username
}
try:
response = requests.post( self.url, data=json.dumps(data), headers=WebHook.DEFAULT_HEADERS)
if not response.ok:
logging.debug( "Failed to send notification via Generic webhook. Server response: {0}".format(response.text))
except Exception as e:
logging.exception(e)
# See https://api.slack.com/messaging/webhooks for more info
def __send_to_slack(self, message):
logging.debug( "[SLACK] {0}".format(message))
# Remember to set your webhook up at https://my.slack.com/services/new/incoming-webhook/
data = {
'text': message,
'username': 'Port Scan Honeypot',
'icon_emoji': ':skull_and_crossbones:'
}
try:
response = requests.post( self.url, data=json.dumps(data), headers=WebHook.DEFAULT_HEADERS)
if not response.ok:
logging.debug( "Failed to send notification via Slack. Server response: {0}".format(response.text))
except Exception as e:
logging.exception(e)
# See https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook
def __send_to_teams(self, message):
logging.debug( "[TEAMS] {0}".format(message))
try:
teams = pymsteams.connectorcard(self.url)
teams.title( "Port scan detected!")
teams.text(message)
teams.send()
except Exception as e:
logging.exception(e)
def __send_to_discord(self, message):
logging.debug( "[DISCORD] {0}".format(message))
data = {
'content': message,
'username': username
}
try:
response = requests.post( self.url, data=json.dumps(data), headers=WebHook.DEFAULT_HEADERS)
if not response.ok:
logging.debug( "Failed to send notification via Discord. Server response: {0}".format(response.text))
except Exception as e:
logging.exception(e)