diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2013-01-26 19:41:04 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2013-01-26 19:41:04 (GMT) |
commit | 2050b07c42c15738662dd9b3c5841694b64ab2a3 (patch) | |
tree | f6283df4b4775f0c4e42e14025d67443f8fdf9b5 /plugins/python/samples | |
parent | b0b35292cb22899b1b23556be452eb827e4010d7 (diff) |
Provided some debug helpers as plugin samples.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@330 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'plugins/python/samples')
-rw-r--r-- | plugins/python/samples/Makefile.am | 7 | ||||
-rw-r--r-- | plugins/python/samples/__init__.py | 2 | ||||
-rw-r--r-- | plugins/python/samples/basic_blocks.py | 80 | ||||
-rw-r--r-- | plugins/python/samples/demo.py | 30 |
4 files changed, 119 insertions, 0 deletions
diff --git a/plugins/python/samples/Makefile.am b/plugins/python/samples/Makefile.am new file mode 100644 index 0000000..38ab6c8 --- /dev/null +++ b/plugins/python/samples/Makefile.am @@ -0,0 +1,7 @@ + +samplesdir = $(datadir)/openida/plugins/python/samples + +samples_DATA = \ + __init__.py \ + basic_blocks.py \ + demo.py diff --git a/plugins/python/samples/__init__.py b/plugins/python/samples/__init__.py new file mode 100644 index 0000000..7186d48 --- /dev/null +++ b/plugins/python/samples/__init__.py @@ -0,0 +1,2 @@ + +from demo import Demo as samples diff --git a/plugins/python/samples/basic_blocks.py b/plugins/python/samples/basic_blocks.py new file mode 100644 index 0000000..583723c --- /dev/null +++ b/plugins/python/samples/basic_blocks.py @@ -0,0 +1,80 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import re +from pychrysalide.analysis import InstrBlock +from pychrysalide.analysis.blocks import FlowBlock + + +class VisitIndent: + + def __init__(self): + self.offset = 0 + + def get_padding(self): + return ' ' * self.offset + + def inc_offset(self): + self.offset = self.offset + 1 + + def dec_offset(self): + self.offset = self.offset - 1 + + +def get_c_address_of_pygobject(obj): + """Parse the string representation of a given object and return its memory address.""" + + ret = re.match('.*(0x[0-9a-f]+)\)>', str(obj), re.I) + + if ret == None: + result = '???' + else: + result = ret.group(1) + + return result + + +def visit_block(block, order, indent): + """Describe each visited basic block.""" + + padding = indent.get_padding() + addr = get_c_address_of_pygobject(block) + + if isinstance(block, FlowBlock): + + start, end = block.boundary_addresses + links = block.get_links_block() + + if links != None: + laddr = get_c_address_of_pygobject(links) + print '%s- flow %s : 0x%08lx -> 0x%08lx (links = %s)' % (padding, addr, start, end, laddr) + + else: + print '%s- flow %s : 0x%08lx -> 0x%08lx' % (padding, addr, start, end) + + else: + + if order != InstrBlock.BVO_OUT: + + print '%s- virtual %s' % (padding, addr) + indent.inc_offset() + + else: + indent.dec_offset() + + return True + + +def show_basic_blocks(binary): + """Print the tree of all basic blocks for each routine of a given binary.""" + + fmt = binary.get_format() + indent = VisitIndent() + + for r in fmt.routines: + + print '==== %s ====' % str(r) + + r.basic_blocks.visit(visit_block, indent) + + print diff --git a/plugins/python/samples/demo.py b/plugins/python/samples/demo.py new file mode 100644 index 0000000..c406231 --- /dev/null +++ b/plugins/python/samples/demo.py @@ -0,0 +1,30 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from pychrysalide import Plugin +from pychrysalide.gui.panels import LogPanel + +from basic_blocks import show_basic_blocks + + +class Demo(Plugin): + """Demonstration plugin.""" + + def init(self, ref): + """Initialize the plugin.""" + + LogPanel.log_message(LogPanel.LMT_WARNING, 'Welcome to the demo Python plugin !') + + return True + + + def get_action(self): + """Register the plugin for given actions.""" + + return Plugin.PGA_BINARY_GROUPED + + + def execute_on_binary(self, binary, action): + """Process registered actions.""" + + show_basic_blocks(binary) |