Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/plugins/msfd.rb
Views: 11705
#1# This plugin provides an msf daemon interface that spawns a listener on a2# defined port (default 55554) and gives each connecting client its own3# console interface. These consoles all share the same framework instance.4# Be aware that the console instance that spawns on the port is entirely5# unauthenticated, so realize that you have been warned.6#78module Msf9###10#11# This class implements the msfd plugin interface.12#13###14class Plugin::Msfd < Msf::Plugin1516#17# The default local hostname that the server listens on.18#19DefaultHost = '127.0.0.1'.freeze2021#22# The default local port that the server listens on.23#24DefaultPort = 555542526#27# Initializes the msfd plugin. The following options are supported in the28# hash by this plugin:29#30# ServerHost31#32# The local hostname to listen on for connections. The default is33# 127.0.0.1.34#35# ServerPort36#37# The local port to listen on for connections. The default is 55554.38#39# SSL40#41# Use SSL42#43# RunInForeground44#45# Instructs the plugin to now execute the daemon in a worker thread and to46# instead allow the caller to manage executing the daemon through the47# ``run'' method.48#49# HostsAllowed50#51# List of hosts (in NBO) allowed to use msfd52#53# HostsDenied54#55# List of hosts (in NBO) not allowed to use msfd56#57def initialize(framework, opts)58super5960# Start listening for connections.61self.server = Rex::Socket::TcpServer.create(62'LocalHost' => opts['ServerHost'] || DefaultHost,63'LocalPort' => opts['ServerPort'] || DefaultPort,64'SSL' => opts['SSL']65)6667# If the run in foreground flag is not specified, then go ahead and fire68# it off in a worker thread.69if (opts['RunInForeground'] != true)70Thread.new do71run(opts)72end73end74end7576#77# Returns 'msfd'78#79def name80'msfd'81end8283#84# Returns the msfd plugin description.85#86def desc87'Provides a console interface to users over a listening TCP port'88end8990#91# Runs the msfd plugin by blocking on new connections and then spawning92# threads to handle the console interface for each client.93#94def run(opts = {})95loop do96client = server.accept9798addr = Rex::Socket.resolv_nbo(client.peerhost)99100if opts['HostsAllowed'] &&101!opts['HostsAllowed'].find { |x| x == addr }102client.close103next104end105106if opts['HostsDenied'] &&107opts['HostsDenied'].find { |x| x == addr }108client.close109next110end111msg = "Msfd: New connection from #{client.peerhost}"112ilog(msg, 'core')113print_status(msg)114115# Spawn a thread for the client connection116Thread.new(client) do |cli|117Msf::Ui::Console::Driver.new(118Msf::Ui::Console::Driver::DefaultPrompt,119Msf::Ui::Console::Driver::DefaultPromptChar,120'Framework' => framework,121'LocalInput' => Rex::Ui::Text::Input::Socket.new(cli),122'LocalOutput' => Rex::Ui::Text::Output::Socket.new(cli),123'AllowCommandPassthru' => false,124'DisableBanner' => opts['DisableBanner'] ? true : false125).run126rescue StandardError => e127elog('Msfd client error', error: e)128ensure129msg = "Msfd: Closing client connection with #{cli.peerhost}"130ilog(msg, 'core')131print_status(msg)132begin133cli.shutdown134cli.close135rescue IOError136end137end138end139end140141#142# Closes the listener service.143#144def cleanup145ilog('Msfd: Shutting down server', 'core')146server.close147end148149protected150151#152# The listening socket instance.153#154attr_accessor :server155156end157end158159160