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

137 lines
3.4 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
module Rmi
module Client
2015-01-08 20:01:04 +00:00
require 'msf/rmi/client/streams'
include Msf::Rmi::Client::Streams
2015-01-08 02:42:44 +00:00
include Exploit::Remote::Tcp
# Returns the target host
#
# @return [String]
def rhost
datastore['RHOST']
end
# Returns the target port
#
# @return [Fixnum]
def rport
datastore['RPORT']
end
# Returns the RMI server peer
#
# @return [String]
def peer
"#{rhost}:#{rport}"
end
2015-01-08 21:46:24 +00:00
# Sends a RMI header stream
2015-01-08 02:42:44 +00:00
#
# @param opts [Hash]
# @option opts [Rex::Socket::Tcp] :sock
2015-01-08 21:46:24 +00:00
# @return [Fixnum] the number of bytes sent
# @see Msf::Rmi::Client::Streams#build_header
2015-01-08 02:42:44 +00:00
def send_header(opts = {})
nsock = opts[:sock] || sock
stream = build_header(opts)
nsock.put(stream.encode + "\x00\x00\x00\x00\x00\x00")
2015-01-08 02:42:44 +00:00
end
2015-01-08 21:46:24 +00:00
# Sends a RMI CALL stream
2015-01-08 02:42:44 +00:00
#
# @param opts [Hash]
# @option opts [Rex::Socket::Tcp] :sock
2015-01-08 21:46:24 +00:00
# @return [Fixnum] the number of bytes sent
# @see Msf::Rmi::Client::Streams#build_call
2015-01-08 02:42:44 +00:00
def send_call(opts = {})
nsock = opts[:sock] || sock
stream = build_call(opts)
nsock.put(stream.encode)
2015-01-08 21:46:24 +00:00
end
2015-01-13 17:02:16 +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)
end
2015-01-08 21:46:24 +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
ack
end
# 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
return_data.return_value
end
# 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 22:04:56 +00:00
begin
res = nsock.get_once
rescue ::EOFError
res = nil
end
2015-01-08 21:46:24 +00:00
until res.nil? || res.length < 1448
data << res
begin
res = nsock.get_once
rescue ::EOFError
res = nil
end
end
2015-01-08 02:42:44 +00:00
2015-01-08 21:46:24 +00:00
data << res if res
data
2015-01-08 02:42:44 +00:00
end
end
end
end