Class: Rex::OLE::Directory

Inherits:
DirEntry show all
Defined in:
lib/rex/ole/directory.rb

Overview

This class serves as the root directory entry in addition to an abstraction around the concept of a directory as a whole.

Instance Attribute Summary collapse

Attributes inherited from DirEntry

#_sidChild, #_sidLeftSib, #_sidRightSib, #sid

Instance Method Summary collapse

Methods inherited from DirEntry

#<<, #each, #find_by_sid, #find_stream_by_name_and_type, #length, #name, #name=, #pack, #start_sector, #start_sector=, #to_s, #type, #type=

Constructor Details

#initialize(stg) ⇒ Directory

Returns a new instance of Directory.



22
23
24
25
26
# File 'lib/rex/ole/directory.rb', line 22

def initialize(stg)
  super

  @num_entries = 1
end

Instance Attribute Details

#num_entriesObject

XXX: num_entries is not maintained once a stream/storage is added!



20
21
22
# File 'lib/rex/ole/directory.rb', line 20

def num_entries
  @num_entries
end

Instance Method Details

#each_entry(&block) ⇒ Object



36
37
38
# File 'lib/rex/ole/directory.rb', line 36

def each_entry(&block)
  yield_entries(self, &block)
end

#flatten_tree(entries, parent) ⇒ Object

NOTE: this may not be necessary if we were to use each_entry



169
170
171
172
173
174
# File 'lib/rex/ole/directory.rb', line 169

def flatten_tree(entries, parent)
  entries << parent
  parent.each { |el|
    flatten_tree(entries, el)
  }
end

#from_s(sid, buf) ⇒ Object

low-level functions



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

def from_s(sid, buf)
  super

  if (@_sidRightSib != DIR_NOSTREAM)
    raise RuntimeError, 'Root Entry is invalid! (has right sibling)'
  end
  if (@_sidLeftSib != DIR_NOSTREAM)
    raise RuntimeError, 'Root Entry is invalid! (has left sibling)'
  end
end


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
# File 'lib/rex/ole/directory.rb', line 46

def link_item(parent, child)
  # set sid, advance count
  child.sid = @num_entries
  @num_entries += 1

  # link item to siblings and/or parent
  if (parent._sidChild == DIR_NOSTREAM)
    parent._sidChild = child.sid
    dlog("Linking #{child.name} as THE child of #{parent.name} as sid #{child.sid}", 'rex', LEV_3)
  else
    sib = nil
    parent.each { |el|
      if (el._sidLeftSib == DIR_NOSTREAM)
        sib = el
        el._sidLeftSib = child.sid
        dlog("Linking #{child.name} as the LEFT sibling of #{sib.name} as sid #{child.sid}", 'rex', LEV_3)
        break
      end
      if (el._sidRightSib == DIR_NOSTREAM)
        sib = el
        el._sidRightSib = child.sid
        dlog("Linking #{child.name} as the RIGHT sibling of #{sib.name} as sid #{child.sid}", 'rex', LEV_3)
        break
      end
    }
    if (not sib)
      raise RuntimeError, 'Unable to find a sibling to link to in the directory'
    end
  end
  parent << child
end

#populate_children(entries, parent, sid) ⇒ Object

recursively add entries to their proper parents :)



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rex/ole/directory.rb', line 153

def populate_children(entries, parent, sid)
  node = entries[sid]
  dlog("populate_children(entries, \"#{parent.name}\", #{sid}) - node: #{node.name}", 'rex', LEV_3)
  parent << node
  if (node.type == STGTY_STORAGE) and (node._sidChild != DIR_NOSTREAM)
    populate_children(entries, node, node._sidChild)
  end
  if (node._sidLeftSib != DIR_NOSTREAM)
    populate_children(entries, parent, node._sidLeftSib)
  end
  if (node._sidRightSib != DIR_NOSTREAM)
    populate_children(entries, parent, node._sidRightSib)
  end
end

#readObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
# File 'lib/rex/ole/directory.rb', line 93

def read
  @children = []
  visited = []
  entries = []
  root_node = nil
  sect = @stg.header._sectDirStart
  while (sect != SECT_END)

    if (visited.include?(sect))
      raise RuntimeError, 'Sector chain loop detected (0x%08x)' % sect
    end
    visited << sect

    sbuf = @stg.read_sector(sect, @stg.header.sector_size)
    while (sbuf.length >= DIRENTRY_SZ)
      debuf = sbuf.slice!(0, DIRENTRY_SZ)

      type = Util.get8(debuf, 0x42)
      case type
      when STGTY_ROOT
        if (entries.length != 0)
          raise RuntimeError, 'Root Entry found, but not first encountered!'
        end
        if (root_node)
          raise RuntimeError, 'Multiple root directory sectors detected (0x%08x)' % sect
        end
        de = self
        root_node = de

      when STGTY_STORAGE
        de = SubStorage.new @stg

      when STGTY_STREAM
        de = Stream.new @stg

      when STGTY_INVALID
        # skip invalid entries
        next

      else
        raise RuntimeError, 'Unsupported directory entry type (0x%02x)' % type
      end

      # read content
      de.from_s(entries.length, debuf)
      entries << de
    end
    sect = @stg.next_sector(sect)
  end

  @num_entries = entries.length

  # sort out the tree structure, starting with the root
  if (@_sidChild != DIR_NOSTREAM)
    populate_children(entries, root_node, @_sidChild)
  end
end

#set_ministream_params(start, size) ⇒ Object



41
42
43
44
# File 'lib/rex/ole/directory.rb', line 41

def set_ministream_params(start, size)
  @_sectStart = start
  @_ulSize = size
end

#writeObject



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
# File 'lib/rex/ole/directory.rb', line 177

def write
  # flatten the directory again
  entries = []
  flatten_tree(entries, self)
  dlog("flattened tree has #{entries.length} entries...", 'rex', LEV_3)

  # count directory sectors
  ds_count = entries.length / 4
  if ((entries.length % 4) > 0)
    # one more sector to hold the rest
    ds_count += 1
  end

  # put the root entry first
  sbuf = self.pack

  # add the rest
  prev_sect = nil
  dir_start = nil
  entries.each { |de|
    # we already got the root entry, no more!
    next if (de.type == STGTY_ROOT)

    dir = de.pack
    dlog("writing dir entry #{de.name}", 'rex', LEV_3)
    sbuf << dir

    if (sbuf.length == @stg.header.sector_size)
      # we have a full sector, add it!
      sect = @stg.write_sector(sbuf, nil, prev_sect)
      prev_sect = sect
      dir_start ||= sect
      # reset..
      sbuf = ""
    end
  }

  # still a partial sector left?
  if (sbuf.length > 0)
    # add it! (NOTE: it will get padded with nul bytes if its not sector sized)
    sect = @stg.write_sector(sbuf, nil, prev_sect)
    prev_sect = sect
    dir_start ||= sect
  end

  @stg.header._sectDirStart = dir_start
end

#yield_entries(de, &block) ⇒ Object

woop, recursive each



30
31
32
33
34
35
# File 'lib/rex/ole/directory.rb', line 30

def yield_entries(de, &block)
  block.call(de)
  de.each { |el|
    yield_entries(el, &block)
  }
end