2006-01-17 01:12:38 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
require 'rex/proto/sunrpc/xdr'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module SunRPC
|
|
|
|
|
|
|
|
# XXX: CPORT!
|
|
|
|
class Client
|
|
|
|
AUTH_NULL = 0
|
|
|
|
AUTH_UNIX = 1
|
|
|
|
|
|
|
|
PMAP_PROG = 100000
|
|
|
|
PMAP_VERS = 2
|
|
|
|
PMAP_GETPORT = 3
|
|
|
|
|
|
|
|
CALL = 0
|
|
|
|
|
|
|
|
MSG_ACCEPTED = 0
|
2006-01-21 02:44:01 +00:00
|
|
|
|
|
|
|
SUCCESS = 0 # RPC executed successfully
|
|
|
|
PROG_UMAVAIL = 1 # Remote hasn't exported program
|
|
|
|
PROG_MISMATCH = 2 # Remote can't support version #
|
|
|
|
PROC_UNAVAIL = 3 # Program can't support procedure
|
|
|
|
GARBAGE_ARGS = 4 # Procedure can't decode params
|
2006-01-17 01:12:38 +00:00
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
attr_reader :rhost, :rport, :proto, :program, :version
|
2006-01-17 01:12:38 +00:00
|
|
|
attr_accessor :pport
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
def initialize(rhost, rport, proto, program, version)
|
|
|
|
if proto.downcase !~ /^(tcp|udp)$/
|
|
|
|
raise ArgumentError, 'Protocol is not "tcp" or "udp"'
|
|
|
|
end
|
|
|
|
|
|
|
|
@rhost, @rport, @program, @version, @proto = \
|
|
|
|
rhost, rport, program, version, proto.downcase
|
|
|
|
|
2006-01-17 01:12:38 +00:00
|
|
|
@pport = nil
|
|
|
|
|
|
|
|
@auth_type = AUTH_NULL
|
|
|
|
@auth_data = ''
|
|
|
|
|
|
|
|
@call_sock = nil
|
|
|
|
end
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
# XXX: Add optional parameter to have proto be something else
|
|
|
|
def create()
|
2006-01-17 01:12:38 +00:00
|
|
|
proto_num = 0
|
2006-01-21 02:44:01 +00:00
|
|
|
if @proto.eql?('tcp')
|
2006-01-17 01:12:38 +00:00
|
|
|
proto_num = 6
|
2006-01-21 02:44:01 +00:00
|
|
|
elsif @proto.eql?('udp')
|
2006-01-17 01:12:38 +00:00
|
|
|
proto_num = 17
|
|
|
|
end
|
|
|
|
|
|
|
|
buf =
|
|
|
|
XDR.encode(CALL, 2, PMAP_PROG, PMAP_VERS, PMAP_GETPORT,
|
|
|
|
@auth_type, [@auth_data, 400], AUTH_NULL, '',
|
|
|
|
@program, @version, proto_num, 0)
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
sock = Client.make_rpc(@proto, @rhost, @rport)
|
|
|
|
Client.send_rpc(sock, buf)
|
|
|
|
ret = Client.recv_rpc(sock)
|
|
|
|
Client.close_rpc(sock)
|
2006-01-17 01:12:38 +00:00
|
|
|
|
|
|
|
arr = XDR.decode!(ret, Integer, Integer, Integer, String, Integer,
|
|
|
|
Integer)
|
|
|
|
if arr[1] != MSG_ACCEPTED || arr[4] != SUCCESS || arr[5] == 0
|
2006-01-21 02:44:01 +00:00
|
|
|
# Check PRO[CG]_*/GARBAGE_ARGS
|
|
|
|
raise 'create failed'
|
2006-01-17 01:12:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@pport = arr[5]
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(procedure, buffer)
|
|
|
|
buf =
|
|
|
|
XDR.encode(CALL, 2, @program, @version, procedure,
|
|
|
|
@auth_type, [@auth_data, 400], AUTH_NULL, '')+
|
|
|
|
buffer
|
|
|
|
|
|
|
|
if !@call_sock
|
2006-01-21 02:44:01 +00:00
|
|
|
@call_sock = Client.make_rpc(@proto, @rhost, @pport)
|
2006-01-17 01:12:38 +00:00
|
|
|
end
|
2006-01-21 02:44:01 +00:00
|
|
|
Client.send_rpc(@call_sock, buf)
|
|
|
|
ret = Client.recv_rpc(@call_sock)
|
2006-01-17 01:12:38 +00:00
|
|
|
|
|
|
|
arr = XDR.decode!(ret, Integer, Integer, Integer, String, Integer)
|
|
|
|
if arr[1] != MSG_ACCEPTED || arr[4] != SUCCESS
|
2006-01-21 02:44:01 +00:00
|
|
|
raise 'call failed'
|
2006-01-17 01:12:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
def destroy
|
|
|
|
Client.close_rpc(@call_sock) if @call_sock
|
|
|
|
@call_sock = nil
|
2006-01-17 01:12:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
def authnull_create
|
2006-01-17 01:12:38 +00:00
|
|
|
@auth_type = AUTH_NULL
|
|
|
|
@auth_data = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def authunix_create(host, uid, gid, groupz)
|
|
|
|
raise ArgumentError, 'Hostname length is too long' if host.length > 255
|
2006-01-21 02:44:01 +00:00
|
|
|
# 10?
|
2006-01-17 01:12:38 +00:00
|
|
|
raise ArgumentError, 'Too many groups' if groupz.length > 10
|
|
|
|
|
|
|
|
@auth_type = AUTH_UNIX
|
|
|
|
@auth_data =
|
|
|
|
XDR.encode(0, host, uid, gid, groupz) # XXX: TIME! GROUPZ?!
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# XXX: Dirty, integrate some sort of request system into create/call?
|
2006-01-21 02:44:01 +00:00
|
|
|
def Client.portmap_req(host, port, rpc_vers, procedure, buffer)
|
2006-01-17 01:12:38 +00:00
|
|
|
buf = XDR.encode(CALL, 2, PMAP_PROG, rpc_vers, procedure,
|
|
|
|
AUTH_NULL, '', AUTH_NULL, '') + buffer
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
sock = Client.make_rpc('tcp', host, port)
|
|
|
|
Client.send_rpc(sock, buf)
|
|
|
|
ret = Client.recv_rpc(sock)
|
|
|
|
Client.close_rpc(sock)
|
2006-01-17 01:12:38 +00:00
|
|
|
|
|
|
|
arr = XDR.decode!(ret, Integer, Integer, Integer, String, Integer)
|
|
|
|
if arr[1] != MSG_ACCEPTED || arr[4] != SUCCESS || arr[5] == 0
|
2006-01-21 02:44:01 +00:00
|
|
|
raise 'portmap_req failed'
|
2006-01-17 01:12:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
# Msf::Config.data_directory
|
2006-01-21 02:44:01 +00:00
|
|
|
# def Client.program2name(number)
|
2006-01-17 01:12:38 +00:00
|
|
|
# File.foreach('data/rpc_names') { |line|
|
|
|
|
# next if line.empty? || line =~ /^\s*#/
|
|
|
|
#
|
|
|
|
# if line =~ /^(\S+?)\s+(\d+)/ && number == $2.to_i
|
|
|
|
# return $1
|
|
|
|
# end
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# return "UNKNOWN-#{number}"
|
|
|
|
# end
|
|
|
|
|
|
|
|
private
|
2006-01-21 02:44:01 +00:00
|
|
|
def Client.make_rpc(proto, host, port)
|
|
|
|
Rex::Socket.create(
|
2006-01-17 01:12:38 +00:00
|
|
|
'PeerHost' => host,
|
|
|
|
'PeerPort' => port,
|
|
|
|
'Proto' => proto)
|
|
|
|
end
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
def Client.send_rpc(sock, buf)
|
2006-01-17 01:12:38 +00:00
|
|
|
buf = gen_xid() + buf
|
2006-01-21 02:44:01 +00:00
|
|
|
if sock.type?.eql?('tcp')
|
2006-01-17 01:12:38 +00:00
|
|
|
buf = XDR.encode(0x80000000 | buf.length) + buf
|
|
|
|
end
|
|
|
|
sock.write(buf)
|
|
|
|
end
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
def Client.recv_rpc(sock)
|
2006-01-17 01:12:38 +00:00
|
|
|
buf = sock.get_once(-1, 5)
|
|
|
|
buf.slice!(0..3)
|
2006-01-21 02:44:01 +00:00
|
|
|
if sock.type?.eql?('tcp')
|
2006-01-17 01:12:38 +00:00
|
|
|
buf.slice!(0..3)
|
|
|
|
end
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
def Client.close_rpc(sock)
|
2006-01-17 01:12:38 +00:00
|
|
|
sock.close
|
|
|
|
end
|
|
|
|
|
2006-01-21 02:44:01 +00:00
|
|
|
def Client.gen_xid
|
2006-01-17 01:12:38 +00:00
|
|
|
return XDR.encode(rand(0xffffffff) + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|