def extract_features_using_pefile(self, pe):

        # Store all extracted features into feature lists
        extracted_dense = {}
        extracted_sparse = {}

        # Now slog through the info and extract the features
        feature_not_found_flag = -99
        feature_default_value = 0
        self._warnings = []

        # Set all the dense features and sparse features to 'feature not found'
        # value and then check later to see if it was found
        for feature in self._dense_feature_list:
            extracted_dense[feature] = feature_not_found_flag
        for feature in self._sparse_feature_list:
            extracted_sparse[feature] = feature_not_found_flag


        # Check to make sure all the section names are standard
        std_sections = ['.text', '.bss', '.rdata', '.data', '.rsrc', '.edata', '.idata', \
                        '.pdata', '.debug', '.reloc', '.stab', '.stabstr', '.tls', \
                        '.crt', '.gnu_deb', '.eh_fram', '.exptbl', '.rodata']
        for i in range(200):
            std_sections.append('/'+str(i))
        std_section_names = 1
        extracted_sparse['section_names'] = []
        for section in pe.sections:
            name = convertToAsciiNullTerm(section.Name).lower()
            extracted_sparse['section_names'].append(name)
            if (name not in std_sections):
                std_section_names = 0

        extracted_dense['std_section_names']      = std_section_names
        extracted_dense['debug_size']             = pe.OPTIONAL_HEADER.DATA_DIRECTORY[6].Size
        extracted_dense['major_version']	      = pe.OPTIONAL_HEADER.MajorImageVersion
        extracted_dense['minor_version']          = pe.OPTIONAL_HEADER.MinorImageVersion
        extracted_dense['iat_rva']			      = pe.OPTIONAL_HEADER.DATA_DIRECTORY[1].VirtualAddress
        extracted_dense['export_size']		      = pe.OPTIONAL_HEADER.DATA_DIRECTORY[0].Size
        extracted_dense['check_sum']	          = pe.OPTIONAL_HEADER.CheckSum
        try:
            extracted_dense['generated_check_sum'] = pe.generate_checksum()
        except ValueError:
            #self._logger.logMessage('warning', 'pe.generate_check_sum() threw an exception, setting to 0!')
            extracted_dense['generated_check_sum'] = 0
        if (len(pe.sections) > 0):
            extracted_dense['virtual_address']     = pe.sections[0].VirtualAddress
            extracted_dense['virtual_size']	       = pe.sections[0].Misc_VirtualSize
        extracted_dense['number_of_sections']      = pe.FILE_HEADER.NumberOfSections
        extracted_dense['compile_date']            = pe.FILE_HEADER.TimeDateStamp
        extracted_dense['number_of_rva_and_sizes'] = pe.OPTIONAL_HEADER.NumberOfRvaAndSizes
        extracted_dense['total_size_pe']	       = len(pe.__data__)


        # Number of import and exports
        if hasattr(pe,'DIRECTORY_ENTRY_IMPORT'):
            extracted_dense['number_of_imports'] = len(pe.DIRECTORY_ENTRY_IMPORT)
            num_imported_symbols = 0
            for module in pe.DIRECTORY_ENTRY_IMPORT:
                num_imported_symbols += len(module.imports)
            extracted_dense['number_of_import_symbols'] = num_imported_symbols

        if hasattr(pe, 'DIRECTORY_ENTRY_BOUND_IMPORT'):
            extracted_dense['number_of_bound_imports'] = len(pe.DIRECTORY_ENTRY_BOUND_IMPORT)
            num_imported_symbols = 0
            for module in pe.DIRECTORY_ENTRY_BOUND_IMPORT:
                num_imported_symbols += len(module.entries)
            extracted_dense['number_of_bound_import_symbols'] = num_imported_symbols

        if hasattr(pe,'DIRECTORY_ENTRY_EXPORT'):
            extracted_dense['number_of_export_symbols'] = len(pe.DIRECTORY_ENTRY_EXPORT.symbols)
            symbol_set = set()
            for symbol in pe.DIRECTORY_ENTRY_EXPORT.symbols:
                symbol_info = 'unknown'
                if (not symbol.name):
                    symbol_info = 'ordinal=' + str(symbol.ordinal)
                else:
                    symbol_info = 'name=' + symbol.name

                symbol_set.add(convertToUTF8('%s'%(symbol_info)).lower())

            # Now convert set to list and add to features
            extracted_sparse['ExportedSymbols'] = list(symbol_set)

        # Specific Import info (Note this will be a sparse field woo hoo!)
        if hasattr(pe, 'DIRECTORY_ENTRY_IMPORT'):
            symbol_set = set()
            for module in pe.DIRECTORY_ENTRY_IMPORT:
                for symbol in module.imports:
                    symbol_info = 'unknown'
                    if symbol.import_by_ordinal is True:
                        symbol_info = 'ordinal=' + str(symbol.ordinal)
                    else:
                        symbol_info = 'name=' + symbol.name
                        #symbol_info['hint'] = symbol.hint
                    if symbol.bound:
                        symbol_info += ' bound=' + str(symbol.bound)

                    symbol_set.add(convertToUTF8('%s:%s'%(module.dll, symbol_info)).lower())

            # Now convert set to list and add to features
            extracted_sparse['imported_symbols'] = list(symbol_set)


        # Do we have a second section
        if (len(pe.sections) >= 2):
            extracted_dense['virtual_size_2']	   = pe.sections[1].Misc_VirtualSize

        extracted_dense['size_image']             = pe.OPTIONAL_HEADER.SizeOfImage
        extracted_dense['size_code']              = pe.OPTIONAL_HEADER.SizeOfCode
        extracted_dense['size_initdata']          = pe.OPTIONAL_HEADER.SizeOfInitializedData
        extracted_dense['size_uninit']            = pe.OPTIONAL_HEADER.SizeOfUninitializedData
        extracted_dense['pe_majorlink']           = pe.OPTIONAL_HEADER.MajorLinkerVersion
        extracted_dense['pe_minorlink']           = pe.OPTIONAL_HEADER.MinorLinkerVersion
        extracted_dense['pe_driver']              = 1 if pe.is_driver() else 0
        extracted_dense['pe_exe']                 = 1 if pe.is_exe() else 0
        extracted_dense['pe_dll']                 = 1 if pe.is_dll() else 0
        extracted_dense['pe_i386']                = 1
        if pe.FILE_HEADER.Machine != 0x014c:
            extracted_dense['pe_i386'] = 0
        extracted_dense['pe_char']                = pe.FILE_HEADER.Characteristics

        # Data directory features!!
        datadirs = { 0: 'IMAGE_DIRECTORY_ENTRY_EXPORT', 1:'IMAGE_DIRECTORY_ENTRY_IMPORT', 2:'IMAGE_DIRECTORY_ENTRY_RESOURCE', 5:'IMAGE_DIRECTORY_ENTRY_BASERELOC', 12:'IMAGE_DIRECTORY_ENTRY_IAT'}
        for idx, datadir in datadirs.items():
            datadir = pefile.DIRECTORY_ENTRY[ idx ]
            if len(pe.OPTIONAL_HEADER.DATA_DIRECTORY) <= idx:
                continue

            directory = pe.OPTIONAL_HEADER.DATA_DIRECTORY[idx]
            extracted_dense['datadir_%s_size' % datadir] = directory.Size

        # Section features
        section_flags = ['IMAGE_SCN_MEM_EXECUTE', 'IMAGE_SCN_CNT_CODE', 'IMAGE_SCN_MEM_WRITE', 'IMAGE_SCN_MEM_READ']
        rawexecsize = 0
        vaexecsize = 0
        for sec in pe.sections:
            if not sec:
                continue

            for char in section_flags:
                # does the section have one of our attribs?
                if hasattr(sec, char):
                    rawexecsize += sec.SizeOfRawData
                    vaexecsize  += sec.Misc_VirtualSize
                    break

            # Take out any weird characters in section names
            secname = convertToAsciiNullTerm(sec.Name).lower()
            secname = secname.replace('.','')
            extracted_dense['sec_entropy_%s' % secname ] = sec.get_entropy()
            extracted_dense['sec_rawptr_%s' % secname] = sec.PointerToRawData
            extracted_dense['sec_rawsize_%s' % secname] = sec.SizeOfRawData
            extracted_dense['sec_vasize_%s' % secname] = sec.Misc_VirtualSize


        extracted_dense['sec_va_execsize'] = vaexecsize
        extracted_dense['sec_raw_execsize'] = rawexecsize

        # Register if there were any pe warnings
        warnings = pe.get_warnings()
        if (warnings):
            extracted_dense['pe_warnings'] = 1
            extracted_sparse['pe_warning_strings'] = warnings
        else:
            extracted_dense['pe_warnings'] = 0


        # Issue a warning if the feature isn't found
        for feature in self._dense_feature_list:
            if (extracted_dense[feature] == feature_not_found_flag):
                extracted_dense[feature] = feature_default_value
                if (self._verbose):
                    self.log('info: Feature: %s not found! Setting to %d' % (feature, feature_default_value))
                    self._warnings.append('Feature: %s not found! Setting to %d' % (feature, feature_default_value))

        # Issue a warning if the feature isn't found
        for feature in self._sparse_feature_list:
            if (extracted_sparse[feature] == feature_not_found_flag):
                extracted_sparse[feature] = feature_default_value
                if (self._verbose):
                    self.log('info: Feature: %s not found! Setting to %d' % (feature, feature_default_value))
                    self._warnings.append('Feature: %s not found! Setting to %d' % (feature, feature_default_value))


        # Set the features for the class var
        self._dense_features = extracted_dense
        self._sparse_features = extracted_sparse

        return self.get_dense_features() #, self.get_sparse_features()

# Helper functions
Process the PE File using the Python pefile module.
dataset/ETHPy150Open ClickSecurity/data_hacking/pefile_classification/pe_features.py/PEFileFeatures.extract_features_using_pefile