2005-07-16 09:37:42 +00:00
|
|
|
module Rex
|
|
|
|
module Proto
|
2005-07-25 09:40:31 +00:00
|
|
|
module DCERPC
|
|
|
|
class Client
|
2005-07-17 08:24:30 +00:00
|
|
|
|
|
|
|
require 'rex/proto/dcerpc/uuid'
|
|
|
|
require 'rex/proto/dcerpc/response'
|
2005-12-13 06:08:40 +00:00
|
|
|
require 'rex/proto/dcerpc/exceptions'
|
2005-07-17 10:24:19 +00:00
|
|
|
require 'rex/text'
|
2005-12-13 06:08:40 +00:00
|
|
|
require 'rex/proto/smb/exceptions'
|
|
|
|
|
2006-01-27 05:29:06 +00:00
|
|
|
attr_accessor :handle, :socket, :options, :last_response, :context, :no_bind, :ispipe, :smb
|
|
|
|
|
|
|
|
# initialize a DCE/RPC Function Call
|
|
|
|
def initialize(handle, socket, useroptions = Hash.new)
|
|
|
|
self.handle = handle
|
|
|
|
self.socket = socket
|
|
|
|
self.options = { 'smb_user' => '', 'smb_pass' => '' } # put default options here...
|
|
|
|
self.options.merge!(useroptions)
|
|
|
|
|
|
|
|
# we must have a valid handle, regardless of everything else
|
|
|
|
raise ArgumentError, 'handle is not a Rex::Proto::DCERPC::Handle' if !self.handle.is_a?(Rex::Proto::DCERPC::Handle)
|
|
|
|
|
|
|
|
# we do this in case socket needs setup first, ie, socket = nil
|
|
|
|
if !self.options['no_socketsetup']
|
|
|
|
self.socket_check()
|
|
|
|
end
|
|
|
|
|
|
|
|
raise ArgumentError, 'socket can not read' if !self.socket.respond_to?(:read)
|
|
|
|
raise ArgumentError, 'socket can not write' if !self.socket.respond_to?(:write)
|
|
|
|
|
|
|
|
if !self.options['no_autobind']
|
|
|
|
self.bind()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def socket_check()
|
|
|
|
if self.socket == nil
|
|
|
|
self.socket_setup()
|
|
|
|
end
|
|
|
|
|
|
|
|
case self.handle.protocol
|
|
|
|
when 'ncacn_ip_tcp'
|
|
|
|
if self.socket.type? != 'tcp'
|
|
|
|
raise "ack, #{self.handle.protocol} requires socket type tcp, not #{self.socket.type?}!"
|
|
|
|
end
|
|
|
|
when 'ncacn_np'
|
|
|
|
if self.socket.class == Rex::Proto::SMB::SimpleClient::OpenFile
|
|
|
|
self.ispipe = 1
|
|
|
|
elsif self.socket.type? == 'tcp'
|
|
|
|
self.smb_connect()
|
|
|
|
else
|
|
|
|
raise "ack, #{self.handle.protocol} requires socket type tcp, not #{self.socket.type?}!"
|
|
|
|
end
|
|
|
|
# don't support ncacn_ip_udp yet
|
|
|
|
## when 'ncacn_ip_udp'
|
|
|
|
## if self.socket.type? != 'udp'
|
|
|
|
## raise "ack, #{self.handle.protocol} requires socket type tcp, not #{self.socket.type?}!"
|
|
|
|
## end
|
|
|
|
else
|
|
|
|
raise "Unsupported protocol : #{self.handle.protocol}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create the appropriate socket based on protocol
|
|
|
|
def socket_setup()
|
2005-12-15 04:46:52 +00:00
|
|
|
ctx = { 'Msf' => options['Msf'], 'MsfExploit' => options['MsfExploit'] }
|
2006-01-27 05:29:06 +00:00
|
|
|
self.socket = case self.handle.protocol
|
|
|
|
when 'ncacn_ip_tcp' then Rex::Socket.create_tcp('PeerHost' => self.handle.address, 'PeerPort' => self.handle.options[0], 'Context' => ctx)
|
|
|
|
when 'ncacn_np' then begin
|
|
|
|
socket = ''
|
|
|
|
begin
|
|
|
|
timeout(10) {
|
|
|
|
socket = Rex::Socket.create_tcp('PeerHost' => self.handle.address, 'PeerPort' => 445, 'Context' => ctx)
|
|
|
|
}
|
|
|
|
rescue Timeout::Error, Rex::ConnectionRefused
|
|
|
|
socket = Rex::Socket.create_tcp('PeerHost' => self.handle.address, 'PeerPort' => 139, 'Context' => ctx)
|
|
|
|
end
|
|
|
|
socket
|
|
|
|
end
|
|
|
|
else nil
|
|
|
|
end
|
2005-12-15 04:46:52 +00:00
|
|
|
|
|
|
|
# Add this socket to the exploit's list of open sockets
|
|
|
|
options['MsfExploit'].add_socket(self.socket) if (options['MsfExploit'])
|
2006-01-27 05:29:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def smb_connect()
|
|
|
|
require 'rex/proto/smb/simpleclient'
|
|
|
|
if self.socket.peerport == 445
|
|
|
|
smb = Rex::Proto::SMB::SimpleClient.new(self.socket, true)
|
|
|
|
elsif self.socket.peerport == 139
|
|
|
|
smb = Rex::Proto::SMB::SimpleClient.new(self.socket)
|
|
|
|
else
|
|
|
|
raise "ACK, how did we get a peerport other than 139 or 445? #{self.socket.peerport}"
|
|
|
|
end
|
|
|
|
smb.client.evasion_level = 0
|
|
|
|
smb.login('*SMBSERVER', self.options['smb_user'], self.options['smb_pass'])
|
|
|
|
smb.connect('IPC$')
|
|
|
|
f = smb.create_pipe(self.handle.options[0])
|
|
|
|
self.socket = f
|
|
|
|
self.smb = smb
|
|
|
|
end
|
|
|
|
|
|
|
|
def read()
|
|
|
|
raw_response = ''
|
|
|
|
|
|
|
|
if self.socket.class == Rex::Proto::SMB::SimpleClient::OpenFile
|
|
|
|
begin
|
|
|
|
if self.options['segment_read']
|
|
|
|
while(true)
|
|
|
|
data = self.socket.read((rand(20)+5), rand(1024)+1)
|
|
|
|
last if ! data.length
|
|
|
|
raw_response += data
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raw_response = self.socket.read()
|
|
|
|
end
|
|
|
|
rescue Rex::Proto::SMB::Exceptions::NoReply
|
|
|
|
# I don't care if I didn't get a reply...
|
|
|
|
rescue Rex::Proto::SMB::Exceptions::ErrorCode => exception
|
|
|
|
if exception.error_code != 0xC000014B
|
|
|
|
raise exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else # must be a regular socket
|
|
|
|
if self.socket.type? == 'tcp'
|
|
|
|
if self.options['segment_read']
|
|
|
|
while (true)
|
|
|
|
data = self.socket.get_once(rand(5)+5, 10)
|
|
|
|
break if data == nil
|
|
|
|
break if ! data.length
|
|
|
|
raw_response << data
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raw_response = self.socket.get_once(-1, 5)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raw_response = self.socket.read(0xFFFFFFFF / 2 - 1) # read max data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raw_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(data)
|
|
|
|
if self.options['segment_write']
|
|
|
|
while (data.length > 0)
|
|
|
|
len = self.socket.write( data.slice!(0, (rand(20)+5)) )
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.socket.write(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
data.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def bind()
|
|
|
|
require 'rex/proto/dcerpc/packet'
|
|
|
|
bind = ''
|
|
|
|
context = ''
|
|
|
|
if self.options['fake_multi_bind']
|
|
|
|
bind, context = Rex::Proto::DCERPC::Packet.make_bind_fake_multi(self.handle.uuid[0], self.handle.uuid[1])
|
|
|
|
else
|
|
|
|
bind, context = Rex::Proto::DCERPC::Packet.make_bind(self.handle.uuid[0], self.handle.uuid[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
raise 'make_bind failed' if !bind
|
|
|
|
|
|
|
|
self.write(bind)
|
|
|
|
raw_response = self.read()
|
|
|
|
response = Rex::Proto::DCERPC::Response.new(raw_response)
|
|
|
|
self.last_response = response
|
|
|
|
if response.type == 12 or response.type == 15
|
|
|
|
if self.last_response.ack_result[context] == 2
|
|
|
|
raise "Could not bind to #{self.handle}"
|
|
|
|
end
|
|
|
|
self.context = context
|
|
|
|
else
|
|
|
|
raise "Could not bind to #{self.handle}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Perform a DCE/RPC Function Call
|
|
|
|
def call(function, data)
|
|
|
|
|
|
|
|
frag_size = data.length
|
|
|
|
if options['frag_size']
|
|
|
|
frag_size = options['frag_size']
|
|
|
|
end
|
2006-03-29 20:46:29 +00:00
|
|
|
object_id = ''
|
|
|
|
if options['object_call']
|
|
|
|
object_id = self.handle.uuid[0]
|
|
|
|
end
|
|
|
|
if options['random_object_id']
|
|
|
|
object_id = Rex::Proto::DCERPC::UUID.uuid_unpack(Rex::Text.rand_text(16))
|
|
|
|
end
|
2006-01-27 05:29:06 +00:00
|
|
|
|
2006-03-29 20:46:29 +00:00
|
|
|
call_packets = Rex::Proto::DCERPC::Packet.make_request(function, data, frag_size, self.context, object_id)
|
2006-01-27 05:29:06 +00:00
|
|
|
call_packets.each { |packet|
|
|
|
|
self.write(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_response = self.read()
|
|
|
|
if (raw_response == nil or raw_response.length == 0)
|
|
|
|
raise Rex::Proto::DCERPC::Exceptions::NoResponse
|
|
|
|
end
|
|
|
|
self.last_response = Rex::Proto::DCERPC::Response.new(raw_response)
|
|
|
|
|
|
|
|
if self.last_response.type == 3
|
|
|
|
e = Rex::Proto::DCERPC::Exceptions::Fault.new
|
|
|
|
e.fault = self.last_response.status
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
|
|
|
self.last_response.stub_data
|
|
|
|
end
|
2005-07-17 10:24:19 +00:00
|
|
|
|
|
|
|
# Process a DCERPC response packet from a socket
|
2005-11-16 17:56:07 +00:00
|
|
|
def self.read_response (socket, timeout=5)
|
2005-07-17 23:28:28 +00:00
|
|
|
|
2005-11-16 17:56:07 +00:00
|
|
|
data = socket.get_once(-1, timeout)
|
|
|
|
|
|
|
|
# We need at least 10 bytes to find the FragLen
|
|
|
|
if (! data or data.length() < 10)
|
2005-07-16 09:37:42 +00:00
|
|
|
return
|
|
|
|
end
|
2005-07-17 23:28:28 +00:00
|
|
|
|
2005-11-16 17:56:07 +00:00
|
|
|
# Pass the first 10 bytes to the constructor
|
|
|
|
resp = Rex::Proto::DCERPC::Response.new(data.slice!(0, 10))
|
2005-07-16 09:37:42 +00:00
|
|
|
|
2005-11-16 17:56:07 +00:00
|
|
|
# Something went wrong in the parser...
|
2005-07-17 08:24:30 +00:00
|
|
|
if (! resp.frag_len)
|
2005-07-16 09:37:42 +00:00
|
|
|
return resp
|
|
|
|
end
|
2005-07-17 23:28:28 +00:00
|
|
|
|
2005-11-16 17:56:07 +00:00
|
|
|
# Do we need to read more data?
|
|
|
|
if (resp.frag_len > (data.length + 10))
|
|
|
|
begin
|
|
|
|
data << socket.timed_read(resp.frag_len - data.length - 10, timeout)
|
|
|
|
rescue Timeout::Error
|
|
|
|
end
|
2005-09-16 03:29:27 +00:00
|
|
|
end
|
2005-11-16 17:56:07 +00:00
|
|
|
|
|
|
|
# Still missing some data...
|
|
|
|
if (data.length() != resp.frag_len - 10)
|
|
|
|
$stderr.puts "Truncated DCERPC response :-("
|
2005-07-16 09:37:42 +00:00
|
|
|
return resp
|
|
|
|
end
|
2005-07-17 23:28:28 +00:00
|
|
|
|
2005-11-16 17:56:07 +00:00
|
|
|
resp.parse(data)
|
2005-07-16 09:37:42 +00:00
|
|
|
return resp
|
|
|
|
end
|
2005-07-17 10:24:19 +00:00
|
|
|
|
2005-07-16 09:37:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-07-25 09:40:31 +00:00
|
|
|
end
|