forked from thinkst/canarytokens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.py
184 lines (151 loc) · 7.02 KB
/
channel.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
""""
Base class for all canarydrop channels.
"""
import datetime
import simplejson
import settings
from exception import DuplicateChannel
from twisted.logger import Logger
log = Logger()
class Channel(object):
CHANNEL = 'Base'
def __init__(self, switchboard=None, name=None):
self.switchboard = switchboard
self.name = name or self.CHANNEL
log.info('Started channel {name}'.format(name=self.name))
class InputChannel(Channel):
CHANNEL = 'InputChannel'
def __init__(self, switchboard=None, name=None, unique_channel=False):
super(InputChannel, self).__init__(switchboard=switchboard,
name=name)
try:
self.register_input_channel()
except DuplicateChannel as e:
if unique_channel:
raise e
def register_input_channel(self,):
self.switchboard.add_input_channel(name=self.name, channel=self)
def format_additional_data(self, **kwargs):
return ''
def format_webhook_canaryalert(self,canarydrop=None, protocol=settings.PROTOCOL,
host=settings.PUBLIC_DOMAIN, **kwargs):
payload = {}
if not host or host == '':
host=settings.PUBLIC_IP
payload['channel'] = self.name
payload['time'] = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S (UTC)")
payload['memo'] = canarydrop.memo
payload['manage_url'] = '{protocol}://{host}/manage?token={token}&auth={auth}'\
.format(protocol=protocol,
host=host,
token=canarydrop['canarytoken'],
auth=canarydrop['auth'])
payload['additional_data'] = kwargs
return payload
def format_slack_canaryalert(self,canarydrop=None, protocol=settings.PROTOCOL,
host=settings.PUBLIC_DOMAIN, **kwargs):
payload = {}
fields = []
if not host or host == '':
host=settings.PUBLIC_IP
manage_link = '{protocol}://{host}/manage?token={token}&auth={auth}'\
.format(protocol=protocol,
host=host,
token=canarydrop['canarytoken'],
auth= canarydrop['auth'])
attachment = {
'title':'Canarytoken Triggered\n',
'title_link': manage_link,
'mrkdwn_in': ['title'],
'fallback' : 'Canarytoken Triggered: {link}'.format(link=manage_link)
}
fields.append({'title':'Channel','value':self.name})
fields.append({'title':'Memo', 'value': canarydrop.memo})
fields.append({'title':'Time', 'value': datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S (UTC)")})
fields.append({'title':'Manage','value': manage_link})
attachment['fields'] = fields
payload['attachments'] = [attachment]
return payload
def format_canaryalert(self, canarydrop=None, protocol=settings.PROTOCOL,
host=settings.PUBLIC_DOMAIN, params=None, **kwargs):
msg = {}
if not host or host == '':
host=settings.PUBLIC_IP
if 'useragent' in kwargs:
msg['useragent'] = kwargs['useragent']
if 'referer' in kwargs:
msg['referer'] = kwargs['referer']
if 'location' in kwargs:
msg['location'] = kwargs['location']
if 'src_ip' in kwargs:
msg['src_ip'] = kwargs['src_ip']
msg['time'] = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S (UTC)")
msg['channel'] = self.name
if 'src_data' in kwargs and 'aws_keys_event_source_ip' in kwargs['src_data']:
msg['src_ip'] = kwargs['src_data']['aws_keys_event_source_ip']
msg['channel'] = 'AWS API Key Token'
if 'src_data' in kwargs and 'aws_keys_event_user_agent' in kwargs['src_data']:
msg['useragent'] = kwargs['src_data']['aws_keys_event_user_agent']
if 'src_data' in kwargs and 'log4_shell_computer_name' in kwargs['src_data']:
msg['log4_shell_computer_name'] = kwargs['src_data']['log4_shell_computer_name']
if params.get('body_length', 999999999) <= 140:
msg['body'] = """Canarydrop@{time} via {channel_name}: """\
.format(channel_name=self.name,
time=msg['time'])
capacity = 140 - len(msg['body'])
msg['body'] += canarydrop.memo[:capacity]
else:
msg['body'] = """
One of your canarydrops was triggered.
Channel: {channel_name}
Time : {time}
Memo : {memo}
{additional_data}
Manage your settings for this Canarydrop:
{protocol}://{host}/manage?token={token}&auth={auth}""".format(
channel_name=self.name,
time=msg['time'],
memo=canarydrop.memo,
additional_data=self.format_additional_data(**kwargs),
protocol=protocol,
host=host,
token=canarydrop['canarytoken'],
auth=canarydrop['auth']
)
msg['manage'] = '{protocol}://{host}/manage?token={token}&auth={auth}'\
.format(protocol=protocol,
host=host,
token=canarydrop['canarytoken'],
auth=canarydrop['auth'])
msg['history'] = '{protocol}://{host}/history?token={token}&auth={auth}'\
.format(protocol=protocol,
host=host,
token=canarydrop['canarytoken'],
auth=canarydrop['auth'])
if params.get('subject_required', False):
msg['subject'] = settings.ALERT_EMAIL_SUBJECT
if params.get('from_display_required', False):
msg['from_display'] = settings.ALERT_EMAIL_FROM_DISPLAY
if params.get('from_address_required', False):
msg['from_address'] = settings.ALERT_EMAIL_FROM_ADDRESS
return msg
def dispatch(self, **kwargs):
self.switchboard.dispatch(input_channel=self.name, **kwargs)
class OutputChannel(Channel):
CHANNEL = 'OutputChannel'
def __init__(self, switchboard=None, name=None):
super(OutputChannel, self).__init__(switchboard=switchboard,
name=name)
self.register_output_channel()
def register_output_channel(self,):
self.switchboard.add_output_channel(name=self.name, channel=self)
def send_alert(self, input_channel=None, canarydrop=None, **kwargs):
if not input_channel:
raise Exception('Cannot send an alert when the input_channel is None')
if not canarydrop:
raise Exception('Cannot send an alert when the canarydrop is None')
self.do_send_alert(input_channel=input_channel,
canarydrop=canarydrop,
**kwargs)
def do_send_alert(self, **kwargs):
pass