def __init__(self, custom_methods, *args, **kwargs): from miasm.jitter.loader.elf import vm_load_elf, preload_elf, libimp_elf from miasm.os_dep import linux_stdlib methods = linux_stdlib.__dict__ methods.update(custom_methods) super(OS_Linux, self).__init__(methods, *args, **kwargs) # Import manager self.libs = libimp_elf() with open(self.fname, "rb") as fstream: self.elf = vm_load_elf(self.jitter.vm, fstream.read(), name=self.fname, **kwargs) preload_elf(self.jitter.vm, self.elf, self.libs) self.entry_point = self.elf.Ehdr.entry # Library calls handler self.jitter.add_lib_handler(self.libs, methods) linux_stdlib.ABORT_ADDR = self.CALL_FINISH_ADDR # Arguments self.argv = [self.PROGRAM_PATH] if self.options.command_line: self.argv += self.options.command_line self.envp = self.options.environment_vars
def __init__(self, custom_methods, *args, **kwargs): from miasm.jitter.loader.elf import vm_load_elf, preload_elf, libimp_elf from miasm.os_dep import linux_stdlib methods = linux_stdlib.__dict__ methods.update(custom_methods) super(OS_Linux, self).__init__(methods, *args, **kwargs) # Import manager self.libs = libimp_elf() with open(self.fname, "rb") as fstream: self.elf = vm_load_elf( self.jitter.vm, fstream.read(), name=self.fname, **kwargs ) preload_elf(self.jitter.vm, self.elf, self.libs) self.entry_point = self.elf.Ehdr.entry # Library calls handler self.jitter.add_lib_handler(self.libs, methods) linux_stdlib.ABORT_ADDR = self.CALL_FINISH_ADDR # Arguments self.argv = [self.PROGRAM_PATH] if self.options.command_line: self.argv += self.options.command_line self.envp = self.options.environment_vars
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)