Advertisement
Guest User

Untitled

a guest
Nov 30th, 2013
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. ###
  2. # Metasploit Plugin: Notify new sessions by E-mail.
  3. # Author: st3@funoverip.net 10 Aug 2011
  4. # Thanks to Carlos Perez for the YAML code
  5. ###
  6.  
  7.  
  8.  
  9. module Msf
  10.  
  11. require 'net/smtp'
  12.  
  13. class Plugin::NotifyMail < Msf::Plugin
  14.  
  15. if not defined?(Notify_mail_yaml)
  16. Notify_mail_yaml = "#{Msf::Config.get_config_root}/notify_mail.yaml"
  17. end
  18.  
  19. class NotifyMailCommandDispatcher < Plugin::NotifyMail
  20. include Msf::Ui::Console::CommandDispatcher
  21.  
  22. @sess_subscribed = false
  23.  
  24. # The dispatcher's name.
  25. def name
  26. "Notify Mail"
  27. end
  28.  
  29. # Returns the hash of commands supported by this dispatcher.
  30. def commands
  31. {
  32. "notify_mail_show" => "Show current settings",
  33. "notify_mail_load" => "Load settings from configuration file",
  34. "notify_mail_save" => "Save settings to configuration file",
  35. "notify_mail_mailfrom" => "Set sender e-mail address",
  36. "notify_mail_mailto" => "Set recipient e-mail address",
  37. "notify_mail_smtpsrv" => "Set SMTP server hostname/IP",
  38. "notify_mail_smtpport" => "Set SMTP server port"
  39. }
  40. end
  41.  
  42. def manage_session_subscriber
  43. # Don't send SMTP messages until all parameters are set
  44. if !@sess_subscribed
  45. if @mailfrom and @mailto and @smtpsrv and @smtpport
  46. self.framework.events.add_session_subscriber(self)
  47. @sess_subscribed = true
  48. end
  49. end
  50. end
  51.  
  52. # Method for reading the YAML File
  53. # Widely inspired from 'growl.rb' (Carlos Perez)
  54. def cmd_notify_mail_load
  55. read = nil
  56. if File.exist?(Notify_mail_yaml)
  57. ldconfig = YAML.load_file("#{Notify_mail_yaml}")
  58. @mailfrom = ldconfig['mailfrom']
  59. @mailto = ldconfig['mailto']
  60. @smtpsrv = ldconfig['smtpsrv']
  61. @smtpport = ldconfig['smtpport']
  62. print_good("Parameters loaded from #{Notify_mail_yaml}")
  63.  
  64. # add session subscriber
  65. manage_session_subscriber
  66. read = true
  67. else
  68. print_error("No such YAML File was found")
  69. print_error("as: #{Notify_mail_yaml}")
  70. return read
  71. end
  72. return read
  73. end
  74.  
  75. # Save Parameters to text file
  76. # Widely inspired from 'growl.rb' (Carlos Perez)
  77. def cmd_notify_mail_save
  78. print_status("Saving paramters to config file")
  79. if @mailfrom and @mailto and @smtpsrv and @smtpport
  80. config = {'mailfrom' => @mailfrom, 'mailto' => @mailto,
  81. 'smtpsrv' => @smtpsrv, 'smtpport' => @smtpport
  82. }
  83. File.open(Notify_mail_yaml, 'w') do |out|
  84. YAML.dump(config, out)
  85. end
  86. print_good("All parameters saved to #{Notify_mail_yaml}")
  87. else
  88. print_error("You have not provided all the parameters!")
  89. end
  90. end
  91.  
  92. # show current parameters values
  93. def cmd_notify_mail_show
  94. print_line("MAILFROM : #{@mailfrom}")
  95. print_line("MAILTO : #{@mailto}")
  96. print_line("SMTPSRV : #{@smtpsrv}")
  97. print_line("SMTPPORT : #{@smtpport}")
  98. end
  99.  
  100. # set MAILFROM
  101. def cmd_notify_mail_mailfrom(*args)
  102. if args[0].nil?
  103. print_error("Usage: notify_mail_mailfrom user@domain.com")
  104. return
  105. end
  106. @mailfrom = args[0]
  107. print_line("MAILFROM => #{@mailfrom}")
  108. manage_session_subscriber
  109. end
  110.  
  111. # set MAILTO
  112. def cmd_notify_mail_mailto(*args)
  113. if args[0].nil?
  114. print_error("Usage: notify_mail_mailto user@domain.com")
  115. return
  116. end
  117. @mailto = args[0]
  118. print_line("MAILTO => #{@mailto}")
  119. manage_session_subscriber
  120. end
  121.  
  122. # set SMTPSRV
  123. def cmd_notify_mail_smtpsrv(*args)
  124. if args[0].nil?
  125. print_error("Usage: notify_mail_smtpsrv mail.hostname.com")
  126. return
  127. end
  128. @smtpsrv = args[0]
  129. print_line("SMTPSRV => #{@smtpsrv}")
  130. manage_session_subscriber
  131. end
  132.  
  133. # set SMTPPORT
  134. def cmd_notify_mail_smtpport(*args)
  135. if args[0].nil?
  136. print_error("Usage: notify_mail_smtpport 25")
  137. return
  138. end
  139. @smtpport = args[0]
  140. print_line("SMTPPORT => #{@smtpport}")
  141. manage_session_subscriber
  142. end
  143.  
  144. # Handle event (send an E-mail)
  145. def on_session_open(session)
  146.  
  147. #
  148. # Mail content
  149. #
  150. mailstr = "From: Metasploit Hanlder <#{@mailfrom}>\n"
  151. mailstr << "To: #{@mailto} <#{@mailto}>\n"
  152. mailstr << "Subject: #{session.type} session #{session.sid} opened (#{session.tunnel_to_s})\n"
  153. mailstr << "\n"
  154. if session.info?
  155. mailstr << "#{session.info.to_s} \n"
  156. end
  157.  
  158. #
  159. # SMTP send
  160. #
  161. begin
  162. Net::SMTP.start(@smtpsrv, @smtpport) do |smtp|
  163. smtp.set_debug_output $stderr
  164. smtp.send_mail mailstr, @mailfrom, @mailto
  165. end
  166. rescue Exception => e
  167. print "Exception occured: " + e
  168. end
  169. print_status("Notification sent to #{@mailto}")
  170. end
  171. end
  172.  
  173. def initialize(framework, opts)
  174. super
  175. add_console_dispatcher(NotifyMailCommandDispatcher)
  176. end
  177.  
  178. def cleanup
  179. remove_console_dispatcher('Notify Mail')
  180. end
  181.  
  182. def name
  183. "Notify Mail"
  184. end
  185.  
  186. def desc
  187. "Send E-mail notification on new sessions"
  188. end
  189. end
  190. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement