-
Notifications
You must be signed in to change notification settings - Fork 4
/
smtp.py
executable file
·145 lines (127 loc) · 4.23 KB
/
smtp.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import time
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import Encoders
from email.Utils import COMMASPACE, formatdate
from Peach.publisher import Publisher
from Peach.Engine.common import PeachException
class SMTPPublisher(Publisher):
def __init__(self, host="localhost",
port=smtplib.SMTP_PORT,
debugLevel=0,
mailFrom="localhost@localhost",
mailTo="localhost@localhost",
username="",
password=""):
Publisher.__init__(self)
self.host = host
try:
self.port = int(port)
except:
raise PeachException("The SMTP publisher parameter for port is not a valid number.")
self.debugLevel = int(debugLevel)
self.mailFrom = mailFrom
self.mailTo = mailTo
self.username = username
self.password = password
self.loadBalance = 0
self._connected = None
def start(self):
if self._connected:
return
print("[*] Connecting to %s:%d ..." % (self.host, self.port))
try:
self.smtp = smtplib.SMTP(self.host, self.port)
except:
raise PeachException("Peer %s:%d is down or connection settings are wrong." % (self.host, self.port))
self._connected = True
self.smtp.set_debuglevel(self.debugLevel)
def connect(self):
pass
def send(self, data):
if not self.loadBalance % 500 and self.loadBalance != 0:
print("[*] Pause ...")
time.sleep(10)
for i in range(3):
try:
self.smtp.sendmail(self.mailFrom, self.mailTo, data)
exception = None
break
except:
exception = sys.exc_info()
time.sleep(5)
if exception:
reason = ""
try:
reason = str(exception[1])
except:
reason = "unknown reason."
message = "SMTP send mail to %s:%d failed: %s" % (self.host, self.port, reason)
if message.find("5.4.0") > -1:
print(message)
else:
self.smtp.close()
raise PeachException(message)
self.loadBalance += 1
def close(self):
pass
def stop(self):
pass
class EmailAttachment(Publisher):
"""
Send fuzzed data as email attachment.
"""
def __init__(self, server, fileName, msgTo, msgFrom="peach@peach.org",
msgSubject="Fuzzing Test",
msgText="Message generated by Peach Fuzzing Platform.\n\nhttp://peachfuzzer.com\n\n - Peach\n"):
Publisher.__init__(self)
self.server = server
self.fileName = fileName
self.msgFrom = msgFrom
self.msgTo = msgTo
self.msgSubject = msgSubject
self.msgText = msgText
def send(self, data):
"""
Publish some data
@type data: string
@param data: Data to publish
"""
# Build Message Body
msg = MIMEMultipart()
msg['From'] = self.msgFrom
msg['To'] = self.msgTo
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = self.msgSubject
msg.attach(MIMEText(self.msgText))
# Attach file
part = MIMEBase('application', 'pdf')
part.set_payload(data)
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % self.fileName)
msg.attach(part)
# Send email
smtp = smtplib.SMTP(self.server)
smtp.sendmail(self.msgFrom, self.msgTo, msg.as_string())
smtp.close()
def connect(self):
"""
Called to connect or open a connection/file.
"""
pass
def close(self):
"""
Close current stream/connection.
"""
pass
class _OleStorage(object):
"""
This class wraps OLE Storage APIs
"""
pass