Skip to content

Instantly share code, notes, and snippets.

@jcubic
Created April 3, 2023 09:43
Show Gist options
  • Save jcubic/f860953a7228c91c8a884fe8556bce60 to your computer and use it in GitHub Desktop.
Save jcubic/f860953a7228c91c8a884fe8556bce60 to your computer and use it in GitHub Desktop.
Script that sends spoofed emails
#!/usr/bin/env python
from smtplib import SMTP_SSL as SMTP
from email.mime.text import MIMEText
from optparse import OptionParser
from email.Utils import formatdate
import sys
if __name__ == '__main__':
from sys import argv
opt = OptionParser()
opt.add_option('', '--host')
opt.add_option('-u', '--user')
opt.add_option('-p', '--password')
opt.add_option('-f', '--sender')
opt.add_option('-t', '--to')
opt.add_option('-m', '--message')
opt.add_option('-s', '--subject')
try:
(options, args) = opt.parse_args()
if len(args) == 0:
if options.host is None:
raise ValueError("no --host option")
elif options.user is None:
raise ValueError("no --user option")
elif options.password is None:
raise ValueError("no --password option")
elif options.sender is None:
raise ValueError("no --from option")
elif options.to is None:
raise ValueError("no --to option")
elif options.message is None:
raise ValueError("no --message option")
elif options.subject is None:
raise ValueError("no --subject option")
else:
text_subtype = 'plain'
msg = MIMEText(options.message, text_subtype)
msg['Subject'] = options.subject
msg['To'] = options.to
msg['From'] = options.sender
msg['Date'] = formatdate()
conn = SMTP(options.host)
conn.set_debuglevel(False)
conn.login(options.user, options.password)
try:
conn.sendmail(options.sender, options.to, msg.as_string())
except Exception, e:
raise e
finally:
conn.quit()
except ValueError, e:
opt.error(e.message)
except Exception, e:
sys.exit("mail failed; %s" % e.message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment