From 652b830e5cb46ed2677e7639462f597b8e5f47c4 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Thu, 7 Sep 2017 22:35:34 -0400 Subject: Add some convenience api's for accessing strings, and iterating over all basic blocks and instructions --- python/binaryview.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'python/binaryview.py') diff --git a/python/binaryview.py b/python/binaryview.py index d002b38f..19af6264 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -88,10 +88,15 @@ class BinaryDataNotification(object): class StringReference(object): - def __init__(self, string_type, start, length): + def __init__(self, bv, string_type, start, length): self.type = string_type self.start = start self.length = length + self.view = bv + + @property + def value(self): + return self.view.read(self.start, self.length) def __repr__(self): return "<%s: %#x, len %#x>" % (self.type, self.start, self.length) @@ -659,6 +664,50 @@ class BinaryView(object): """ _BinaryViewAssociatedDataStore.set_default(name, value) + @property + def basic_blocks(self): + """A generator of all BasicBlock objects in the BinaryView""" + for func in self: + for block in func.basic_blocks: + yield block + + @property + def llil_basic_blocks(self): + """A generator of all LowLevelILBasicBlock objects in the BinaryView""" + for func in self: + for il_block in func.low_level_il.basic_blocks: + yield il_block + + @property + def mlil_basic_blocks(self): + """A generator of all MediumLevelILBasicBlock objects in the BinaryView""" + for func in self: + for il_block in func.medium_level_il.basic_blocks: + yield il_block + + @property + def instructions(self): + """A generator of instruction tokens and their start addresses""" + for block in self.basic_blocks: + start = block.start + for i in block: + yield (i[0], start) + start += i[1] + + @property + def llil_instructions(self): + """A generator of llil instructions""" + for block in self.llil_basic_blocks: + for i in block: + yield i + + @property + def mlil_instructions(self): + """A generator of mlil instructions""" + for block in self.mlil_basic_blocks: + for i in block: + yield i + def __del__(self): for i in self.notifications.values(): i._unregister() @@ -2601,7 +2650,7 @@ class BinaryView(object): strings = core.BNGetStringsInRange(self.handle, start, length, count) result = [] for i in xrange(0, count.value): - result.append(StringReference(StringType(strings[i].type), strings[i].start, strings[i].length)) + result.append(StringReference(self, StringType(strings[i].type), strings[i].start, strings[i].length)) core.BNFreeStringReferenceList(strings) return result -- cgit v1.3.1 From eecc63abf9c528ab209d56fef62cb3be7518f4e2 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Thu, 21 Sep 2017 16:27:04 -0400 Subject: Add segment permission properties --- python/binaryview.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'python/binaryview.py') diff --git a/python/binaryview.py b/python/binaryview.py index 19af6264..9304d412 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -412,6 +412,18 @@ class Segment(object): self.data_length = data_length self.flags = flags + @property + def executable(self): + return (self.flags & SegmentFlag.SegmentExecutable) != 0 + + @property + def writable(self): + return (self.flags & SegmentFlag.SegmentWritable) != 0 + + @property + def readable(self): + return (self.flags & SegmentFlag.SegmentReadable) != 0 + @property def end(self): return self.start + self.length @@ -421,9 +433,9 @@ class Segment(object): def __repr__(self): return "" % (self.start, self.end, - "r" if (self.flags & SegmentFlag.SegmentReadable) != 0 else "-", - "w" if (self.flags & SegmentFlag.SegmentWritable) != 0 else "-", - "x" if (self.flags & SegmentFlag.SegmentExecutable) != 0 else "-") + "r" if self.readable else "-", + "w" if self.writable else "-", + "x" if self.executable else "-") class Section(object): -- cgit v1.3.1