Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zznop committed Oct 19, 2019
0 parents commit 3954337
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2019 zznop

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# bn-brainfuck

## Description

Architecture module and loader for Brainfuck.

..No, I didn't have anything better to do.

![screenshot bn-brainfuck](screenshot.png)

218 changes: 218 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
from binaryninja import *
import re
import traceback


__author__ = 'zznop'
__copyright__ = 'Copyright 2019, zznop'
__license__ = 'GPL'
__version__ = '1.0'
__email__ = '[email protected]'


def cond_branch(il, cond, dest):
"""
Creates a llil conditional branch expression
:param il: LLIL object
:param cond: Flag condition
:param dest: Branch destination
"""

t = il.get_label_for_address(Architecture['Brainfuck'], il[dest].constant)
if t is None:
t = LowLevelILLabel()
f = il.get_label_for_address(Architecture['Brainfuck'], il.current_address+1)
if f is None:
f = LowLevelILLabel()
il.append(il.if_expr(cond, t, f))


class Brainfuck(Architecture):
"""
This class is responsible for disassembling and lifting Brainfuck code
"""

name = 'Brainfuck'
address_size = 1
default_int_size = 1
instr_alignment = 1
max_instr_length = 1
regs = {
'sp' : function.RegisterInfo('sp', 1), # Not used, but required
'cp' : function.RegisterInfo('cp', 1), # Cell pointer
}

stack_pointer = 'sp' # Not use, but required
node_starts = []
node_ends = []

flags = ['z']
flag_roles = { 'z' : FlagRole.ZeroFlagRole }
flags_required_for_flag_condition = { LowLevelILFlagCondition.LLFC_NE : ['z'] }
flag_write_types = ['z']
flags_written_by_flag_write_type = { 'z' : ['z'] }

Tokens = {
'+' : [
InstructionTextToken(InstructionTextTokenType.InstructionToken, 'inc'),
InstructionTextToken(InstructionTextTokenType.OperandSeparatorToken, ' '),
InstructionTextToken(InstructionTextTokenType.TextToken, '['),
InstructionTextToken(InstructionTextTokenType.RegisterToken, 'cp'),
InstructionTextToken(InstructionTextTokenType.TextToken, ']'),
],
'-' : [
InstructionTextToken(InstructionTextTokenType.InstructionToken, 'dec'),
InstructionTextToken(InstructionTextTokenType.OperandSeparatorToken, ' '),
InstructionTextToken(InstructionTextTokenType.TextToken, '['),
InstructionTextToken(InstructionTextTokenType.RegisterToken, 'cp'),
InstructionTextToken(InstructionTextTokenType.TextToken, ']'),
],
'>' : [
InstructionTextToken(InstructionTextTokenType.InstructionToken, 'inc'),
InstructionTextToken(InstructionTextTokenType.OperandSeparatorToken, ' '),
InstructionTextToken(InstructionTextTokenType.RegisterToken, 'cp'),
],
'<' : [
InstructionTextToken(InstructionTextTokenType.InstructionToken, 'dec'),
InstructionTextToken(InstructionTextTokenType.OperandSeparatorToken, ' '),
InstructionTextToken(InstructionTextTokenType.RegisterToken, 'cp'),
],
'[' : [
InstructionTextToken(InstructionTextTokenType.InstructionToken, 'nop'),
],
']' : [
InstructionTextToken(InstructionTextTokenType.InstructionToken, 'jnz'),
],
'.' : [
InstructionTextToken(InstructionTextTokenType.InstructionToken, 'stdout'),
],
',' : [
InstructionTextToken(InstructionTextTokenType.InstructionToken, 'stdin'),
],
}

InstructionIL = {
'+' : lambda il, value: il.store(1, il.reg(1, 'cp'), il.add(1, il.load(1, il.reg(1, 'cp')), il.const(1, 1))),
'-' : lambda il, value: il.store(1, il.reg(1, 'cp'), il.sub(1, il.load(1, il.reg(1, 'cp')), il.const(1, 1))),
'>' : lambda il, value: il.set_reg(1, 'cp', il.add(1, il.reg(1, 'cp'), il.const(1, 1))),
'<' : lambda il, value: il.set_reg(1, 'cp', il.sub(1, il.reg(1, 'cp'), il.const(1, 1)), flags='z'),
'[' : lambda il, value: il.nop(),
']' : lambda il, value: cond_branch(il, il.flag_condition(LowLevelILFlagCondition.LLFC_NE), il.const(4, value)),
'.' : lambda il, value: il.system_call(),
',' : lambda il, value: il.system_call(),
}

def get_instruction_info(self, data, addr):
"""
Provide information on branch operations
:param data: Opcode data
:param addr: Start address of data
"""

res = function.InstructionInfo()
res.length = 1
if data == ']':
Brainfuck.node_ends.append(addr)
res.add_branch(BranchType.FalseBranch, addr+1)
res.add_branch(BranchType.TrueBranch, Brainfuck.node_starts[Brainfuck.node_ends.index(addr)])
elif data == '[':
Brainfuck.node_starts.append(addr)
return res

def get_instruction_text(self, data, addr):
"""
Get tokens used to display instruction disassembly
:param data: Opcode data
:param addr: Start address of data
"""

tokens = Brainfuck.Tokens.get(data, None)
return (tokens, 1)

def get_instruction_low_level_il(self, data, addr, il):
"""
Lift instructions to LLIL
:param data: Opcode data
:param addr: Start address of data
:param il: LLIL object
"""

value = None
data = data[0]
if data == ']':
value = Brainfuck.node_starts[Brainfuck.node_ends.index(addr)]

instr = Brainfuck.InstructionIL[data](il, value)
if instr is not None:
il.append(instr)

return 1

class BrainfuckView(binaryview.BinaryView):
"""
This class is responsible for loading Brainfuck code files
"""

name = 'BF'
long_name = 'Brainfuck'

def __init__(self, data):
binaryview.BinaryView.__init__(self, parent_view=data, file_metadata=data.file)
self.platform = Architecture['Brainfuck'].standalone_platform
self.raw = data

@classmethod
def is_valid_for_data(self, data):
"""
Determine if we're compatible. Ensure the file consists solely of BF
code
:param data: File data stream
:return: True if our loader is compatible, False if it is not
"""

try:
data = data.read(0, 16).decode('utf-8')
except UnicodeError:
return False

bf_re = re.compile('[+\-<>.,\[\]\n]+')
if bf_re.match(data):
return True

return False

def init(self):
"""
Load the file and create a single code segment
:return: True on success, False on failure
"""

try:
# Create code segment
self.add_auto_segment(0, len(self.raw), 0, len(self.raw),
SegmentFlag.SegmentReadable|SegmentFlag.SegmentExecutable)

# Create code section
self.add_auto_section(
'.text', 0, len(self.raw),
SectionSemantics.ReadOnlyCodeSectionSemantics
)

# Setup the entry point
self.add_entry_point(0)
self.define_auto_symbol(Symbol(SymbolType.FunctionSymbol, 0, '_start'))

return True
except Exception:
log.log_error(traceback.format_exc())
return False

Brainfuck.register()
BrainfuckView.register()

17 changes: 17 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"pluginmetadataversion": 1,
"name": "brainfuck",
"type": ["architecture"],
"api": ["python2", "python3"],
"description": "Architecture module and loader for Brainfuck",
"longdescription": "Architecture module and loader for Brainfuck, an esoteric programming language",
"license": {
"name": "MIT",
"text": "Copyright (c) 2019 zznop\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
},
"platforms": ["Linux", "Windows", "Catalina"],
"dependencies": {},
"version": "1.0",
"author": "zznop",
"minimumbinaryninjaversion": 0
}
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3954337

Please sign in to comment.