Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/dcerpc/response.rb
Views: 11704
# -*- coding: binary -*-12module Rex3module Proto4module DCERPC5class Response67attr_accessor :frag_len, :auth_len, :type, :vers_major, :vers_minor8attr_accessor :flags, :data_rep, :call_id, :max_frag_xmit, :max_frag_recv9attr_accessor :assoc_group, :sec_addr_len, :sec_addr, :num_results10attr_accessor :nack_reason, :xfer_syntax_uuid, :xfer_syntax_vers11attr_accessor :ack_reason, :ack_result, :ack_xfer_syntax_uuid, :ack_xfer_syntax_vers12attr_accessor :alloc_hint, :context_id, :cancel_cnt, :status, :stub_data13attr_accessor :raw1415FLAG_FIRST_FRAG = 1 << 016FLAG_LAST_FRAG = 1 << 11718# Create a new DCERPC::Response object19# This can be initialized in two ways:20# 1) Call .new() with the first 10 bytes of packet, then call parse on the rest21# 2) Call .new() with the full packet contents22def initialize(data)2324self.ack_result = []25self.ack_reason = []26self.ack_xfer_syntax_uuid = []27self.ack_xfer_syntax_vers = []2829if (! data or data.length < 10)30raise Rex::Proto::DCERPC::Exceptions::InvalidPacket, 'DCERPC response packet is incomplete'31end3233if (data.length == 10)34self.frag_len = data[8,2].unpack('v')[0]35self.raw = data36end3738if (data.length > 10)39self.raw = data40self.parse41end42end4344# Parse the contents of a DCERPC response packet and fill out all the fields45def parse(body = '')46self.raw = self.raw + body47self.type = self.raw[2,1].unpack('C')[0]4849uuid = Rex::Proto::DCERPC::UUID50data = self.raw5152if(not data)53raise Rex::Proto::DCERPC::Exceptions::InvalidPacket, 'DCERPC response packet is incomplete'54end5556# BIND_ACK == 12, ALTER_CONTEXT_RESP == 1557if (self.type == 12 or self.type == 15)5859# Decode most of the DCERPC header60self.vers_major,61self.vers_minor,62trash,63self.flags,64self.data_rep,65self.frag_len,66self.auth_len,67self.call_id,68self.max_frag_xmit,69self.max_frag_recv,70self.assoc_group,71self.sec_addr_len = data.unpack('CCCCNvvVvvVv')727374if(not self.frag_len or data.length < self.frag_len)75raise Rex::Proto::DCERPC::Exceptions::InvalidPacket, 'DCERPC response packet is incomplete'76end7778# Keep an offset into the packet handy79x = 08081# XXX This is still somewhat broken (4 digit ports)82self.sec_addr = data[26, self.sec_addr_len]8384# Move the pointer into the packet forward85x += 26 + self.sec_addr_len8687# Align the pointer on a dword boundary88while (x % 4 != 0)89x += 190end9192# Figure out how many results we have (multiple-context binds)93self.num_results = data[ x, 4 ].unpack('V')[0]9495# Move the pointer to the ack_result[0] offset96x += 49798# Initialize the ack_result index99ack = 0100101# Scan through all results and add them to the result arrays102while ack < self.num_results103self.ack_result[ack] = data[ x + 0, 2 ].unpack('v')[0]104self.ack_reason[ack] = data[ x + 2, 2 ].unpack('v')[0]105self.ack_xfer_syntax_uuid[ack] = uuid.uuid_unpack(data[ x + 4, 16 ])106self.ack_xfer_syntax_vers[ack] = data[ x + 20, 4 ].unpack('V')[0]107x += 24108ack += 1109end110111# End of BIND_ACK || ALTER_CONTEXT_RESP112end113114# BIND_NACK == 13115if (self.type == 13)116117# Decode most of the DCERPC header118self.vers_major,119self.vers_minor,120trash,121self.flags,122self.data_rep,123self.frag_len,124self.auth_len,125self.call_id,126self.nack_reason = data.unpack('CCCCNvvVv')127end128129# RESPONSE == 2130if (self.type == 2)131132# Decode the DCERPC response header133self.vers_major,134self.vers_minor,135trash,136self.flags,137self.data_rep,138self.frag_len,139self.auth_len,140self.call_id,141self.alloc_hint,142self.context_id,143self.cancel_cnt = data.unpack('CCCCNvvVVvC')144145stub_offset = 24146# Error out if the whole header was not read147if !(self.alloc_hint and self.context_id and self.cancel_cnt)148raise Rex::Proto::DCERPC::Exceptions::InvalidPacket, 'DCERPC response packet is incomplete'149end150151# Put the application data into self.stub_data152self.stub_data = data[stub_offset..self.frag_len - self.auth_len]153# End of RESPONSE154end155156# FAULT == 3157if (self.type == 3)158159# Decode the DCERPC response header160self.vers_major,161self.vers_minor,162trash,163self.flags,164self.data_rep,165self.frag_len,166self.auth_len,167self.call_id,168self.alloc_hint,169self.context_id,170self.cancel_cnt,171trash,172self.status = data.unpack('CCCCNvvVVvCCV')173174# Put the application data into self.stub_data175self.stub_data = data[data.length - self.alloc_hint, 0xffff]176# End of FAULT177end178179end180181protected182# attr_accessor :raw183184end185end186end187end188189190191