diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/__init__.py | 93 | ||||
| -rw-r--r-- | python/examples/instruction-iterator.py | 52 |
2 files changed, 118 insertions, 27 deletions
diff --git a/python/__init__.py b/python/__init__.py index ebaf030e..61b22472 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -2157,34 +2157,12 @@ class Function(object): core.BNGetFunctionLowLevelIL(self.handle)), self) @property - def low_level_il_basic_blocks(self): - """Function low level IL basic blocks (read-only)""" - count = ctypes.c_ulonglong() - blocks = core.BNGetFunctionLowLevelILBasicBlockList(self.handle, count) - result = [] - for i in xrange(0, count.value): - result.append(BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i]))) - core.BNFreeBasicBlockList(blocks, count.value) - return result - - @property def lifted_il(self): """Function lifted IL (read-only)""" return LowLevelILFunction(self.arch, core.BNNewLowLevelILFunctionReference( core.BNGetFunctionLiftedIL(self.handle)), self) @property - def lifted_il_basic_blocks(self): - """Function lifted IL basic blocks (read-only)""" - count = ctypes.c_ulonglong() - blocks = core.BNGetFunctionLiftedILBasicBlockList(self.handle, count) - result = [] - for i in xrange(0, count.value): - result.append(BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i]))) - core.BNFreeBasicBlockList(blocks, count.value) - return result - - @property def type(self): """Function type (read-only)""" return Type(core.BNGetFunctionType(self.handle)) @@ -2212,6 +2190,14 @@ class Function(object): core.BNFreeIndirectBranchList(branches) return result + def __iter__(self): + count = ctypes.c_ulonglong() + blocks = core.BNGetFunctionBasicBlockList(self.handle, count) + for i in xrange(0, count.value): + yield BasicBlock(self._view, core.BNNewBasicBlockReference(blocks[i])) + core.BNFreeBasicBlockList(blocks, count.value) + raise StopIteration + def __setattr__(self, name, value): try: object.__setattr__(self,name,value) @@ -2294,7 +2280,7 @@ class Function(object): result = [] for i in xrange(0, count.value): result.append(StackVariableReference(refs[i].sourceOperand, Type(core.BNNewTypeReference(refs[i].type)), - refs[i].name, refs[i].startingOffset, refs[i].referencedOffset)) + refs[i].name, refs[i].startingOffset, refs[i].referencedOffset)) core.BNFreeStackVariableReferenceList(refs, count.value) return result @@ -2465,9 +2451,33 @@ class BasicBlock(object): else: return "<block: 0x%x-0x%x>" % (self.start, self.end) + def __iter__(self): + start = self.start + end = self.end + + idx = start + while idx < end: + data = self.view.read(idx, 16) + inst_info = self.view.arch.get_instruction_info(data, idx) + inst_text = self.view.arch.get_instruction_text(data, idx) + + yield inst_text + idx += inst_info.length + raise StopIteration + def mark_recent_use(): core.BNMarkBasicBlockAsRecentlyUsed(self.handle) +class LowLevelILBasicBlock(BasicBlock): + def __init__(self, view, handle, owner): + super(LowLevelILBasicBlock, self).__init__(view, handle) + self.il_function = owner + + def __iter__(self): + for idx in xrange(self.start, self.end): + yield self.il_function[idx] + raise StopIteration + class FunctionGraphTextLine: def __init__(self, addr, tokens): self.address = addr @@ -2852,7 +2862,7 @@ class Architecture(object): info = core.BNGetArchitectureRegisterInfo(self.handle, regs[i]) full_width_reg = core.BNGetArchitectureRegisterName(self.handle, info.fullWidthRegister) self.regs[name] = RegisterInfo(full_width_reg, info.size, info.offset, - core.BNImplicitRegisterExtend_names[info.extend], regs[i]) + core.BNImplicitRegisterExtend_names[info.extend], regs[i]) core.BNFreeRegisterList(regs) count = ctypes.c_ulonglong() @@ -3962,7 +3972,7 @@ class LowLevelILFunction(object): if handle is not None: self.handle = core.handle_of_type(handle, core.BNLowLevelILFunction) else: - self.handle = core.BNCreateLowLevelILFunction() + self.handle = core.BNCreateLowLevelILFunction(arch.handle) def __del__(self): core.BNFreeLowLevelILFunction(self.handle) @@ -3985,6 +3995,20 @@ class LowLevelILFunction(object): """Number of temporary flags (read-only)""" return core.BNGetLowLevelILTemporaryFlagCount(self.handle) + @property + def basic_blocks(self): + """Low level IL basic blocks (read-only)""" + count = ctypes.c_ulonglong() + blocks = core.BNGetLowLevelILBasicBlockList(self.handle, count) + result = [] + view = None + if self.source_function is not None: + view = self.source_function.view + for i in xrange(0, count.value): + result.append(LowLevelILBasicBlock(view, core.BNNewBasicBlockReference(blocks[i]), self)) + core.BNFreeBasicBlockList(blocks, count.value) + return result + def __setattr__(self, name, value): try: object.__setattr__(self,name,value) @@ -4006,6 +4030,18 @@ class LowLevelILFunction(object): def __setitem__(self, i): raise IndexError, "instruction modification not implemented" + def __iter__(self): + count = ctypes.c_ulonglong() + blocks = core.BNGetLowLevelILBasicBlockList(self.handle, count) + result = [] + view = None + if self.source_function is not None: + view = self.source_function.view + for i in xrange(0, count.value): + yield LowLevelILBasicBlock(view, core.BNNewBasicBlockReference(blocks[i]), self) + core.BNFreeBasicBlockList(blocks, count.value) + raise StopIteration + def clear_indirect_branches(self): core.BNLowLevelILClearIndirectBranches(self.handle) @@ -4252,8 +4288,11 @@ class LowLevelILFunction(object): core.BNLowLevelILSetExprSourceOperand(self.handle, expr.index, n) return expr - def finalize(self): - core.BNFinalizeLowLevelILFunction(self.handle) + def finalize(self, func = None): + if func is None: + core.BNFinalizeLowLevelILFunction(self.handle, None) + else: + core.BNFinalizeLowLevelILFunction(self.handle, func.handle) def add_label_for_address(self, arch, addr): if arch is not None: diff --git a/python/examples/instruction-iterator.py b/python/examples/instruction-iterator.py new file mode 100644 index 00000000..7b03b095 --- /dev/null +++ b/python/examples/instruction-iterator.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +import sys +try: + import binaryninja +except ImportError: + sys.path.append("/Applications/Binary Ninja.app/Contents/Resources/python/") + import binaryninja +import time + +if sys.platform.lower().startswith("linux"): + bintype="ELF" +elif sys.platform.lower() == "darwin": + bintype="Mach-O" +else: + raise Exception, "%s is not supported on this plugin" % sys.platform + +if len(sys.argv) > 1: + target = sys.argv[1] +else: + target = "/bin/ls" + +bv = binaryninja.BinaryViewType[bintype].open(target) +bv.update_analysis() + +"""Until update_analysis_and_wait is complete, sleep is necessary as the analysis is multi-threaded.""" +time.sleep(1) + +print "-------- %s --------" % target +print "START: 0x%x" % bv.start +print "ENTRY: 0x%x" % bv.entry_point +print "ARCH: %s" % bv.arch.name +print "\n-------- Function List --------" + +""" print all the functions, their basic blocks, and their il instructions """ +for func in bv.functions: + print repr(func) + for block in func.low_level_il: + print "\t{0}".format(block) + + for insn in block: + print "\t\t{0}".format(insn) + + +""" print all the functions, their basic blocks, and their mc instructions """ +for func in bv.functions: + print repr(func) + for block in func: + print "\t{0}".format(block) + + for insn in block: + print "\t\t{0}".format(insn) |
