Package envi :: Module expression
[hide private]
[frames] | no frames]

Source Code for Module envi.expression

 1  """
 
 2  Unified expression helpers.
 
 3  """ 
 4  
 
5 -def evaluate(pycode, locals):
6 return eval(pycode, {}, locals)
7
8 -class ExpressionLocals(dict):
9 """ 10 An object to act as the locals dictionary for the evaluation 11 of envi expressions. You may pass in an envi.resolver.SymbolResolver 12 object to automagically use symbols in your expressions. 13 """
14 - def __init__(self, symobj=None):
15 dict.__init__(self) 16 self.symobj = symobj
17
18 - def __getitem__(self, name):
19 if self.symobj != None: 20 ret = self.symobj.getSymByName(name) 21 if ret != None: return ret 22 return dict.__getitem__(self, name)
23
24 -class MemoryExpressionLocals(ExpressionLocals):
25
26 - def __init__(self, memobj, symobj=None):
27 ExpressionLocals.__init__(self, symobj=symobj) 28 self.memobj = memobj 29 self.update({ 30 'mapbase':self.mapbase, 31 'maplen':self.maplen, 32 'ispoi':self.ispoi, 33 'mem':self.mem, 34 'poi':self.poi, 35 'sym':self.sym, 36 })
37
38 - def sym(self, symstr):
39 ''' 40 An easy to use utility for symbols which have un-pythonic names. 41 42 Example x = sym('kernel32.??2@$$FYAPAXI@Z') 43 ''' 44 return long(evaluate(symstr, self))
45
46 - def mapbase(self, address):
47 """ 48 The expression mapbase(address) returns the base address of the 49 memory mapped area containing "address" 50 """ 51 map = self.memobj.getMemoryMap(address) 52 if not map: 53 raise Exception("ERROR - un-mapped address in mapbase()") 54 return map[0]
55
56 - def maplen(self, address):
57 """ 58 The expression maplen(address) returns the length of the 59 memory mapped area containing "address". 60 """ 61 map = self.memobj.getMemoryMap(address) 62 if not map: 63 raise Exception("ERROR - un-mapped address in maplen()") 64 return map[1]
65
66 - def ispoi(self, addr):
67 """ 68 The expression ispoi(value) returns True if the specified value 69 is a valid pointer. Otherwise, False. 70 """ 71 return self.memobj.isValidPointer(addr)
72
73 - def mem(self, addr, size):
74 """ 75 Read and return memory. 76 77 Example: mem(ecx, 20) 78 """ 79 return self.memobj.readMemory(addr, size)
80
81 - def poi(self, address):
82 """ 83 When expression contains "poi(addr)" this will return 84 the address pointed to by addr. 85 """ 86 return self.memobj.readMemoryFormat(address, "P")[0]
87