def run_emulator_tests(self, arch, tests): emu = arch.getEmulator() self.setEmuDefaults(emu) for test in tests: byts = test['bytes'] try: op = arch.archParseOpcode(binascii.unhexlify(byts), 0, 0x40) except envi.InvalidInstruction: self.fail('Failed to parse opcode bytes: %s' % byts) with emu.snap(): # do any required setup for name, valu in test['setup'][0].items(): emu.setRegisterByName(name, valu) for expr, valu in test['setup'][1].items(): addr = e_expr.evaluate(expr, emu.getRegisters()) valu = emu.writeMemory(addr, valu) # run the emulator instruction emu.executeOpcode(op) # test both the registers and stack values for name, valu in test['tests'][0].items(): reg = emu.getRegisterByName(name) self.assertEqual(reg, valu, msg='(reg: %r) Given != Got for %s (%s)' % (name, byts, str(op))) for expr, valu in test['tests'][1].items(): addr = e_expr.evaluate(expr, emu.getRegisters()) mem = emu.readMemory(addr, len(valu)) self.assertEqual(mem, valu, msg='(mem: 0x%x) %r != %r' % (addr, mem, valu))
def test_envi_expr_evaluate(self): import envi.expression as e_expr x={'foo(bar)': 0x40, 'foo':0x60} self.assertEqual(e_expr.evaluate('foo(bar) + 1', x), 65) self.assertEqual(e_expr.evaluate('foo + 1', x), 97) self.assertEqual(e_expr.evaluate('32 + 1', x), 33)
def parseExpression(emu, expr, lcls={}): ''' localized updated expression parser for the emulator at any state ''' lcls.update(emu.vw.getExpressionLocals()) lcls.update(emu.getRegisters()) return e_expr.evaluate(expr, lcls)
def parseExpression(self, expression): """ Parse a python expression with many useful helpers mapped into the execution namespace. Example: trace.parseExpression("ispoi(ecx+ntdll.RtlAllocateHeap)") """ locs = VtraceExpressionLocals(self) return long(e_expr.evaluate(expression, locs))
def do_funcmeta(self, line): """ Show/Set function metadata. Usage: funcmeta <func_expr> [key <value_expr>] """ # FIXME make a search thing here! argv = e_cli.splitargs(line) if len(argv) == 0: return self.do_help("funcmeta") if len(argv) == 1: va = self.parseExpression(argv[0]) meta = self.getFunctionMetaDict(va) self.vprint(pprint.pformat(meta)) elif len(argv) == 3: va = self.parseExpression(argv[0]) name = argv[1] locs = self.getExpressionLocals() val = e_expr.evaluate(argv[2], locs) self.setFunctionMeta(va, name, val)
def parseExpression(self, expr): return long(e_expr.evaluate(expr, self.getExpressionLocals()))
def parseExpression(self, expr): l = self.getExpressionLocals() return long(e_expr.evaluate(expr, l))
def parseExpression(self, expr): l = self.getExpressionLocals() return int(e_expr.evaluate(expr, l))