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/packet.rb
Views: 11704
# -*- coding: binary -*-1module Rex2module Proto3module DCERPC4class Packet56require 'rex/text'78UUID = Rex::Proto::DCERPC::UUID910# Create a standard DCERPC BIND request packet11def self.make_bind(uuid, vers, xfer_syntax_uuid=UUID.xfer_syntax_uuid, xfer_syntax_vers=UUID.xfer_syntax_vers)1213# Process the version strings ("1.0", 1.0, "1", 1)14bind_vers_maj, bind_vers_min = UUID.vers_to_nums(vers)15xfer_vers_maj, xfer_vers_min = UUID.vers_to_nums(xfer_syntax_vers)1617if UUID.is? xfer_syntax_uuid18xfer_syntax_uuid = UUID.uuid_pack(xfer_syntax_uuid)19end2021# Create the bind request packet22buff =23[245, # major version 5250, # minor version 02611, # bind type273, # flags280x10000000, # data representation2972, # frag length300, # auth length310, # call id325840, # max xmit frag335840, # max recv frag340, # assoc group351, # num ctx items360, # context id371, # num trans items38UUID.uuid_pack(uuid), # interface uuid39bind_vers_maj, # interface major version40bind_vers_min, # interface minor version41xfer_syntax_uuid, # transfer syntax42xfer_vers_maj, # syntax major version43xfer_vers_min, # syntax minor version44].pack('CCCCNvvVvvVVvvA16vvA16vv')4546return buff, 047end4849# Create an obfuscated DCERPC BIND request packet50def self.make_bind_fake_multi(uuid, vers, bind_head=0, bind_tail=0)5152bind_head = bind_head.to_i53bind_tail = bind_tail.to_i54bind_head = rand(6)+10 if bind_head == 055bind_tail = rand(4)+1 if bind_head == 05657u = Rex::Proto::DCERPC::UUID5859# Process the version strings ("1.0", 1.0, "1", 1)60bind_vers_maj, bind_vers_min = UUID.vers_to_nums(vers)61xfer_vers_maj, xfer_vers_min = UUID.vers_to_nums(UUID.xfer_syntax_vers)6263bind_total = bind_head + bind_tail + 164bind_size = (bind_total * 44) + 2865real_ctx, ctx = 0, 06667# Create the header of the bind request68data =69[705, # major version 5710, # minor version 07211, # bind type733, # flags740x10000000, # data representation75bind_size, # frag length760, # auth length770, # call id785840, # max xmit frag795840, # max recv frag800, # assoc group81bind_total, # num ctx items82].pack('CCCCNvvVvvVV')8384# Generate the fake UUIDs prior to the real one851.upto(bind_head) do ||86# Generate some random UUID and versions87rand_uuid = Rex::Text.rand_text(16)88rand_imaj = rand(6)89rand_imin = rand(4)9091data +=92[93ctx, # context id941, # num trans items95rand_uuid, # interface uuid96rand_imaj, # interface major version97rand_imin, # interface minor version98UUID.xfer_syntax_uuid, # transfer syntax99xfer_vers_maj, # syntax major version100xfer_vers_min, # syntax minor version101].pack('vvA16vvA16vv')102ctx += 1103end104105# Stuff the real UUID onto the end of the buffer106real_ctx = ctx;107data +=108[109ctx, # context id1101, # num trans items111UUID.uuid_pack(uuid), # interface uuid112bind_vers_maj, # interface major version113bind_vers_min, # interface minor version114UUID.xfer_syntax_uuid, # transfer syntax115xfer_vers_maj, # syntax major version116xfer_vers_min, # syntax minor version117].pack('vvA16vvA16vv')118ctx += 1119120121# Generate the fake UUIDs after the real one1221.upto(bind_tail) do ||123# Generate some random UUID and versions124rand_uuid = Rex::Text.rand_text(16)125rand_imaj = rand(6)126rand_imin = rand(4)127128data +=129[130ctx, # context id1311, # num trans items132rand_uuid, # interface uuid133rand_imaj, # interface major version134rand_imin, # interface minor version135UUID.xfer_syntax_uuid, # transfer syntax136xfer_vers_maj, # syntax major version137xfer_vers_min, # syntax minor version138].pack('vvA16vvA16vv')139ctx += 1140end141142# Return both the bind packet and the real context_id143return data, real_ctx144end145146# Create a standard DCERPC ALTER_CONTEXT request packet147def self.make_alter_context(uuid, vers)148u = Rex::Proto::DCERPC::UUID149150# Process the version strings ("1.0", 1.0, "1", 1)151bind_vers_maj, bind_vers_min = UUID.vers_to_nums(vers)152xfer_vers_maj, xfer_vers_min = UUID.vers_to_nums(UUID.xfer_syntax_vers)153154buff =155[1565, # major version 51570, # minor version 015814, # alter context1593, # flags1600x10000000, # data representation16172, # frag length1620, # auth length1630, # call id1645840, # max xmit frag1655840, # max recv frag1660, # assoc group1671, # num ctx items1680, # context id1691, # num trans items170UUID.uuid_pack(uuid), # interface uuid171bind_vers_maj, # interface major version172bind_vers_min, # interface minor version173UUID.xfer_syntax_uuid, # transfer syntax174xfer_vers_maj, # syntax major version175xfer_vers_min, # syntax minor version176].pack('CCCCNvvVvvVVvvA16vvA16vv')177end178179180# Used to create a piece of a DCERPC REQUEST packet181def self.make_request_chunk(flags=3, opnum=0, data="", ctx=0, object_id = '')182183flags = flags.to_i184opnum = opnum.to_i185ctx = ctx.to_i186187dlen = data.length188flen = dlen + 24189190use_object = 0191192object_str = ''193194if object_id.size > 0195flags |= 0x80196flen = flen + 16197object_str = UUID.uuid_pack(object_id)198end199200buff =201[2025, # major version 52030, # minor version 02040, # request type205flags, # flags2060x10000000, # data representation207flen, # frag length2080, # auth length2090, # call id210dlen, # alloc hint211ctx, # context id212opnum, # operation number213].pack('CCCCNvvVVvv') + object_str + data214end215216# Used to create standard DCERPC REQUEST packet(s)217def self.make_request(opnum=0, data="", size=data.length, ctx=0, object_id = '')218219opnum = opnum.to_i220size = [4000, size.to_i].min221ctx = ctx.to_i222223chunks, frags = [], []224ptr = 0225226# Break the request into fragments of 'size' bytes227while ptr < data.length228chunks.push( data[ ptr, size ] )229ptr += size230end231232# Process requests with no stub data233if chunks.length == 0234frags.push( make_request_chunk(3, opnum, '', ctx, object_id) )235return frags236end237238# Process requests with only one fragment239if chunks.length == 1240frags.push( make_request_chunk(3, opnum, chunks[0], ctx, object_id) )241return frags242end243244# Create the first fragment of the request245frags.push( make_request_chunk(1, opnum, chunks.shift, ctx, object_id) )246247# Create all of the middle fragments248while chunks.length != 1249frags.push( make_request_chunk(0, opnum, chunks.shift, ctx, object_id) )250end251252# Create the last fragment of the request253frags.push( make_request_chunk(2, opnum, chunks.shift, ctx, object_id) )254255return frags256end257258end259end260end261end262263264