2008-12-02 22:09:34 +00:00
|
|
|
require "xmlrpc/client"
|
2011-05-12 20:03:55 +00:00
|
|
|
require "rex"
|
2008-12-02 22:09:34 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
module RPC
|
|
|
|
|
|
|
|
# Loosely based on the XMLRPC::ClientS class
|
|
|
|
# Reimplemented for Metasploit
|
|
|
|
|
|
|
|
class Client < ::XMLRPC::Client
|
|
|
|
|
|
|
|
attr_accessor :sock, :token
|
2010-03-11 22:21:41 +00:00
|
|
|
|
2008-12-02 22:09:34 +00:00
|
|
|
# Use a TCP socket to do RPC
|
|
|
|
def initialize(info={})
|
2010-03-11 22:21:41 +00:00
|
|
|
|
2008-12-02 22:09:34 +00:00
|
|
|
@buff = ""
|
|
|
|
self.sock = Rex::Socket::Tcp.create(
|
|
|
|
'PeerHost' => info[:host],
|
|
|
|
'PeerPort' => info[:port],
|
|
|
|
'SSL' => info[:ssl]
|
|
|
|
)
|
|
|
|
end
|
2010-03-11 22:21:41 +00:00
|
|
|
|
2008-12-02 22:09:34 +00:00
|
|
|
# This override hooks into the RPCXML library
|
|
|
|
def do_rpc(request,async)
|
2010-03-11 22:21:41 +00:00
|
|
|
|
2010-10-08 06:14:44 +00:00
|
|
|
begin
|
2011-02-04 03:33:37 +00:00
|
|
|
self.sock.put(request + "\x00")
|
|
|
|
|
2010-10-08 06:14:44 +00:00
|
|
|
while(not @buff.index("\x00"))
|
|
|
|
if ::IO.select([self.sock], nil, nil, 30)
|
|
|
|
resp = self.sock.sysread(32768)
|
|
|
|
@buff << resp if resp
|
|
|
|
end
|
2008-12-02 22:09:34 +00:00
|
|
|
end
|
2010-10-08 06:14:44 +00:00
|
|
|
rescue ::Exception => e
|
2011-02-04 03:33:37 +00:00
|
|
|
self.close
|
2010-10-08 06:14:44 +00:00
|
|
|
raise EOFError, "XMLRPC connection closed"
|
2008-12-02 22:09:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
mesg,left = @buff.split("\x00", 2)
|
|
|
|
@buff = left.to_s
|
|
|
|
mesg
|
|
|
|
end
|
2010-03-11 22:21:41 +00:00
|
|
|
|
2008-12-02 22:09:34 +00:00
|
|
|
def login(user,pass)
|
|
|
|
res = self.call("auth.login", user, pass)
|
|
|
|
if(not (res and res['result'] == "success"))
|
|
|
|
raise RuntimeError, "authentication failed"
|
|
|
|
end
|
|
|
|
self.token = res['token']
|
|
|
|
true
|
|
|
|
end
|
2010-03-11 22:21:41 +00:00
|
|
|
|
2008-12-02 22:09:34 +00:00
|
|
|
# Prepend the authentication token as the first parameter
|
2010-03-11 22:21:41 +00:00
|
|
|
# of every call except auth.login. Requires the
|
2008-12-02 22:09:34 +00:00
|
|
|
def call(meth, *args)
|
|
|
|
if(meth != "auth.login")
|
|
|
|
if(not self.token)
|
|
|
|
raise RuntimeError, "client not authenticated"
|
|
|
|
end
|
|
|
|
args.unshift(self.token)
|
|
|
|
end
|
|
|
|
super(meth, *args)
|
|
|
|
end
|
|
|
|
|
2010-04-12 19:25:29 +00:00
|
|
|
def close
|
2011-02-04 03:33:37 +00:00
|
|
|
self.sock.close rescue nil
|
|
|
|
self.sock = nil
|
2010-04-12 19:25:29 +00:00
|
|
|
end
|
|
|
|
|
2008-12-02 22:09:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-03-11 22:21:41 +00:00
|
|
|
|