-
Notifications
You must be signed in to change notification settings - Fork 66
/
emailaction.py
52 lines (45 loc) · 2.04 KB
/
emailaction.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
# -*- coding: utf-8 -*-
import re
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from .basicaction import BasicAction
from pastepwn.util import TemplatingEngine
class EmailAction(BasicAction):
"""This action takes a username, password, receiver mail address,
hostname, port and when executed sends out an e-mail to the receiver
containing the paste"""
name = "EmailAction"
def __init__(self, username, password, receiver, hostname, port=465, template=None):
super().__init__()
mail_regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
if username is None or not re.match(mail_regex, username):
raise ValueError('Invalid username !')
else:
self.username = username
if receiver is None or not re.match(mail_regex, receiver):
raise ValueError('Invalid reciever address !')
else:
self.receiver = receiver
self.password = password
self.hostname = hostname
self.port = port
self.template = template
def perform(self, paste, analyzer_name=None):
"""
Sends an email to the specified receiver with the paste's content
:param paste: The paste passed by the ActionHandler
:param analyzer_name: The name of the analyzer which matched the paste
:return: None
"""
text = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template)
email = MIMEMultipart()
email['From'] = self.username
email['To'] = self.receiver
email['Subject'] = 'Paste matched by pastepwn via analyzer "{}"'.format(analyzer_name)
email.attach(MIMEText(text, 'plain'))
# TODO there should be a way to use starttls - check https://realpython.com/python-send-email/
with smtplib.SMTP_SSL(self.hostname, self.port) as smtp:
smtp.login(self.username, self.password)
text = email.as_string()
smtp.sendmail(self.username, self.receiver, text)