Class: Rex::OLE::FAT

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

Instance Method Summary collapse

Methods inherited from DIFAT

#+, #<<, #[], #[]=, #each, #initialize, #length, #reset, #slice!, #to_s

Constructor Details

This class inherits a constructor from Rex::OLE::DIFAT

Instance Method Details

#allocate_sector(type = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rex/ole/fat.rb', line 45

def allocate_sector(type=nil)
	idx = @entries.index(SECT_FREE)
	if (not idx)
		# add a sector worth
		idx = @entries.length
		@stg.header.idx_per_sect.times {
			@entries << SECT_FREE
		}
	end

	# mark the sector as in use
	if (type)
		@entries[idx] = type
	else
		# default normal sectors to end of chain
		@entries[idx] = SECT_END
	end
	idx
end

#read(difat) ⇒ Object

low-level functions



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

def read(difat)
	@entries = []
	cnt = left = @stg.header._csectFat
	difat.each { |fs|
		break if (left == 0)

		if (fs != SECT_FREE)
			buf = @stg.read_sector(fs, @stg.header.sector_size)
			arr = Util.get32array(buf)

			# hax!
			if (@entries[fs] == SECT_DIF)
				# chop the next ptr
				@entries += arr.slice!(0, arr.length - 1)
			else
				@entries += arr
			end
			left -= 1
		end
	}

	if (left != 0)
		raise RuntimeError, 'Only found %u of %u sectors' % [(cnt - left), cnt]
	end
end

#write(difat) ⇒ Object



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
94
# File 'lib/rex/ole/fat.rb', line 65

def write(difat)
	# we build the difat as we write these..
	difat.reset

	# allocate the sectors
	fat_sects = []
	left = @entries.length
	while (left > 0)
		if (left > @stg.header.idx_per_sect)
			left -= @stg.header.idx_per_sect
		else
			left = 0
		end
		fat_sects << allocate_sector(SECT_FAT)
	end

	# write the fat into the difat/allocated sectors
	copy = @entries.dup
	fat_sects.each { |fs|
		part = copy.slice!(0, @stg.header.idx_per_sect)
		sbuf = Util.pack32array(part)

		if (sbuf.length != @stg.header.sector_size)
			raise RuntimeError, 'Unsupported number of fat sectors (not multiple of idx per sect)'
		end

		@stg.write_sector_raw(fs, sbuf)
		difat << fs
	}
end