Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/parser/fs/ntfs.rb
Views: 11783
# -*- coding: binary -*-1module Rex2module Parser3###4#5# This class parses the contents of an NTFS partition file.6# Author : Danil Bazin <danil.bazin[at]hsc.fr> @danilbaz7#8###9class NTFS10#11# Initialize the NTFS class with an already open file handler12#13DATA_ATTRIBUTE_ID = 12814INDEX_ROOT_ID = 14415INDEX_ALLOCATION_ID = 16016def initialize(file_handler)17@file_handler = file_handler18data = @file_handler.read(4096)19# Boot sector reading20@bytes_per_sector = data[11, 2].unpack('v')[0]21@sector_per_cluster = data[13].unpack('C')[0]22@cluster_per_mft_record = data[64].unpack('c')[0]23if @cluster_per_mft_record < 024@bytes_per_mft_record = 2**(-@cluster_per_mft_record)25@cluster_per_mft_record = @bytes_per_mft_record.to_f / @bytes_per_sector / @sector_per_cluster26else27@bytes_per_mft_record = @bytes_per_sector * @sector_per_cluster * @cluster_per_mft_record28end29@bytes_per_cluster = @sector_per_cluster * @bytes_per_sector30@mft_logical_cluster_number = data[48, 8].unpack('Q<')[0]31@mft_offset = @mft_logical_cluster_number * @sector_per_cluster * @bytes_per_sector32@file_handler.seek(@mft_offset)33@mft = @file_handler.read(@bytes_per_mft_record)34end3536#37# Gather the MFT entry corresponding to his number38#39def mft_record_from_mft_num(mft_num)40mft_num_offset = mft_num * @cluster_per_mft_record41mft_data_attribute = mft_record_attribute(@mft)[DATA_ATTRIBUTE_ID]['data']42cluster_from_attribute_non_resident(mft_data_attribute, mft_num_offset, @bytes_per_mft_record)43end4445#46# Get the size of the file in the $FILENAME (64) attribute47#48def real_size_from_filenameattribute(attribute)49filename_attribute = attribute50filename_attribute[48, 8].unpack('Q<')[0]51end5253#54# Gather the name of the file from the $FILENAME (64) attribute55#56def filename_from_filenameattribute(attribute)57filename_attribute = attribute58length_of_name = filename_attribute[64].ord59# uft16 *260d = ::Encoding::Converter.new('UTF-16LE', 'UTF-8')61d.convert(filename_attribute[66, (length_of_name * 2)])62end6364#65# Get the file from the MFT number66# The size must be given because the $FILENAME attribute67# in the MFT entry does not contain it68# The file is in $DATA (128) Attribute69#70def file_content_from_mft_num(mft_num, size)71mft_record = mft_record_from_mft_num(mft_num)72attribute_list = mft_record_attribute(mft_record)73if attribute_list[DATA_ATTRIBUTE_ID]['resident']74return attribute_list[DATA_ATTRIBUTE_ID]['data']75else76data_attribute = attribute_list[DATA_ATTRIBUTE_ID]['data']77return cluster_from_attribute_non_resident(data_attribute)[0, size]78end79end8081#82# parse one index record and return the name, MFT number and size of the file83#84def parse_index(index_entry)85res = {}86filename_size = index_entry[10, 2].unpack('v')[0]87filename_attribute = index_entry[16, filename_size]88# Should be 8 bytes but it doesn't work89# mft_offset = index_entry[0.unpack('Q<',:8])[0]90# work with 4 bytes91mft_offset = index_entry[0, 4].unpack('V')[0]92res[filename_from_filenameattribute(filename_attribute)] = {93'mft_offset' => mft_offset,94'file_size' => real_size_from_filenameattribute(filename_attribute) }95res96end9798#99# parse index_record in $INDEX_ROOT and recursively index_record in100# INDEX_ALLOCATION101#102def parse_index_list(index_record, index_allocation_attribute)103offset_index_entry_list = index_record[0, 4].unpack('V')[0]104index_size = index_record[offset_index_entry_list + 8, 2].unpack('v')[0]105index_size_in_bytes = index_size * @bytes_per_cluster106index_entry = index_record[offset_index_entry_list, index_size]107res = {}108while index_entry[12, 4].unpack('V')[0] & 2 != 2109res.update(parse_index(index_entry))110# if son111if index_entry[12, 4].unpack('V')[0] & 1 == 1112# should be 8 bytes length113vcn = index_entry[-8, 4].unpack('V')[0]114vcn_in_bytes = vcn * @bytes_per_cluster115res_son = parse_index_list(index_allocation_attribute[vcn_in_bytes + 24, index_size_in_bytes], index_allocation_attribute)116res.update(res_son)117end118offset_index_entry_list += index_size119index_size = index_record[offset_index_entry_list + 8, 2].unpack('v')[0]120index_size_in_bytes = index_size * @bytes_per_cluster121index_entry = index_record [offset_index_entry_list, index_size]122end123# if son on the last124if index_entry[12, 4].unpack('V')[0] & 1 == 1125# should be 8 bytes length126vcn = index_entry[-8, 4].unpack('V')[0]127vcn_in_bytes = vcn * @bytes_per_cluster128res_son = parse_index_list(index_allocation_attribute[vcn_in_bytes + 24, index_size_in_bytes], index_allocation_attribute)129res.update(res_son)130end131res132end133134#135# return the list of files in attribute directory and their MFT number and size136#137def index_list_from_attributes(attributes)138index_root_attribute = attributes[INDEX_ROOT_ID]139index_record = index_root_attribute[16, index_root_attribute.length - 16]140if attributes.key?(INDEX_ALLOCATION_ID)141return parse_index_list(index_record, attributes[INDEX_ALLOCATION_ID])142else143return parse_index_list(index_record, '')144end145end146147def cluster_from_attribute_non_resident(attribute, cluster_num = 0, size_max = ((2**31) - 1))148lowvcn = attribute[16, 8].unpack('Q<')[0]149highvcn = attribute[24, 8].unpack('Q<')[0]150offset = attribute[32, 2].unpack('v')[0]151real_size = attribute[48, 8].unpack('Q<')[0]152attribut = ''153run_list_num = lowvcn154old_offset = 0155while run_list_num <= highvcn156first_runlist_byte = attribute[offset].ord157run_offset_size = first_runlist_byte >> 4158run_length_size = first_runlist_byte & 15159run_length = attribute[offset + 1, run_length_size]160run_length += "\x00" * (8 - run_length_size)161run_length = run_length.unpack('Q<')[0]162163offset_run_offset = offset + 1 + run_length_size164run_offset = attribute[offset_run_offset, run_offset_size]165if run_offset[-1].ord & 128 == 128166run_offset += "\xFF" * (8 - run_offset_size)167else168run_offset += "\x00" * (8 - run_offset_size)169end170run_offset = run_offset.unpack('q<')[0]171#offset relative to previous offset172run_offset += old_offset173174size_wanted = [run_length * @bytes_per_cluster, size_max - attribut.length].min175if cluster_num + (size_max / @bytes_per_cluster) >= run_list_num && (cluster_num < run_length + run_list_num)176run_list_offset_in_cluster = run_offset + [cluster_num - run_list_num, 0].max177run_list_offset = (run_list_offset_in_cluster) * @bytes_per_cluster178run_list_offset = run_list_offset.to_i179@file_handler.seek(run_list_offset)180181data = ''182while data.length < size_wanted183# Use a 4Mb block size to avoid target memory consumption184data << @file_handler.read([size_wanted - data.length, 2**22].min)185end186attribut << data187end188offset += run_offset_size + run_length_size + 1189run_list_num += run_length190old_offset = run_offset191end192attribut = attribut[0, real_size]193attribut194end195196#197# return the attribute list from the MFT record198# deal with resident and non resident attributes (but not $DATA due to performance issue)199# if lazy = True, this function only gather essential non resident attributes200# (INDEX_ALLOCATION). Non resident attributes can still be gathered later with201# cluster_from_attribute_non_resident function.202#203def mft_record_attribute(mft_record, lazy=true)204attribute_list_offset = mft_record[20, 2].unpack('C')[0]205curs = attribute_list_offset206attribute_identifier = mft_record[curs, 4].unpack('V')[0]207res = {}208while attribute_identifier != 0xFFFFFFFF209# attribute_size=mft_record[curs + 4, 4].unpack('V')[0]210# should be on 4 bytes but doesnt work211attribute_size = mft_record[curs + 4, 2].unpack('v')[0]212# resident213if mft_record[curs + 8] == "\x00"214content_size = mft_record[curs + 16, 4].unpack('V')[0]215content_offset = mft_record[curs + 20, 2].unpack('v')[0]216res[attribute_identifier] = mft_record[curs + content_offset, content_size]217else218# non resident219if attribute_identifier == INDEX_ALLOCATION_ID or220(!lazy and attribute_identifier != DATA_ATTRIBUTE_ID)221res[attribute_identifier] = cluster_from_attribute_non_resident(mft_record[curs, attribute_size])222else223res[attribute_identifier] = mft_record[curs, attribute_size]224end225end226if attribute_identifier == DATA_ATTRIBUTE_ID227res[attribute_identifier] = {228'data' => res[attribute_identifier],229'resident' => mft_record[curs + 8] == "\x00" }230end231curs += attribute_size232attribute_identifier = mft_record[curs, 4].unpack('V')[0]233end234res235end236237#238# return the file path in the NTFS partition239#240def file(path)241repertory = mft_record_from_mft_num(5)242index_entry = {}243path.split('\\').each do |r|244attributes = mft_record_attribute(repertory)245index = index_list_from_attributes(attributes)246unless index.key?(r)247fail ArgumentError, 'File path does not exist', caller248end249index_entry = index[r]250repertory = mft_record_from_mft_num(index_entry['mft_offset'])251end252file_content_from_mft_num(index_entry['mft_offset'], index_entry['file_size'])253end254end255end256end257258259