def parse(self, data, vm=None, **kwargs): from miasm.jitter.loader.pe import vm_load_pe, guess_arch from miasm.loader import pe_init # Parse signature if not data.startswith(b'MZ'): raise ContainerSignatureException() # Build executable instance try: if vm is not None: self._executable = vm_load_pe(vm, data) else: self._executable = pe_init.PE(data) except Exception as error: raise ContainerParsingException('Cannot read PE: %s' % error) # Check instance validity if not self._executable.isPE() or \ self._executable.NTsig.signature_value != 0x4550: raise ContainerSignatureException() # Guess the architecture self._arch = guess_arch(self._executable) # Build the bin_stream instance and set the entry point try: self._bin_stream = bin_stream_pe(self._executable) ep_detected = self._executable.Opthdr.AddressOfEntryPoint self._entry_point = self._executable.rva2virt(ep_detected) except Exception as error: raise ContainerParsingException('Cannot read PE: %s' % error)
def parse(self, data, vm=None, addr=0, apply_reloc=False, **kwargs): """Load an ELF from @data @data: bytes containing the ELF bytes @vm (optional): VmMngr instance. If set, load the ELF in virtual memory @addr (optional): base address the ELF in virtual memory @apply_reloc (optional): if set, apply relocation during ELF loading @addr and @apply_reloc are only meaningful in the context of a non-empty @vm """ from miasm.jitter.loader.elf import vm_load_elf, guess_arch, \ fill_loc_db_with_symbols from miasm.loader import elf_init # Parse signature if not data.startswith(b'\x7fELF'): raise ContainerSignatureException() # Build executable instance try: if vm is not None: self._executable = vm_load_elf( vm, data, loc_db=self.loc_db, base_addr=addr, apply_reloc=apply_reloc ) else: self._executable = elf_init.ELF(data) except Exception as error: raise ContainerParsingException('Cannot read ELF: %s' % error) # Guess the architecture self._arch = guess_arch(self._executable) # Build the bin_stream instance and set the entry point try: self._bin_stream = bin_stream_elf(self._executable) self._entry_point = self._executable.Ehdr.entry + addr except Exception as error: raise ContainerParsingException('Cannot read ELF: %s' % error) if vm is None: # Add known symbols (vm_load_elf already does it) fill_loc_db_with_symbols(self._executable, self.loc_db, addr)