def read_program(filename): ''' Identifies the program header (ELF/PE) and returns an ELF, PE or DataIO instance. Args: filename (str): the program to read. Returns: an instance of currently supported program format (ELF, PE) ''' obj = None try: # open file as a ELF object: p = elf.Elf(filename) logger.info("ELF format detected") return p except elf.ElfError: pass try: # open file as a PE object: p = pe.PE(filename) logger.info("PE format detected") return p except pe.PEError: pass logger.warning('unknown format') try: data = open(filename, 'rb') except (TypeError, IOError): data = filename return DataIO(data)
def read_program(filename): ''' Identifies the program header (ELF/PE) and returns an ELF, PE or DataIO instance. Args: filename (str): the program to read. Returns: an instance of currently supported program format (ELF, PE) ''' try: data = open(filename, 'rb') except (TypeError, IOError): data = bytes(filename) f = DataIO(data) try: # open file as a ELF object: p = elf.Elf(f) logger.info("ELF format detected") return p except elf.ElfError: f.seek(0) logger.debug('ElfError raised for %s' % f.name) try: # open file as a PE object: p = pe.PE(f) logger.info("PE format detected") return p except pe.PEError: f.seek(0) logger.debug('PEError raised for %s' % f.name) try: # open file as a HEX object: p = utils.HEX(f) logger.info("HEX format detected") return p except utils.FormatError: f.seek(0) logger.debug(' HEX FormatError raised for %s' % f.name) try: # open file as a SREC object: p = utils.SREC(f) logger.info("SREC format detected") return p except utils.FormatError: f.seek(0) logger.debug(' SREC FormatError raised for %s' % f.name) logger.warning('unknown format') return f
def read_program(file): obj = None try: # open file as a ELF object: p = elf.Elf(file) logger.info("ELF file detected") return p except elf.ElfError: pass try: # open file as a PE object: p = pe.PE(file) logger.info("PE file detected") return p except pe.PEError: pass logger.error('unknown file format') raise ValueError
def read_program(filename): obj = None try: # open file as a ELF object: p = elf.Elf(filename) logger.info("ELF format detected") return p except elf.ElfError: pass try: # open file as a PE object: p = pe.PE(filename) logger.info("PE format detected") return p except pe.PEError: pass logger.warning('unknown format') try: data = file(filename,'rb') except (TypeError,IOError): data = filename return DataIO(data)
def read_program(filename): """ Identifies the program header and returns an ELF, PE, Mach-O or DataIO. Args: filename (str): the program to read. Returns: an instance of currently supported program format (ELF, PE, Mach-O, HEX, SREC) """ try: data = open(filename, "rb") except (ValueError, TypeError, IOError): data = bytes(filename) f = DataIO(data) try: from amoco.system import elf # open file as a ELF object: p = elf.Elf(f) logger.info("ELF format detected") return p except (elf.StructureError, elf.ElfError): f.seek(0) logger.debug("ElfError raised for %s" % f.name) try: from amoco.system import pe # open file as a PE object: p = pe.PE(f) logger.info("PE format detected") return p except (pe.StructureError, pe.PEError): f.seek(0) logger.debug("PEError raised for %s" % f.name) try: from amoco.system import macho # open file as a Mach-O object: p = macho.MachO(f) logger.info("Mach-O format detected") return p except (macho.StructureError, macho.MachOError): f.seek(0) logger.debug("MachOError raised for %s" % f.name) try: from amoco.system.structs.HEX import HEX, HEXError # open file as a HEX object: p = HEX(f) logger.info("HEX format detected") return p except HEXError: f.seek(0) logger.debug(" HEX FormatError raised for %s" % f.name) try: from amoco.system.structs.SREC import SREC, SRECError # open file as a SREC object: p = SREC(f) logger.info("SREC format detected") return p except SRECError: f.seek(0) logger.debug(" SREC FormatError raised for %s" % f.name) logger.warning("unknown format") return shellcode(f)