-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathpushbullet_notifications.py
107 lines (96 loc) · 3.45 KB
/
pushbullet_notifications.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
import king_phisher.plugins as plugin_opts
import king_phisher.server.database.manager as db_manager
import king_phisher.server.database.models as db_models
import king_phisher.server.plugins as plugins
import king_phisher.server.signals as signals
import king_phisher.utilities as utilities
try:
import pushbullet
except ImportError:
has_pushbullet = False
else:
has_pushbullet = True
EXAMPLE_CONFIG = """\
api_keys: <api-key>
identifier: King Phisher
mask: false
"""
class Plugin(plugins.ServerPlugin):
authors = ['Brandan Geise']
classifiers = ['Plugin :: Server :: Notifications']
title = 'Pushbullet Notifications'
description = """
A plugin that uses Pushbullet's API to send push notifications
on new website visits and submitted credentials.
"""
homepage = 'https://github.com/securestate/king-phisher-plugins'
options = [
plugin_opts.OptionString(
name='api_keys',
description='Pushbullet API key, if multiple, separate with comma'
),
plugin_opts.OptionString(
name='identifier',
description='King Phisher server identifier to send in push notification header',
default='King Phisher'
),
plugin_opts.OptionBoolean(
name='mask',
description='Partially mask email and campaign values',
default=False
)
]
req_min_version = '1.4.0'
req_packages = {
'pushbullet.py': has_pushbullet
}
version = '1.2'
def initialize(self):
signals.server_initialized.connect(self.on_server_initialized)
return True
def on_server_initialized(self, server):
signals.db_session_inserted.connect(self.on_kp_db_event, sender='visits')
signals.db_session_inserted.connect(self.on_kp_db_event, sender='credentials')
self.send_notification('Pushbullet notifications are now active')
def on_kp_db_event(self, sender, targets, session):
for event in targets:
message = db_manager.get_row_by_id(session, db_models.Message, event.message_id)
target_email, campaign_name = self.check_mask(message)
if sender == 'visits':
message = "New visit from {0} for campaign '{1}'".format(target_email, campaign_name)
elif sender == 'credentials':
message = "New credentials received from {0} for campaign '{1}'".format(target_email, campaign_name)
else:
return
self.send_notification(message)
def check_mask(self, message):
if self.config['mask']:
target_email = self.mask_string(message.target_email)
campaign_name = self.mask_string(message.campaign.name)
else:
target_email = message.target_email
campaign_name = message.campaign.name
return target_email, campaign_name
def mask_string(self, word):
if utilities.is_valid_email_address(word):
email_user, email_domain = word.split('@')
safe_string = "{0}@{1}{2}{3}".format(email_user, email_domain[:1], ('*' * (len(email_domain) - 2)), email_domain[-1:])
else:
safe_string = "{0}{1}{2}".format(word[:1], ('*' * (len(word) - 2)), word[-1:])
return safe_string
def send_notification(self, message):
api_keys = tuple(k.strip() for k in self.config['api_keys'].split(', '))
for key in api_keys:
device = None
if ':' in key:
device, key = key.split(':')
pb = pushbullet.Pushbullet(key)
if device:
try:
device = pb.get_device(device)
except pushbullet.errors.InvalidKeyError:
self.logger.error("failed to get pushbullet device: {0}".format(device))
try:
pb.push_note(self.config['identifier'], message, device=device)
except pushbullet.errors.PushError as error:
self.logger.error('failed to send the pushbullet note')