msfd.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #
  2. # This plugin provides an msf daemon interface that spawns a listener on a
  3. # defined port (default 55554) and gives each connecting client its own
  4. # console interface. These consoles all share the same framework instance.
  5. # Be aware that the console instance that spawns on the port is entirely
  6. # unauthenticated, so realize that you have been warned.
  7. #
  8. module Msf
  9. ###
  10. #
  11. # This class implements the msfd plugin interface.
  12. #
  13. ###
  14. class Plugin::Msfd < Msf::Plugin
  15. #
  16. # The default local hostname that the server listens on.
  17. #
  18. DefaultHost = '127.0.0.1'.freeze
  19. #
  20. # The default local port that the server listens on.
  21. #
  22. DefaultPort = 55554
  23. #
  24. # Initializes the msfd plugin. The following options are supported in the
  25. # hash by this plugin:
  26. #
  27. # ServerHost
  28. #
  29. # The local hostname to listen on for connections. The default is
  30. # 127.0.0.1.
  31. #
  32. # ServerPort
  33. #
  34. # The local port to listen on for connections. The default is 55554.
  35. #
  36. # SSL
  37. #
  38. # Use SSL
  39. #
  40. # RunInForeground
  41. #
  42. # Instructs the plugin to now execute the daemon in a worker thread and to
  43. # instead allow the caller to manage executing the daemon through the
  44. # ``run'' method.
  45. #
  46. # HostsAllowed
  47. #
  48. # List of hosts (in NBO) allowed to use msfd
  49. #
  50. # HostsDenied
  51. #
  52. # List of hosts (in NBO) not allowed to use msfd
  53. #
  54. def initialize(framework, opts)
  55. super
  56. # Start listening for connections.
  57. self.server = Rex::Socket::TcpServer.create(
  58. 'LocalHost' => opts['ServerHost'] || DefaultHost,
  59. 'LocalPort' => opts['ServerPort'] || DefaultPort,
  60. 'SSL' => opts['SSL']
  61. )
  62. # If the run in foreground flag is not specified, then go ahead and fire
  63. # it off in a worker thread.
  64. if (opts['RunInForeground'] != true)
  65. Thread.new do
  66. run(opts)
  67. end
  68. end
  69. end
  70. #
  71. # Returns 'msfd'
  72. #
  73. def name
  74. 'msfd'
  75. end
  76. #
  77. # Returns the msfd plugin description.
  78. #
  79. def desc
  80. 'Provides a console interface to users over a listening TCP port'
  81. end
  82. #
  83. # Runs the msfd plugin by blocking on new connections and then spawning
  84. # threads to handle the console interface for each client.
  85. #
  86. def run(opts = {})
  87. loop do
  88. client = server.accept
  89. addr = Rex::Socket.resolv_nbo(client.peerhost)
  90. if opts['HostsAllowed'] &&
  91. !opts['HostsAllowed'].find { |x| x == addr }
  92. client.close
  93. next
  94. end
  95. if opts['HostsDenied'] &&
  96. opts['HostsDenied'].find { |x| x == addr }
  97. client.close
  98. next
  99. end
  100. msg = "Msfd: New connection from #{client.peerhost}"
  101. ilog(msg, 'core')
  102. print_status(msg)
  103. # Spawn a thread for the client connection
  104. Thread.new(client) do |cli|
  105. Msf::Ui::Console::Driver.new(
  106. Msf::Ui::Console::Driver::DefaultPrompt,
  107. Msf::Ui::Console::Driver::DefaultPromptChar,
  108. 'Framework' => framework,
  109. 'LocalInput' => Rex::Ui::Text::Input::Socket.new(cli),
  110. 'LocalOutput' => Rex::Ui::Text::Output::Socket.new(cli),
  111. 'AllowCommandPassthru' => false,
  112. 'DisableBanner' => opts['DisableBanner'] ? true : false
  113. ).run
  114. rescue StandardError => e
  115. elog('Msfd client error', error: e)
  116. ensure
  117. msg = "Msfd: Closing client connection with #{cli.peerhost}"
  118. ilog(msg, 'core')
  119. print_status(msg)
  120. begin
  121. cli.shutdown
  122. cli.close
  123. rescue IOError
  124. end
  125. end
  126. end
  127. end
  128. #
  129. # Closes the listener service.
  130. #
  131. def cleanup
  132. ilog('Msfd: Shutting down server', 'core')
  133. server.close
  134. end
  135. protected
  136. #
  137. # The listening socket instance.
  138. #
  139. attr_accessor :server
  140. end
  141. end