Python unicorn.UC_HOOK_CODE Examples

The following are 5 code examples of unicorn.UC_HOOK_CODE(). 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: rainbow.py    From rainbow with GNU Lesser General Public License v3.0 6 votes vote down vote up
def setup(self, sca_mode):
        """ Sets up a stack and adds base hooks to the engine """
        ## Add a stack
        self.map_space(*self.STACK)

        ## Add hooks
        self.mem_unmapped_hook = self.emu.hook_add(uc.UC_HOOK_MEM_UNMAPPED, self.unmapped_hook)
        self.block_hook = self.emu.hook_add(uc.UC_HOOK_BLOCK, self.block_handler)
        if sca_mode:
            if (self.sca_HD):
                self.ct_hook = self.emu.hook_add(uc.UC_HOOK_CODE, self.sca_code_traceHD)
            else:
                self.ct_hook = self.emu.hook_add(uc.UC_HOOK_CODE, self.sca_code_trace)
            self.tm_hook = self.emu.hook_add(
                uc.UC_HOOK_MEM_READ | uc.UC_HOOK_MEM_WRITE, self.sca_trace_mem
            )
        else:
            self.code_hook = self.emu.hook_add(uc.UC_HOOK_CODE, self.code_trace)
            self.mem_access_hook = self.emu.hook_add( uc.UC_HOOK_MEM_READ | uc.UC_HOOK_MEM_WRITE, self.trace_mem) 
Example #2
Source File: unicorn_trace.py    From bootloader_instrumentation_suite with MIT License 6 votes vote down vote up
def setup_emulator(self):
        # init register values
        for r in self.machine.initregs:
            regval = self.controller.get_reg_value(r, True)
            regnum = self.machine.get_reg_id(r)
            self.emu.reg_write(regnum, regval)

        mappings = self.machine.get_mappings()
        for m in mappings:
            self.emu.mem_map(m.start, m.size, unicorn.UC_PROT_ALL)
            bs = self.machine.read_memory(m.start, m.size)
            self.emu.mem_write(m.start, bs)
        self.emu.hook_add(unicorn.UC_HOOK_MEM_WRITE,
                          self.write_hook)
        self.emu.hook_add(unicorn.UC_HOOK_CODE,
                          self.i_hook)
        self.emu.hook_add(unicorn.UC_HOOK_MEM_READ_UNMAPPED |
                          unicorn.UC_HOOK_MEM_WRITE_UNMAPPED,
                            self.hook_mem_invalid)
        self.machine.hook_syscall(self.emu, self.hook_syscall) 
Example #3
Source File: qemu.py    From Sibyl with GNU General Public License v3.0 5 votes vote down vote up
def verbose_mode(self):
        self.mu.hook_add(unicorn.UC_HOOK_MEM_READ_UNMAPPED, self.hook_mem_invalid)
        self.mu.hook_add(unicorn.UC_HOOK_CODE, self.hook_code) 
Example #4
Source File: emulator.py    From cemu with MIT License 5 votes vote down vote up
def create_new_vm(self) -> None:
        """
        Create a new VM, and sets up the hooks
        """
        arch, mode, endian = get_arch_mode("unicorn", self.root.arch)
        self.vm = unicorn.Uc(arch, mode | endian)
        self.vm.hook_add(unicorn.UC_HOOK_BLOCK, self.hook_block)
        self.vm.hook_add(unicorn.UC_HOOK_CODE, self.hook_code)
        self.vm.hook_add(unicorn.UC_HOOK_INTR, self.hook_interrupt)
        self.vm.hook_add(unicorn.UC_HOOK_MEM_WRITE, self.hook_mem_access)
        self.vm.hook_add(unicorn.UC_HOOK_MEM_READ, self.hook_mem_access)
        if is_x86(self.root.arch):
            self.vm.hook_add(unicorn.UC_HOOK_INSN, self.hook_syscall, None, 1, 0, unicorn.x86_const.UC_X86_INS_SYSCALL)
        return 
Example #5
Source File: unicorn_protocol.py    From avatar2 with Apache License 2.0 5 votes vote down vote up
def set_breakpoint(self, line, hardware=True, temporary=False, regex=False, condition=None,
                       ignore_count=0, thread=0):
        """Insert a breakpoint.

        :param line:         address to break at
        :param hardware:     whether this breakpoint is hardware (ignored, always True)
        :param temporary:    whether this breakpoint is temporary (one shot)
        :param regex:        not supported
        :param condition:    not supported
        :param ignore_count: amount of times the breakpoint should be ignored before firing
        :param thread:       not supported
        :return: breakpoint number
        """
        if not hardware:
            self.log.warning('Software breakpoints are not supported, falling back to hardware')
        if regex:
            self.log.warning('Regex breakpoints are not supported, ignoring regex')
        if condition is not None:
            self.log.warning('Conditional breakpoints are not supported, ignoring condition')
        if thread:
            self.log.warning('Thread-specific breakpoints are not supported, ignoring thread')
        # TODO line <-> addr
        bkptno = len(self._breakpoints)
        hook = self.uc.hook_add(unicorn.UC_HOOK_CODE, self._breakpoint_hook, begin=line,
                                end=line, user_data=bkptno)
        self._breakpoints.append(UnicornBreakpoint(hooks=[hook], temporary=temporary,
                                                   ignore_count=ignore_count))
        return bkptno