forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
msfrpcd
executable file
·247 lines (221 loc) · 9.34 KB
/
msfrpcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env ruby
# -*- coding: binary -*-
#
# $Id$
#
# This user interface listens on a port and provides clients that connect to
# it with an RPC or JSON-RPC interface to the Metasploit Framework.
#
# $Revision$
#
RPC_TYPE = 'Msg'
WS_TAG = 'msf-ws'
WS_RPC_TAG = 'msf-json-rpc'
WS_CONF = "#{WS_RPC_TAG}.ru"
WS_ENV = 'production'
def start_json_rpc_service(conf:, address:, port:, ssl:, ssl_key:, ssl_cert:,
ssl_disable_verify:, daemonize:, log:, pid:)
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
if File.file?(pid)
ws_pid = Msf::Util::ServiceHelper.tail(pid)
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}"
$stdout.puts "[*] Deleting MSF JSON-RPC web service PID file #{pid}"
File.delete(pid)
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,
env: WS_ENV,
daemonize: daemonize,
log: log,
pid: pid,
tag: WS_RPC_TAG)
Msf::Util::ServiceHelper.run_cmd("#{thin_cmd} start")
end
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)
$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'
if File.file?(pid)
$stdout.puts "[*] Deleting MSF JSON-RPC web service PID file #{pid}"
File.delete(pid)
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,
env: WS_ENV,
daemonize: daemonize,
log: log,
pid: pid,
tag: WS_RPC_TAG)
Msf::Util::ServiceHelper.run_cmd("#{thin_cmd} stop")
end
end
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}..."
child_pid = Process.fork()
if child_pid
$stderr.puts "[*] #{RPC_TYPE.upcase}RPC background PID #{child_pid}"
exit(0)
end
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']
require 'rex/parser/arguments'
ws_ssl_key_default = File.join(Msf::Config.config_directory, "#{WS_TAG}-key.pem")
ws_ssl_cert_default = File.join(Msf::Config.config_directory, "#{WS_TAG}-cert.pem")
ws_log = File.join(Msf::Config.config_directory, 'logs', "#{WS_RPC_TAG}.log")
ws_rpc_pid = File.join(Msf::Config.config_directory, "#{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 = {}
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" ],
"-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})" ],
"-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
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
when "-t"
opts['TokenTimeout'] = val.to_i
when "-f"
foreground = true
when "-u"
opts['URI'] = val
when "-n"
frameworkOpts['DisableDatabase'] = true
when "-j"
json_rpc = true
when "-k"
ws_ssl_key = val
when "-c"
ws_ssl_cert = val
when "-v"
ssl_enable_verify = true
when "-h"
print("\nUsage: #{File.basename(__FILE__)} <options>\n" + arguments.usage)
exit
end
}
$0 = "msfrpcd"
begin
if json_rpc
if !File.file?(ws_ssl_key_default) || !File.file?(ws_ssl_cert_default)
$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"})..."
$stderr.puts "[*] URI: /api/v1/json-rpc"
$stderr.puts "[*] JSON-RPC server log: #{ws_log}" unless foreground
$stderr.puts "[*] JSON-RPC server PID file: #{ws_rpc_pid}" unless foreground
ws_conf_full_path = File.expand_path(File.join(File.dirname(msfbase), WS_CONF))
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,
daemonize: !foreground,
log: ws_log,
pid: ws_rpc_pid)
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,
daemonize: !foreground,
log: ws_log,
pid: ws_rpc_pid) if json_rpc
end
end