From e79372f8ac7d0c8652d5d06f65b058fd63bf78ce Mon Sep 17 00:00:00 2001 From: geefunkmasterpro Date: Wed, 27 Feb 2013 21:33:48 +1100 Subject: [PATCH 1/2] Added auth field to config so that emails are harder to track to sender Added error handling to identify: - errors creating the mail headers - errors processing JSON input - errors in the mailer configuration --- extensions/social_engineering/config.yaml | 3 +- .../mass_mailer/mass_mailer.rb | 53 +++++++++++-------- .../rest/socialengineering.rb | 13 +++-- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/extensions/social_engineering/config.yaml b/extensions/social_engineering/config.yaml index a2b03233f..4a2d0af37 100644 --- a/extensions/social_engineering/config.yaml +++ b/extensions/social_engineering/config.yaml @@ -21,8 +21,9 @@ beef: use_auth: true use_tls: true helo: "gmail.com" # this is usually the domain name - from: "youruser@gmail.com" + auth: "youruser@gmail.com" password: "yourpass" + from: "fromemail@gmail.com" # available templates templates: default: diff --git a/extensions/social_engineering/mass_mailer/mass_mailer.rb b/extensions/social_engineering/mass_mailer/mass_mailer.rb index c62ebf7c8..aa586fe13 100644 --- a/extensions/social_engineering/mass_mailer/mass_mailer.rb +++ b/extensions/social_engineering/mass_mailer/mass_mailer.rb @@ -20,8 +20,9 @@ module BeEF @host = @config.get("#{@config_prefix}.host") @port = @config.get("#{@config_prefix}.port") @helo = @config.get("#{@config_prefix}.helo") - @from = @config.get("#{@config_prefix}.from") + @auth = @config.get("#{@config_prefix}.auth") @password = @config.get("#{@config_prefix}.password") + @from = @config.get("#{@config_prefix}.from") end # tos_hash is an Hash like: @@ -47,7 +48,7 @@ module BeEF smtp.enable_starttls(@ctx) unless @config.get("#{@config_prefix}.use_tls") == false if @config.get("#{@config_prefix}.use_auth") - smtp.start(@helo, @from, @password, :login) do |smtp| + smtp.start(@helo, @auth, @password, :login) do |smtp| tos_hash.each do |to, name| message = compose_email(fromname, to, name, subject, link, linktext, template) smtp.send_message(message, @from, to) @@ -68,32 +69,38 @@ module BeEF end def compose_email(fromname, to, name, subject, link, linktext, template) - msg_id = random_string(50) - boundary = "------------#{random_string(24)}" - rel_boundary = "------------#{random_string(24)}" + begin + msg_id = random_string(50) + boundary = "------------#{random_string(24)}" + rel_boundary = "------------#{random_string(24)}" - header = email_headers(@from, fromname, @user_agent, to, subject, msg_id, boundary) - plain_body = email_plain_body(parse_template(name, link, linktext, "#{@templates_dir}#{template}/mail.plain", template), boundary) - rel_header = email_related(rel_boundary) - html_body = email_html_body(parse_template(name, link, linktext, "#{@templates_dir}#{template}/mail.html", template),rel_boundary) - images = "" - @config.get("#{@config_prefix}.templates.#{template}.images").each do |image| - images += email_add_image(image, "#{@templates_dir}#{template}/#{image}",rel_boundary) - end + header = email_headers(@from, fromname, @user_agent, to, subject, msg_id, boundary) + plain_body = email_plain_body(parse_template(name, link, linktext, "#{@templates_dir}#{template}/mail.plain", template), boundary) + rel_header = email_related(rel_boundary) + html_body = email_html_body(parse_template(name, link, linktext, "#{@templates_dir}#{template}/mail.html", template),rel_boundary) - attachments = "" - if @config.get("#{@config_prefix}.templates.#{template}.attachments") != nil - @config.get("#{@config_prefix}.templates.#{template}.attachments").each do |attachment| - attachments += email_add_attachment(attachment, "#{@templates_dir}#{template}/#{attachment}",rel_boundary) - end - end + images = "" + @config.get("#{@config_prefix}.templates.#{template}.images").each do |image| + images += email_add_image(image, "#{@templates_dir}#{template}/#{image}",rel_boundary) + end - close = email_close(boundary) + attachments = "" + if @config.get("#{@config_prefix}.templates.#{template}.attachments") != nil + @config.get("#{@config_prefix}.templates.#{template}.attachments").each do |attachment| + attachments += email_add_attachment(attachment, "#{@templates_dir}#{template}/#{attachment}",rel_boundary) + end + end - message = header + plain_body + rel_header + html_body + images + attachments + close - print_debug "Raw Email content:\n #{message}" - message + close = email_close(boundary) + rescue Exception => e + print_error "Error constructing email." + raise + end + + message = header + plain_body + rel_header + html_body + images + attachments + close + print_debug "Raw Email content:\n #{message}" + message end def email_headers(from, fromname, user_agent, to, subject, msg_id, boundary) diff --git a/extensions/social_engineering/rest/socialengineering.rb b/extensions/social_engineering/rest/socialengineering.rb index b03447145..8bc5f6ffb 100644 --- a/extensions/social_engineering/rest/socialengineering.rb +++ b/extensions/social_engineering/rest/socialengineering.rb @@ -106,11 +106,16 @@ module BeEF halt 401 end end - - mass_mailer = BeEF::Extension::SocialEngineering::MassMailer.instance - mass_mailer.send_email(template, fromname, subject, link, linktext, recipients) rescue Exception => e - print_error "Invalid JSON input passed to endpoint /api/seng/clone_page" + print_error "Invalid JSON input passed to endpoint /api/seng/send_emails" + error 400 + end + + begin + mass_mailer = BeEF::Extension::SocialEngineering::MassMailer.instance + mass_mailer.send_email(template, fromname, subject, link, linktext, recipients) + rescue Exception => e + print_error "Invalid mailer configuration" error 400 end end From 66d0e3535b4a924673e968f033504a95b67f3d8c Mon Sep 17 00:00:00 2001 From: geefunkmasterpro Date: Wed, 27 Feb 2013 23:29:08 +1100 Subject: [PATCH 2/2] Added fromaddr to mass mailer JSON interface so emails can be sent from any address without restart. Removed fromaddr entry from config.yaml. --- extensions/social_engineering/config.yaml | 1 - .../mass_mailer/mass_mailer.rb | 19 +++++++++---------- .../rest/socialengineering.rb | 6 ++++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/extensions/social_engineering/config.yaml b/extensions/social_engineering/config.yaml index 4a2d0af37..be302af75 100644 --- a/extensions/social_engineering/config.yaml +++ b/extensions/social_engineering/config.yaml @@ -23,7 +23,6 @@ beef: helo: "gmail.com" # this is usually the domain name auth: "youruser@gmail.com" password: "yourpass" - from: "fromemail@gmail.com" # available templates templates: default: diff --git a/extensions/social_engineering/mass_mailer/mass_mailer.rb b/extensions/social_engineering/mass_mailer/mass_mailer.rb index aa586fe13..3ab99ad17 100644 --- a/extensions/social_engineering/mass_mailer/mass_mailer.rb +++ b/extensions/social_engineering/mass_mailer/mass_mailer.rb @@ -22,13 +22,12 @@ module BeEF @helo = @config.get("#{@config_prefix}.helo") @auth = @config.get("#{@config_prefix}.auth") @password = @config.get("#{@config_prefix}.password") - @from = @config.get("#{@config_prefix}.from") end # tos_hash is an Hash like: # 'antisnatchor@gmail.com' => 'Michele' # 'ciccio@pasticcio.com' => 'Ciccio' - def send_email(template, fromname, subject, link, linktext, tos_hash) + def send_email(template, fromname, fromaddr, subject, link, linktext, tos_hash) # create new SSL context and disable CA chain validation if @config.get("#{@config_prefix}.use_tls") @ctx = OpenSSL::SSL::SSLContext.new @@ -38,7 +37,7 @@ module BeEF n = tos_hash.size x = 1 - print_info "Sending #{n} mail(s) from [#{@from}] - name [#{fromname}] using template [#{template}]:" + print_info "Sending #{n} mail(s) from [#{fromaddr}] - name [#{fromname}] using template [#{template}]:" print_info "subject: #{subject}" print_info "link: #{link}" print_info "linktext: #{linktext}" @@ -50,17 +49,17 @@ module BeEF if @config.get("#{@config_prefix}.use_auth") smtp.start(@helo, @auth, @password, :login) do |smtp| tos_hash.each do |to, name| - message = compose_email(fromname, to, name, subject, link, linktext, template) - smtp.send_message(message, @from, to) + message = compose_email(fromname, fromaddr, to, name, subject, link, linktext, template) + smtp.send_message(message, fromaddr, to) print_info "Mail #{x}/#{n} to [#{to}] sent." x += 1 end end else - smtp.start(@helo, @from) do |smtp| + smtp.start(@helo, @auth) do |smtp| tos_hash.each do |to, name| - message = compose_email(fromname, to, name, subject, link, linktext, template) - smtp.send_message(message, @from, to) + message = compose_email(fromname, fromaddr, to, name, subject, link, linktext, template) + smtp.send_message(message, fromaddr, to) print_info "Mail #{x}/#{n} to [#{to}] sent." x += 1 end @@ -68,14 +67,14 @@ module BeEF end end - def compose_email(fromname, to, name, subject, link, linktext, template) + def compose_email(fromname, fromaddr, to, name, subject, link, linktext, template) begin msg_id = random_string(50) boundary = "------------#{random_string(24)}" rel_boundary = "------------#{random_string(24)}" - header = email_headers(@from, fromname, @user_agent, to, subject, msg_id, boundary) + header = email_headers(fromaddr, fromname, @user_agent, to, subject, msg_id, boundary) plain_body = email_plain_body(parse_template(name, link, linktext, "#{@templates_dir}#{template}/mail.plain", template), boundary) rel_header = email_related(rel_boundary) html_body = email_html_body(parse_template(name, link, linktext, "#{@templates_dir}#{template}/mail.html", template),rel_boundary) diff --git a/extensions/social_engineering/rest/socialengineering.rb b/extensions/social_engineering/rest/socialengineering.rb index 8bc5f6ffb..d424644f0 100644 --- a/extensions/social_engineering/rest/socialengineering.rb +++ b/extensions/social_engineering/rest/socialengineering.rb @@ -70,6 +70,7 @@ module BeEF # "template": "default", # "subject": "Hi from BeEF", # "fromname": "BeEF", + # "fromaddr": "beef@beef.com", # "link": "http://www.microsoft.com/security/online-privacy/phishing-symptoms.aspx", # "linktext": "http://beefproject.com", # "recipients": [{ @@ -85,10 +86,11 @@ module BeEF template = body["template"] subject = body["subject"] fromname = body["fromname"] + fromaddr = body["fromaddr"] link = body["link"] linktext = body["linktext"] - if template.nil? || subject.nil? || fromname.nil? || link.nil? || linktext.nil? + if template.nil? || subject.nil? || fromaddr.nil? || fromname.nil? || link.nil? || linktext.nil? print_error "All parameters are mandatory." halt 401 end @@ -113,7 +115,7 @@ module BeEF begin mass_mailer = BeEF::Extension::SocialEngineering::MassMailer.instance - mass_mailer.send_email(template, fromname, subject, link, linktext, recipients) + mass_mailer.send_email(template, fromname, fromaddr, subject, link, linktext, recipients) rescue Exception => e print_error "Invalid mailer configuration" error 400