Skip to content

Commit 08df0bf

Browse files
committed
Land rapid7#4858, RPC client true/truthy fix
* Misc ruby cleanup and fixing the issue that caused MSP-12235, rolling back the full rollback of PR 4823
2 parents df80d56 + 9e5231f commit 08df0bf

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

lib/msf/core/rpc/v10/client.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def initialize(info={})
3131

3232
def login(user,pass)
3333
res = self.call("auth.login", user, pass)
34-
if(not (res and res['result'] == "success"))
34+
unless (res && res['result'] == "success")
3535
raise RuntimeError, "authentication failed"
3636
end
3737
self.token = res['token']
@@ -41,16 +41,16 @@ def login(user,pass)
4141
# Prepend the authentication token as the first parameter
4242
# of every call except auth.login. Requires the
4343
def call(meth, *args)
44-
if(meth != "auth.login")
45-
if(not self.token)
44+
unless meth == "auth.login"
45+
unless self.token
4646
raise RuntimeError, "client not authenticated"
4747
end
4848
args.unshift(self.token)
4949
end
5050

5151
args.unshift(meth)
5252

53-
if not @cli
53+
unless @cli
5454
@cli = Rex::Proto::Http::Client.new(info[:host], info[:port], info[:context], info[:ssl], info[:ssl_version])
5555
@cli.set_config(
5656
:vhost => info[:host],
@@ -69,10 +69,12 @@ def call(meth, *args)
6969
res = @cli.send_recv(req)
7070
@cli.close
7171

72-
if res and [200, 401, 403, 500].include?(res.code)
72+
if res && [200, 401, 403, 500].include?(res.code)
7373
resp = MessagePack.unpack(res.body)
7474

75-
if resp and resp.kind_of?(::Hash) and resp['error'] == true
75+
# Boolean true versus truthy check required here;
76+
# RPC responses such as { "error" => "Here I am" } and { "error" => "" } must be accommodated.
77+
if resp && resp.kind_of?(::Hash) && resp['error'] == true
7678
raise Msf::RPC::ServerException.new(resp['error_code'] || res.code, resp['error_message'] || resp['error_string'], resp['error_class'], resp['error_backtrace'])
7779
end
7880

@@ -83,7 +85,7 @@ def call(meth, *args)
8385
end
8486

8587
def close
86-
if @cli and @cli.conn?
88+
if @cli && @cli.conn?
8789
@cli.close
8890
end
8991
@cli = nil

lib/msf/core/rpc/v10/service.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,21 @@ def process(req)
112112
end
113113
end
114114

115-
if not (req.headers["Content-Type"] and req.headers["Content-Type"] == "binary/message-pack")
115+
unless (req.headers["Content-Type"] && req.headers["Content-Type"] == "binary/message-pack")
116116
raise ArgumentError, "Invalid Content Type"
117117
end
118118

119119
msg = MessagePack.unpack(req.body)
120120

121-
if not (msg and msg.kind_of?(::Array) and msg.length > 0)
121+
unless (msg && msg.kind_of?(::Array) && msg.length > 0)
122122
raise ArgumentError, "Invalid Message Format"
123123
end
124124

125125
msg.map { |a| a.respond_to?(:force_encoding) ? a.force_encoding(self.str_encoding) : a }
126126

127127
group, funct = msg.shift.split(".", 2)
128128

129-
if not self.handlers[group]
129+
unless self.handlers[group]
130130
raise ArgumentError, "Unknown API Group: '#{group.inspect}'"
131131
end
132132

@@ -138,13 +138,13 @@ def process(req)
138138
mname << '_noauth'
139139
end
140140

141-
if not self.handlers[group].respond_to?(mname)
141+
unless self.handlers[group].respond_to?(mname)
142142
raise ArgumentError, "Unknown API Call: '#{mname.inspect}'"
143143
end
144144

145145
if doauth
146146
token = msg.shift
147-
if not authenticate(token)
147+
unless authenticate(token)
148148
raise ::Msf::RPC::Exception.new(401, "Invalid Authentication Token")
149149
end
150150
end
@@ -203,7 +203,7 @@ def authenticate(token)
203203
stale = []
204204

205205

206-
if not (token and token.kind_of?(::String))
206+
unless (token && token.kind_of?(::String))
207207
return false
208208
end
209209

@@ -212,17 +212,17 @@ def authenticate(token)
212212

213213
self.tokens.each_key do |t|
214214
user,ctime,mtime,perm = self.tokens[t]
215-
if ! perm and mtime + self.token_timeout < Time.now.to_i
215+
if !perm && mtime + self.token_timeout < Time.now.to_i
216216
stale << t
217217
end
218218
end
219219

220220
stale.each { |t| self.tokens.delete(t) }
221221

222-
if not self.tokens[token]
222+
unless self.tokens[token]
223223

224224
begin
225-
if framework.db.active and ::Mdm::ApiKey.find_by_token(token)
225+
if framework.db.active && ::Mdm::ApiKey.find_by_token(token)
226226
return true
227227
end
228228
rescue ::Exception => e

msfrpc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ opts = {
4141
}
4242

4343
# Parse command line arguments.
44-
arguments.parse(ARGV) { |opt, idx, val|
44+
arguments.parse(ARGV) do |opt, idx, val|
4545
case opt
4646
when "-a"
4747
opts['ServerHost'] = val
@@ -57,16 +57,16 @@ arguments.parse(ARGV) { |opt, idx, val|
5757
print("\nUsage: #{File.basename(__FILE__)} <options>\n" + arguments.usage)
5858
exit
5959
end
60-
}
60+
end
6161

6262

63-
if(not opts['ServerHost'])
63+
unless opts['ServerHost']
6464
$stderr.puts "[-] Error: a server IP must be specified (-a)"
6565
$stderr.puts arguments.usage
6666
exit(0)
6767
end
6868

69-
if(not opts['Pass'])
69+
unless opts['Pass']
7070
$stderr.puts "[-] Error: a password must be specified (-P)"
7171
$stderr.puts arguments.usage
7272
exit(0)
@@ -83,10 +83,11 @@ rpc = Msf::RPC::Client.new(
8383
:ssl => opts['SSL']
8484
)
8585

86-
res = rpc.login(opts['User'], opts['Pass'])
86+
rpc.login(opts['User'], opts['Pass'])
8787

8888
puts "[*] The 'rpc' object holds the RPC client interface"
89-
puts ""
89+
puts "[*] Use rpc.call('group.command') to make RPC calls"
90+
puts ''
9091

9192
while(ARGV.shift)
9293
end

msfrpcd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ arguments.parse(ARGV) { |opt, idx, val|
7272
end
7373
}
7474

75-
if(not opts['Pass'])
75+
unless opts['Pass']
7676
$stderr.puts "[-] Error: a password must be specified (-P)"
7777
exit(0)
7878
end
@@ -83,7 +83,7 @@ rpctype = 'MSG'
8383

8484
$stderr.puts "[*] #{rpctype}RPC starting on #{opts['ServerHost']}:#{opts['ServerPort']} (#{opts['SSL'] ? "SSL" : "NO SSL"}):#{opts['ServerType']}..."
8585

86-
$stderr.puts "[*] URI: #{opts['URI']}" if(opts['URI'])
86+
$stderr.puts "[*] URI: #{opts['URI']}" if opts['URI']
8787

8888
require 'msf/base'
8989
require 'msf/ui'

plugins/msgrpc.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#!/usr/bin/env ruby
22
#
3-
# $Id$
4-
#
53
# This plugin provides an msf daemon interface that spawns a listener on a
64
# defined port (default 55552) and gives each connecting client its own
75
# console interface. These consoles all share the same framework instance.
86
# Be aware that the console instance that spawns on the port is entirely
97
# unauthenticated, so realize that you have been warned.
108
#
11-
# $Revision$
12-
#
139

1410
require "msf/core/rpc/v10/service"
1511
require "fileutils"
@@ -43,7 +39,7 @@ def initialize(framework, opts)
4339

4440
host = opts['ServerHost'] || DefaultHost
4541
port = opts['ServerPort'] || DefaultPort
46-
ssl = (opts['SSL'] and opts['SSL'].to_s =~ /^[ty]/i) ? true : false
42+
ssl = (opts['SSL'] && opts['SSL'].to_s =~ /^[ty]/i) ? true : false
4743
cert = opts['SSLCert']
4844

4945
user = opts['User'] || "msf"
@@ -67,7 +63,7 @@ def initialize(framework, opts)
6763

6864
# If the run in foreground flag is not specified, then go ahead and fire
6965
# it off in a worker thread.
70-
if (opts['RunInForeground'] != true)
66+
unless opts['RunInForeground'] == true
7167
# Store a handle to the thread so we can kill it during
7268
# cleanup when we get unloaded.
7369
self.thread = Thread.new { run }

0 commit comments

Comments
 (0)