2005-07-16 09:37:42 +00:00
|
|
|
require 'rex/proto/dcerpc/uuid'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Proto
|
2005-07-25 09:40:31 +00:00
|
|
|
module DCERPC
|
|
|
|
class Response
|
2005-07-16 09:37:42 +00:00
|
|
|
|
|
|
|
attr_accessor :frag_len, :auth_len, :type, :vers_major, :vers_minor
|
|
|
|
attr_accessor :flags, :data_rep, :call_id, :max_frag_xmit, :max_frag_recv
|
2005-07-17 23:28:28 +00:00
|
|
|
attr_accessor :assoc_group, :sec_addr_len, :sec_addr, :num_results
|
|
|
|
attr_accessor :nack_reason, :xfer_syntax_uuid, :xfer_syntax_vers
|
|
|
|
attr_accessor :ack_reason, :ack_result, :ack_xfer_syntax_uuid, :ack_xfer_syntax_vers
|
2005-07-16 09:37:42 +00:00
|
|
|
attr_accessor :alloc_hint, :context_id, :cancel_cnt, :status, :stub_data
|
|
|
|
|
2005-09-23 03:43:04 +00:00
|
|
|
# Create a new DCERPC::Response object
|
|
|
|
# This can be initialized in two ways:
|
|
|
|
# 1) Call .new() with the first 10 bytes of packet, then call parse on the rest
|
|
|
|
# 2) Call .new() with the full packet contents
|
|
|
|
def initialize (data)
|
|
|
|
|
2005-07-17 23:28:28 +00:00
|
|
|
self.ack_result = []
|
|
|
|
self.ack_reason = []
|
|
|
|
self.ack_xfer_syntax_uuid = []
|
|
|
|
self.ack_xfer_syntax_vers = []
|
2005-09-23 03:43:04 +00:00
|
|
|
|
|
|
|
if (data.length < 10)
|
|
|
|
raise ArgumentError, 'Packet header must be at least 10 bytes long'
|
|
|
|
end
|
|
|
|
|
|
|
|
if (data.length == 10)
|
|
|
|
self.frag_len = data[8,2].unpack('v')[0]
|
|
|
|
self.raw = data
|
|
|
|
end
|
|
|
|
|
|
|
|
if (data.length > 10)
|
|
|
|
self.raw = data
|
|
|
|
self.parse('')
|
|
|
|
end
|
|
|
|
|
2005-07-16 09:37:42 +00:00
|
|
|
end
|
|
|
|
|
2005-07-17 23:28:28 +00:00
|
|
|
# Parse the contents of a DCERPC response packet and fill out all the fields
|
2005-07-16 09:37:42 +00:00
|
|
|
def parse (body)
|
|
|
|
self.raw = self.raw + body
|
2005-07-17 23:28:28 +00:00
|
|
|
self.type = self.raw[2,1].unpack('C')[0]
|
2005-07-16 09:37:42 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
uuid = Rex::Proto::DCERPC::UUID
|
2005-07-16 09:37:42 +00:00
|
|
|
data = self.raw
|
2005-07-17 23:28:28 +00:00
|
|
|
|
2005-07-16 09:37:42 +00:00
|
|
|
# BIND_ACK == 12, ALTER_CONTEXT_RESP == 15
|
|
|
|
if (self.type == 12 or self.type == 15)
|
|
|
|
|
|
|
|
# Decode most of the DCERPC header
|
2005-07-17 08:24:30 +00:00
|
|
|
self.vers_major,
|
|
|
|
self.vers_minor,
|
|
|
|
trash,
|
|
|
|
self.flags,
|
|
|
|
self.data_rep,
|
|
|
|
self.frag_len,
|
|
|
|
self.auth_len,
|
|
|
|
self.call_id,
|
|
|
|
self.max_frag_xmit,
|
|
|
|
self.max_frag_recv,
|
|
|
|
self.assoc_group,
|
2005-07-17 08:28:52 +00:00
|
|
|
self.sec_addr_len = data.unpack('CCCCNvvVvvVv')
|
2005-07-16 09:37:42 +00:00
|
|
|
|
2005-07-17 23:28:28 +00:00
|
|
|
# Keep an offset into the packet handy
|
|
|
|
x = 0
|
|
|
|
|
2005-07-16 09:37:42 +00:00
|
|
|
# XXX This is still somewhat broken (4 digit ports)
|
|
|
|
self.sec_addr = data[26, self.sec_addr_len]
|
|
|
|
|
|
|
|
# Move the pointer into the packet forward
|
2005-09-23 03:43:04 +00:00
|
|
|
x += 26 + self.sec_addr_len
|
|
|
|
|
2005-07-17 23:28:28 +00:00
|
|
|
# Figure out how many results we have (multiple-context binds)
|
|
|
|
self.num_results = data[ x, 4 ].unpack('V')[0]
|
|
|
|
|
|
|
|
# Move the pointer to the ack_result[0] offset
|
|
|
|
x += 4
|
|
|
|
|
|
|
|
# Initialize the ack_result index
|
|
|
|
ack = 0
|
2005-07-16 09:37:42 +00:00
|
|
|
|
2005-07-17 23:28:28 +00:00
|
|
|
# Scan through all results and add them to the result arrays
|
|
|
|
while ack < self.num_results
|
|
|
|
self.ack_result[ack] = data[ x + 0, 2 ].unpack('v')[0]
|
|
|
|
self.ack_reason[ack] = data[ x + 2, 2 ].unpack('v')[0]
|
|
|
|
self.ack_xfer_syntax_uuid[ack] = uuid.uuid_unpack(data[ x + 4, 16 ])
|
|
|
|
self.ack_xfer_syntax_vers[ack] = data[ x + 20, 4 ].unpack('V')[0]
|
|
|
|
x += 24
|
|
|
|
ack += 1
|
2005-07-16 09:37:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# End of BIND_ACK || ALTER_CONTEXT_RESP
|
|
|
|
end
|
2005-07-17 23:28:28 +00:00
|
|
|
|
|
|
|
# BIND_NACK == 13
|
|
|
|
if (self.type == 13)
|
|
|
|
|
|
|
|
# Decode most of the DCERPC header
|
|
|
|
self.vers_major,
|
|
|
|
self.vers_minor,
|
|
|
|
trash,
|
|
|
|
self.flags,
|
|
|
|
self.data_rep,
|
|
|
|
self.frag_len,
|
|
|
|
self.auth_len,
|
|
|
|
self.call_id,
|
|
|
|
self.nack_reason = data.unpack('CCCCNvvVv')
|
|
|
|
end
|
2005-07-16 09:37:42 +00:00
|
|
|
|
|
|
|
# RESPONSE == 2
|
2005-07-17 08:28:52 +00:00
|
|
|
if (self.type == 2)
|
|
|
|
|
2005-07-16 09:37:42 +00:00
|
|
|
# Decode the DCERPC response header
|
2005-07-17 08:28:52 +00:00
|
|
|
self.vers_major,
|
|
|
|
self.vers_minor,
|
|
|
|
trash,
|
|
|
|
self.flags,
|
|
|
|
self.data_rep,
|
|
|
|
self.frag_len,
|
|
|
|
self.auth_len,
|
|
|
|
self.call_id,
|
|
|
|
self.alloc_hint,
|
|
|
|
self.context_id,
|
|
|
|
self.cancel_cnt = data.unpack('CCCCNvvVVvC')
|
2005-07-16 09:37:42 +00:00
|
|
|
|
|
|
|
# Put the application data into self.stub_data
|
|
|
|
self.stub_data = data[data.length - self.alloc_hint, 0xffff]
|
2005-07-17 23:28:28 +00:00
|
|
|
|
2005-07-16 09:37:42 +00:00
|
|
|
# End of RESPONSE
|
|
|
|
end
|
|
|
|
|
|
|
|
# FAULT == 2
|
2005-07-17 08:28:52 +00:00
|
|
|
if (self.type == 3)
|
|
|
|
|
2005-07-16 09:37:42 +00:00
|
|
|
# Decode the DCERPC response header
|
2005-07-17 08:28:52 +00:00
|
|
|
self.vers_major,
|
|
|
|
self.vers_minor,
|
|
|
|
trash,
|
|
|
|
self.flags,
|
|
|
|
self.data_rep,
|
|
|
|
self.frag_len,
|
|
|
|
self.auth_len,
|
|
|
|
self.call_id,
|
|
|
|
self.alloc_hint,
|
|
|
|
self.context_id,
|
|
|
|
self.cancel_cnt,
|
|
|
|
trash,
|
|
|
|
self.status = data.unpack('CCCCNvvVVvCCV')
|
2005-07-16 09:37:42 +00:00
|
|
|
|
|
|
|
# Put the application data into self.stub_data
|
2005-07-17 23:28:28 +00:00
|
|
|
self.stub_data = data[data.length - self.alloc_hint, 0xffff]
|
2005-07-16 09:37:42 +00:00
|
|
|
# End of FAULT
|
2005-07-17 08:24:30 +00:00
|
|
|
end
|
2005-07-16 09:37:42 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
attr_accessor :raw
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-07-25 09:40:31 +00:00
|
|
|
end
|