2008-12-02 22:09:34 +00:00
|
|
|
#!/usr/bin/env ruby
|
2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2008-12-02 22:09:34 +00:00
|
|
|
#
|
2010-05-03 17:13:09 +00:00
|
|
|
# $Id$
|
|
|
|
#
|
2008-12-02 22:09:34 +00:00
|
|
|
# This user interface listens on a port and provides clients that connect to
|
2019-01-09 03:40:15 +00:00
|
|
|
# it with an RPC or JSON-RPC interface to the Metasploit Framework.
|
2008-12-02 22:09:34 +00:00
|
|
|
#
|
2010-05-03 17:13:09 +00:00
|
|
|
# $Revision$
|
|
|
|
#
|
2008-12-02 22:09:34 +00:00
|
|
|
|
2019-01-09 03:40:15 +00:00
|
|
|
RPC_TYPE = 'Msg'
|
2019-01-09 22:19:15 +00:00
|
|
|
WS_TAG = 'msf-ws'
|
|
|
|
WS_RPC_TAG = 'msf-json-rpc'
|
|
|
|
WS_CONF = "#{WS_RPC_TAG}.ru"
|
|
|
|
WS_ENV = 'production'
|
2019-01-09 03:40:15 +00:00
|
|
|
|
|
|
|
|
2019-01-09 22:19:15 +00:00
|
|
|
def start_json_rpc_service(conf:, address:, port:, ssl:, ssl_key:, ssl_cert:,
|
|
|
|
ssl_disable_verify:, daemonize:, log:, pid:)
|
2019-01-09 03:40:15 +00:00
|
|
|
unless File.file?(conf)
|
|
|
|
$stdout.puts "[-] No MSF JSON-RPC web service configuration found at #{conf}, not starting"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if MSF JSON-RPC web service is already started
|
2019-01-09 22:19:15 +00:00
|
|
|
if File.file?(pid)
|
|
|
|
ws_pid = Msf::Util::ServiceHelper.tail(pid)
|
2019-01-09 03:40:15 +00:00
|
|
|
if ws_pid.nil? || !Msf::Util::ServiceHelper.process_active?(ws_pid.to_i)
|
|
|
|
$stdout.puts "[-] MSF JSON-RPC web service PID file found, but no active process running as PID #{ws_pid}"
|
2019-01-09 22:19:15 +00:00
|
|
|
$stdout.puts "[*] Deleting MSF JSON-RPC web service PID file #{pid}"
|
|
|
|
File.delete(pid)
|
2019-01-09 03:40:15 +00:00
|
|
|
else
|
|
|
|
$stdout.puts "[*] MSF JSON-RPC web service is already running as PID #{ws_pid}"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# attempt to start MSF JSON-RPC service
|
|
|
|
thin_cmd = Msf::Util::ServiceHelper.thin_cmd(conf: conf,
|
|
|
|
address: address,
|
|
|
|
port: port,
|
|
|
|
ssl: ssl,
|
|
|
|
ssl_key: ssl_key,
|
|
|
|
ssl_cert: ssl_cert,
|
|
|
|
ssl_disable_verify: ssl_disable_verify,
|
2019-01-09 22:19:15 +00:00
|
|
|
env: WS_ENV,
|
2019-01-09 03:40:15 +00:00
|
|
|
daemonize: daemonize,
|
2019-01-09 22:19:15 +00:00
|
|
|
log: log,
|
|
|
|
pid: pid,
|
|
|
|
tag: WS_RPC_TAG)
|
2019-01-09 03:40:15 +00:00
|
|
|
Msf::Util::ServiceHelper.run_cmd("#{thin_cmd} start")
|
|
|
|
end
|
|
|
|
|
2019-01-09 22:19:15 +00:00
|
|
|
def stop_json_rpc_service(conf:, address:, port:, ssl:, ssl_key:, ssl_cert:,
|
|
|
|
ssl_disable_verify:, daemonize:, log:, pid:)
|
|
|
|
ws_pid = Msf::Util::ServiceHelper.tail(pid)
|
2019-01-09 03:40:15 +00:00
|
|
|
$stdout.puts ''
|
|
|
|
if ws_pid.nil? || !Msf::Util::ServiceHelper.process_active?(ws_pid.to_i)
|
|
|
|
$stdout.puts '[*] MSF JSON-RPC web service is no longer running'
|
2019-01-09 22:19:15 +00:00
|
|
|
if File.file?(pid)
|
|
|
|
$stdout.puts "[*] Deleting MSF JSON-RPC web service PID file #{pid}"
|
|
|
|
File.delete(pid)
|
2019-01-09 03:40:15 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
$stdout.puts "[*] Stopping MSF JSON-RPC web service PID #{ws_pid}"
|
|
|
|
thin_cmd = Msf::Util::ServiceHelper.thin_cmd(conf: conf,
|
|
|
|
address: address,
|
|
|
|
port: port,
|
|
|
|
ssl: ssl,
|
|
|
|
ssl_key: ssl_key,
|
|
|
|
ssl_cert: ssl_cert,
|
|
|
|
ssl_disable_verify: ssl_disable_verify,
|
2019-01-09 22:19:15 +00:00
|
|
|
env: WS_ENV,
|
2019-01-09 03:40:15 +00:00
|
|
|
daemonize: daemonize,
|
2019-01-09 22:19:15 +00:00
|
|
|
log: log,
|
|
|
|
pid: pid,
|
|
|
|
tag: WS_RPC_TAG)
|
2019-01-09 03:40:15 +00:00
|
|
|
Msf::Util::ServiceHelper.run_cmd("#{thin_cmd} stop")
|
|
|
|
end
|
2009-01-30 06:27:10 +00:00
|
|
|
end
|
|
|
|
|
2019-01-09 03:40:15 +00:00
|
|
|
def start_rpc_service(opts, frameworkOpts, foreground)
|
|
|
|
# Fork into the background if requested
|
|
|
|
begin
|
|
|
|
if foreground
|
|
|
|
$stdout.puts "[*] #{RPC_TYPE.upcase}RPC ready at #{Time.now}."
|
|
|
|
else
|
|
|
|
$stderr.puts "[*] #{RPC_TYPE.upcase}RPC backgrounding at #{Time.now}..."
|
|
|
|
exit(0) if Process.fork()
|
|
|
|
end
|
|
|
|
rescue ::NotImplementedError
|
|
|
|
$stderr.puts "[-] Background mode is not available on this platform"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create an instance of the framework
|
|
|
|
$framework = Msf::Simple::Framework.create(frameworkOpts)
|
|
|
|
|
|
|
|
# Run the plugin instance in the foreground.
|
|
|
|
begin
|
|
|
|
$framework.plugins.load("#{RPC_TYPE.downcase}rpc", opts).run
|
|
|
|
rescue ::Interrupt
|
|
|
|
$stderr.puts "[*] Shutting down"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if $PROGRAM_NAME == __FILE__
|
|
|
|
msfbase = __FILE__
|
|
|
|
while File.symlink?(msfbase)
|
|
|
|
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
|
|
|
|
end
|
|
|
|
|
|
|
|
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), 'lib')))
|
|
|
|
require 'msfenv'
|
|
|
|
|
|
|
|
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
|
|
|
|
|
2019-01-09 22:19:15 +00:00
|
|
|
require 'msf/base'
|
|
|
|
require 'msf/ui'
|
|
|
|
require 'msf/util/service_helper'
|
|
|
|
require 'msf/base/config'
|
2019-01-09 03:40:15 +00:00
|
|
|
require 'rex/parser/arguments'
|
|
|
|
|
2019-01-09 22:19:15 +00:00
|
|
|
ws_ssl_key_default = File.join(Msf::Config.get_config_root, "#{WS_TAG}-key.pem")
|
|
|
|
ws_ssl_cert_default = File.join(Msf::Config.get_config_root, "#{WS_TAG}-cert.pem")
|
|
|
|
ws_log = File.join(Msf::Config.get_config_root, 'logs', "#{WS_RPC_TAG}.log")
|
|
|
|
ws_rpc_pid = File.join(Msf::Config.get_config_root, "#{WS_RPC_TAG}.pid")
|
|
|
|
ws_ssl_key = ws_ssl_key_default
|
|
|
|
ws_ssl_cert = ws_ssl_cert_default
|
|
|
|
ssl_enable_verify = false
|
|
|
|
foreground = false
|
|
|
|
json_rpc = false
|
|
|
|
frameworkOpts = {}
|
|
|
|
|
2019-01-09 03:40:15 +00:00
|
|
|
opts = {
|
|
|
|
'RunInForeground' => true,
|
|
|
|
'SSL' => true,
|
|
|
|
'ServerHost' => '0.0.0.0',
|
|
|
|
'ServerPort' => 55553,
|
|
|
|
'ServerType' => RPC_TYPE,
|
|
|
|
'TokenTimeout' => 300,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Declare the argument parser for msfrpcd
|
|
|
|
arguments = Rex::Parser::Arguments.new(
|
|
|
|
"-a" => [ true, "Bind to this IP address (default: #{opts['ServerHost']})" ],
|
|
|
|
"-p" => [ true, "Bind to this port (default: #{opts['ServerPort']})" ],
|
|
|
|
"-U" => [ true, "Specify the username to access msfrpcd" ],
|
|
|
|
"-P" => [ true, "Specify the password to access msfrpcd" ],
|
|
|
|
"-u" => [ true, "URI for Web server" ],
|
|
|
|
"-t" => [ true, "Token Timeout seconds (default: #{opts['TokenTimeout']})" ],
|
|
|
|
"-S" => [ false, "Disable SSL on the RPC socket" ],
|
|
|
|
"-f" => [ false, "Run the daemon in the foreground" ],
|
|
|
|
"-n" => [ false, "Disable database" ],
|
|
|
|
"-j" => [ false, "(JSON-RPC) Start JSON-RPC server" ],
|
2019-01-09 22:19:15 +00:00
|
|
|
"-k" => [ false, "(JSON-RPC) Path to private key (default: #{ws_ssl_key_default})" ],
|
|
|
|
"-c" => [ false, "(JSON-RPC) Path to certificate (default: #{ws_ssl_cert_default})" ],
|
2019-01-09 03:40:15 +00:00
|
|
|
"-v" => [ false, "(JSON-RPC) SSL enable verify (optional) client cert requests" ],
|
|
|
|
"-h" => [ false, "Help banner" ])
|
|
|
|
|
|
|
|
# Parse command line arguments.
|
|
|
|
arguments.parse(ARGV) { |opt, idx, val|
|
|
|
|
case opt
|
2013-09-30 18:47:53 +00:00
|
|
|
when "-a"
|
|
|
|
opts['ServerHost'] = val
|
|
|
|
when "-S"
|
|
|
|
opts['SSL'] = false
|
|
|
|
when "-p"
|
|
|
|
opts['ServerPort'] = val
|
|
|
|
when '-U'
|
|
|
|
opts['User'] = val
|
|
|
|
when '-P'
|
|
|
|
opts['Pass'] = val
|
2015-05-01 20:04:55 +00:00
|
|
|
when "-t"
|
|
|
|
opts['TokenTimeout'] = val.to_i
|
2013-09-30 18:47:53 +00:00
|
|
|
when "-f"
|
|
|
|
foreground = true
|
|
|
|
when "-u"
|
|
|
|
opts['URI'] = val
|
|
|
|
when "-n"
|
|
|
|
frameworkOpts['DisableDatabase'] = true
|
2019-01-09 03:40:15 +00:00
|
|
|
when "-j"
|
|
|
|
json_rpc = true
|
|
|
|
when "-k"
|
|
|
|
ws_ssl_key = val
|
|
|
|
when "-c"
|
|
|
|
ws_ssl_cert = val
|
|
|
|
when "-v"
|
|
|
|
ssl_enable_verify = true
|
2013-09-30 18:47:53 +00:00
|
|
|
when "-h"
|
|
|
|
print("\nUsage: #{File.basename(__FILE__)} <options>\n" + arguments.usage)
|
|
|
|
exit
|
2019-01-09 03:40:15 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
$0 = "msfrpcd"
|
|
|
|
|
|
|
|
begin
|
|
|
|
if json_rpc
|
|
|
|
|
2019-01-09 22:19:15 +00:00
|
|
|
if !File.file?(ws_ssl_key_default) || !File.file?(ws_ssl_cert_default)
|
2019-01-09 03:40:15 +00:00
|
|
|
$stdout.puts "[-] It doesn't appear msfdb has been run; please run 'msfdb init' first."
|
|
|
|
abort
|
|
|
|
end
|
|
|
|
|
|
|
|
$stderr.puts "[*] JSON-RPC starting on #{opts['ServerHost']}:#{opts['ServerPort']} (#{opts['SSL'] ? "SSL" : "NO SSL"})..."
|
2019-01-09 21:00:45 +00:00
|
|
|
$stderr.puts "[*] URI: /api/v1/json-rpc"
|
2019-01-09 22:19:15 +00:00
|
|
|
$stderr.puts "[*] JSON-RPC server log: #{ws_log}" unless foreground
|
2019-01-09 03:40:15 +00:00
|
|
|
|
2019-01-09 22:19:15 +00:00
|
|
|
ws_conf_full_path = File.expand_path(File.join(File.dirname(msfbase), WS_CONF))
|
2019-01-09 03:40:15 +00:00
|
|
|
|
|
|
|
start_json_rpc_service(conf: ws_conf_full_path,
|
|
|
|
address: opts['ServerHost'],
|
|
|
|
port: opts['ServerPort'],
|
|
|
|
ssl: opts['SSL'],
|
|
|
|
ssl_key: ws_ssl_key,
|
|
|
|
ssl_cert: ws_ssl_cert,
|
|
|
|
ssl_disable_verify: !ssl_enable_verify,
|
2019-01-09 22:19:15 +00:00
|
|
|
daemonize: !foreground,
|
|
|
|
log: ws_log,
|
|
|
|
pid: ws_rpc_pid)
|
2019-01-09 03:40:15 +00:00
|
|
|
else
|
|
|
|
unless opts['Pass']
|
|
|
|
$stderr.puts "[-] Error: a password must be specified (-P)"
|
|
|
|
exit(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
$stderr.puts "[*] #{RPC_TYPE.upcase}RPC starting on #{opts['ServerHost']}:#{opts['ServerPort']} (#{opts['SSL'] ? "SSL" : "NO SSL"}):#{opts['ServerType']}..."
|
|
|
|
$stderr.puts "[*] URI: #{opts['URI']}" if opts['URI']
|
|
|
|
|
|
|
|
start_rpc_service(opts, frameworkOpts, foreground)
|
|
|
|
end
|
|
|
|
rescue ::Interrupt
|
|
|
|
stop_json_rpc_service(conf: ws_conf_full_path,
|
|
|
|
address: opts['ServerHost'],
|
|
|
|
port: opts['ServerPort'],
|
|
|
|
ssl: opts['SSL'],
|
|
|
|
ssl_key: ws_ssl_key,
|
|
|
|
ssl_cert: ws_ssl_cert,
|
|
|
|
ssl_disable_verify: !ssl_enable_verify,
|
2019-01-09 22:19:15 +00:00
|
|
|
daemonize: !foreground,
|
|
|
|
log: ws_log,
|
|
|
|
pid: ws_rpc_pid) if json_rpc
|
2013-09-30 18:47:53 +00:00
|
|
|
end
|
2019-01-09 03:40:15 +00:00
|
|
|
end
|