def _run_code(self, asm_list, address, ctx_init): reil_instrs = self._asm_to_reil(asm_list, address) _, x86_ctx_out = pyasmjit.x86_64_execute("\n".join(asm_list), ctx_init) reil_ctx_out, _ = self.reil_emulator.execute(reil_instrs, start=address << 8, registers=ctx_init) return x86_ctx_out, reil_ctx_out
def __test_jcc(self, jmp_cond): untouched_value = 0x45454545 touched_value = 0x31313131 asm = [ "mov rax, 0x{:x}".format(untouched_value), "j" + jmp_cond + " {:s}", "mov rax, 0x{:x}".format(touched_value), "xchg rax, rax", ] asm_reil = list(asm) asm_reil[1] = asm_reil[1].format(str(0xdeadbeef + 0x3)) asm_pyasmjit = list(asm) asm_pyasmjit[1] = asm_pyasmjit[1].format("$+0x07") reil_instrs = self._asm_to_reil(asm_reil, 0xdeadbeef) ctx_init = self._init_context() _, x86_ctx_out = pyasmjit.x86_64_execute("\n".join(asm_pyasmjit), ctx_init) reil_ctx_out, _ = self.reil_emulator.execute( reil_instrs, start=0xdeadbeef << 8, registers=ctx_init ) cmp_result = self._compare_contexts(ctx_init, x86_ctx_out, reil_ctx_out) if not cmp_result: self._save_failing_context(ctx_init) self.assertTrue(cmp_result, self._print_contexts(ctx_init, x86_ctx_out, reil_ctx_out))
def test_add(self): code = """ add rax, rbx """ ctx_in = { 'rax': 0x1, 'rbx': 0x2, } rv, ctx_out = pyasmjit.x86_64_execute(code, ctx_in) self.assertEqual(0x3, ctx_out['rax'])
def test_cmpxchg(self): asm = ["cmpxchg ebx, ecx"] ctx_init = self._init_context() _, x86_ctx_out = pyasmjit.x86_64_execute("\n".join(asm), ctx_init) reil_ctx_out, _ = self.reil_emulator.execute( self._asm_to_reil(asm, 0xdeadbeef), start=0xdeadbeef << 8, end=(0xdeadbeef + 0x1) << 8, registers=ctx_init ) cmp_result = self._compare_contexts(ctx_init, x86_ctx_out, reil_ctx_out) if not cmp_result: self._save_failing_context(ctx_init) self.assertTrue(cmp_result, self._print_contexts(ctx_init, x86_ctx_out, reil_ctx_out))
def test_cmpxchg(self): asm = ["cmpxchg ebx, ecx"] ctx_init = self._init_context() _, x86_ctx_out = pyasmjit.x86_64_execute("\n".join(asm), ctx_init) reil_ctx_out, _ = self.reil_emulator.execute( self._asm_to_reil(asm, 0xdeadbeef), start=0xdeadbeef << 8, end=(0xdeadbeef + 0x1) << 8, registers=ctx_init) cmp_result = self._compare_contexts(ctx_init, x86_ctx_out, reil_ctx_out) if not cmp_result: self._save_failing_context(ctx_init) self.assertTrue( cmp_result, self._print_contexts(ctx_init, x86_ctx_out, reil_ctx_out))
def __test_jcc(self, jmp_cond): untouched_value = 0x45454545 touched_value = 0x31313131 asm = [ "mov rax, 0x{:x}".format(untouched_value), "j" + jmp_cond + " {:s}", "mov rax, 0x{:x}".format(touched_value), "xchg rax, rax", ] asm_reil = list(asm) asm_reil[1] = asm_reil[1].format(str(0xdeadbeef + 0x3)) asm_pyasmjit = list(asm) asm_pyasmjit[1] = asm_pyasmjit[1].format("$+0x07") reil_instrs = self._asm_to_reil(asm_reil, 0xdeadbeef) ctx_init = self._init_context() _, x86_ctx_out = pyasmjit.x86_64_execute("\n".join(asm_pyasmjit), ctx_init) reil_ctx_out, _ = self.reil_emulator.execute(reil_instrs, start=0xdeadbeef << 8, registers=ctx_init) cmp_result = self._compare_contexts(ctx_init, x86_ctx_out, reil_ctx_out) if not cmp_result: self._save_failing_context(ctx_init) self.assertTrue( cmp_result, self._print_contexts(ctx_init, x86_ctx_out, reil_ctx_out))