Python unicorn.UcError() Examples

The following are 5 code examples of unicorn.UcError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module unicorn , or try the search function .
Example #1
Source File: abstractcpu.py    From manticore with GNU Affero General Public License v3.0 6 votes vote down vote up
def concrete_emulate(self, insn):
        """
        Start executing in Unicorn from this point until we hit a syscall or reach break_unicorn_at

        :param capstone.CsInsn insn: The instruction object to emulate
        """

        if not self.emu:
            self.emu = ConcreteUnicornEmulator(self)
            self.emu._stop_at = self._break_unicorn_at
        try:
            self.emu.emulate(insn)
        except unicorn.UcError as e:
            if e.errno == unicorn.UC_ERR_INSN_INVALID:
                text_bytes = " ".join("%02x" % x for x in insn.bytes)
                logger.error(
                    "Unimplemented instruction: 0x%016x:\t%s\t%s\t%s",
                    insn.address,
                    text_bytes,
                    insn.mnemonic,
                    insn.op_str,
                )
            raise InstructionEmulationError(str(e)) 
Example #2
Source File: qemu.py    From Sibyl with GNU General Public License v3.0 5 votes vote down vote up
def run(self, pc, timeout_seconds=1):
        # checking which instruction you want to emulate: THUMB/THUMB2 or others
        # Note we start at ADDRESS | 1 to indicate THUMB mode.
        try:
            if self.ask_arch == 'armt' and self.ask_attrib == 'l':
                self.mu.emu_start(pc | 1, END_ADDR, timeout_seconds * unicorn.UC_SECOND_SCALE)
            else:
                self.mu.emu_start(pc, END_ADDR, timeout_seconds * unicorn.UC_SECOND_SCALE)
        except unicorn.UcError as e:
            if getattr(self.cpu, self.ira.pc.name) != END_ADDR:
                raise UnexpectedStopException()
        finally:
            self.mu.emu_stop() 
Example #3
Source File: unicorn_protocol.py    From avatar2 with Apache License 2.0 5 votes vote down vote up
def write_memory(self, address, wordsize, val, num_words=1, raw=False):
        """Write memory.

        :param address:   the address to write to
        :param wordsize:  size of a written word (1, 2, 4 or 8)
        :param val:       the value to write
        :type val:        int if num_words == 1 and raw == False,
                          list of int if num_words > 1 and raw == False,
                          str or byte if raw == True
        :param num_words: the amount of words to write
        :param raw:       whether to write in raw mode
        :return: True on success, False otherwise
        """
        if raw:
            raw_mem = val
        else:
            # TODO: endianness support
            num2fmt = {1: 'B', 2: 'H', 4: 'I', 8: 'Q'}
            fmt = '<{}{}'.format(num_words, num2fmt[wordsize])
            if num_words == 1:
                raw_mem = struct.pack(fmt, val)
            else:
                raw_mem = struct.pack(fmt, *val)

        try:
            self.uc.mem_write(address, raw_mem)
            return True
        except unicorn.UcError:
            self.log.debug('Failed memory write @ 0x{:x}'.format(address))
            return False

    # Register protocol 
Example #4
Source File: abstractcpu.py    From manticore with GNU Affero General Public License v3.0 5 votes vote down vote up
def backup_emulate(self, insn):
        """
        If we could not handle emulating an instruction, use Unicorn to emulate
        it.

        :param capstone.CsInsn instruction: The instruction object to emulate
        """

        if not hasattr(self, "backup_emu"):
            self.backup_emu = UnicornEmulator(self)
        try:
            self.backup_emu.emulate(insn)
        except unicorn.UcError as e:
            if e.errno == unicorn.UC_ERR_INSN_INVALID:
                text_bytes = " ".join("%02x" % x for x in insn.bytes)
                logger.error(
                    "Unimplemented instruction: 0x%016x:\t%s\t%s\t%s",
                    insn.address,
                    text_bytes,
                    insn.mnemonic,
                    insn.op_str,
                )
            raise InstructionEmulationError(str(e))
        finally:
            # We have been seeing occasional Unicorn issues with it not clearing
            # the backing unicorn instance. Saw fewer issues with the following
            # line present.
            del self.backup_emu 
Example #5
Source File: headers.py    From unipacker with GNU General Public License v2.0 5 votes vote down vote up
def get_imp(uc, rva, base_addr, SizeOfImportTable, read_values=False):
    rva += base_addr
    entry_size = len(bytes(IMAGE_IMPORT_DESCRIPTOR()))
    num_of_dll = SizeOfImportTable // entry_size
    imp_info_list = []
    imp_struct_list = []
    for i in range(num_of_dll):
        uc_imp = uc.mem_read(rva, entry_size)
        imp_struct = IMAGE_IMPORT_DESCRIPTOR.from_buffer(uc_imp)
        imp_struct_list.append(imp_struct)
        if read_values:
            try:
                dll_name = get_string(getattr(imp_struct, "Name") + base_addr, uc, break_on_unprintable=True)
                imp_names = []
                rva_to_iat = getattr(imp_struct, "FirstThunk")
                while True:
                    new_val = struct.unpack("<I", uc.mem_read(base_addr + rva_to_iat, 4))[0]
                    if new_val == 0:
                        break
                    imp_name = (get_string(new_val + 2 + base_addr, uc, break_on_unprintable=True), rva_to_iat)
                    imp_names.append(imp_name)
                    rva_to_iat += 4

                imp_info_list.append(ImportValues(imp_struct, dll_name, imp_names))
            except UcError as ue:
                return imp_info_list

        rva += entry_size

    if read_values:
        return imp_info_list
    return imp_struct_list


# Deprecated