summaryrefslogtreecommitdiff
path: root/python/examples
diff options
context:
space:
mode:
authorRusty Wagner <rusty@vector35.com>2017-06-23 18:36:37 -0400
committerRusty Wagner <rusty@vector35.com>2017-06-23 18:36:37 -0400
commitc51f3bed6bdef577253feee0e85a71fda1931bd7 (patch)
tree1cf26ff28e0b9bfb6bfbed044ba7ca520d63f862 /python/examples
parent4b988c0d24c8d8c8dc67485f3aaeb7106eb4af18 (diff)
parentcca0fe6ea60eb7f6b35cc433a6fff96cc65b3ff8 (diff)
Merge branch 'dev'
Diffstat (limited to 'python/examples')
-rw-r--r--python/examples/angr_plugin.py2
-rw-r--r--python/examples/bin_info.py3
-rw-r--r--python/examples/breakpoint.py2
-rwxr-xr-xpython/examples/export_svg.py27
-rw-r--r--python/examples/instruction_iterator.py2
-rw-r--r--python/examples/jump_table.py2
-rw-r--r--python/examples/nds.py2
-rw-r--r--python/examples/nes.py51
-rw-r--r--python/examples/notification_callbacks.py51
-rw-r--r--python/examples/nsf.py2
-rw-r--r--python/examples/print_syscalls.py2
-rw-r--r--python/examples/version_switcher.py2
12 files changed, 112 insertions, 36 deletions
diff --git a/python/examples/angr_plugin.py b/python/examples/angr_plugin.py
index 90217d65..c84373be 100644
--- a/python/examples/angr_plugin.py
+++ b/python/examples/angr_plugin.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
diff --git a/python/examples/bin_info.py b/python/examples/bin_info.py
index 4c4ab8fd..17a96685 100644
--- a/python/examples/bin_info.py
+++ b/python/examples/bin_info.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
@@ -38,7 +38,6 @@ def get_bininfo(bv):
sys.exit(1)
bv = BinaryViewType.get_view_of_file(filename)
- log.redirect_output_to_log()
log.log_to_stdout(True)
contents = "## %s ##\n" % bv.file.filename
diff --git a/python/examples/breakpoint.py b/python/examples/breakpoint.py
index b1297e26..a2801511 100644
--- a/python/examples/breakpoint.py
+++ b/python/examples/breakpoint.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
diff --git a/python/examples/export_svg.py b/python/examples/export_svg.py
index 89bc41a1..7814c9fd 100755
--- a/python/examples/export_svg.py
+++ b/python/examples/export_svg.py
@@ -1,6 +1,7 @@
# from binaryninja import *
import os
import webbrowser
+import time
try:
from urllib import pathname2url # Python 2.x
except:
@@ -34,7 +35,7 @@ def save_svg(bv, function):
outputfile = get_save_filename_input('File name for export_svg', 'HTML files (*.html)', filename)
if outputfile is None:
return
- content = render_svg(function)
+ content = render_svg(function, origname)
output = open(outputfile, 'w')
output.write(content)
output.close()
@@ -54,7 +55,7 @@ def instruction_data_flow(function, address):
return 'Opcode: {bytes}'.format(bytes=padded)
-def render_svg(function):
+def render_svg(function, origname):
graph = function.create_graph()
graph.layout_and_wait()
heightconst = 15
@@ -67,7 +68,13 @@ def render_svg(function):
@import url(https://fonts.googleapis.com/css?family=Source+Code+Pro);
body {
background-color: rgb(42, 42, 42);
+ color: rgb(220, 220, 220);
+ font-family: "Source Code Pro", "Lucida Console", "Consolas", monospace;
}
+ a, a:visited {
+ color: rgb(200, 200, 200);
+ font-weight: bold;
+ }
svg {
background-color: rgb(42, 42, 42);
display: block;
@@ -80,6 +87,10 @@ def render_svg(function):
fill: none;
stroke-width: 1px;
}
+ .back_edge {
+ fill: none;
+ stroke-width: 2px;
+ }
.UnconditionalBranch, .IndirectBranch {
stroke: rgb(128, 198, 233);
color: rgb(128, 198, 233);
@@ -97,7 +108,7 @@ def render_svg(function):
fill: currentColor;
}
text {
- font-family: 'Source Code Pro';
+ font-family: "Source Code Pro", "Lucida Console", "Consolas", monospace;
font-size: 9pt;
fill: rgb(224, 224, 224);
}
@@ -197,10 +208,16 @@ def render_svg(function):
points += str(x * widthconst) + "," + str(y * heightconst) + " "
x, y = edge.points[-1]
points += str(x * widthconst) + "," + str(y * heightconst + 0) + " "
- edges += ' <polyline class="edge {type}" points="{points}" marker-end="url(#arrow-{type})"/>\n'.format(type=BranchType(edge.type).name, points=points)
+ if edge.back_edge:
+ edges += ' <polyline class="back_edge {type}" points="{points}" marker-end="url(#arrow-{type})"/>\n'.format(type=BranchType(edge.type).name, points=points)
+ else:
+ edges += ' <polyline class="edge {type}" points="{points}" marker-end="url(#arrow-{type})"/>\n'.format(type=BranchType(edge.type).name, points=points)
output += ' ' + edges + '\n'
output += ' </g>\n'
- output += '</svg></html>'
+ output += '</svg>'
+
+ output += '<p>This CFG generated by <a href="https://binary.ninja/">Binary Ninja</a> from {filename} on {timestring}.</p>'.format(filename = origname, timestring = time.strftime("%c"))
+ output += '</html>'
return output
diff --git a/python/examples/instruction_iterator.py b/python/examples/instruction_iterator.py
index 7ff2d692..f55e1c1b 100644
--- a/python/examples/instruction_iterator.py
+++ b/python/examples/instruction_iterator.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
diff --git a/python/examples/jump_table.py b/python/examples/jump_table.py
index 439e2ab6..419cc188 100644
--- a/python/examples/jump_table.py
+++ b/python/examples/jump_table.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
diff --git a/python/examples/nds.py b/python/examples/nds.py
index ff137b4b..34d8a292 100644
--- a/python/examples/nds.py
+++ b/python/examples/nds.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
diff --git a/python/examples/nes.py b/python/examples/nes.py
index 4122cde0..e55a90b7 100644
--- a/python/examples/nes.py
+++ b/python/examples/nes.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
@@ -191,21 +191,21 @@ OperandTokens = [
def indirect_load(il, value):
if (value & 0xff) == 0xff:
- lo_addr = il.const(2, value)
- hi_addr = il.const(2, (value & 0xff00) | ((value + 1) & 0xff))
+ lo_addr = il.const_pointer(2, value)
+ hi_addr = il.const_pointer(2, (value & 0xff00) | ((value + 1) & 0xff))
lo = il.zero_extend(2, il.load(1, lo_addr))
hi = il.shift_left(2, il.zero_extend(2, il.load(1, hi_addr)), il.const(2, 8))
return il.or_expr(2, lo, hi)
- return il.load(2, il.const(2, value))
+ return il.load(2, il.const_pointer(2, value))
def load_zero_page_16(il, value):
if il[value].operation == LowLevelILOperation.LLIL_CONST:
- if il[value].value == 0xff:
- lo = il.zero_extend(2, il.load(1, il.const(2, 0xff)))
- hi = il.shift_left(2, il.zero_extend(2, il.load(1, il.const(2, 0)), il.const(2, 8)))
+ if il[value].constant == 0xff:
+ lo = il.zero_extend(2, il.load(1, il.const_pointer(2, 0xff)))
+ hi = il.shift_left(2, il.zero_extend(2, il.load(1, il.const_pointer(2, 0)), il.const(2, 8)))
return il.or_expr(2, lo, hi)
- return il.load(2, il.const(2, il[value].value))
+ return il.load(2, il.const_pointer(2, il[value].constant))
il.append(il.set_reg(1, LLIL_TEMP(0), value))
value = il.reg(1, LLIL_TEMP(0))
lo_addr = value
@@ -217,23 +217,23 @@ def load_zero_page_16(il, value):
OperandIL = [
lambda il, value: None, # NONE
- lambda il, value: il.load(1, il.const(2, value)), # ABS
+ lambda il, value: il.load(1, il.const_pointer(2, value)), # ABS
lambda il, value: il.const(2, value), # ABS_DEST
lambda il, value: il.load(1, il.add(2, il.const(2, value), il.zero_extend(2, il.reg(1, "x")))), # ABS_X
lambda il, value: il.add(2, il.const(2, value), il.zero_extend(2, il.reg(1, "x"))), # ABS_X_DEST
lambda il, value: il.load(1, il.add(2, il.const(2, value), il.zero_extend(2, il.reg(1, "y")))), # ABS_Y
lambda il, value: il.add(2, il.const(2, value), il.zero_extend(2, il.reg(1, "y"))), # ABS_Y_DEST
lambda il, value: il.reg(1, "a"), # ACCUM
- lambda il, value: il.const(2, value), # ADDR
+ lambda il, value: il.const_pointer(2, value), # ADDR
lambda il, value: il.const(1, value), # IMMED
lambda il, value: indirect_load(il, value), # IND
lambda il, value: il.load(1, load_zero_page_16(il, il.add(1, il.const(1, value), il.reg(1, "x")))), # IND_X
lambda il, value: load_zero_page_16(il, il.add(1, il.const(1, value), il.reg(1, "x"))), # IND_X_DEST
lambda il, value: il.load(1, il.add(2, load_zero_page_16(il, il.const(1, value)), il.reg(1, "y"))), # IND_Y
lambda il, value: il.add(2, load_zero_page_16(il, il.const(1, value)), il.reg(1, "y")), # IND_Y_DEST
- lambda il, value: il.const(2, value), # REL
- lambda il, value: il.load(1, il.const(2, value)), # ZERO
- lambda il, value: il.const(2, value), # ZERO_DEST
+ lambda il, value: il.const_pointer(2, value), # REL
+ lambda il, value: il.load(1, il.const_pointer(2, value)), # ZERO
+ lambda il, value: il.const_pointer(2, value), # ZERO_DEST
lambda il, value: il.load(1, il.zero_extend(2, il.add(1, il.const(1, value), il.reg(1, "x")))), # ZERO_X
lambda il, value: il.zero_extend(2, il.add(1, il.const(1, value), il.reg(1, "x"))), # ZERO_X_DEST
lambda il, value: il.load(1, il.zero_extend(2, il.add(1, il.const(1, value), il.reg(1, "y")))), # ZERO_Y
@@ -244,7 +244,7 @@ OperandIL = [
def cond_branch(il, cond, dest):
t = None
if il[dest].operation == LowLevelILOperation.LLIL_CONST:
- t = il.get_label_for_address(Architecture['6502'], il[dest].value)
+ t = il.get_label_for_address(Architecture['6502'], il[dest].constant)
if t is None:
t = LowLevelILLabel()
indirect = True
@@ -262,7 +262,7 @@ def cond_branch(il, cond, dest):
def jump(il, dest):
label = None
if il[dest].operation == LowLevelILOperation.LLIL_CONST:
- label = il.get_label_for_address(Architecture['6502'], il[dest].value)
+ label = il.get_label_for_address(Architecture['6502'], il[dest].constant)
if label is None:
il.append(il.jump(dest))
else:
@@ -300,7 +300,7 @@ def rti(il):
InstructionIL = {
- "adc": lambda il, operand: il.set_reg(1, "a", il.add_carry(1, il.reg(1, "a"), operand, flags = "*")),
+ "adc": lambda il, operand: il.set_reg(1, "a", il.add_carry(1, il.reg(1, "a"), operand, il.flag("c"), flags = "*")),
"asl": lambda il, operand: il.store(1, operand, il.shift_left(1, il.load(1, operand), il.const(1, 1), flags = "czs")),
"asl@": lambda il, operand: il.set_reg(1, "a", il.shift_left(1, operand, il.const(1, 1), flags = "czs")),
"and": lambda il, operand: il.set_reg(1, "a", il.and_expr(1, il.reg(1, "a"), operand, flags = "zs")),
@@ -341,13 +341,13 @@ InstructionIL = {
"php": lambda il, operand: il.push(1, get_p_value(il)),
"pla": lambda il, operand: il.set_reg(1, "a", il.pop(1), flags = "zs"),
"plp": lambda il, operand: set_p_value(il, il.pop(1)),
- "rol": lambda il, operand: il.store(1, operand, il.rotate_left_carry(1, il.load(1, operand), il.const(1, 1), flags = "czs")),
- "rol@": lambda il, operand: il.set_reg(1, "a", il.rotate_left_carry(1, il.reg(1, "a"), il.const(1, 1), flags = "czs")),
- "ror": lambda il, operand: il.store(1, operand, il.rotate_right_carry(1, il.load(1, operand), il.const(1, 1), flags = "czs")),
- "ror@": lambda il, operand: il.set_reg(1, "a", il.rotate_right_carry(1, il.reg(1, "a"), il.const(1, 1), flags = "czs")),
+ "rol": lambda il, operand: il.store(1, operand, il.rotate_left_carry(1, il.load(1, operand), il.const(1, 1), il.flag("c"), flags = "czs")),
+ "rol@": lambda il, operand: il.set_reg(1, "a", il.rotate_left_carry(1, il.reg(1, "a"), il.const(1, 1), il.flag("c"), flags = "czs")),
+ "ror": lambda il, operand: il.store(1, operand, il.rotate_right_carry(1, il.load(1, operand), il.const(1, 1), il.flag("c"), flags = "czs")),
+ "ror@": lambda il, operand: il.set_reg(1, "a", il.rotate_right_carry(1, il.reg(1, "a"), il.const(1, 1), il.flag("c"), flags = "czs")),
"rti": lambda il, operand: rti(il),
"rts": lambda il, operand: il.ret(il.add(2, il.pop(2), il.const(2, 1))),
- "sbc": lambda il, operand: il.set_reg(1, "a", il.sub_borrow(1, il.reg(1, "a"), operand, flags = "*")),
+ "sbc": lambda il, operand: il.set_reg(1, "a", il.sub_borrow(1, il.reg(1, "a"), operand, il.flag("c"), flags = "*")),
"sec": lambda il, operand: il.set_flag("c", il.const(0, 1)),
"sed": lambda il, operand: il.set_flag("d", il.const(0, 1)),
"sei": lambda il, operand: il.set_flag("i", il.const(0, 1)),
@@ -469,6 +469,15 @@ class M6502(Architecture):
return length
+ def perform_get_flag_write_low_level_il(self, op, size, write_type, flag, operands, il):
+ if flag == 'c':
+ if (op == LowLevelILOperation.LLIL_SUB) or (op == LowLevelILOperation.LLIL_SBB):
+ # Subtraction carry flag is inverted from the commom implementation
+ return il.not_expr(0, self.get_default_flag_write_low_level_il(op, size, FlagRole.CarryFlagRole, operands, il))
+ # Other operations use a normal carry flag
+ return self.get_default_flag_write_low_level_il(op, size, FlagRole.CarryFlagRole, operands, il)
+ return Architecture.perform_get_flag_write_low_level_il(self, op, size, write_type, flag, operands, il)
+
def perform_is_never_branch_patch_available(self, data, addr):
if (data[0] == "\x10") or (data[0] == "\x30") or (data[0] == "\x50") or (data[0] == "\x70") or (data[0] == "\x90") or (data[0] == "\xb0") or (data[0] == "\xd0") or (data[0] == "\xf0"):
return True
diff --git a/python/examples/notification_callbacks.py b/python/examples/notification_callbacks.py
new file mode 100644
index 00000000..b7c9cde9
--- /dev/null
+++ b/python/examples/notification_callbacks.py
@@ -0,0 +1,51 @@
+from binaryninja import BinaryDataNotification, PluginCommand, log_info
+import inspect
+
+def reg_notif(view):
+ demo_notification = DemoNotification(view)
+ view.register_notification(demo_notification)
+
+class DemoNotification(BinaryDataNotification):
+ def __init__(self, view):
+ self.view = view
+
+ def data_written(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def data_inserted(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def data_removed(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def function_added(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def function_removed(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def function_updated(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def data_var_added(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def data_var_updated(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def data_var_removed(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def string_found(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def string_removed(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def type_defined(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+ def type_undefined(self, *args):
+ log_info(inspect.stack()[0][3] + str(args))
+
+PluginCommand.register("Register Notification", "", reg_notif)
diff --git a/python/examples/nsf.py b/python/examples/nsf.py
index b1bac3a8..b0164065 100644
--- a/python/examples/nsf.py
+++ b/python/examples/nsf.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
diff --git a/python/examples/print_syscalls.py b/python/examples/print_syscalls.py
index 2af4d38d..d995ad1e 100644
--- a/python/examples/print_syscalls.py
+++ b/python/examples/print_syscalls.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
diff --git a/python/examples/version_switcher.py b/python/examples/version_switcher.py
index 9d5bbf05..3e1cab40 100644
--- a/python/examples/version_switcher.py
+++ b/python/examples/version_switcher.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-# Copyright (c) 2015-2016 Vector 35 LLC
+# Copyright (c) 2015-2017 Vector 35 LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to