|
| 1 | +module Rex |
| 2 | + module Parser |
| 3 | + |
| 4 | + ### |
| 5 | + # |
| 6 | + # This class parses the contents of an NTFS partition file. |
| 7 | + # Author : Danil Bazin <danil.bazin[at]hsc.fr> @danilbaz |
| 8 | + # |
| 9 | + ### |
| 10 | + class NTFS |
| 11 | + # |
| 12 | + # Initialize the NTFS class with an already open file handler |
| 13 | + # |
| 14 | + def initialize(file_handler) |
| 15 | + @file_handler = file_handler |
| 16 | + data = @file_handler.read(4096) |
| 17 | + # Boot sector reading |
| 18 | + @bytes_per_sector = data[11, 2].unpack("S")[0] |
| 19 | + @sector_per_cluster = data[13].unpack("C")[0] |
| 20 | + @cluster_per_mft_record = data[64].unpack("c")[0] |
| 21 | + if @cluster_per_mft_record < 0 |
| 22 | + @bytes_per_mft_record = 2**(-@cluster_per_mft_record) |
| 23 | + @cluster_per_mft_record = @bytes_per_mft_record.to_f / @bytes_per_sector / @sector_per_cluster |
| 24 | + else |
| 25 | + @bytes_per_mft_record = @bytes_per_sector * @sector_per_cluster * @cluster_per_mft_record |
| 26 | + end |
| 27 | + @bytes_per_cluster = @sector_per_cluster * @bytes_per_sector |
| 28 | + @mft_logical_cluster_number = data[48, 8].unpack("Q")[0] |
| 29 | + @mft_offset = @mft_logical_cluster_number * @sector_per_cluster * @bytes_per_sector |
| 30 | + @file_handler.seek(@mft_offset) |
| 31 | + @mft = @file_handler.read(@bytes_per_mft_record) |
| 32 | + end |
| 33 | + |
| 34 | + # |
| 35 | + # Gather the MFT entry corresponding to his number |
| 36 | + # |
| 37 | + def mft_record_from_mft_num(mft_num) |
| 38 | + cluster_from_attribute_non_resident(mft_record_attribute(@mft)[128]["data"], mft_num * @cluster_per_mft_record, @bytes_per_mft_record) |
| 39 | + end |
| 40 | + |
| 41 | + # |
| 42 | + # Get the size of the file in the $FILENAME (64) attribute |
| 43 | + # |
| 44 | + def real_size_from_filenameattribute(attribute) |
| 45 | + filename_attribute = attribute |
| 46 | + filename_attribute[48, 8].unpack("Q")[0] |
| 47 | + end |
| 48 | + |
| 49 | + # |
| 50 | + # Gather the name of the file from the $FILENAME (64) attribute |
| 51 | + # |
| 52 | + def filename_from_filenameattribute(attribute) |
| 53 | + filename_attribute = attribute |
| 54 | + length_of_name = filename_attribute[64].ord |
| 55 | + # uft16 *2 |
| 56 | + d = ::Encoding::Converter.new("UTF-16LE", "UTF-8") |
| 57 | + d.convert(filename_attribute[66, (length_of_name * 2)]) |
| 58 | + end |
| 59 | + |
| 60 | + # |
| 61 | + # Get the file from the MFT number |
| 62 | + # The size must be gived because the $FILENAME attribute |
| 63 | + # in the MFT entry does not contain it |
| 64 | + # The file is in $DATA (128) Attribute |
| 65 | + # |
| 66 | + def file_content_from_mft_num(mft_num, size) |
| 67 | + mft_record = mft_record_from_mft_num(mft_num) |
| 68 | + attribute_list = mft_record_attribute(mft_record) |
| 69 | + if attribute_list[128]["resident"] |
| 70 | + return attribute_list[128]["data"] |
| 71 | + else |
| 72 | + return cluster_from_attribute_non_resident(attribute_list[128]["data"])[0, size] |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + # |
| 77 | + # parse one index record and return the name, MFT number and size of the file |
| 78 | + # |
| 79 | + def parse_index(index_entry) |
| 80 | + res = {} |
| 81 | + filename_size = index_entry[10, 2].unpack("S")[0] |
| 82 | + filename_attribute = index_entry[16, filename_size] |
| 83 | + # Should be 8 bytes but it doesn't work |
| 84 | + # mft_offset = index_entry[0.unpack("Q",:8])[0] |
| 85 | + # work with 4 bytes |
| 86 | + mft_offset = index_entry[0, 4].unpack("<I")[0] |
| 87 | + res[filename_from_filenameattribute(filename_attribute)] = { "mft_offset" => mft_offset, "file_size" => real_size_from_filenameattribute(filename_attribute) } |
| 88 | + res |
| 89 | + end |
| 90 | + |
| 91 | + # |
| 92 | + # parse index_record in $INDEX_ROOT and recursively index_record in |
| 93 | + # INDEX_ALLOCATION |
| 94 | + # |
| 95 | + def parse_index_list(index_record, index_allocation_attribute) |
| 96 | + offset_index_entry_list = index_record[0, 4].unpack("<I")[0] |
| 97 | + index_size = index_record[offset_index_entry_list + 8, 2].unpack("S")[0] |
| 98 | + index_entry = index_record[offset_index_entry_list, index_size] |
| 99 | + res = {} |
| 100 | + while index_entry[12, 4].unpack("<I")[0] & 2 != 2 |
| 101 | + res.update(parse_index(index_entry)) |
| 102 | + # if son |
| 103 | + if index_entry[12, 4].unpack("<I")[0] & 1 == 1 |
| 104 | + # should be 8 bytes length |
| 105 | + vcn = index_entry[-8, 4].unpack("I")[0] |
| 106 | + res_son = parse_index_list(index_allocation_attribute[vcn * @bytes_per_cluster + 24, index_size * @bytes_per_cluster], index_allocation_attribute) |
| 107 | + res.update(res_son) |
| 108 | + end |
| 109 | + offset_index_entry_list += index_size |
| 110 | + index_size = index_record[offset_index_entry_list + 8, 2].unpack("S")[0] |
| 111 | + index_entry = index_record [offset_index_entry_list, index_size] |
| 112 | + end |
| 113 | + # if son on the last |
| 114 | + if index_entry[12, 4].unpack("<I")[0] & 1 == 1 |
| 115 | + # should be 8 bytes length |
| 116 | + vcn = index_entry[-8, 4].unpack("I")[0] |
| 117 | + res_son = parse_index_list(index_allocation_attribute[vcn * @bytes_per_cluster + 24, index_size * @bytes_per_cluster], index_allocation_attribute) |
| 118 | + res.update(res_son) |
| 119 | + end |
| 120 | + res |
| 121 | + end |
| 122 | + |
| 123 | + # |
| 124 | + # return the list of files in attribute directory and their MFT number and size |
| 125 | + # |
| 126 | + def index_list_from_attributes(attributes) |
| 127 | + index_root_attribute = attributes[144] |
| 128 | + index_record = index_root_attribute[16, index_root_attribute.length - 16] |
| 129 | + if attributes.key?(160) |
| 130 | + return parse_index_list(index_record, attributes[160]) |
| 131 | + else |
| 132 | + return parse_index_list(index_record, "") |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + def cluster_from_attribute_non_resident(attribute, cluster_num = 0, size_max = ((2**31) - 1)) |
| 137 | + lowvcn = attribute[16, 8].unpack("<Q")[0] |
| 138 | + highvcn = attribute[24, 8].unpack("<Q")[0] |
| 139 | + offset = attribute[32, 2].unpack("<S")[0] |
| 140 | + real_size = attribute[48, 8].unpack("<Q")[0] |
| 141 | + attribut = "" |
| 142 | + run_list_num = lowvcn |
| 143 | + old_offset = 0 |
| 144 | + |
| 145 | + while run_list_num <= highvcn |
| 146 | + first_runlist_byte = attribute[offset].ord |
| 147 | + run_offset_size = first_runlist_byte >> 4 |
| 148 | + run_length_size = first_runlist_byte & 15 |
| 149 | + run_length = attribute[offset + 1, run_length_size] |
| 150 | + run_length += "\x00" * (8 - run_length_size) |
| 151 | + run_length = run_length.unpack("<Q")[0] |
| 152 | + |
| 153 | + offset_run_offset = offset + 1 + run_length_size |
| 154 | + run_offset = attribute[offset_run_offset, run_offset_size] |
| 155 | + if run_offset[-1].ord & 128 == 128 |
| 156 | + run_offset += "\xFF".force_encoding("BINARY") * (8 - run_offset_size) |
| 157 | + else |
| 158 | + run_offset += "\x00" * (8 - run_offset_size) |
| 159 | + end |
| 160 | + run_offset = run_offset.unpack("<q")[0] |
| 161 | + |
| 162 | + size_wanted = [run_length * @bytes_per_cluster, size_max - attribut.length].min |
| 163 | + |
| 164 | + if cluster_num + (size_wanted / @bytes_per_cluster) >= run_list_num && (cluster_num < run_length + run_list_num) |
| 165 | + run_list_offset = (run_offset + old_offset + [cluster_num - run_list_num, 0].max) * @bytes_per_cluster |
| 166 | + run_list_offset = run_list_offset.to_i |
| 167 | + @file_handler.seek(run_list_offset) |
| 168 | + |
| 169 | + data = "" |
| 170 | + while data.length < size_wanted |
| 171 | + data += @file_handler.read(size_wanted - data.length) |
| 172 | + end |
| 173 | + attribut += data |
| 174 | + end |
| 175 | + offset += run_offset_size + run_length_size + 1 |
| 176 | + run_list_num += run_length |
| 177 | + old_offset = run_offset |
| 178 | + end |
| 179 | + attribut = attribut[0, real_size] |
| 180 | + attribut |
| 181 | + end |
| 182 | + |
| 183 | + # |
| 184 | + # return the attribute list from the MFT record |
| 185 | + # deal with resident and non resident attributes (but not $DATA due to perforemence issue) |
| 186 | + # |
| 187 | + def mft_record_attribute(mft_record) |
| 188 | + attribute_list_offset = mft_record[20, 2].unpack("C")[0] |
| 189 | + curs = attribute_list_offset |
| 190 | + attribute_identifier = mft_record[curs, 4].unpack("I")[0] |
| 191 | + res = {} |
| 192 | + while attribute_identifier != 0xFFFFFFFF |
| 193 | + # attribute_size=mft_record[curs + 4, 4].unpack("I")[0] |
| 194 | + # should be on 4 bytes but doesnt work |
| 195 | + attribute_size = mft_record[curs + 4, 2].unpack("S")[0] |
| 196 | + #print_debug("attribute_size: #{attribute_size}, attribute_identifier: #{attribute_identifier}") |
| 197 | + # resident |
| 198 | + if mft_record[curs + 8] == "\x00" |
| 199 | + content_size = mft_record[curs + 16, 4].unpack("<I")[0] |
| 200 | + content_offset = mft_record[curs + 20, 2].unpack("S")[0] |
| 201 | + res[attribute_identifier] = mft_record[curs + content_offset, content_size] |
| 202 | + else |
| 203 | + # non resident |
| 204 | + if attribute_identifier == 128 |
| 205 | + res[attribute_identifier] = mft_record[curs, attribute_size] |
| 206 | + else |
| 207 | + res[attribute_identifier] = cluster_from_attribute_non_resident(mft_record[curs, attribute_size]) |
| 208 | + end |
| 209 | + end |
| 210 | + if attribute_identifier == 128 |
| 211 | + res[attribute_identifier] = { "data" => res[attribute_identifier], "resident" => mft_record[curs + 8] == "\x00" } |
| 212 | + end |
| 213 | + curs += attribute_size |
| 214 | + attribute_identifier = mft_record[curs, 4].unpack("I")[0] |
| 215 | + end |
| 216 | + res |
| 217 | + end |
| 218 | + |
| 219 | + # |
| 220 | + # return the file path in the NTFS partition |
| 221 | + # |
| 222 | + def file(path) |
| 223 | + repertory = mft_record_from_mft_num(5) |
| 224 | + index_entry = {} |
| 225 | + for r in path.split("\\") |
| 226 | + attributes = mft_record_attribute(repertory) |
| 227 | + index = index_list_from_attributes(attributes) |
| 228 | + #print_debug("#{index}") |
| 229 | + unless index.key?(r) |
| 230 | + fail ArgumentError, "File path does not exist", caller |
| 231 | + end |
| 232 | + index_entry = index[r] |
| 233 | + repertory = mft_record_from_mft_num(index_entry["mft_offset"]) |
| 234 | + end |
| 235 | + file_content_from_mft_num(index_entry["mft_offset"], index_entry["file_size"]) |
| 236 | + end |
| 237 | + end |
| 238 | + end |
| 239 | +end |
| 240 | +# f = open(ARGV[0],"r") |
| 241 | +# ntfs = NTFS.new(f) |
| 242 | +# puts ntfs.file(ARGV[1]) |
| 243 | +# f.close |
0 commit comments