avatar
Untitled

Guest 87 20th Oct, 2019

                                           
                         ###
# Metasploit Plugin: Notify new sessions by E-mail.
###



module Msf

require 'net/smtp'

class Plugin::NotifyMail < Msf::Plugin

	if not defined?(Notify_mail_yaml)
		Notify_mail_yaml = "#{Msf::Config.get_config_root}/notify_mail.yaml"
	end

	class NotifyMailCommandDispatcher < Plugin::NotifyMail
		include Msf::Ui::Console::CommandDispatcher

		@sess_subscribed = false

		# The dispatcher's name.
		def name
			"Notify Mail"
		end

		# Returns the hash of commands supported by this dispatcher.
		def commands
			{
				"notify_mail_show" => "Show current settings",
				"notify_mail_load" => "Load settings from configuration file",
				"notify_mail_save" => "Save settings to configuration file",
				"notify_mail_mailfrom" => "Set sender e-mail address",
				"notify_mail_mailto" => "Set recipient e-mail address",
				"notify_mail_smtpsrv" => "Set SMTP server hostname/IP",
				"notify_mail_smtpport" => "Set SMTP server port"
			}
		end
		
		def manage_session_subscriber
                        # Don't send SMTP messages until all parameters are set
			if !@sess_subscribed
				if @mailfrom and @mailto and @smtpsrv and @smtpport
					self.framework.events.add_session_subscriber(self)
					@sess_subscribed = true
				end
			end
		end

		# Method for reading the YAML File
                # Widely inspired from 'growl.rb' (Carlos Perez)
		def cmd_notify_mail_load
			read = nil
			if File.exist?(Notify_mail_yaml)
				ldconfig = YAML.load_file("#{Notify_mail_yaml}")
				@mailfrom  = ldconfig['mailfrom']
				@mailto    = ldconfig['mailto']
				@smtpsrv   = ldconfig['smtpsrv']
				@smtpport  = ldconfig['smtpport']
				print_good("Parameters loaded from #{Notify_mail_yaml}")
				
				# add session subscriber
				manage_session_subscriber
				read = true
			else
				print_error("No such YAML File was found")
				print_error("as: #{Notify_mail_yaml}")
				return read
			end
			return read
		end

		# Save Parameters to text file
                # Widely inspired from 'growl.rb' (Carlos Perez)
		def cmd_notify_mail_save
			print_status("Saving paramters to config file")
			if @mailfrom and @mailto and @smtpsrv and @smtpport
				config = {'mailfrom' => @mailfrom, 'mailto' => @mailto,
					'smtpsrv' => @smtpsrv, 'smtpport' => @smtpport
				}
				File.open(Notify_mail_yaml, 'w') do |out|
					YAML.dump(config, out)
				end
				print_good("All parameters saved to #{Notify_mail_yaml}")
			else
				print_error("You have not provided all the parameters!")
			end
		end

                # show current parameters values
		def cmd_notify_mail_show
			print_line("MAILFROM : #{@mailfrom}")
			print_line("MAILTO   : #{@mailto}")
			print_line("SMTPSRV  : #{@smtpsrv}")
			print_line("SMTPPORT : #{@smtpport}")
		end

                # set MAILFROM
                def cmd_notify_mail_mailfrom(*args)
                        if args[0].nil?
				print_error("Usage: notify_mail_mailfrom user@domain.com")
                                return
                        end
                        @mailfrom = args[0]
                        print_line("MAILFROM => #{@mailfrom}")
                        manage_session_subscriber
                end

                # set MAILTO
                def cmd_notify_mail_mailto(*args)
                        if args[0].nil?
                                print_error("Usage: notify_mail_mailto user@domain.com")
                                return
                        end
                        @mailto = args[0]
                        print_line("MAILTO => #{@mailto}")
                        manage_session_subscriber
                end

                # set SMTPSRV
                def cmd_notify_mail_smtpsrv(*args)
                        if args[0].nil?
                                print_error("Usage: notify_mail_smtpsrv mail.hostname.com")
                                return
                        end
                        @smtpsrv = args[0]
                        print_line("SMTPSRV => #{@smtpsrv}")
                        manage_session_subscriber
                end

                # set SMTPPORT
                def cmd_notify_mail_smtpport(*args)
                        if args[0].nil?
                                print_error("Usage: notify_mail_smtpport 25")
                                return
                        end
                        @smtpport = args[0]
                        print_line("SMTPPORT => #{@smtpport}")
                        manage_session_subscriber
                end

                # Handle event (send an E-mail)
		def on_session_open(session)
	
			#
			# Mail content
			#
        	        mailstr =  "From: Metasploit Hanlder <#{@mailfrom}>\n"
			mailstr << "To: #{@mailto} <#{@mailto}>\n"
			mailstr << "Subject: #{session.type} session #{session.sid} opened (#{session.tunnel_to_s})\n"
			mailstr << "\n"
			if session.info?
				mailstr << "#{session.info.to_s} \n"
			end

			#
			# SMTP send
			#
                        begin
                                Net::SMTP.start(@smtpsrv, @smtpport) do |smtp|
                                        smtp.set_debug_output $stderr
                                        smtp.send_mail mailstr, @mailfrom, @mailto
                                end
                        rescue Exception => e
                                print "Exception occured: " + e
                        end
                        print_status("Notification sent to #{@mailto}")
		end
	end
	
	def initialize(framework, opts)
		super
		add_console_dispatcher(NotifyMailCommandDispatcher)
	end

	def cleanup
		remove_console_dispatcher('Notify Mail')
	end

	def name
		"Notify Mail"
	end

	def desc
		"Send E-mail notification on new sessions"
	end	
end
end
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes