def get_ir(self, instr): args = instr.args instr_ir, extra_ir = get_mnemo_expr(self, instr, *args) for i, x in enumerate(instr_ir): x = m2_expr.ExprAff(x.dst, x.src.replace_expr( {self.pc: m2_expr.ExprInt32(instr.offset + 4)})) instr_ir[i] = x for b in extra_ir: for irs in b.irs: for i, x in enumerate(irs): x = m2_expr.ExprAff(x.dst, x.src.replace_expr( {self.pc: m2_expr.ExprInt32(instr.offset + 4)})) irs[i] = x return instr_ir, extra_ir
def emit_syscall(ir, instr): e = [] # EXCEPT_PRIV_INSNの設定 e.append( expr.ExprAff(regs.exception_flags, expr.ExprInt32(sem.EXCEPT_PRIV_INSN))) # syscallの次命令のポインタを保存 e.append( expr.ExprAff(expr.ExprMem(expr.ExprInt64(ADDR_SYSCALL_NEXTIP), 64), expr.ExprId(ir.get_next_label(instr), instr.mode))) return e, []
def ast_int2expr(a): return m2_expr.ExprInt32(a)
class imm32_noarg(object): int2expr = lambda self, x: m2_expr.ExprInt32(x)
def test_Variables_Identifier(self): import miasm2.expression.expression as m2_expr from miasm2.expression.expression_helper import Variables_Identifier # Build a complex expression cst = m2_expr.ExprInt16(0x100) eax = m2_expr.ExprId("EAX") ebx = m2_expr.ExprId("EBX") ax = eax[0:16] expr = eax + ebx expr = m2_expr.ExprCompose(ax, expr[16:32]) expr2 = m2_expr.ExprMem((eax + ebx) ^ (eax), size=16) expr2 = expr2 | ax | expr2 | cst exprf = expr - expr + m2_expr.ExprCompose(expr2, cst) # Identify variables vi = Variables_Identifier(exprf) # Use __str__ print vi # Test the result new_expr = vi.equation ## Force replace in the variable dependency order for var_id, var_value in reversed(vi.vars.items()): new_expr = new_expr.replace_expr({var_id: var_value}) self.assertEqual(exprf, new_expr) # Test prefix vi = Variables_Identifier(exprf, var_prefix="prefix_v") ## Use __str__ print vi ## Test the result new_expr = vi.equation ### Force replace in the variable dependency order for var_id, var_value in reversed(vi.vars.items()): new_expr = new_expr.replace_expr({var_id: var_value}) self.assertEqual(exprf, new_expr) # Test an identify on an expression already containing identifier vi = Variables_Identifier(exprf) vi2 = Variables_Identifier(vi.equation) ## Test the result new_expr = vi2.equation ### Force replace in the variable dependency order for var_id, var_value in reversed(vi2.vars.items()): new_expr = new_expr.replace_expr({var_id: var_value}) self.assertEqual(vi.equation, new_expr) ## Corner case: each sub var depends on itself mem1 = m2_expr.ExprMem(ebx, size=32) mem2 = m2_expr.ExprMem(mem1, size=32) cst2 = m2_expr.ExprInt32(-1) expr_mini = ((eax ^ mem2 ^ cst2) & (mem2 ^ (eax + mem2)))[31:32] ## Build vi = Variables_Identifier(expr_mini) vi2 = Variables_Identifier(vi.equation) ## Test the result new_expr = vi2.equation ### Force replace in the variable dependency order for var_id, var_value in reversed(vi2.vars.items()): new_expr = new_expr.replace_expr({var_id: var_value}) self.assertEqual(vi.equation, new_expr)
def sllv(ir, instr, a, b, c): """Shifts a register value @b left by the amount specified in @c and places the value in the destination register @a. Zeroes are shifted in.""" e = [] e.append(m2_expr.ExprAff(a, b << (c & m2_expr.ExprInt32(0x1F)))) return e, []
def srav(ir, instr, a, b, c): e = [] value = m2_expr.ExprOp('a>>', b, c&m2_expr.ExprInt32(0x1F)) e.append(m2_expr.ExprAff(a, value)) return e, []
def nor(ir, instr, a, b, c): """Bitwise logical Nors two registers @b, @c and stores the result in a register @a""" e = [] e.append(m2_expr.ExprAff(a, (b|c)^m2_expr.ExprInt32(0xFFFFFFFF))) return e, []