Files
metasploit-gs/plugins/msfd.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

159 lines
3.9 KiB
Ruby
Raw Normal View History

2005-11-28 21:38:48 +00:00
#
# This plugin provides an msf daemon interface that spawns a listener on a
# defined port (default 55554) and gives each connecting client its own
# console interface. These consoles all share the same framework instance.
2005-11-28 23:49:48 +00:00
# Be aware that the console instance that spawns on the port is entirely
# unauthenticated, so realize that you have been warned.
2005-11-28 21:38:48 +00:00
#
module Msf
2023-01-30 12:25:46 +11:00
###
#
# This class implements the msfd plugin interface.
#
###
class Plugin::Msfd < Msf::Plugin
#
# The default local hostname that the server listens on.
#
2023-01-30 13:05:34 +11:00
DefaultHost = '127.0.0.1'.freeze
2023-01-30 12:25:46 +11:00
#
# The default local port that the server listens on.
#
DefaultPort = 55554
#
# Initializes the msfd plugin. The following options are supported in the
# hash by this plugin:
#
# ServerHost
#
# The local hostname to listen on for connections. The default is
# 127.0.0.1.
#
# ServerPort
#
# The local port to listen on for connections. The default is 55554.
#
# SSL
#
# Use SSL
#
# RunInForeground
#
# Instructs the plugin to now execute the daemon in a worker thread and to
# instead allow the caller to manage executing the daemon through the
# ``run'' method.
#
# HostsAllowed
#
# List of hosts (in NBO) allowed to use msfd
#
# HostsDenied
#
# List of hosts (in NBO) not allowed to use msfd
#
def initialize(framework, opts)
super
# Start listening for connections.
self.server = Rex::Socket::TcpServer.create(
'LocalHost' => opts['ServerHost'] || DefaultHost,
'LocalPort' => opts['ServerPort'] || DefaultPort,
'SSL' => opts['SSL']
)
# If the run in foreground flag is not specified, then go ahead and fire
# it off in a worker thread.
if (opts['RunInForeground'] != true)
Thread.new do
run(opts)
end
end
2013-09-30 13:47:53 -05:00
end
2023-01-30 12:25:46 +11:00
#
# Returns 'msfd'
#
def name
'msfd'
end
2013-09-30 13:47:53 -05:00
2023-01-30 12:25:46 +11:00
#
# Returns the msfd plugin description.
#
def desc
'Provides a console interface to users over a listening TCP port'
2023-01-30 12:25:46 +11:00
end
2013-09-30 13:47:53 -05:00
2023-01-30 12:25:46 +11:00
#
# Runs the msfd plugin by blocking on new connections and then spawning
# threads to handle the console interface for each client.
#
def run(opts = {})
2023-01-30 13:05:34 +11:00
loop do
2023-01-30 12:25:46 +11:00
client = server.accept
2013-09-30 13:47:53 -05:00
2023-01-30 12:25:46 +11:00
addr = Rex::Socket.resolv_nbo(client.peerhost)
2013-09-30 13:47:53 -05:00
2023-01-30 13:05:34 +11:00
if opts['HostsAllowed'] &&
2023-01-30 12:25:46 +11:00
!opts['HostsAllowed'].find { |x| x == addr }
client.close
next
end
2013-09-30 13:47:53 -05:00
2023-01-30 13:05:34 +11:00
if opts['HostsDenied'] &&
2023-01-30 12:25:46 +11:00
opts['HostsDenied'].find { |x| x == addr }
client.close
next
end
msg = "Msfd: New connection from #{client.peerhost}"
ilog(msg, 'core')
print_status(msg)
2013-09-30 13:47:53 -05:00
2023-01-30 12:25:46 +11:00
# Spawn a thread for the client connection
Thread.new(client) do |cli|
2013-09-30 13:47:53 -05:00
Msf::Ui::Console::Driver.new(
Msf::Ui::Console::Driver::DefaultPrompt,
Msf::Ui::Console::Driver::DefaultPromptChar,
2023-01-30 12:25:46 +11:00
'Framework' => framework,
'LocalInput' => Rex::Ui::Text::Input::Socket.new(cli),
2013-09-30 13:47:53 -05:00
'LocalOutput' => Rex::Ui::Text::Output::Socket.new(cli),
2015-10-07 01:50:36 -05:00
'AllowCommandPassthru' => false,
2023-01-30 12:25:46 +11:00
'DisableBanner' => opts['DisableBanner'] ? true : false
).run
rescue StandardError => e
elog('Msfd client error', error: e)
2013-09-30 13:47:53 -05:00
ensure
msg = "Msfd: Closing client connection with #{cli.peerhost}"
ilog(msg, 'core')
print_status(msg)
begin
cli.shutdown
cli.close
rescue IOError
end
end
2023-01-30 12:25:46 +11:00
end
2013-09-30 13:47:53 -05:00
end
2023-01-30 12:25:46 +11:00
#
# Closes the listener service.
#
def cleanup
ilog('Msfd: Shutting down server', 'core')
server.close
end
2005-11-28 21:38:48 +00:00
2023-01-30 12:25:46 +11:00
protected
2005-11-28 21:38:48 +00:00
2023-01-30 12:25:46 +11:00
#
# The listening socket instance.
#
attr_accessor :server
2005-11-28 21:38:48 +00:00
2023-01-30 12:25:46 +11:00
end
2008-12-19 23:18:39 +00:00
end