2008-12-02 22:09:34 +00:00
|
|
|
require "xmlrpc/client"
|
|
|
|
require "rex"
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.sock.put(request + "\x00")
|
2010-03-11 22:21:41 +00:00
|
|
|
|
|
|
|
while(not @buff.index("\x00"))
|
|
|
|
resp = self.sock.get
|
|
|
|
if ( (!resp or resp == "") and @buff.index("\x00").nil?)
|
|
|
|
raise RuntimeError, "XMLRPC connection closed: #{resp.inspect}"
|
2008-12-02 22:09:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@buff << resp if resp
|
|
|
|
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
|
|
|
|
self.sock.close
|
|
|
|
end
|
|
|
|
|
2008-12-02 22:09:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-03-11 22:21:41 +00:00
|
|
|
|