summaryrefslogtreecommitdiff
path: root/python/examples
diff options
context:
space:
mode:
Diffstat (limited to 'python/examples')
-rw-r--r--python/examples/README.md39
-rw-r--r--python/examples/arm-syscall.py21
-rw-r--r--python/examples/bin-info.py37
-rw-r--r--python/examples/breakpoint.py16
4 files changed, 113 insertions, 0 deletions
diff --git a/python/examples/README.md b/python/examples/README.md
new file mode 100644
index 00000000..4ad4ac03
--- /dev/null
+++ b/python/examples/README.md
@@ -0,0 +1,39 @@
+# Binary Ninja Python API Examples
+
+The following examples demonstrate the Binary Ninja API. They include both stand-alone examples that directly call into the core, as well as examples meant to be loaded as plugins.
+
+## Stand-alone
+
+* bin-info.py - general binary information
+* arm-syscall.py - extract syscall numbers from IL for arm Mach-O files
+
+To use the stand-alone Python examples, make sure your `PYTHON_PATH` includes the API, like:
+
+```
+PYTHONPATH=$PYTHONPATH:/Applications/Binary\ Ninja.app/Contents/Resources/python
+```
+
+## GUI Plugins
+
+* nes.py - 6502 CPU architecture including LLIL lifting and `.NES` file format parser
+* breakpoint.py - small example showing how to modify a file and register a GUI menu item
+
+Plugins are meant to be loaded into a running Binary Ninja GUI and should either be copied or symlinked into the appropriate plugin folder. You'll need to then re-start Binary Ninja.
+
+### OSX
+
+```
+~/Library/Application Support/Binary Ninja/plugins
+```
+
+### Windows
+
+```
+%APPDATA%\Binary Ninja\plugins
+```
+
+### Linux
+
+```
+~/.binaryninja/plugins
+```
diff --git a/python/examples/arm-syscall.py b/python/examples/arm-syscall.py
new file mode 100644
index 00000000..cbd8f4bf
--- /dev/null
+++ b/python/examples/arm-syscall.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+"""
+ Thanks to @theqlabs from arm.ninja for the nice writeup and idea for this plugin:
+ http://arm.ninja/2016/03/08/intro-to-binary-ninja-api/
+"""
+import sys, binaryninja, time
+if len(sys.argv) > 1:
+ target = sys.argv[1]
+else:
+ raise ValueError("Missing argument to binary.")
+
+bv = binaryninja.BinaryViewType["Mach-O"].open(target)
+bv.update_analysis()
+
+"""Until update_analysis_and_wait is complete, sleep is necessary as the analysis is multi-threaded."""
+time.sleep(5)
+
+for func in bv.functions:
+ for il in func.low_level_il:
+ if il.operation == core.LLIL_SYSCALL:
+ print "System call address: %x - %d" % (il.address, func.get_reg_value_at_low_level_il_instruction(il.address, bv.platform.system_call_convention.int_arg_regs[0]).value)
diff --git a/python/examples/bin-info.py b/python/examples/bin-info.py
new file mode 100644
index 00000000..6ec00bce
--- /dev/null
+++ b/python/examples/bin-info.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+import sys, binaryninja, 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 --------"
+
+for func in bv.functions:
+ print func.symbol.name
+
+
+print "\n-------- First 10 strings --------"
+
+for i in xrange(10):
+ start = bv.strings[i].start
+ length = bv.strings[i].length
+ string = bv.read(start,length)
+ print "0x%x (%d):\t%s" % (start, length, string)
diff --git a/python/examples/breakpoint.py b/python/examples/breakpoint.py
new file mode 100644
index 00000000..44df19e2
--- /dev/null
+++ b/python/examples/breakpoint.py
@@ -0,0 +1,16 @@
+from binaryninja import *
+
+def write_breakpoint(view, start, length):
+ """Sample function to show registering a plugin menu item for a range of bytes. Also possible:
+ register
+ register_for_address
+ register_for_function
+ """
+ if view.arch.name.startswith("x86"):
+ view.write(start, "\xcc" * length)
+ elif view.arch.name == "armv7":
+ view.write(start, "\x7a\x00\x20\xe1" * (length/4))
+ else:
+ log_error("No support for breakpoint on %s" % view.arch.name)
+
+PluginCommand.register_for_range("Convert to breakpoint", "Fill region with breakpoint instructions.", write_breakpoint)