forked from rsmusllp/king-phisher-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmime_headers.py
71 lines (64 loc) · 2.68 KB
/
mime_headers.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
import distutils.version
import re
import king_phisher.version as version
import king_phisher.client.plugins as plugins
_min_version = '1.10.0'
StrictVersion = distutils.version.StrictVersion
api_compatible = StrictVersion(version.distutils_version) >= StrictVersion(_min_version)
class MimeHeaderParseError(ValueError):
def __init__(self, message, header_line):
super(MimeHeaderParseError, self).__init__(message, header_line)
self.message = message
self.header_line = header_line
class Plugin(plugins.ClientPlugin):
authors = ['Spencer McIntyre']
classifiers = ['Plugin :: Client :: Email :: Spam Evasion']
title = 'Custom Message MIME Headers'
description = """
Add custom MIME headers to messages that are sent. This can, for example be
used to add a Sender and / or a Return-Path header to outgoing messages.
Headers are rendered as template strings and can use variables that are
valid in messages.
"""
homepage = 'https://github.com/securestate/king-phisher-plugins'
options = [
plugins.ClientOptionString(
'headers',
'The MIME headers to add to each\nof the messages, one per line.',
display_name='MIME Headers',
**({'multiline': True} if api_compatible else {})
)
]
req_min_version = _min_version
version = '1.0.1'
_headers_split_regex = re.compile('^(?P<header>[\w-]+):\s*(?P<value>.+)?$')
def initialize(self):
mailer_tab = self.application.main_tabs['mailer']
self.signal_connect('message-create', self.signal_message_create, gobject=mailer_tab)
self.signal_connect('send-precheck', self.signal_send_precheck, gobject=mailer_tab)
return True
def get_headers(self, target=None):
headers = self.config.get('headers').split('\n')
for header_line in headers:
header_line = header_line.strip()
if not header_line:
continue
rendered_header_line = self.render_template_string(header_line, target=target, description='header line', log_to_mailer=False)
if rendered_header_line is None:
raise MimeHeaderParseError('failed to render', header_line)
match = self._headers_split_regex.match(rendered_header_line)
if match is None:
raise MimeHeaderParseError('failed to parse', rendered_header_line)
yield match.group('header'), match.group('value')
def signal_message_create(self, mailer_tab, target, message):
for header, value in self.get_headers(target):
message[header] = value
def signal_send_precheck(self, _):
try:
headers = tuple(self.get_headers())
except MimeHeaderParseError as error:
mailer_tab = self.application.main_tabs['mailer']
text_insert = mailer_tab.tabs['send_messages'].text_insert
text_insert("Custom MIME header error ({0}): {1}\n".format(error.message, error.header_line))
return False
return True