a2ccabb9e813069b8776edbf2b98ccee6e1772a1
bn8 Initial commit

bn8 authored 11 years ago

1) #! /usr/bin/env python
2) # -*- coding: utf-8 -*-
3) 
4) # smtpt -- Simple command-line tool to test SMTP server
5) # By: Benjamin RENARD <brenard@easter-eggs.com>
6) #
7) # Copyright (C) 2011 Easter-eggs
Benjamin Renard Fixed project URL

Benjamin Renard authored 11 years ago

8) # http://git.zionetrix.net/mailt
bn8 Initial commit

bn8 authored 11 years ago

9) #
10) # This file is part of MailT.
11) #
12) # MailT is free software; you can redistribute it and/or modify
13) # it under the terms of the GNU General Public License as published
14) # by the Free Software Foundation, either version 3 of the License,
15) # or (at your option) any later version.
16) #
17) # MailT is distributed in the hope that it will be useful,
18) # but WITHOUT ANY WARRANTY; without even the implied warranty of
19) # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20) # GNU Affero General Public License for more details.
21) #
22) # You should have received a copy of the GNU General Public License
23) # along with this program.  If not, see <http://www.gnu.org/licenses/>.
24) 
25) from optparse import OptionParser
26) 
27) import os
28) import smtplib
29) import email.utils
30) from email.MIMEText import MIMEText
31) from email.MIMEMultipart import MIMEMultipart
32) from email.MIMEBase import MIMEBase
33) from email import Encoders
Benjamin Renard Add interactive authenticat...

Benjamin Renard authored 8 years ago

34) import getpass
bn8 Initial commit

bn8 authored 11 years ago

35) 
36) import logging
37) import sys
38) 
39) parser = OptionParser()
40) 
41) default_subject="Simple test message"
42) 
43) parser.add_option('-s',
44)                   '--subject',
45)                   action="store",
46)                   type="string",
47)                   dest="subject",
48)                   help="The subject",
49)                   default=default_subject)
50) 
51) parser.add_option('-f',
52)                   '--from',
53)                   action="store",
54)                   type="string",
55)                   dest="author",
56)                   help="The sender email",
57)                   default="author@example.com")
58) 
59) parser.add_option('-t',
60)                   '--to',
61)                   action="store",
62)                   type="string",
63)                   dest="to",
64)                   help="The recipient email",
65)                   default="recipient@example.com")
66) 
67) parser.add_option('-c',
68)                   '--content',
69)                   action="store",
70)                   type="string",
71)                   dest="content",
72)                   help="The content",
73)                   default="This is the body of the message.")
74) 
75) parser.add_option('-H',
76)                   '--host',
77)                   action="store",
78)                   type="string",
79)                   dest="host",
80)                   help="The SMTP host",
81)                   default="127.0.0.1")
82) 
83) parser.add_option('-p',
84)                   '--port',
85)                   action="store",
86)                   type="string",
87)                   dest="port",
88)                   help="The SMTP port",
89)                   default=None)
90) 
91) parser.add_option('-S',
92)                   '--ssl',
93)                   action="store_true",
94)                   dest="ssl",
95)                   help="SMTP Over SSL (SMTPS)",
96)                   default=False)
97) 
98) parser.add_option('-T',
99)                   '--starttls',
100)                   action="store_true",
101)                   dest="starttls",
102)                   help="Use STARTTLS",
103)                   default=False)
104) 
105) parser.add_option('-U',
106)                   '--user',
107)                   action="store",
108)                   type="string",
109)                   dest="user",
110)                   help="SMTP Auth - User login",
111)                   default=None)
112) 
113) parser.add_option('-P',
114)                   '--password',
115)                   action="store",
116)                   type="string",
117)                   dest="password",
118)                   help="SMTP Auth - User password",
119)                   default=None)
120) 
121) parser.add_option('-a',
122)                   '--attachement',
123)                   action="append",
124)                   type="string",
125)                   dest="attach",
126)                   help="The attachment(s) file(s) path",
127)                   default=None)
128) 
129) parser.add_option('-d',
130)                   '--debug',
131)                   action="store_true",
132)                   dest="debug")
133) 
134) parser.add_option('-v',
135)                   '--verbose',
136)                   action="store_true",
137)                   dest="verbose")
138) 
139) parser.add_option('--spam',
140)                   action="store_true",
141)                   dest="spam")
142) 
143) parser.add_option('--virus',
144)                   action="store_true",
145)                   dest="virus")
146) 
147) 
148) (options, args) = parser.parse_args()
149) 
150) if options.spam:
151) 	if options.subject == default_subject:
152) 		options.subject = "Test spam message"
153) 	options.content="""This is the GTUBE, the
154) 	Generic
155) 	Test for
156) 	Unsolicited
157) 	Bulk
158) 	Email
159) 
160) If your spam filter supports it, the GTUBE provides a test by which you
161) can verify that the filter is installed correctly and is detecting incoming
162) spam. You can send yourself a test mail containing the following string of
163) characters (in upper case and with no white spaces and line breaks):
164) 
165) XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
166) 
167) You should send this test mail from an account outside of your network."""
168) elif options.virus:
169) 	if options.subject == default_subject:
170) 		options.subject = "Test virus message"
Benjamin Renard Fixed smtpt virus message

Benjamin Renard authored 11 years ago

171) 	options.content="The attached file contain the EICAR standard antivirus test string."
bn8 Initial commit

bn8 authored 11 years ago

172) 
173) if not options.port:
174) 	if options.ssl:
175) 		options.port=465
176) 	else:
177) 		options.port=25
178) 
179) if options.debug:
180) 	loglevel=logging.DEBUG
181) elif options.verbose:
182) 	loglevel=logging.INFO
183) else:
184) 	loglevel=logging.WARNING
185) 
186) logging.basicConfig(level=loglevel,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
187) 
188) # Create the message
189) msg = MIMEMultipart()
190) msg['To'] = email.utils.formataddr(('Recipient', options.to))
191) logging.info("To : %s" % options.to)
192) msg['From'] = email.utils.formataddr(('Author', options.author))
193) logging.info("From : %s" % options.author)
194) msg['Subject'] = options.subject
195) logging.info("Subject : %s" % options.subject)
Benjamin Renard Add required Date header

Benjamin Renard authored 8 years ago

196) msg['Date'] = email.utils.formatdate(None,True)
bn8 Initial commit

bn8 authored 11 years ago

197) 
198) 
199) msg.attach( MIMEText(options.content) )
200) 
201) if options.attach:
202) 	for filepath in options.attach:
203) 		logging.info("Attache file %s" % filepath)
204) 		part = MIMEBase('application', "octet-stream")
205) 		part.set_payload( open(filepath,"rb").read() )
206) 		Encoders.encode_base64(part)
207) 		part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filepath))
208) 		msg.attach(part)
209) 
Benjamin Renard Fixed smtpt virus message

Benjamin Renard authored 11 years ago

210) if options.virus:
211) 	part = MIMEBase('application', "octet-stream")
212) 	part.set_payload("X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*")
213) 	part.add_header('Content-Disposition', 'attachment; filename="eicar.bin"')
214) 	msg.attach(part)
215) 
bn8 Initial commit

bn8 authored 11 years ago

216) if options.ssl:
217) 	logging.info("Establish SSL connexion to server")
218) 	server = smtplib.SMTP_SSL(options.host,options.port)
219) else:
220) 	logging.info("Establish connexion to server")
221) 	server = smtplib.SMTP(options.host,options.port)
222) 
223) 	if options.starttls:
224) 		try:
225) 			logging.info("Start TLS")
226) 			server.starttls()
227) 		except Exception as e:
228) 			logging.critical('Error using STARTTLS : %s' % e)
229) 			server.quit()
230) 			sys.exit(2)
231) 
232) if options.debug:
233) 	logging.info("Establish connexion to server")
234) 	server.set_debuglevel(True)
235) 
236) if options.user:
237) 	error = None
Benjamin Renard Add interactive authenticat...

Benjamin Renard authored 8 years ago

238) 	if not options.password:
239) 		options.password=getpass.getpass()