def __init__(self,filename): try: f = open(filename,'rb') except (TypeError,IOError): f = str(filename) data = DataIO(f) self.data = data # parse DOS header: self.DOS = DOSHdr(data) # parse PE header: self.NT = COFFHdr(data,self.DOS.e_lfanew) # parse Optional Header: self.Opt = OptionalHdr(data,self.DOS.e_lfanew+len(self.NT)) self.basemap = self.Opt.ImageBase if self.NT.SizeOfOptionalHeader != len(self.Opt): logger.warning('Optional header size mismatch') # read Sections: self.sections = [] offset = self.DOS.e_lfanew + len(self.NT) + self.NT.SizeOfOptionalHeader for i in range(self.NT.NumberOfSections): s = SectionHdr(data,offset) self.sections.append(s) offset += len(s) self.functions = self.__functions() self.variables = self.__variables() self.tls = self.__tls()
def __init__(self, romfile, cpu): try: f = open(romfile, "rb") except (ValueError, TypeError, IOError): print("romfile '%s' not found" % romfile) else: rom = DataIO(f) super().__init__(shellcode(rom), cpu) # setup memory space: # ------------------- # [0x0000-0x00FF] zero page: self.state.mmap.write(0, b'\0' * 0x100) # [0x0100-0x01FF] stack: self.state.mmap.write(0x100, b'\0' * 0x100) # [0x0200-0x02FF] input buffer (keyboard/floppy): self.state.mmap.write(0x200, b'\0' * 0x100) # [0x0300-0x03FF] program space & system API: self.state.mmap.write(0x300, b'\0' * 0x100) # [0x0400-0x07FF] video page1: self.state.mmap.write(0x400, b'\0' * 0x400) # [0x0800-0x0BFF] video page2: self.state.mmap.write(0x800, b'\0' * 0x400) # [0x0C00-0x1FFF] is free... # [0x2000-0x3FFF] high-res video page1: self.state.mmap.write(0x2000, b'\0' * 0x2000) # [0x4000-0x5FFF] high-res video page2: self.state.mmap.write(0x4000, b'\0' * 0x2000) # [0x6000-0xBFFF] is free... # [0xC000-0xC0FF] memory-mapped I/O: for io, addr in IOMAP: xf = cpu.ext(io, size=8) xf.stub = Apple2c.stub(xf.ref) self.state.mmap.write(addr, xf) # [0xC100-0xFFFF] ROM memory: self.setup_rom()
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 test_parser_elf32(samples): for filename in samples: if filename[-4:] == '.elf': with open(filename, 'rb') as f: p = Elf(DataIO(f)) assert p.Ehdr.e_ident.ELFMAG == b'ELF' assert p.Ehdr.e_ident.EI_CLASS == 1
def test_parser_hex(samples): for filename in samples: if filename[-4:] == '.hex': with open(filename, 'rb') as f: p = HEX(DataIO(f)) assert len(p.L) == 163 assert isinstance(p.L[0], HEXline) assert p.L[-1].HEXcode == EndOfFile
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 decode(self): mem = [] for l in self.L: if l.SRECtype in (Data16, Data24, Data32): mem.append((l.address, l.data)) m = MemoryMap() for (k, v) in mem: m.write(k, v) if len(m._zones) == 1: self.__dataio = DataIO(m._zones[None].dump())
def __init__(self, filename): from amoco.system.loader import read_program self.rom = read_program(filename) self.ivt = IVT(self.rom, offset=self.IVT_offset) assert self.ivt.self != 0 ILR = DataIO(self.rom[0:4096]) RawExec.__init__(self, ILR, cpu=cpu_armv7) start = self.ivt.self - self.IVT_offset self.relocate(start) if self.ivt.boot_data: self.boot_data = BootData(self.mmap, self.ivt.boot_data) off = self.boot_data.start - start data = self.rom[off:off + self.boot_data.size] self.mmap.write(self.boot_data.start, data) assert self.ivt.csf self.csf = CSF(self.mmap, self.ivt.csf)
def decode(self): seg = 0 ela = 0 lines = [] for l in self.L: if l.HEXcode == ExtendedSegmentAddress: seg = l.base elif l.HEXcode == ExtendedLinearAddress: ela = l.ela elif l.HEXcode == Data: if ela: address = (ela << 16) + l.address elif seg: address = (seg * 16) + l.address else: address = l.address lines.append((address, l.data)) m = MemoryMap() self.__lines = lines for k, v in lines: m.write(k, v) if len(m._zones) == 1: self.__dataio = DataIO(m._zones[None].dump())
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 __init__(self): self.__file = DataIO(b'')
def test_raw_001(samples): for f in samples: if f[-4:] == '.raw': p = RawExec(DataIO(open(f, 'rb')))
def test_raw_002(sc1): p = RawExec(DataIO(sc1))
def __init__(self): self.data = DataIO(b'')
def __init__(self, filename): try: f = open(filename, 'rb') except (TypeError, IOError): from amoco.system.core import DataIO f = DataIO(bytes(filename)) self.__file = f data = self.__file.read(52) if len(data) < 52: data = data.ljust(52, b'\x00') self.Ehdr = Elf32_Ehdr(data) self.dynamic = False # read program header table: should not raise any errors self.Phdr = [] if self.Ehdr.e_phoff: self.__file.seek(self.Ehdr.e_phoff) n, l = self.Ehdr.e_phnum, self.Ehdr.e_phentsize data = self.__file.read(n * l) for pht in range(n): logger.progress(pht, n, u'parsing Phdrs ') self.Phdr.append(Elf32_Phdr(data[pht * l:])) if self.Phdr[-1].p_type == PT_LOAD: if not self.basemap: self.basemap = self.Phdr[-1].p_vaddr elif self.Phdr[-1].p_type == PT_DYNAMIC: self.dynamic = True elif not self.Phdr[-1].p_type in ELF_CONSTS['p_type'].keys(): logger.verbose(u'invalid segment detected (removed)') self.Phdr.pop() # read section header table: unused by loader, can raise error self.Shdr = [] if self.Ehdr.e_shoff: try: self.__file.seek(self.Ehdr.e_shoff) n, l = self.Ehdr.e_shnum, self.Ehdr.e_shentsize data = self.__file.read(n * l) for sht in range(n): logger.progress(sht, n, 'parsing Shdrs ') S = Elf32_Shdr(data[sht * l:]) if S.sh_type in ELF_CONSTS['sh_type'].keys(): self.Shdr.append(S) else: raise StandardError except: logger.verbose('invalid section detected (all Shdr removed)') self.Shdr = [] # read section's name string table: n = self.Ehdr.e_shstrndx if n != SHN_UNDEF and n in range(len(self.Shdr)): S = self.Shdr[self.Ehdr.e_shstrndx] self.__file.seek(S.sh_offset) data = self.__file.read(S.sh_size) if S.sh_type != SHT_STRTAB: logger.verbose('section names not a string table') for s in self.Shdr: s.name = '' else: for s in self.Shdr: name = data[s.sh_name:].split(b'\0')[0] s.name = codecs.decode(name) self.functions = self.__functions() self.variables = self.__variables()
def test_raw_002(sc1): p = RawExec(shellcode(DataIO(sc1)))
def test_parser_srec(samples): for filename in samples: if filename[-4:] == '.srec': with open(filename, 'rb') as f: p = SREC(DataIO(f))
def test_parser_elf64(samples): for filename in samples: if filename[-4:] == '.elf64': with open(filename, 'rb') as f: p = Elf64(DataIO(f)) assert p.Ehdr.e_ident['ELFMAG'] == b'ELF'
def test_raw_001(samples): for filename in samples: if filename[-4:] == '.raw': with open(filename, 'rb') as f: p = RawExec(DataIO(f))