Class: Rex::OLE::DirEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/ole/direntry.rb

Overview

This class serves as the base class for SubStorage, Stream, and Directory head

Direct Known Subclasses

Directory, Stream, SubStorage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stg) ⇒ DirEntry

Returns a new instance of DirEntry.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rex/ole/direntry.rb', line 19

def initialize(stg)
  @stg = stg

  # default to a root entry :)
  @sid = 0
  @_ab = "Root Entry"
  @_cb = nil              # NOTE: this is not used until pack
  @_mse = STGTY_ROOT
  @_bflags = 0
  @_sidLeftSib = SECT_FREE
  @_sidRightSib = SECT_FREE
  @_sidChild = SECT_FREE
  @_clsId = CLSID.new
  @_dwUserFlags = 0
  @_ctime = "\x00" * 8
  @_mtime = "\x00" * 8
  @_sectStart = SECT_END
  @_ulSize = 0

  # keep track of logical children (in a tree)
  @children = []
end

Instance Attribute Details

#_sidChildObject

Returns the value of attribute _sidChild.



17
18
19
# File 'lib/rex/ole/direntry.rb', line 17

def _sidChild
  @_sidChild
end

#_sidLeftSibObject

Returns the value of attribute _sidLeftSib.



17
18
19
# File 'lib/rex/ole/direntry.rb', line 17

def _sidLeftSib
  @_sidLeftSib
end

#_sidRightSibObject

Returns the value of attribute _sidRightSib.



17
18
19
# File 'lib/rex/ole/direntry.rb', line 17

def _sidRightSib
  @_sidRightSib
end

#sidObject

Returns the value of attribute sid.



16
17
18
# File 'lib/rex/ole/direntry.rb', line 16

def sid
  @sid
end

Instance Method Details

#<<(expr) ⇒ Object



47
48
49
# File 'lib/rex/ole/direntry.rb', line 47

def <<(expr)
  @children << expr
end

#eachObject



51
52
53
54
55
# File 'lib/rex/ole/direntry.rb', line 51

def each
  @children.each { |de|
    yield de
  }
end

#find_by_sid(sid, de = self) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rex/ole/direntry.rb', line 94

def find_by_sid(sid, de=self)
  if (de.sid == sid)
    return de
  end
  @children.each { |cde|
    ret = find_by_sid(sid, cde)
    if (ret)
      return ret
    end
  }
  nil
end

#find_stream_by_name_and_type(name, type) ⇒ Object

NOTE: this will not look at children



82
83
84
85
86
87
88
89
90
91
# File 'lib/rex/ole/direntry.rb', line 82

def find_stream_by_name_and_type(name, type)
  @children.each { |de|
    next if (de.type != type)

    if (de.name == name)
      return de
    end
  }
  nil
end

#from_s(sid, buf) ⇒ Object

low-level functions



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rex/ole/direntry.rb', line 111

def from_s(sid, buf)
  @sid = sid
  @_ab           = Util.getUnicodeString(buf[0x00,64])
  @_cb           = Util.get16(buf, 0x40)

  # too big?
  if (@_cb > 0x40)
    raise RuntimeError, 'Invalid directory entry name length %#x' % @_cb
  end

  # mismatch?
  if (@_ab.length > 0)
    declen = ((@_cb) / 2) - 1
    if (declen != @_ab.length)
      raise RuntimeError, 'Directory entry name and length mismatch (%d != %d)' % [declen, @_ab.length]
    end
  end

  @_mse          = Util.get8(buf, 0x42)
  @_bflags       = Util.get8(buf, 0x43)
  @_sidLeftSib   = Util.get32(buf, 0x44)
  @_sidRightSib  = Util.get32(buf, 0x48)
  @_sidChild     = Util.get32(buf, 0x4c)

  # only used for storages..
  @_clsId = CLSID.new(buf[0x50,16])
  @_dwUserFlags  = Util.get32(buf, 0x60)
  @_ctime        = buf[0x64,8]
  @_mtime        = buf[0x6c,8]

  # only used for streams...
  @_sectStart    = Util.get32(buf, 0x74)
  if (@stg.header._uMajorVersion == 4)
    @_ulSize       = Util.get64(buf, 0x78)
  else
    @_ulSize       = Util.get32(buf, 0x78)
  end

  # ignore _dptPropType and pad
end

#lengthObject



43
44
45
# File 'lib/rex/ole/direntry.rb', line 43

def length
  @_ulSize
end

#nameObject



65
66
67
# File 'lib/rex/ole/direntry.rb', line 65

def name
  @_ab
end

#name=(arg) ⇒ Object



68
69
70
71
# File 'lib/rex/ole/direntry.rb', line 68

def name=(arg)
  # XXX: validate?
  @_ab = arg
end

#packObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rex/ole/direntry.rb', line 153

def pack
  @_sectStart ||= SECT_END
  @_cb = (@_ab.length + 1) * 2

  data = ""
  data << Util.putUnicodeString(@_ab) # gets padded/truncated to 0x40 bytes
  data << Util.pack16(@_cb)
  data << Util.pack8(@_mse)
  data << Util.pack8(@_bflags)
  data << Util.pack32(@_sidLeftSib)
  data << Util.pack32(@_sidRightSib)
  data << Util.pack32(@_sidChild)
  data << @_clsId.pack
  data << Util.pack32(@_dwUserFlags)
  data << @_ctime
  data << @_mtime
  data << Util.pack32(@_sectStart)
  data << Util.pack64(@_ulSize)
  data
end

#start_sectorObject



73
74
75
# File 'lib/rex/ole/direntry.rb', line 73

def start_sector
  @_sectStart
end

#start_sector=(expr) ⇒ Object



76
77
78
# File 'lib/rex/ole/direntry.rb', line 76

def start_sector=(expr)
  @_sectStart = expr
end

#to_s(extra_spaces = 0) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/rex/ole/direntry.rb', line 175

def to_s(extra_spaces=0)
  @_sectStart ||= SECT_END
  @_cb = (@_ab.length + 1) * 2

  spstr = " " * extra_spaces

  ret = "%s{\n" % spstr
  ret << "%s  sid => 0x%x" % [spstr, @sid]
  ret << ",\n"
  ret << "%s  _ab => \"%s\"" % [spstr, Util.Printable(@_ab)]
  ret << ",\n"
  ret << "%s  _cb => 0x%04x" % [spstr, @_cb]
  ret << ",\n"
  ret << "%s  _mse => 0x%02x" % [spstr, @_mse]
  ret << ",\n"
  ret << "%s  _bflags => 0x%02x" % [spstr, @_bflags]
  ret << ",\n"
  ret << "%s  _sidLeftSib => 0x%08x" % [spstr, @_sidLeftSib]
  ret << ",\n"
  ret << "%s  _sidRightSib => 0x%08x" % [spstr, @_sidRightSib]
  ret << ",\n"
  ret << "%s  _sidChild => 0x%08x" % [spstr, @_sidChild]
  ret << ",\n"
  ret << "%s  _clsId => %s" % [spstr, @_clsId.to_s]
  ret << ",\n"
  ret << "%s  _dwUserFlags => 0x%08x" % [spstr, @_dwUserFlags]
  ret << ",\n"
  ret << "%s  _ctime => %s" % [spstr, Rex::Text.to_hex_dump(@_ctime).strip]
  ret << "\n"
  ret << "%s  _mtime => %s" % [spstr, Rex::Text.to_hex_dump(@_mtime).strip]
  ret << "\n"
  ret << "%s  _sectStart => 0x%08x" % [spstr, @_sectStart]
  ret << ",\n"
  ret << "%s  _ulSize => 0x%016x" % [spstr, @_ulSize]
  if (@_mse == STGTY_STREAM)
    ret << ",\n"
    ret << "%s  data =>\n" % spstr
    if (@data)
      #ret << Util.Printable(@data)
      ret << Rex::Text.to_hex_dump(@data).strip
    else
      if (@_ulSize > 0)
        ret << "--NOT OPENED YET--"
      end
    end
  elsif (@_mse == STGTY_STORAGE) or (@_mse == STGTY_ROOT)
    if (@children.length > 0)
      ret << ",\n"
      ret << "%s  *children* =>\n" % spstr
      @children.each { |de|
        ret << de.to_s(extra_spaces+2)
        ret << "\n"
      }
    end
  end
  ret << "\n"
  ret << "%s}" % spstr
end

#typeObject



58
59
60
# File 'lib/rex/ole/direntry.rb', line 58

def type
  @_mse
end

#type=(arg) ⇒ Object



61
62
63
# File 'lib/rex/ole/direntry.rb', line 61

def type=(arg)
  @_mse = arg
end