metasploit-framework/lib/msf/java/rmi/client.rb

139 lines
3.6 KiB
Ruby
Raw Normal View History

2015-01-08 02:42:44 +00:00
# -*- coding: binary -*-
require 'rex/proto/rmi'
require 'rex/java/serialization'
require 'stringio'
module Msf
2015-02-10 16:58:57 +00:00
module Java
module Rmi
module Client
2015-01-08 02:42:44 +00:00
2015-02-10 16:58:57 +00:00
require 'msf/java/rmi/client/streams'
2015-01-08 20:01:04 +00:00
2015-02-10 16:58:57 +00:00
include Msf::Java::Rmi::Client::Streams
include Exploit::Remote::Tcp
2015-01-08 02:42:44 +00:00
2015-02-10 16:58:57 +00:00
# Returns the target host
#
# @return [String]
def rhost
datastore['RHOST']
end
2015-01-08 02:42:44 +00:00
2015-02-10 16:58:57 +00:00
# Returns the target port
#
# @return [Fixnum]
def rport
datastore['RPORT']
end
2015-01-08 02:42:44 +00:00
2015-02-10 16:58:57 +00:00
# Returns the RMI server peer
#
# @return [String]
def peer
"#{rhost}:#{rport}"
end
2015-01-08 02:42:44 +00:00
2015-02-10 16:58:57 +00:00
# Sends a RMI header stream
#
# @param opts [Hash]
# @option opts [Rex::Socket::Tcp] :sock
# @return [Fixnum] the number of bytes sent
# @see Msf::Rmi::Client::Streams#build_header
def send_header(opts = {})
nsock = opts[:sock] || sock
stream = build_header(opts)
nsock.put(stream.encode + "\x00\x00\x00\x00\x00\x00")
end
2015-01-08 21:46:24 +00:00
2015-02-10 16:58:57 +00:00
# Sends a RMI CALL stream
#
# @param opts [Hash]
# @option opts [Rex::Socket::Tcp] :sock
# @return [Fixnum] the number of bytes sent
# @see Msf::Rmi::Client::Streams#build_call
def send_call(opts = {})
nsock = opts[:sock] || sock
stream = build_call(opts)
nsock.put(stream.encode)
end
2015-01-13 17:02:16 +00:00
2015-02-10 16:58:57 +00:00
# Sends a RMI DGCACK stream
#
# @param opts [Hash]
# @option opts [Rex::Socket::Tcp] :sock
# @return [Fixnum] the number of bytes sent
# @see Msf::Rmi::Client::Streams#build_dgc_ack
def send_dgc_ack(opts = {})
nsock = opts[:sock] || sock
stream = build_dgc_ack(opts)
nsock.put(stream.encode)
2015-01-08 21:46:24 +00:00
end
2015-02-10 16:58:57 +00:00
# Reads the Protocol Ack
#
# @param opts [Hash]
# @option opts [Rex::Socket::Tcp] :sock
# @return [Rex::Proto::Rmi::Model::ProtocolAck]
# @see Rex::Proto::Rmi::Model::ProtocolAck.decode
def recv_protocol_ack(opts = {})
nsock = opts[:sock] || sock
data = safe_get_once(nsock)
begin
ack = Rex::Proto::Rmi::Model::ProtocolAck.decode(StringIO.new(data))
rescue ::RuntimeError
return nil
end
2015-01-08 21:46:24 +00:00
2015-02-10 16:58:57 +00:00
ack
2015-01-08 21:46:24 +00:00
end
2015-02-10 16:58:57 +00:00
# Reads a ReturnData message and returns the java serialized stream
# with the return data value.
#
# @param opts [Hash]
# @option opts [Rex::Socket::Tcp] :sock
# @return [Rex::Java::Serialization::Stream]
# @see Rex::Proto::Rmi::Model::ReturnData.decode
def recv_return(opts = {})
nsock = opts[:sock] || sock
data = safe_get_once(nsock)
begin
return_data = Rex::Proto::Rmi::Model::ReturnData.decode(StringIO.new(data))
rescue ::RuntimeError
return nil
end
2015-01-08 21:46:24 +00:00
2015-02-10 16:58:57 +00:00
return_data.return_value
2015-01-08 22:04:56 +00:00
end
2015-02-10 16:58:57 +00:00
# Helper method to read fragmented data from a ```Rex::Socket::Tcp```
#
# @param opts [Hash]
# @option opts [Rex::Socket::Tcp] :sock
# @return [String]
def safe_get_once(nsock = sock)
data = ''
2015-01-08 21:46:24 +00:00
begin
res = nsock.get_once
rescue ::EOFError
res = nil
end
2015-01-08 02:42:44 +00:00
2015-02-10 16:58:57 +00:00
until res.nil? || res.length < 1448
data << res
begin
res = nsock.get_once
rescue ::EOFError
res = nil
end
end
data << res if res
data
end
2015-01-08 02:42:44 +00:00
end
end
end
end