2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-09-16 03:29:27 +00:00
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module DCERPC
|
|
|
|
class Packet
|
|
|
|
|
|
|
|
require 'rex/proto/dcerpc/uuid'
|
|
|
|
require 'rex/proto/dcerpc/response'
|
|
|
|
require 'rex/text'
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID = Rex::Proto::DCERPC::UUID
|
2005-09-16 03:29:27 +00:00
|
|
|
|
|
|
|
# Create a standard DCERPC BIND request packet
|
2012-05-24 23:10:26 +00:00
|
|
|
def self.make_bind(uuid, vers)
|
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Process the version strings ("1.0", 1.0, "1", 1)
|
2006-03-30 15:13:14 +00:00
|
|
|
bind_vers_maj, bind_vers_min = UUID.vers_to_nums(vers)
|
|
|
|
xfer_vers_maj, xfer_vers_min = UUID.vers_to_nums(UUID.xfer_syntax_vers)
|
2005-09-16 03:29:27 +00:00
|
|
|
|
|
|
|
# Create the bind request packet
|
2012-05-24 23:10:26 +00:00
|
|
|
buff =
|
2005-09-16 03:29:27 +00:00
|
|
|
[
|
|
|
|
5, # major version 5
|
|
|
|
0, # minor version 0
|
|
|
|
11, # bind type
|
|
|
|
3, # flags
|
|
|
|
0x10000000, # data representation
|
|
|
|
72, # frag length
|
|
|
|
0, # auth length
|
|
|
|
0, # call id
|
|
|
|
5840, # max xmit frag
|
|
|
|
5840, # max recv frag
|
|
|
|
0, # assoc group
|
|
|
|
1, # num ctx items
|
|
|
|
0, # context id
|
|
|
|
1, # num trans items
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID.uuid_pack(uuid), # interface uuid
|
2005-09-16 03:29:27 +00:00
|
|
|
bind_vers_maj, # interface major version
|
|
|
|
bind_vers_min, # interface minor version
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID.xfer_syntax_uuid, # transfer syntax
|
2005-09-16 03:29:27 +00:00
|
|
|
xfer_vers_maj, # syntax major version
|
2012-05-24 23:10:26 +00:00
|
|
|
xfer_vers_min, # syntax minor version
|
2005-09-16 03:29:27 +00:00
|
|
|
].pack('CCCCNvvVvvVVvvA16vvA16vv')
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
return buff, 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create an obfuscated DCERPC BIND request packet
|
2006-05-03 05:53:37 +00:00
|
|
|
def self.make_bind_fake_multi(uuid, vers, bind_head=0, bind_tail=0)
|
|
|
|
|
2012-03-06 15:28:29 +00:00
|
|
|
bind_head = bind_head.to_i
|
|
|
|
bind_tail = bind_tail.to_i
|
2006-05-03 05:53:37 +00:00
|
|
|
bind_head = rand(6)+10 if bind_head == 0
|
|
|
|
bind_tail = rand(4)+1 if bind_head == 0
|
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
u = Rex::Proto::DCERPC::UUID
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Process the version strings ("1.0", 1.0, "1", 1)
|
2006-03-30 15:13:14 +00:00
|
|
|
bind_vers_maj, bind_vers_min = UUID.vers_to_nums(vers)
|
|
|
|
xfer_vers_maj, xfer_vers_min = UUID.vers_to_nums(UUID.xfer_syntax_vers)
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
bind_total = bind_head + bind_tail + 1
|
|
|
|
bind_size = (bind_total * 44) + 28
|
|
|
|
real_ctx, ctx = 0, 0
|
|
|
|
|
|
|
|
# Create the header of the bind request
|
2012-05-24 23:10:26 +00:00
|
|
|
data =
|
2005-09-16 03:29:27 +00:00
|
|
|
[
|
|
|
|
5, # major version 5
|
|
|
|
0, # minor version 0
|
|
|
|
11, # bind type
|
|
|
|
3, # flags
|
|
|
|
0x10000000, # data representation
|
|
|
|
bind_size, # frag length
|
|
|
|
0, # auth length
|
|
|
|
0, # call id
|
|
|
|
5840, # max xmit frag
|
|
|
|
5840, # max recv frag
|
|
|
|
0, # assoc group
|
|
|
|
bind_total, # num ctx items
|
|
|
|
].pack('CCCCNvvVvvVV')
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Generate the fake UUIDs prior to the real one
|
|
|
|
1.upto(bind_head) do ||
|
|
|
|
# Generate some random UUID and versions
|
|
|
|
rand_uuid = Rex::Text.rand_text(16)
|
2012-05-24 23:10:26 +00:00
|
|
|
rand_imaj = rand(6)
|
2005-09-16 03:29:27 +00:00
|
|
|
rand_imin = rand(4)
|
2012-05-24 23:10:26 +00:00
|
|
|
|
|
|
|
data +=
|
2005-09-16 03:29:27 +00:00
|
|
|
[
|
|
|
|
ctx, # context id
|
2012-05-24 23:10:26 +00:00
|
|
|
1, # num trans items
|
2005-09-16 03:29:27 +00:00
|
|
|
rand_uuid, # interface uuid
|
|
|
|
rand_imaj, # interface major version
|
|
|
|
rand_imin, # interface minor version
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID.xfer_syntax_uuid, # transfer syntax
|
2005-09-16 03:29:27 +00:00
|
|
|
xfer_vers_maj, # syntax major version
|
2012-05-24 23:10:26 +00:00
|
|
|
xfer_vers_min, # syntax minor version
|
2005-09-16 03:29:27 +00:00
|
|
|
].pack('vvA16vvA16vv')
|
|
|
|
ctx += 1
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Stuff the real UUID onto the end of the buffer
|
|
|
|
real_ctx = ctx;
|
2012-05-24 23:10:26 +00:00
|
|
|
data +=
|
2005-09-16 03:29:27 +00:00
|
|
|
[
|
|
|
|
ctx, # context id
|
2012-05-24 23:10:26 +00:00
|
|
|
1, # num trans items
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID.uuid_pack(uuid), # interface uuid
|
2005-09-16 03:29:27 +00:00
|
|
|
bind_vers_maj, # interface major version
|
|
|
|
bind_vers_min, # interface minor version
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID.xfer_syntax_uuid, # transfer syntax
|
2005-09-16 03:29:27 +00:00
|
|
|
xfer_vers_maj, # syntax major version
|
2012-05-24 23:10:26 +00:00
|
|
|
xfer_vers_min, # syntax minor version
|
2005-09-16 03:29:27 +00:00
|
|
|
].pack('vvA16vvA16vv')
|
|
|
|
ctx += 1
|
2012-05-24 23:10:26 +00:00
|
|
|
|
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Generate the fake UUIDs after the real one
|
|
|
|
1.upto(bind_tail) do ||
|
|
|
|
# Generate some random UUID and versions
|
|
|
|
rand_uuid = Rex::Text.rand_text(16)
|
2012-05-24 23:10:26 +00:00
|
|
|
rand_imaj = rand(6)
|
2005-09-16 03:29:27 +00:00
|
|
|
rand_imin = rand(4)
|
2012-05-24 23:10:26 +00:00
|
|
|
|
|
|
|
data +=
|
2005-09-16 03:29:27 +00:00
|
|
|
[
|
|
|
|
ctx, # context id
|
2012-05-24 23:10:26 +00:00
|
|
|
1, # num trans items
|
2005-09-16 03:29:27 +00:00
|
|
|
rand_uuid, # interface uuid
|
|
|
|
rand_imaj, # interface major version
|
|
|
|
rand_imin, # interface minor version
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID.xfer_syntax_uuid, # transfer syntax
|
2005-09-16 03:29:27 +00:00
|
|
|
xfer_vers_maj, # syntax major version
|
2012-05-24 23:10:26 +00:00
|
|
|
xfer_vers_min, # syntax minor version
|
2005-09-16 03:29:27 +00:00
|
|
|
].pack('vvA16vvA16vv')
|
|
|
|
ctx += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return both the bind packet and the real context_id
|
|
|
|
return data, real_ctx
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Create a standard DCERPC ALTER_CONTEXT request packet
|
2012-05-24 23:10:26 +00:00
|
|
|
def self.make_alter_context(uuid, vers)
|
2005-09-16 03:29:27 +00:00
|
|
|
u = Rex::Proto::DCERPC::UUID
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Process the version strings ("1.0", 1.0, "1", 1)
|
2006-03-30 15:13:14 +00:00
|
|
|
bind_vers_maj, bind_vers_min = UUID.vers_to_nums(vers)
|
|
|
|
xfer_vers_maj, xfer_vers_min = UUID.vers_to_nums(UUID.xfer_syntax_vers)
|
2005-09-16 03:29:27 +00:00
|
|
|
|
2012-05-24 23:10:26 +00:00
|
|
|
buff =
|
2005-09-16 03:29:27 +00:00
|
|
|
[
|
|
|
|
5, # major version 5
|
|
|
|
0, # minor version 0
|
|
|
|
14, # alter context
|
|
|
|
3, # flags
|
|
|
|
0x10000000, # data representation
|
|
|
|
72, # frag length
|
|
|
|
0, # auth length
|
|
|
|
0, # call id
|
|
|
|
5840, # max xmit frag
|
|
|
|
5840, # max recv frag
|
|
|
|
0, # assoc group
|
|
|
|
1, # num ctx items
|
|
|
|
0, # context id
|
|
|
|
1, # num trans items
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID.uuid_pack(uuid), # interface uuid
|
2005-09-16 03:29:27 +00:00
|
|
|
bind_vers_maj, # interface major version
|
|
|
|
bind_vers_min, # interface minor version
|
2006-03-30 15:13:14 +00:00
|
|
|
UUID.xfer_syntax_uuid, # transfer syntax
|
2005-09-16 03:29:27 +00:00
|
|
|
xfer_vers_maj, # syntax major version
|
2012-05-24 23:10:26 +00:00
|
|
|
xfer_vers_min, # syntax minor version
|
2006-01-27 05:29:06 +00:00
|
|
|
].pack('CCCCNvvVvvVVvvA16vvA16vv')
|
2005-09-16 03:29:27 +00:00
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
|
|
|
|
# Used to create a piece of a DCERPC REQUEST packet
|
2006-05-15 16:44:44 +00:00
|
|
|
def self.make_request_chunk(flags=3, opnum=0, data="", ctx=0, object_id = '')
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2012-03-06 15:28:29 +00:00
|
|
|
flags = flags.to_i
|
|
|
|
opnum = opnum.to_i
|
|
|
|
ctx = ctx.to_i
|
2005-09-16 03:29:27 +00:00
|
|
|
|
|
|
|
dlen = data.length
|
|
|
|
flen = dlen + 24
|
2006-03-30 15:13:14 +00:00
|
|
|
|
|
|
|
use_object = 0
|
|
|
|
|
|
|
|
object_str = ''
|
|
|
|
|
|
|
|
if object_id.size > 0
|
|
|
|
flags |= 0x80
|
|
|
|
flen = flen + 16
|
|
|
|
object_str = UUID.uuid_pack(object_id)
|
|
|
|
end
|
|
|
|
|
2012-05-24 23:10:26 +00:00
|
|
|
buff =
|
2005-09-16 03:29:27 +00:00
|
|
|
[
|
|
|
|
5, # major version 5
|
|
|
|
0, # minor version 0
|
|
|
|
0, # request type
|
|
|
|
flags, # flags
|
|
|
|
0x10000000, # data representation
|
|
|
|
flen, # frag length
|
|
|
|
0, # auth length
|
|
|
|
0, # call id
|
|
|
|
dlen, # alloc hint
|
|
|
|
ctx, # context id
|
|
|
|
opnum, # operation number
|
2006-03-30 15:13:14 +00:00
|
|
|
].pack('CCCCNvvVVvv') + object_str + data
|
2012-05-24 23:10:26 +00:00
|
|
|
end
|
2005-09-16 03:29:27 +00:00
|
|
|
|
|
|
|
# Used to create standard DCERPC REQUEST packet(s)
|
2006-05-15 16:44:44 +00:00
|
|
|
def self.make_request(opnum=0, data="", size=data.length, ctx=0, object_id = '')
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2012-03-06 15:28:29 +00:00
|
|
|
opnum = opnum.to_i
|
|
|
|
size = [4000, size.to_i].min
|
|
|
|
ctx = ctx.to_i
|
2005-12-13 06:08:40 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
chunks, frags = [], []
|
|
|
|
ptr = 0
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Break the request into fragments of 'size' bytes
|
|
|
|
while ptr < data.length
|
|
|
|
chunks.push( data[ ptr, size ] )
|
|
|
|
ptr += size
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Process requests with no stub data
|
|
|
|
if chunks.length == 0
|
2006-03-30 15:13:14 +00:00
|
|
|
frags.push( make_request_chunk(3, opnum, '', ctx, object_id) )
|
2005-09-16 03:29:27 +00:00
|
|
|
return frags
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Process requests with only one fragment
|
|
|
|
if chunks.length == 1
|
2006-03-30 15:13:14 +00:00
|
|
|
frags.push( make_request_chunk(3, opnum, chunks[0], ctx, object_id) )
|
2005-09-16 03:29:27 +00:00
|
|
|
return frags
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create the first fragment of the request
|
2006-03-30 15:13:14 +00:00
|
|
|
frags.push( make_request_chunk(1, opnum, chunks.shift, ctx, object_id) )
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Create all of the middle fragments
|
|
|
|
while chunks.length != 1
|
2006-03-30 15:13:14 +00:00
|
|
|
frags.push( make_request_chunk(0, opnum, chunks.shift, ctx, object_id) )
|
2005-09-16 03:29:27 +00:00
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
# Create the last fragment of the request
|
2006-03-30 15:13:14 +00:00
|
|
|
frags.push( make_request_chunk(2, opnum, chunks.shift, ctx, object_id) )
|
2005-09-16 03:29:27 +00:00
|
|
|
|
|
|
|
return frags
|
2012-05-24 23:10:26 +00:00
|
|
|
end
|
|
|
|
|
2005-09-16 03:29:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-06 15:28:29 +00:00
|
|
|
end
|