Class: RubySMB::Dcerpc::Srvsvc::NetShareEnumAll

Inherits:
BinData::Record
  • Object
show all
Defined in:
lib/ruby_smb/dcerpc/srvsvc/net_share_enum_all.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_response(response) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_smb/dcerpc/srvsvc/net_share_enum_all.rb', line 35

def self.parse_response(response)

  shares = []

  res = response.dup
  win_error = res.slice!(-4, 4).unpack("V")[0]

  if win_error != 0
    raise RuntimeError, "Invalid DCERPC response: win_error = #{win_error}"
  end

  # Remove unused data
  res.slice!(0, 12) # level, CTR header, Reference ID of CTR
  share_count = res.slice!(0, 4).unpack("V")[0]
  res.slice!(0, 4) # Reference ID of CTR1
  share_max_count = res.slice!(0, 4).unpack("V")[0]

  if share_max_count != share_count
    raise RuntimeError, "Invalid DCERPC response: count != count max (#{share_count}/#{share_max_count})"
  end

  # ReferenceID / Type / ReferenceID of Comment
  types = res.slice!(0, share_count * 12).scan(/.{12}/n).map { |a| a[4, 2].unpack("v")[0] }

  share_count.times do |t|
    length, offset, max_length = res.slice!(0, 12).unpack("VVV")
    if offset != 0
      raise RuntimeError, "Invalid DCERPC response: offset != 0 (#{offset})"
    end

    if length != max_length
      raise RuntimeError, "Invalid DCERPC response: length !=max_length (#{length}/#{max_length})"
    end
    name = res.slice!(0, 2 * length).gsub('\x00', '')
    res.slice!(0, 2) if length % 2 == 1 # pad

    comment_length, comment_offset, comment_max_length = res.slice!(0, 12).unpack("VVV")

    if comment_offset != 0
      raise RuntimeError, "Invalid DCERPC response: comment_offset != 0 (#{comment_offset})"
    end

    if comment_length != comment_max_length
      raise RuntimeError, "Invalid DCERPC response: comment_length != comment_max_length (#{comment_length}/#{comment_max_length})"
    end

    comment = res.slice!(0, 2 * comment_length)

    res.slice!(0, 2) if comment_length % 2 == 1 # pad

    name = name.gsub("\x00", "")
    s_type = ['DISK', 'PRINTER', 'DEVICE', 'IPC', 'SPECIAL', 'TEMPORARY'][types[t]].gsub("\x00", "")
    comment = comment.gsub("\x00", "")

    shares << [name, s_type, comment]
  end

  shares
end

Instance Method Details

#pad_lengthObject



30
31
32
33
# File 'lib/ruby_smb/dcerpc/srvsvc/net_share_enum_all.rb', line 30

def pad_length
  offset = (server_unc.abs_offset + server_unc.to_binary_s.length) % 4
  (4 - offset) % 4
end