Mirror of metasploit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugin.rb 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # -*- coding: binary -*-
  2. require 'rex/sync/ref'
  3. module Msf
  4. ###
  5. #
  6. # This module represents an abstract plugin that can be loaded into the
  7. # context of a framework instance. Plugins are meant to provide an easy way
  8. # to augment the feature set of the framework by being able to load and unload
  9. # them during the course of a framework's lifetime. For instance, a plugin
  10. # could be loaded to alter the default behavior of new sessions, such as by
  11. # scripting meterpreter sessions that are created. The possibilities are
  12. # endless!
  13. #
  14. # All plugins must exist under the Msf::Plugin namespace. Plugins are
  15. # reference counted to allow them to be loaded more than once if they're a
  16. # singleton.
  17. #
  18. ###
  19. class Plugin
  20. include Framework::Offspring
  21. include Rex::Ref
  22. #
  23. # Create an instance of the plugin using the supplied framework instance.
  24. # We use create instead of new directly so that singleton plugins can just
  25. # return their singleton instance.
  26. #
  27. def self.create(framework, opts = {})
  28. new(framework, opts)
  29. end
  30. #
  31. # Initializes the plugin instance with the supplied framework instance.
  32. # The opts parameter is a hash of custom arguments that may be useful for a
  33. # plugin. Some of the pre-defined arguments are:
  34. #
  35. # LocalInput
  36. #
  37. # The local input handle that implements the Rex::Ui::Text::Input
  38. # interface.
  39. #
  40. # LocalOutput
  41. #
  42. # The local output handle that implements the Rex::Ui::Output interface.
  43. #
  44. def initialize(framework, opts = {})
  45. self.framework = framework
  46. self.opts = opts
  47. refinit
  48. end
  49. #
  50. # Allows the plugin to clean up as it is being unloaded.
  51. #
  52. def cleanup
  53. end
  54. ##
  55. #
  56. # Accessors
  57. #
  58. ##
  59. #
  60. # Returns the name of the plugin.
  61. #
  62. def name
  63. "unnamed"
  64. end
  65. #
  66. # A short description of the plugin.
  67. #
  68. def desc
  69. end
  70. ##
  71. #
  72. # Accessors
  73. #
  74. ##
  75. #
  76. # Returns the local output handle if one was passed into the constructor.
  77. #
  78. def output
  79. opts['LocalOutput']
  80. end
  81. #
  82. # Returns the local input handle if one was passed into the constructor.
  83. #
  84. def input
  85. opts['LocalInput']
  86. end
  87. ##
  88. #
  89. # Output wrappers for the plugin that uses the 'LocalOutput' hash entry
  90. # if one was passed into the constructor.
  91. #
  92. ##
  93. #
  94. # Prints an error message.
  95. #
  96. def print_error(msg='')
  97. output.print_error(msg) if (output)
  98. end
  99. #
  100. # Prints a 'good' message.
  101. #
  102. def print_good(msg='')
  103. output.print_good(msg) if (output)
  104. end
  105. #
  106. # Prints a status line.
  107. #
  108. def print_status(msg='')
  109. output.print_status(msg) if (output)
  110. end
  111. #
  112. # Prints an undecorated line of information.
  113. #
  114. def print_line(msg='')
  115. output.print_line(msg) if (output)
  116. end
  117. #
  118. # Prints a warning
  119. #
  120. def print_warning(msg='')
  121. output.print_warning(msg) if (output)
  122. end
  123. #
  124. # Prints a message with no decoration.
  125. #
  126. def print(msg='')
  127. output.print(msg) if (output)
  128. end
  129. #
  130. # Flushes any buffered output.
  131. #
  132. def flush
  133. output.flush(msg) if (output)
  134. end
  135. protected
  136. attr_accessor :opts # :nodoc:
  137. ##
  138. #
  139. # Console command dispatcher helpers.
  140. #
  141. ##
  142. #
  143. # Adds the console dispatcher.
  144. #
  145. def add_console_dispatcher(disp)
  146. if (opts['ConsoleDriver'])
  147. opts['ConsoleDriver'].append_dispatcher(disp)
  148. end
  149. end
  150. #
  151. # Removes the console dispatcher.
  152. #
  153. def remove_console_dispatcher(name)
  154. if (opts['ConsoleDriver'])
  155. opts['ConsoleDriver'].remove_dispatcher(name)
  156. end
  157. end
  158. end
  159. end