From a8cdf5410e5fa34201157e2403662a5993579e1e Mon Sep 17 00:00:00 2001 From: DoronZ Date: Tue, 25 Aug 2020 22:20:51 +0300 Subject: [PATCH 01/18] command: function-lines: add before flag --- commands.md | 3 ++- fa/commands/function_lines.py | 20 +++++++++++++------ .../sublime/sig.sublime-completions | 2 +- setup.py | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/commands.md b/commands.md index b0e6154..bdce349 100644 --- a/commands.md +++ b/commands.md @@ -376,7 +376,7 @@ optional arguments: ``` ## function-lines ``` -usage: function-lines [-h] [--after] +usage: function-lines [-h] [--after | --before] get all function's lines @@ -395,6 +395,7 @@ EXAMPLE: optional arguments: -h, --help show this help message and exit --after include only function lines which occur after currentresultset + --before include only function lines which occur before current resultset ``` ## function-start ``` diff --git a/fa/commands/function_lines.py b/fa/commands/function_lines.py index af20fe4..e161387 100644 --- a/fa/commands/function_lines.py +++ b/fa/commands/function_lines.py @@ -26,22 +26,30 @@ def get_parser(): p = utils.ArgumentParserNoExit('function-lines', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('--after', action='store_true', + g = p.add_mutually_exclusive_group() + g.add_argument('--after', action='store_true', help='include only function lines which occur after current' 'resultset') + g.add_argument('--before', action='store_true', + help='include only function lines which occur before ' + 'current resultset') return p @context.ida_context -def function_lines(addresses, after=False): +def function_lines(addresses, after=False, before=False): for address in addresses: for item in idautils.FuncItems(address): - if not after: - yield item - else: + if after: if item > address: yield item + elif before: + if item < address: + yield item + else: + yield item def run(segments, args, addresses, interpreter=None, **kwargs): - return list(function_lines(addresses, args.after)) + return list(function_lines(addresses, after=args.after, + before=args.before)) diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index 71405a7..c713046 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -84,7 +84,7 @@ { "kind": "snippet", "trigger": "function-lines", - "contents": "function-lines --after" + "contents": "function-lines --after | --before" }, { "kind": "snippet", diff --git a/setup.py b/setup.py index 6a3238e..1f16944 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='fa', - version='0.1.9', + version='0.2', description='FA Plugin', author='DoronZ', author_email='doron88@gmail.com', From adfe3da9a4f09b63d7beff9c4378c6dce1395923 Mon Sep 17 00:00:00 2001 From: DoronZ Date: Tue, 25 Aug 2020 22:32:53 +0300 Subject: [PATCH 02/18] command: refactor checkpoints into variables --- README.md | 12 +- commands.md | 106 +++++++++--------- fa/commands/intersect.py | 16 +-- .../{back_to_checkpoint.py => load.py} | 14 +-- fa/commands/{checkpoint.py => store.py} | 14 +-- fa/fainterp.py | 8 +- fa/signatures/test-project-elf/test-basic.sig | 12 +- fa/signatures/test-project-ida/test-basic.sig | 12 +- .../test-project-ida/test-ida-context.sig | 8 +- .../sublime/sig.sublime-completions | 22 ++-- tests/test_commands/test_elf.py | 2 +- tests/test_commands/test_idalink.py | 2 +- 12 files changed, 114 insertions(+), 114 deletions(-) rename fa/commands/{back_to_checkpoint.py => load.py} (54%) rename fa/commands/{checkpoint.py => store.py} (55%) diff --git a/README.md b/README.md index 45af3b8..b9a8e25 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ To view the list of available commands, [view the list below](#available-command function-lines # save this result - checkpoint printf-lines + store printf-lines # look for: li r7, ??? verify-operand li --op0 7 @@ -223,8 +223,8 @@ To view the list of available commands, [view the list below](#available-command # define the constant set-const IMPORTANT_OFFSET - # back to previous results - back-to-checkpoint printf-lines + # load previous results + load printf-lines # look for: li r7, ??? verify-operand li --op0 8 @@ -301,8 +301,8 @@ To view the list of available commands, [view the list below](#available-command # sort results sort - # mark resultset checkpoint - checkpoint BLs + # store resultset in 'BLs' + store BLs # set first bl to malloc function single 0 @@ -312,7 +312,7 @@ To view the list of available commands, [view the list below](#available-command # go back to the results from 4 commands ago # (the sort results) - back-to-checkpoint BLs + load BLs # rename next symbol :) single 1 diff --git a/commands.md b/commands.md index bdce349..117fe8f 100644 --- a/commands.md +++ b/commands.md @@ -8,8 +8,6 @@ Below is the list of available commands: - [append](#append) - [argument](#argument) - [back](#back) -- [back-to-checkpoint](#back-to-checkpoint) -- [checkpoint](#checkpoint) - [clear](#clear) - [find](#find) - [find-bytes](#find-bytes) @@ -23,6 +21,7 @@ Below is the list of available commands: - [intersect](#intersect) - [keystone-find-opcodes](#keystone-find-opcodes) - [keystone-verify-opcodes](#keystone-verify-opcodes) +- [load](#load) - [locate](#locate) - [make-code](#make-code) - [make-comment](#make-comment) @@ -44,6 +43,7 @@ Below is the list of available commands: - [set-type](#set-type) - [single](#single) - [sort](#sort) +- [store](#store) - [trace](#trace) - [unique](#unique) - [verify-aligned](#verify-aligned) @@ -190,51 +190,6 @@ EXAMPLE: positional arguments: amount amount of command results to go back by -optional arguments: - -h, --help show this help message and exit -``` -## back-to-checkpoint -``` -usage: back-to-checkpoint [-h] name - -go back to previous result-set saved by 'checkpoint' command. - -EXAMPLE: - results = [0, 4, 8] - checkpoint foo - - find-bytes --or 12345678 - results = [0, 4, 8, 10, 20] - - -> back-to-checkpoint foo - results = [0, 4, 8] - -positional arguments: - name name of checkpoint in history to go back to - -optional arguments: - -h, --help show this help message and exit -``` -## checkpoint -``` -usage: checkpoint [-h] name - -save current result-set as a checkpoint. -You can later restore the result-set using 'back-to-checkpoint' - -EXAMPLE: - results = [0, 4, 8] - -> checkpoint foo - - find-bytes --or 12345678 - results = [0, 4, 8, 10, 20] - - back-to-checkpoint foo - results = [0, 4, 8] - -positional arguments: - name name of checkpoint to use - optional arguments: -h, --help show this help message and exit ``` @@ -438,25 +393,25 @@ optional arguments: ``` ## intersect ``` -usage: intersect [-h] checkpoints [checkpoints ...] +usage: intersect [-h] variables [variables ...] -intersect two or more checkpoints +intersect two or more variables EXAMPLE: results = [0, 4, 8] - checkpoint a + store a ... results = [0, 12, 20] - checkpoint b + store b -> intersect a b results = [0] positional arguments: - checkpoints checkpoint names + variables variable names optional arguments: - -h, --help show this help message and exit + -h, --help show this help message and exit ``` ## keystone-find-opcodes ``` @@ -507,6 +462,28 @@ optional arguments: --bele figure out the endianity from IDA instead of explicit mode --until UNTIL keep going onwards opcode-opcode until verified ``` +## load +``` +usage: load [-h] name + +go back to previous result-set saved by 'store' command. + +EXAMPLE: + results = [0, 4, 8] + store foo + + find-bytes 12345678 + results = [0, 4, 8, 10, 20] + + -> load foo + results = [0, 4, 8] + +positional arguments: + name name of variable in history to go back to + +optional arguments: + -h, --help show this help message and exit +``` ## locate ``` usage: locate [-h] name @@ -796,6 +773,29 @@ EXAMPLE: -> sort result = [0, 4, 8 ,12] +optional arguments: + -h, --help show this help message and exit +``` +## store +``` +usage: store [-h] name + +save current result-set in a variable. +You can later load the result-set using 'load' + +EXAMPLE: + results = [0, 4, 8] + -> store foo + + find-bytes --or 12345678 + results = [0, 4, 8, 10, 20] + + load foo + results = [0, 4, 8] + +positional arguments: + name name of variable to use + optional arguments: -h, --help show this help message and exit ``` diff --git a/fa/commands/intersect.py b/fa/commands/intersect.py index 6df32b2..e6c17f0 100644 --- a/fa/commands/intersect.py +++ b/fa/commands/intersect.py @@ -1,14 +1,14 @@ from argparse import RawTextHelpFormatter from fa import utils -DESCRIPTION = '''intersect two or more checkpoints +DESCRIPTION = '''intersect two or more variables EXAMPLE: results = [0, 4, 8] - checkpoint a + store a ... results = [0, 12, 20] - checkpoint b + store b -> intersect a b results = [0] @@ -19,15 +19,15 @@ def get_parser(): p = utils.ArgumentParserNoExit('intersect', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('checkpoints', nargs='+', help='checkpoint names') + p.add_argument('variables', nargs='+', help='variable names') return p def run(segments, args, addresses, interpreter=None, **kwargs): - first_checkpoint = args.checkpoints[0] - results = set(interpreter.checkpoints[first_checkpoint]) + first_var = args.variables[0] + results = set(interpreter.variables[first_var]) - for c in args.checkpoints[1:]: - results.intersection_update(interpreter.checkpoints[c]) + for c in args.variables[1:]: + results.intersection_update(interpreter.variables[c]) return list(results) diff --git a/fa/commands/back_to_checkpoint.py b/fa/commands/load.py similarity index 54% rename from fa/commands/back_to_checkpoint.py rename to fa/commands/load.py index b09981a..52c93f8 100644 --- a/fa/commands/back_to_checkpoint.py +++ b/fa/commands/load.py @@ -1,28 +1,28 @@ from argparse import RawTextHelpFormatter from fa import utils -DESCRIPTION = '''go back to previous result-set saved by 'checkpoint' command. +DESCRIPTION = '''go back to previous result-set saved by 'store' command. EXAMPLE: results = [0, 4, 8] - checkpoint foo + store foo - find-bytes --or 12345678 + find-bytes 12345678 results = [0, 4, 8, 10, 20] - -> back-to-checkpoint foo + -> load foo results = [0, 4, 8] ''' def get_parser(): - p = utils.ArgumentParserNoExit('back-to-checkpoint', + p = utils.ArgumentParserNoExit('load', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('name', help='name of checkpoint in history to go back ' + p.add_argument('name', help='name of variable in history to go back ' 'to') return p def run(segments, args, addresses, interpreter=None, **kwargs): - return interpreter.checkpoints[args.name] + return interpreter.variables[args.name] diff --git a/fa/commands/checkpoint.py b/fa/commands/store.py similarity index 55% rename from fa/commands/checkpoint.py rename to fa/commands/store.py index 0f01e55..f7685a7 100644 --- a/fa/commands/checkpoint.py +++ b/fa/commands/store.py @@ -1,29 +1,29 @@ from argparse import RawTextHelpFormatter from fa import utils -DESCRIPTION = '''save current result-set as a checkpoint. -You can later restore the result-set using 'back-to-checkpoint' +DESCRIPTION = '''save current result-set in a variable. +You can later load the result-set using 'load' EXAMPLE: results = [0, 4, 8] - -> checkpoint foo + -> store foo find-bytes --or 12345678 results = [0, 4, 8, 10, 20] - back-to-checkpoint foo + load foo results = [0, 4, 8] ''' def get_parser(): - p = utils.ArgumentParserNoExit('checkpoint', + p = utils.ArgumentParserNoExit('store', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('name', help='name of checkpoint to use') + p.add_argument('name', help='name of variable to use') return p def run(segments, args, addresses, interpreter=None, **kwargs): - interpreter.checkpoints[args.name] = addresses + interpreter.variables[args.name] = addresses return addresses diff --git a/fa/fainterp.py b/fa/fainterp.py index db8acfb..56bcb6a 100644 --- a/fa/fainterp.py +++ b/fa/fainterp.py @@ -39,7 +39,7 @@ def __init__(self, config_path=CONFIG_PATH): self._symbols = {} self._consts = {} self.history = [] - self.checkpoints = {} + self.variables = {} self.endianity = '<' self._config_path = config_path @@ -312,7 +312,7 @@ def save_signature(self, signature): hjson.dump(signature, f, indent=4) def find_from_instructions_list(self, instructions, - clear_checkpoints=False, + clear_variables=False, decremental=False, addresses=None): """ Run the given instruction list and output the result @@ -327,8 +327,8 @@ def find_from_instructions_list(self, instructions, self.history = [] - if clear_checkpoints: - self.checkpoints = {} + if clear_variables: + self.variables = {} for line in instructions: line = line.strip() diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index 3d5bfad..5637298 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -3,7 +3,7 @@ "instructions": [ add 80 set-name test_add - checkpoint 80 + store 80 offset 1 set-name test_pos_offset @@ -16,8 +16,8 @@ set-name test_add_offset_range clear - back-to-checkpoint 80 - set-name test_back_to_checkpoint + load 80 + set-name test_load offset 1 align 4 @@ -125,7 +125,7 @@ add 2 add 3 - checkpoint a + store a clear @@ -133,11 +133,11 @@ add 8 add 12 - checkpoint b + store b clear - checkpoint c + store c intersect a b set-name test_intersect_ab diff --git a/fa/signatures/test-project-ida/test-basic.sig b/fa/signatures/test-project-ida/test-basic.sig index 3d5bfad..5637298 100644 --- a/fa/signatures/test-project-ida/test-basic.sig +++ b/fa/signatures/test-project-ida/test-basic.sig @@ -3,7 +3,7 @@ "instructions": [ add 80 set-name test_add - checkpoint 80 + store 80 offset 1 set-name test_pos_offset @@ -16,8 +16,8 @@ set-name test_add_offset_range clear - back-to-checkpoint 80 - set-name test_back_to_checkpoint + load 80 + set-name test_load offset 1 align 4 @@ -125,7 +125,7 @@ add 2 add 3 - checkpoint a + store a clear @@ -133,11 +133,11 @@ add 8 add 12 - checkpoint b + store b clear - checkpoint c + store c intersect a b set-name test_intersect_ab diff --git a/fa/signatures/test-project-ida/test-ida-context.sig b/fa/signatures/test-project-ida/test-ida-context.sig index 0b34b49..f86d616 100644 --- a/fa/signatures/test-project-ida/test-ida-context.sig +++ b/fa/signatures/test-project-ida/test-ida-context.sig @@ -9,12 +9,12 @@ function-start set-name test_function_start - checkpoint func + store func function-end set-name test_function_end - back-to-checkpoint func + load func offset 10 function-lines single 0 @@ -23,7 +23,7 @@ function-lines verify-operand ldr --op0 0 set-name test_verify_operand - checkpoint ref + store ref verify-ref --code --data set-name test_verify_ref_no_name @@ -31,7 +31,7 @@ goto-ref --data set-name test_verify_goto_ref - back-to-checkpoint ref + load ref verify-ref --name test_verify_goto_ref --code --data set-name test_verify_ref_name diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index c713046..7ac71bd 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -36,16 +36,6 @@ "trigger": "back", "contents": "back ${1:amount}" }, - { - "kind": "snippet", - "trigger": "back-to-checkpoint", - "contents": "back-to-checkpoint ${1:name}" - }, - { - "kind": "snippet", - "trigger": "checkpoint", - "contents": "checkpoint ${1:name}" - }, { "kind": "snippet", "trigger": "clear", @@ -99,7 +89,7 @@ { "kind": "snippet", "trigger": "intersect", - "contents": "intersect ${1:checkpoints} ${2:checkpoints} ..." + "contents": "intersect ${1:variables} ${2:variables} ..." }, { "kind": "snippet", @@ -111,6 +101,11 @@ "trigger": "keystone-verify-opcodes", "contents": "keystone-verify-opcodes --bele --until ${1:UNTIL} ${2:arch} ${3:mode} ${4:code}" }, + { + "kind": "snippet", + "trigger": "load", + "contents": "load ${1:name}" + }, { "kind": "snippet", "trigger": "locate", @@ -216,6 +211,11 @@ "trigger": "sort", "contents": "sort " }, + { + "kind": "snippet", + "trigger": "store", + "contents": "store ${1:name}" + }, { "kind": "snippet", "trigger": "trace", diff --git a/tests/test_commands/test_elf.py b/tests/test_commands/test_elf.py index 67a38ea..65c050b 100644 --- a/tests/test_commands/test_elf.py +++ b/tests/test_commands/test_elf.py @@ -17,7 +17,7 @@ def test_elf_symbols(sample_elf): assert symbols['test_pos_offset'] == 81 assert symbols['test_neg_offset'] == 80 assert symbols['test_add_offset_range'] == 100 - assert symbols['test_back_to_checkpoint'] == 80 + assert symbols['test_load'] == 80 assert symbols['test_align'] == 84 assert symbols['test_most_common'] == 2 assert symbols['test_sort'] == 3 diff --git a/tests/test_commands/test_idalink.py b/tests/test_commands/test_idalink.py index e57bbdd..26b1cd3 100644 --- a/tests/test_commands/test_idalink.py +++ b/tests/test_commands/test_idalink.py @@ -76,7 +76,7 @@ def test_ida_symbols(ida, sample_elf): assert symbols['test_pos_offset'] == 81 assert symbols['test_neg_offset'] == 80 assert symbols['test_add_offset_range'] == 100 - assert symbols['test_back_to_checkpoint'] == 80 + assert symbols['test_load'] == 80 assert symbols['test_align'] == 84 assert symbols['test_most_common'] == 2 assert symbols['test_sort'] == 3 From b700f1070246e7d8e37e99d215d6fe67539e1204 Mon Sep 17 00:00:00 2001 From: DoronZ Date: Wed, 26 Aug 2020 00:06:13 +0300 Subject: [PATCH 03/18] fainterp: add branch support --- README.md | 33 ++++++++++ fa/fainterp.py | 63 ++++++++++++++++--- fa/signatures/test-project-elf/test-basic.sig | 24 +++++++ fa/signatures/test-project-ida/test-basic.sig | 23 +++++++ .../test-project-ida/test-ida-context.sig | 7 +++ tests/test_commands/test_elf.py | 7 +++ tests/test_commands/test_idalink.py | 9 +++ 7 files changed, 159 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b9a8e25..c9012b4 100644 --- a/README.md +++ b/README.md @@ -323,6 +323,39 @@ To view the list of available commands, [view the list below](#available-command } ``` +#### Conditional branches + +```hjson +{ + name: set_opcode_const + instructions: [ + # goto printf function + locate printf + + # goto 'case_opcode_bl' if current opcode is bl + beq case_opcode_bl 'verify-operand bl' + + # mark as 'case_opcode_bl' label + label case_opcode_bl + + # set bl const + set-const is_bl + + # finish script by jumping to end + b end + + # mark as 'case_opcode_ldr' label + label case_opcode_ldr + + # set is_ldr const + set-const is_ldr + + # mark script end + label end + ] +} +``` + #### Python script to find a list of symbols ```python diff --git a/fa/fainterp.py b/fa/fainterp.py index 56bcb6a..850d882 100644 --- a/fa/fainterp.py +++ b/fa/fainterp.py @@ -11,6 +11,8 @@ import hjson +from fa.utils import ArgumentParserNoExit + CONFIG_PATH = os.path.join( os.path.dirname(os.path.abspath(__file__)), '..', 'config.ini') DEFAULT_SIGNATURES_ROOT = os.path.join( @@ -311,6 +313,35 @@ def save_signature(self, signature): with open(filename, 'w') as f: hjson.dump(signature, f, indent=4) + @staticmethod + def _get_labeled_instructions(instructions): + labels = {} + processed_instructions = [] + + label_parser = ArgumentParserNoExit('label') + label_parser.add_argument('name') + + pc = 0 + for line in instructions: + line = line.strip() + + if len(line) == 0: + continue + + if line.startswith('#'): + # treat as comment + continue + + if line.startswith('label '): + args = label_parser.parse_args(shlex.split(line)[1:]) + labels[args.name] = pc + continue + + processed_instructions.append(line) + pc += 1 + + return labels, processed_instructions + def find_from_instructions_list(self, instructions, clear_variables=False, decremental=False, addresses=None): @@ -330,21 +361,38 @@ def find_from_instructions_list(self, instructions, if clear_variables: self.variables = {} - for line in instructions: - line = line.strip() + labels, instructions = self._get_labeled_instructions(instructions) - if len(line) == 0: - continue + beq = ArgumentParserNoExit('cmp') + beq.add_argument('label') + beq.add_argument('cond') - if line.startswith('#'): - # treat as comment - continue + b_parser = ArgumentParserNoExit('branch') + b_parser.add_argument('label') + + pc = 0 + while pc < len(instructions): + line = instructions[pc] + print(line) if line == 'stop-if-empty': if len(addresses) == 0: return addresses else: + pc += 1 continue + elif line.startswith('beq '): + args = beq.parse_args(shlex.split(line)[1:]) + if 0 != len(self.find_from_instructions_list( + [args.cond], addresses=addresses)): + pc = labels[args.label] + else: + pc += 1 + continue + elif line.startswith('b '): + args = b_parser.parse_args(shlex.split(line)[1:]) + pc = labels[args.label] + continue # normal commands @@ -365,6 +413,7 @@ def find_from_instructions_list(self, instructions, addresses = new_addresses self.history.append(addresses) + pc += 1 return addresses diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index 5637298..edad0c9 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -144,5 +144,29 @@ intersect a b c set-name test_intersect_abc + + clear + + add 1 + add 2 + + beq is_single1 verify-single + set-name test_is_single_false1 + b end1 + label is_single1 + set-name test_is_single_true1 + label end1 + + clear + + add 1 + + beq is_single2 verify-single + set-name test_is_single_false2 + b end2 + label is_single2 + set-name test_is_single_true2 + label end2 + ] } diff --git a/fa/signatures/test-project-ida/test-basic.sig b/fa/signatures/test-project-ida/test-basic.sig index 5637298..7742908 100644 --- a/fa/signatures/test-project-ida/test-basic.sig +++ b/fa/signatures/test-project-ida/test-basic.sig @@ -144,5 +144,28 @@ intersect a b c set-name test_intersect_abc + + clear + + add 1 + add 2 + + beq is_single1 verify-single + set-name test_is_single_false1 + b end1 + label is_single1 + set-name test_is_single_true1 + label end1 + + clear + + add 1 + + beq is_single2 verify-single + set-name test_is_single_false2 + b end2 + label is_single2 + set-name test_is_single_true2 + label end2 ] } diff --git a/fa/signatures/test-project-ida/test-ida-context.sig b/fa/signatures/test-project-ida/test-ida-context.sig index f86d616..a03fd85 100644 --- a/fa/signatures/test-project-ida/test-ida-context.sig +++ b/fa/signatures/test-project-ida/test-ida-context.sig @@ -82,5 +82,12 @@ set-name test_argument clear + + arm-find-all 'mov r0, 1; bx lr' + + beq test_branch1 'verify-operand mov --op0 0' + set-name test_branch1_false + label test_branch1 + set-name test_branch1_true ] } diff --git a/tests/test_commands/test_elf.py b/tests/test_commands/test_elf.py index 65c050b..7c89f73 100644 --- a/tests/test_commands/test_elf.py +++ b/tests/test_commands/test_elf.py @@ -37,3 +37,10 @@ def test_elf_symbols(sample_elf): assert 'test_ond_81' not in symbols assert symbols['test_intersect_ab'] == 2 assert 'test_intersect_abc' not in symbols + + # test for branches + assert 'test_is_single_false1' in symbols + assert 'test_is_single_true1' not in symbols + + assert 'test_is_single_false2' not in symbols + assert 'test_is_single_true2' in symbols diff --git a/tests/test_commands/test_idalink.py b/tests/test_commands/test_idalink.py index 26b1cd3..ac07145 100644 --- a/tests/test_commands/test_idalink.py +++ b/tests/test_commands/test_idalink.py @@ -97,6 +97,13 @@ def test_ida_symbols(ida, sample_elf): assert symbols['test_intersect_ab'] == 2 assert 'test_intersect_abc' not in symbols + # test for branches + assert 'test_is_single_false1' in symbols + assert 'test_is_single_true1' not in symbols + + assert 'test_is_single_false2' not in symbols + assert 'test_is_single_true2' in symbols + # from test-ida-context assert symbols['test_find_bytes_ida'] == 0x1240 assert symbols['test_xref'] == 0x125c @@ -112,3 +119,5 @@ def test_ida_symbols(ida, sample_elf): assert symbols['test_find_immediate'] == 0x1240 assert symbols['test_operand'] == 1 assert symbols['test_argument'] == 0x00001250 + assert 'test_branch1_false' not in symbols + assert 'test_branch1_true' in symbols From df070043fe6e496a2dc3456314a488070247b220 Mon Sep 17 00:00:00 2001 From: DoronZ Date: Wed, 26 Aug 2020 00:22:28 +0300 Subject: [PATCH 04/18] fainterp: save last-temp-sig as raw --- fa/fainterp.py | 15 ++++++++++----- fa/ida_plugin.py | 4 +--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/fa/fainterp.py b/fa/fainterp.py index 850d882..bd68615 100644 --- a/fa/fainterp.py +++ b/fa/fainterp.py @@ -292,26 +292,31 @@ def get_alias(self): return retval - def save_signature(self, signature): + def save_signature(self, filename): """ Save given signature object (by dictionary) into active project as a new SIG file. If symbol name already exists, then create another file (never overwrites). - :param signature: Dictionary of signature object + :param filename: Dictionary of signature object :return: None """ + with open(filename) as f: + sig = hjson.load(f) + f.seek(0) + sig_text = f.read() + filename = os.path.join( self._signatures_root, self._project, - signature['name'] + '.sig') + sig['name'] + '.sig') i = 1 while os.path.exists(filename): filename = os.path.join(self._signatures_root, self._project, - signature['name'] + '.{}.sig'.format(i)) + sig['name'] + '.{}.sig'.format(i)) i += 1 with open(filename, 'w') as f: - hjson.dump(signature, f, indent=4) + f.write(sig_text) @staticmethod def _get_labeled_instructions(instructions): diff --git a/fa/ida_plugin.py b/fa/ida_plugin.py index aa9211e..738f0eb 100644 --- a/fa/ida_plugin.py +++ b/fa/ida_plugin.py @@ -174,14 +174,12 @@ def prompt_save_signature(self): :return: None """ self.verify_project() - with open(TEMP_SIG_FILENAME) as f: - sig = hjson.load(f) if ida_kernwin.ask_yn(1, 'Are you sure you want ' 'to save this signature?') != 1: return - self.save_signature(sig) + self.save_signature(TEMP_SIG_FILENAME) def find(self, symbol_name, decremental=False): """ From 57568d609feeec2c0fc535cd2621e5454c86cf3d Mon Sep 17 00:00:00 2001 From: DoronZ Date: Wed, 26 Aug 2020 01:26:02 +0300 Subject: [PATCH 05/18] fainterp: refactor branch to an evaled expression --- README.md | 24 +++++++++++++++---- fa/commands/alias | 1 + fa/fainterp.py | 23 +++++++++--------- fa/signatures/test-project-elf/test-basic.sig | 17 +++++++++---- fa/signatures/test-project-ida/test-basic.sig | 18 ++++++++++---- .../test-project-ida/test-ida-context.sig | 6 ++++- tests/test_commands/test_elf.py | 1 + 7 files changed, 64 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index c9012b4..a379676 100644 --- a/README.md +++ b/README.md @@ -332,22 +332,36 @@ To view the list of available commands, [view the list below](#available-command # goto printf function locate printf + # save current resultset + store tmp + + # is_bl = is current opcode a bl? + verify-operand bl + store is_bl + + # load previous resultset + load tmp + # goto 'case_opcode_bl' if current opcode is bl - beq case_opcode_bl 'verify-operand bl' + if is_bl case_opcode_bl # mark as 'case_opcode_bl' label label case_opcode_bl - # set bl const + # make: #define is_bl (1) + clear + add 1 set-const is_bl # finish script by jumping to end b end - # mark as 'case_opcode_ldr' label - label case_opcode_ldr + # mark as 'not_bl' label + label not_bl - # set is_ldr const + # make: #define is_ldr (1) + clear + add 1 set-const is_ldr # mark script end diff --git a/fa/commands/alias b/fa/commands/alias index b05e94c..066ba27 100644 --- a/fa/commands/alias +++ b/fa/commands/alias @@ -6,3 +6,4 @@ arm-find-all = keystone-find-opcodes --bele KS_ARCH_ARM KS_MODE_ARM thumb-find-all = keystone-find-opcodes --bele KS_ARCH_ARM KS_MODE_THUMB arm-verify = keystone-verify-opcodes --bele KS_ARCH_ARM KS_MODE_ARM find-imm = find-immediate +save = store diff --git a/fa/fainterp.py b/fa/fainterp.py index bd68615..a271928 100644 --- a/fa/fainterp.py +++ b/fa/fainterp.py @@ -368,9 +368,9 @@ def find_from_instructions_list(self, instructions, labels, instructions = self._get_labeled_instructions(instructions) - beq = ArgumentParserNoExit('cmp') - beq.add_argument('label') - beq.add_argument('cond') + if_parser = ArgumentParserNoExit('if') + if_parser.add_argument('cond') + if_parser.add_argument('label') b_parser = ArgumentParserNoExit('branch') b_parser.add_argument('label') @@ -380,16 +380,20 @@ def find_from_instructions_list(self, instructions, line = instructions[pc] print(line) + for k, v in self.get_alias().items(): + # handle aliases + if line.startswith(k): + line = line.replace(k, v) + if line == 'stop-if-empty': if len(addresses) == 0: return addresses else: pc += 1 continue - elif line.startswith('beq '): - args = beq.parse_args(shlex.split(line)[1:]) - if 0 != len(self.find_from_instructions_list( - [args.cond], addresses=addresses)): + elif line.startswith('if '): + args = if_parser.parse_args(shlex.split(line)[1:]) + if eval(args.cond, self.variables): pc = labels[args.label] else: pc += 1 @@ -401,11 +405,6 @@ def find_from_instructions_list(self, instructions, # normal commands - for k, v in self.get_alias().items(): - # handle aliases - if line.startswith(k): - line = line.replace(k, v) - new_addresses = [] try: new_addresses = self.run_command(line, addresses) diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index edad0c9..c2a87be 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -150,22 +150,31 @@ add 1 add 2 - beq is_single1 verify-single + verify-single + store is_single1 + if is_single1 is_single_label1 + add 1 set-name test_is_single_false1 b end1 - label is_single1 + label is_single_label1 set-name test_is_single_true1 + label end1 clear add 1 - beq is_single2 verify-single + verify-single + store is_single2 + + if is_single2 is_single_label2 set-name test_is_single_false2 b end2 - label is_single2 + + label is_single_label2 set-name test_is_single_true2 + label end2 ] diff --git a/fa/signatures/test-project-ida/test-basic.sig b/fa/signatures/test-project-ida/test-basic.sig index 7742908..c2a87be 100644 --- a/fa/signatures/test-project-ida/test-basic.sig +++ b/fa/signatures/test-project-ida/test-basic.sig @@ -150,22 +150,32 @@ add 1 add 2 - beq is_single1 verify-single + verify-single + store is_single1 + if is_single1 is_single_label1 + add 1 set-name test_is_single_false1 b end1 - label is_single1 + label is_single_label1 set-name test_is_single_true1 + label end1 clear add 1 - beq is_single2 verify-single + verify-single + store is_single2 + + if is_single2 is_single_label2 set-name test_is_single_false2 b end2 - label is_single2 + + label is_single_label2 set-name test_is_single_true2 + label end2 + ] } diff --git a/fa/signatures/test-project-ida/test-ida-context.sig b/fa/signatures/test-project-ida/test-ida-context.sig index a03fd85..61362c5 100644 --- a/fa/signatures/test-project-ida/test-ida-context.sig +++ b/fa/signatures/test-project-ida/test-ida-context.sig @@ -85,8 +85,12 @@ arm-find-all 'mov r0, 1; bx lr' - beq test_branch1 'verify-operand mov --op0 0' + verify-operand mov --op0 0 + store tmp + + if tmp test_branch1 set-name test_branch1_false + label test_branch1 set-name test_branch1_true ] diff --git a/tests/test_commands/test_elf.py b/tests/test_commands/test_elf.py index 7c89f73..50441f7 100644 --- a/tests/test_commands/test_elf.py +++ b/tests/test_commands/test_elf.py @@ -39,6 +39,7 @@ def test_elf_symbols(sample_elf): assert 'test_intersect_abc' not in symbols # test for branches + print(symbols) assert 'test_is_single_false1' in symbols assert 'test_is_single_true1' not in symbols From 93bb66f5908ccf62ca917a60d881c66cbc06148f Mon Sep 17 00:00:00 2001 From: DoronZ Date: Thu, 27 Aug 2020 00:05:26 +0300 Subject: [PATCH 06/18] fainterp: use stack frames to modify pc --- commands.md | 79 +++++++++++- fa/commands/b.py | 30 +++++ fa/commands/intersect.py | 4 +- fa/commands/load.py | 2 +- fa/commands/python_if.py | 37 ++++++ fa/commands/stop_if_empty.py | 28 +++++ fa/commands/store.py | 2 +- fa/fainterp.py | 113 ++++++++++-------- fa/signatures/test-project-elf/test-basic.sig | 4 +- fa/signatures/test-project-ida/test-basic.sig | 4 +- .../test-project-ida/test-ida-context.sig | 2 +- .../sublime/sig.sublime-completions | 15 +++ scripts/git/pre-commit | 6 +- 13 files changed, 258 insertions(+), 68 deletions(-) create mode 100644 fa/commands/b.py create mode 100644 fa/commands/python_if.py create mode 100644 fa/commands/stop_if_empty.py diff --git a/commands.md b/commands.md index 117fe8f..8cecad1 100644 --- a/commands.md +++ b/commands.md @@ -1,12 +1,13 @@ # FA Command List Below is the list of available commands: -- [stop-if-empty](#stop-if-empty) +- [label](#label) - [add](#add) - [add-offset-range](#add-offset-range) - [align](#align) - [and](#and) - [append](#append) - [argument](#argument) +- [b](#b) - [back](#back) - [clear](#clear) - [find](#find) @@ -35,6 +36,7 @@ Below is the list of available commands: - [operand](#operand) - [or](#or) - [print](#print) +- [python-if](#python-if) - [run](#run) - [set-const](#set-const) - [set-enum](#set-enum) @@ -43,6 +45,7 @@ Below is the list of available commands: - [set-type](#set-type) - [single](#single) - [sort](#sort) +- [stop-if-empty](#stop-if-empty) - [store](#store) - [trace](#trace) - [unique](#unique) @@ -56,10 +59,9 @@ Below is the list of available commands: - [verify-str](#verify-str) - [xref](#xref) - [xrefs-to](#xrefs-to) -## stop-if-empty +## label ``` -builtin interpreter command. -stops parsing current SIG if current resultset is empty +builtin interpreter command. mark a label ``` ## add ``` @@ -168,6 +170,29 @@ EXAMPLE: positional arguments: arg argument number +optional arguments: + -h, --help show this help message and exit +``` +## b +``` +usage: b [-h] label + +branch unconditionally to label + +EXAMPLE: + results = [] + + add 1 + -> b skip + add 2 + label skip + add 3 + + results = [1, 3] + +positional arguments: + label label to jump to + optional arguments: -h, --help show this help message and exit ``` @@ -667,6 +692,35 @@ prints the current result-set (for debugging) positional arguments: phrase optional string +optional arguments: + -h, --help show this help message and exit +``` +## python-if +``` +usage: python-if [-h] cond label + +perform an 'if' statement to create conditional branches + +EXAMPLE: + results = [0, 4, 8] + + verify-single + store a + + # jump to a_is_single_label since a == [] + -> python-if a a_is_single_label + set-name a_isnt_single + b end + + label a_is_single_label + set-name a_is_single + + label end + +positional arguments: + cond condition to evaluate (being eval'ed) + label label to jump to if condition is true + optional arguments: -h, --help show this help message and exit ``` @@ -773,6 +827,23 @@ EXAMPLE: -> sort result = [0, 4, 8 ,12] +optional arguments: + -h, --help show this help message and exit +``` +## stop-if-empty +``` +usage: stop-if-empty [-h] + +exit if current resultset is empty + +EXAMPLE: + results = [] + + -> stop-if-empty + add 1 + + results = [] + optional arguments: -h, --help show this help message and exit ``` diff --git a/fa/commands/b.py b/fa/commands/b.py new file mode 100644 index 0000000..bfa0442 --- /dev/null +++ b/fa/commands/b.py @@ -0,0 +1,30 @@ +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''branch unconditionally to label + +EXAMPLE: + results = [] + + add 1 + -> b skip + add 2 + label skip + add 3 + + results = [1, 3] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('b', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('label', help='label to jump to') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + interpreter.set_pc(args.label) + interpreter.dec_pc() + return addresses diff --git a/fa/commands/intersect.py b/fa/commands/intersect.py index e6c17f0..3ec9904 100644 --- a/fa/commands/intersect.py +++ b/fa/commands/intersect.py @@ -25,9 +25,9 @@ def get_parser(): def run(segments, args, addresses, interpreter=None, **kwargs): first_var = args.variables[0] - results = set(interpreter.variables[first_var]) + results = set(interpreter.get_variable(first_var)) for c in args.variables[1:]: - results.intersection_update(interpreter.variables[c]) + results.intersection_update(interpreter.get_variable(c)) return list(results) diff --git a/fa/commands/load.py b/fa/commands/load.py index 52c93f8..5d4b46f 100644 --- a/fa/commands/load.py +++ b/fa/commands/load.py @@ -25,4 +25,4 @@ def get_parser(): def run(segments, args, addresses, interpreter=None, **kwargs): - return interpreter.variables[args.name] + return interpreter.get_variable(args.name) diff --git a/fa/commands/python_if.py b/fa/commands/python_if.py new file mode 100644 index 0000000..3b6385a --- /dev/null +++ b/fa/commands/python_if.py @@ -0,0 +1,37 @@ +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''perform an 'if' statement to create conditional branches + +EXAMPLE: + results = [0, 4, 8] + + verify-single + store a + + # jump to a_is_single_label since a == [] + -> python-if a a_is_single_label + set-name a_isnt_single + b end + + label a_is_single_label + set-name a_is_single + + label end +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('python-if', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('cond', help='condition to evaluate (being eval\'ed)') + p.add_argument('label', help='label to jump to if condition is true') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + if eval(args.cond, interpreter.get_all_variables()): + interpreter.set_pc(args.label) + interpreter.dec_pc() + return addresses diff --git a/fa/commands/stop_if_empty.py b/fa/commands/stop_if_empty.py new file mode 100644 index 0000000..54f12bf --- /dev/null +++ b/fa/commands/stop_if_empty.py @@ -0,0 +1,28 @@ +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''exit if current resultset is empty + +EXAMPLE: + results = [] + + -> stop-if-empty + add 1 + + results = [] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('stop-if-empty', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + if len(addresses) == 0: + # just a big enough value which is always greater + # then max available pc + interpreter.set_pc(0xffffffff) + return addresses diff --git a/fa/commands/store.py b/fa/commands/store.py index f7685a7..8c38374 100644 --- a/fa/commands/store.py +++ b/fa/commands/store.py @@ -25,5 +25,5 @@ def get_parser(): def run(segments, args, addresses, interpreter=None, **kwargs): - interpreter.variables[args.name] = addresses + interpreter.set_variable(args.name, addresses) return addresses diff --git a/fa/fainterp.py b/fa/fainterp.py index a271928..96714e7 100644 --- a/fa/fainterp.py +++ b/fa/fainterp.py @@ -4,7 +4,7 @@ from configparser import ConfigParser from abc import ABCMeta, abstractmethod -from collections import OrderedDict +from collections import OrderedDict, namedtuple import shlex import sys import os @@ -21,6 +21,13 @@ os.path.dirname(os.path.abspath(__file__)), 'commands') +class InterpreterState: + def __init__(self): + self.pc = 0 + self.variables = {} + self.labels = {} + + class FaInterp: """ FA Interpreter base class @@ -41,15 +48,50 @@ def __init__(self, config_path=CONFIG_PATH): self._symbols = {} self._consts = {} self.history = [] - self.variables = {} self.endianity = '<' self._config_path = config_path + self.stack = [] if (config_path is not None) and (os.path.exists(config_path)): self._signatures_root = os.path.expanduser( self.config_get('global', 'signatures_root')) self._project = self.config_get('global', 'project', None) + def _push_stack_frame(self): + self.stack.append(InterpreterState()) + + def _pop_stack_frame(self): + self.stack.pop() + + def set_labels(self, labels): + self.stack[-1].labels = labels + + def get_pc(self): + return self.stack[-1].pc + + def set_pc(self, pc): + if isinstance(pc, int): + self.stack[-1].pc = pc + elif isinstance(pc, str): + self.stack[-1].pc = self.stack[-1].labels[pc] + else: + raise KeyError('invalid pc: {}'.format(pc)) + + def dec_pc(self): + self.stack[-1].pc -= 1 + + def inc_pc(self): + self.stack[-1].pc += 1 + + def set_variable(self, name, value): + self.stack[-1].variables[name] = value + + def get_variable(self, name): + return self.stack[-1].variables[name] + + def get_all_variables(self): + return self.stack[-1].variables + @abstractmethod def set_input(self, input_): """ @@ -318,14 +360,15 @@ def save_signature(self, filename): with open(filename, 'w') as f: f.write(sig_text) - @staticmethod - def _get_labeled_instructions(instructions): + def _get_labeled_instructions(self, instructions): labels = {} processed_instructions = [] label_parser = ArgumentParserNoExit('label') label_parser.add_argument('name') + alias_items = self.get_alias().items() + pc = 0 for line in instructions: line = line.strip() @@ -342,19 +385,20 @@ def _get_labeled_instructions(instructions): labels[args.name] = pc continue + for k, v in alias_items: + # handle aliases + if line.startswith(k): + line = line.replace(k, v) + processed_instructions.append(line) pc += 1 return labels, processed_instructions - def find_from_instructions_list(self, instructions, - clear_variables=False, - decremental=False, addresses=None): + def find_from_instructions_list(self, instructions, addresses=None): """ Run the given instruction list and output the result :param instructions: instruction list - :param decremental: should stop and return the output *before* the last - command that returned an empty list of results :param addresses: input address list (if any) :return: output address list """ @@ -363,47 +407,16 @@ def find_from_instructions_list(self, instructions, self.history = [] - if clear_variables: - self.variables = {} + self._push_stack_frame() labels, instructions = self._get_labeled_instructions(instructions) + + self.set_labels(labels) - if_parser = ArgumentParserNoExit('if') - if_parser.add_argument('cond') - if_parser.add_argument('label') + n = len(instructions) - b_parser = ArgumentParserNoExit('branch') - b_parser.add_argument('label') - - pc = 0 - while pc < len(instructions): - line = instructions[pc] - print(line) - - for k, v in self.get_alias().items(): - # handle aliases - if line.startswith(k): - line = line.replace(k, v) - - if line == 'stop-if-empty': - if len(addresses) == 0: - return addresses - else: - pc += 1 - continue - elif line.startswith('if '): - args = if_parser.parse_args(shlex.split(line)[1:]) - if eval(args.cond, self.variables): - pc = labels[args.label] - else: - pc += 1 - continue - elif line.startswith('b '): - args = b_parser.parse_args(shlex.split(line)[1:]) - pc = labels[args.label] - continue - - # normal commands + while self.get_pc() < n: + line = instructions[self.get_pc()] new_addresses = [] try: @@ -412,13 +425,11 @@ def find_from_instructions_list(self, instructions, FaInterp.log('failed to run: {}. error: {}' .format(line, str(m))) - if decremental and len(new_addresses) == 0 and len(addresses) > 0: - return addresses - addresses = new_addresses self.history.append(addresses) - pc += 1 + self.inc_pc() + self._pop_stack_frame() return addresses def find_from_sig_json(self, signature_json, decremental=False): @@ -432,7 +443,7 @@ def find_from_sig_json(self, signature_json, decremental=False): self.log('interpreting SIG for: {}'.format(signature_json['name'])) start = time.time() retval = self.find_from_instructions_list( - signature_json['instructions'], decremental) + signature_json['instructions']) self.log('interpretation took: {}s'.format(time.time() - start)) return retval diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index c2a87be..df5bf30 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -152,7 +152,7 @@ verify-single store is_single1 - if is_single1 is_single_label1 + python-if is_single1 is_single_label1 add 1 set-name test_is_single_false1 b end1 @@ -168,7 +168,7 @@ verify-single store is_single2 - if is_single2 is_single_label2 + python-if is_single2 is_single_label2 set-name test_is_single_false2 b end2 diff --git a/fa/signatures/test-project-ida/test-basic.sig b/fa/signatures/test-project-ida/test-basic.sig index c2a87be..df5bf30 100644 --- a/fa/signatures/test-project-ida/test-basic.sig +++ b/fa/signatures/test-project-ida/test-basic.sig @@ -152,7 +152,7 @@ verify-single store is_single1 - if is_single1 is_single_label1 + python-if is_single1 is_single_label1 add 1 set-name test_is_single_false1 b end1 @@ -168,7 +168,7 @@ verify-single store is_single2 - if is_single2 is_single_label2 + python-if is_single2 is_single_label2 set-name test_is_single_false2 b end2 diff --git a/fa/signatures/test-project-ida/test-ida-context.sig b/fa/signatures/test-project-ida/test-ida-context.sig index 61362c5..a9a5389 100644 --- a/fa/signatures/test-project-ida/test-ida-context.sig +++ b/fa/signatures/test-project-ida/test-ida-context.sig @@ -88,7 +88,7 @@ verify-operand mov --op0 0 store tmp - if tmp test_branch1 + python-if tmp test_branch1 set-name test_branch1_false label test_branch1 diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index 7ac71bd..786c73f 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -31,6 +31,11 @@ "trigger": "argument", "contents": "argument ${1:arg}" }, + { + "kind": "snippet", + "trigger": "b", + "contents": "b ${1:label}" + }, { "kind": "snippet", "trigger": "back", @@ -171,6 +176,11 @@ "trigger": "print", "contents": "print ${1:phrase}" }, + { + "kind": "snippet", + "trigger": "python-if", + "contents": "python-if ${1:cond} ${2:label}" + }, { "kind": "snippet", "trigger": "run", @@ -211,6 +221,11 @@ "trigger": "sort", "contents": "sort " }, + { + "kind": "snippet", + "trigger": "stop-if-empty", + "contents": "stop-if-empty " + }, { "kind": "snippet", "trigger": "store", diff --git a/scripts/git/pre-commit b/scripts/git/pre-commit index c58267e..cddaacd 100644 --- a/scripts/git/pre-commit +++ b/scripts/git/pre-commit @@ -22,12 +22,10 @@ SUBLIME_COMP = os.path.join( def main(): command_usage = OrderedDict() - command_usage['stop-if-empty'] = 'stop-if-empty' + command_usage['label'] = 'label' command_help = OrderedDict() - command_help['stop-if-empty'] = 'builtin interpreter command. \n' \ - 'stops parsing current SIG if current ' \ - 'resultset is empty\n' + command_help['label'] = 'builtin interpreter command. mark a label\n' commands = os.listdir(COMMANDS_ROOT) commands.sort() From 7cea4387d7100e19b74c6270e9edc549c96f410b Mon Sep 17 00:00:00 2001 From: DoronZ Date: Thu, 27 Aug 2020 00:22:11 +0300 Subject: [PATCH 07/18] commands: eval all int parameters --- commands.md | 61 ------------------- fa/commands/add.py | 4 +- fa/commands/add_offset_range.py | 9 +-- fa/commands/align.py | 4 +- fa/commands/and.py | 38 ------------ fa/commands/append.py | 15 ----- fa/commands/argument.py | 4 +- fa/commands/offset.py | 4 +- fa/commands/operand.py | 4 +- fa/commands/or.py | 38 ------------ fa/commands/single.py | 4 +- fa/commands/verify_operand.py | 6 +- fa/commands/xrefs_to.py | 1 - fa/signatures/test-project-elf/test-basic.sig | 34 ----------- fa/signatures/test-project-ida/test-basic.sig | 34 ----------- .../sublime/sig.sublime-completions | 15 ----- tests/test_commands/test_elf.py | 5 -- tests/test_commands/test_idalink.py | 5 -- 18 files changed, 20 insertions(+), 265 deletions(-) delete mode 100644 fa/commands/and.py delete mode 100644 fa/commands/append.py delete mode 100644 fa/commands/or.py diff --git a/commands.md b/commands.md index 8cecad1..af30224 100644 --- a/commands.md +++ b/commands.md @@ -4,8 +4,6 @@ Below is the list of available commands: - [add](#add) - [add-offset-range](#add-offset-range) - [align](#align) -- [and](#and) -- [append](#append) - [argument](#argument) - [b](#b) - [back](#back) @@ -34,7 +32,6 @@ Below is the list of available commands: - [most-common](#most-common) - [offset](#offset) - [operand](#operand) -- [or](#or) - [print](#print) - [python-if](#python-if) - [run](#run) @@ -113,41 +110,6 @@ EXAMPLE: positional arguments: value -optional arguments: - -h, --help show this help message and exit -``` -## and -``` -usage: and [-h] cmd [cmd ...] - -[DEPRECATED] -intersect with another command's resultset - -EXAMPLE: - results = [80] - -> and offset 0 - results = [80] - -EXAMPLE #2: - results = [80] - -> and offset 1 - results = [] - -positional arguments: - cmd command - -optional arguments: - -h, --help show this help message and exit -``` -## append -``` -usage: append [-h] cmd [cmd ...] - -append results from another command - -positional arguments: - cmd command - optional arguments: -h, --help show this help message and exit ``` @@ -657,29 +619,6 @@ EXAMPLE #1: positional arguments: op operand number -optional arguments: - -h, --help show this help message and exit -``` -## or -``` -usage: or [-h] cmd [cmd ...] - -[DEPRECATED] -unite with another command's resultset - -EXAMPLE: - results = [80] - -> or offset 0 - results = [80] - -EXAMPLE #2: - results = [80] - -> or offset 1 - results = [80, 81] - -positional arguments: - cmd command - optional arguments: -h, --help show this help message and exit ``` diff --git a/fa/commands/add.py b/fa/commands/add.py index 9588bd8..a66d4c0 100644 --- a/fa/commands/add.py +++ b/fa/commands/add.py @@ -14,9 +14,9 @@ def get_parser(): p = utils.ArgumentParserNoExit('add', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('value', type=int) + p.add_argument('value') return p def run(segments, args, addresses, interpreter=None, **kwargs): - return addresses + [args.value] + return addresses + [eval(args.value)] diff --git a/fa/commands/add_offset_range.py b/fa/commands/add_offset_range.py index 4499aaa..27334d6 100644 --- a/fa/commands/add_offset_range.py +++ b/fa/commands/add_offset_range.py @@ -15,9 +15,9 @@ def get_parser(): p = utils.ArgumentParserNoExit('add-offset-range', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('start', type=int) - p.add_argument('end', type=int) - p.add_argument('step', type=int) + p.add_argument('start') + p.add_argument('end') + p.add_argument('step') return p @@ -28,5 +28,6 @@ def add_offset_range(addresses, start, end, step): def run(segments, args, addresses, interpreter=None, **kwargs): - gen = add_offset_range(addresses, args.start, args.end, args.step) + gen = add_offset_range(addresses, eval(args.start), eval(args.end), + eval(args.step)) return list(gen) diff --git a/fa/commands/align.py b/fa/commands/align.py index 0a9c6d6..866f63d 100644 --- a/fa/commands/align.py +++ b/fa/commands/align.py @@ -14,7 +14,7 @@ def get_parser(): p = utils.ArgumentParserNoExit('align', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('value', type=int) + p.add_argument('value') return p @@ -23,4 +23,4 @@ def align(addresses, value): def run(segments, args, addresses, interpreter=None, **kwargs): - return list(align(addresses, args.value)) + return list(align(addresses, eval(args.value))) diff --git a/fa/commands/and.py b/fa/commands/and.py deleted file mode 100644 index 9b14687..0000000 --- a/fa/commands/and.py +++ /dev/null @@ -1,38 +0,0 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''[DEPRECATED] -intersect with another command's resultset - -EXAMPLE: - results = [80] - -> and offset 0 - results = [80] - -EXAMPLE #2: - results = [80] - -> and offset 1 - results = [] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('and', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('cmd', nargs='+', help='command') - return p - - -@utils.deprecated -def and_(addresses, cmd, interpreter): - results = set(addresses) - innert_command_results = interpreter.find_from_instructions_list( - [cmd], addresses=addresses) - results.intersection_update(innert_command_results) - return list(results) - - -def run(segments, args, addresses, interpreter=None, **kwargs): - cmd = args.cmd[0] + ' ' + ''.join('"{}"'.format(c) for c in args.cmd[1:]) - return and_(addresses, cmd, interpreter) diff --git a/fa/commands/append.py b/fa/commands/append.py deleted file mode 100644 index 18192be..0000000 --- a/fa/commands/append.py +++ /dev/null @@ -1,15 +0,0 @@ -from fa import utils - - -def get_parser(): - p = utils.ArgumentParserNoExit('append', - description='append results from another ' - 'command') - p.add_argument('cmd', nargs='+', help='command') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - cmd = args.cmd[0] + ' ' + ''.join('"{}"'.format(c) for c in args.cmd[1:]) - return addresses + interpreter.find_from_instructions_list( - [cmd], addresses=addresses) diff --git a/fa/commands/argument.py b/fa/commands/argument.py index fb276d0..f4f34be 100644 --- a/fa/commands/argument.py +++ b/fa/commands/argument.py @@ -24,7 +24,7 @@ def get_parser(): p = utils.ArgumentParserNoExit('argument', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('arg', type=int, help='argument number') + p.add_argument('arg', help='argument number') return p @@ -41,4 +41,4 @@ def argument(addresses, arg): def run(segments, args, addresses, interpreter=None, **kwargs): - return list(argument(addresses, args.arg)) + return list(argument(addresses, eval(args.arg))) diff --git a/fa/commands/offset.py b/fa/commands/offset.py index 7452db1..a30031f 100644 --- a/fa/commands/offset.py +++ b/fa/commands/offset.py @@ -14,7 +14,7 @@ def get_parser(): p = utils.ArgumentParserNoExit('offset', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('offset', type=int) + p.add_argument('offset') return p @@ -24,4 +24,4 @@ def offset(addresses, advance_by): def run(segments, args, addresses, interpreter=None, **kwargs): - return list(offset(addresses, args.offset)) + return list(offset(addresses, eval(args.offset))) diff --git a/fa/commands/operand.py b/fa/commands/operand.py index 671ffb5..474252f 100644 --- a/fa/commands/operand.py +++ b/fa/commands/operand.py @@ -23,7 +23,7 @@ def get_parser(): p = utils.ArgumentParserNoExit('operand', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('op', type=int, help='operand number') + p.add_argument('op', help='operand number') return p @@ -34,4 +34,4 @@ def operand(addresses, op): def run(segments, args, addresses, interpreter=None, **kwargs): - return list(operand(addresses, args.op)) + return list(operand(addresses, eval(args.op))) diff --git a/fa/commands/or.py b/fa/commands/or.py deleted file mode 100644 index b042401..0000000 --- a/fa/commands/or.py +++ /dev/null @@ -1,38 +0,0 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''[DEPRECATED] -unite with another command's resultset - -EXAMPLE: - results = [80] - -> or offset 0 - results = [80] - -EXAMPLE #2: - results = [80] - -> or offset 1 - results = [80, 81] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('or', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('cmd', nargs='+', help='command') - return p - - -@utils.deprecated -def or_(addresses, cmd, interpreter): - results = set(addresses) - results.update( - interpreter.find_from_instructions_list([cmd], addresses=addresses)) - - return list(results) - - -def run(segments, args, addresses, interpreter=None, **kwargs): - cmd = args.cmd[0] + ' ' + ''.join('"{}"'.format(c) for c in args.cmd[1:]) - return or_(addresses, cmd, interpreter) diff --git a/fa/commands/single.py b/fa/commands/single.py index 2d3b983..01b441e 100644 --- a/fa/commands/single.py +++ b/fa/commands/single.py @@ -14,7 +14,7 @@ def get_parser(): p = utils.ArgumentParserNoExit('single', description=DESCRIPTION, formatter_class=RawTextHelpFormatter) - p.add_argument('index', type=int, default=0, help='result index') + p.add_argument('index', default='0', help='result index') return p @@ -26,4 +26,4 @@ def single(addresses, index): def run(segments, args, addresses, interpreter=None, **kwargs): - return single(addresses, args.index) + return single(addresses, eval(args.index)) diff --git a/fa/commands/verify_operand.py b/fa/commands/verify_operand.py index eda9e1a..8a01842 100644 --- a/fa/commands/verify_operand.py +++ b/fa/commands/verify_operand.py @@ -67,9 +67,9 @@ def verify_operand(addresses, mnem, op0=None, op1=None, op2=None): def run(segments, args, addresses, interpreter=None, **kwargs): - op0 = [int(i) for i in args.op0.split(',')] if args.op0 else None - op1 = [int(i) for i in args.op1.split(',')] if args.op1 else None - op2 = [int(i) for i in args.op2.split(',')] if args.op2 else None + op0 = [eval(i) for i in args.op0.split(',')] if args.op0 else None + op1 = [eval(i) for i in args.op1.split(',')] if args.op1 else None + op2 = [eval(i) for i in args.op2.split(',')] if args.op2 else None return list(verify_operand(addresses, args.name, op0=op0, diff --git a/fa/commands/xrefs_to.py b/fa/commands/xrefs_to.py index eb4bd06..7dbe27b 100644 --- a/fa/commands/xrefs_to.py +++ b/fa/commands/xrefs_to.py @@ -23,7 +23,6 @@ def get_parser(): return p -@utils.deprecated @context.ida_context def run(segments, args, addresses, interpreter=None, **kwargs): if args.name: diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index df5bf30..e9a4b12 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -70,12 +70,6 @@ clear - add 1 - append add 2 - set-name test_append - - clear - find-bytes 11223344 set-name test_find_bytes @@ -93,34 +87,6 @@ clear - add 80 - or offset 0 - verify-single - set-name test_or_80 - - clear - - add 80 - or offset 1 - sort - single -1 - set-name test_or_81 - - clear - - add 80 - and offset 0 - verify-single - set-name test_and_80 - - clear - - add 80 - and offset 1 - set-name test_and_81 - - clear - add 1 add 2 add 3 diff --git a/fa/signatures/test-project-ida/test-basic.sig b/fa/signatures/test-project-ida/test-basic.sig index df5bf30..e9a4b12 100644 --- a/fa/signatures/test-project-ida/test-basic.sig +++ b/fa/signatures/test-project-ida/test-basic.sig @@ -70,12 +70,6 @@ clear - add 1 - append add 2 - set-name test_append - - clear - find-bytes 11223344 set-name test_find_bytes @@ -93,34 +87,6 @@ clear - add 80 - or offset 0 - verify-single - set-name test_or_80 - - clear - - add 80 - or offset 1 - sort - single -1 - set-name test_or_81 - - clear - - add 80 - and offset 0 - verify-single - set-name test_and_80 - - clear - - add 80 - and offset 1 - set-name test_and_81 - - clear - add 1 add 2 add 3 diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index 786c73f..308dd4c 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -16,16 +16,6 @@ "trigger": "align", "contents": "align ${1:value}" }, - { - "kind": "snippet", - "trigger": "and", - "contents": "and ${1:cmd} ${2:cmd} ..." - }, - { - "kind": "snippet", - "trigger": "append", - "contents": "append ${1:cmd} ${2:cmd} ..." - }, { "kind": "snippet", "trigger": "argument", @@ -166,11 +156,6 @@ "trigger": "operand", "contents": "operand ${1:op}" }, - { - "kind": "snippet", - "trigger": "or", - "contents": "or ${1:cmd} ${2:cmd} ..." - }, { "kind": "snippet", "trigger": "print", diff --git a/tests/test_commands/test_elf.py b/tests/test_commands/test_elf.py index 50441f7..f91a68a 100644 --- a/tests/test_commands/test_elf.py +++ b/tests/test_commands/test_elf.py @@ -27,14 +27,9 @@ def test_elf_symbols(sample_elf): assert symbols['test_alias'] == 0x123c assert symbols['test_keystone_find_opcodes'] == 0x123c assert symbols['test_keystone_verify_opcodes'] == 0x123c - assert symbols['test_append'] == 2 assert symbols['test_find_bytes'] == 0x1240 assert symbols['test_find_str'] == 0x1242 assert symbols['test_find'] == 76 - assert symbols['test_or_80'] == 80 - assert symbols['test_or_81'] == 81 - assert symbols['test_and_80'] == 80 - assert 'test_ond_81' not in symbols assert symbols['test_intersect_ab'] == 2 assert 'test_intersect_abc' not in symbols diff --git a/tests/test_commands/test_idalink.py b/tests/test_commands/test_idalink.py index ac07145..4c4f6cd 100644 --- a/tests/test_commands/test_idalink.py +++ b/tests/test_commands/test_idalink.py @@ -86,14 +86,9 @@ def test_ida_symbols(ida, sample_elf): assert symbols['test_alias'] == 0x123c assert symbols['test_keystone_find_opcodes'] == 0x123c assert symbols['test_keystone_verify_opcodes'] == 0x123c - assert symbols['test_append'] == 2 assert symbols['test_find_bytes'] == 0x1240 assert symbols['test_find_str'] == 0x1242 assert symbols['test_find'] == 76 - assert symbols['test_or_80'] == 80 - assert symbols['test_or_81'] == 81 - assert symbols['test_and_80'] == 80 - assert 'test_ond_81' not in symbols assert symbols['test_intersect_ab'] == 2 assert 'test_intersect_abc' not in symbols From dc0fb0662898e69776e9e52b6025e4f7d5165400 Mon Sep 17 00:00:00 2001 From: DoronZ Date: Thu, 27 Aug 2020 00:28:51 +0300 Subject: [PATCH 08/18] ida_plugin: remove decremntal search option --- fa/ida_plugin.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/fa/ida_plugin.py b/fa/ida_plugin.py index 738f0eb..fe2795e 100644 --- a/fa/ida_plugin.py +++ b/fa/ida_plugin.py @@ -186,14 +186,11 @@ def find(self, symbol_name, decremental=False): Find symbol by name (as specified in SIG file) Show an IDA waitbox while doing so :param symbol_name: symbol name - :param decremental: Should stop before reaching a command with no - results? :return: output address list """ ida_kernwin.replace_wait_box('Searching symbol: \'{}\'...' .format(symbol_name)) - return super(IdaLoader, self).find(symbol_name, - decremental=decremental) + return super(IdaLoader, self).find(symbol_name) def get_python_symbols(self, file_name=None): """ From 9ecceb9ba4acd08aade5960b95f850fe0b7f7f66 Mon Sep 17 00:00:00 2001 From: DoronZ Date: Thu, 27 Aug 2020 00:36:23 +0300 Subject: [PATCH 09/18] doc: improve documentation --- README.md | 22 +++++++++---------- fa/commands/b.py | 1 + fa/commands/python_if.py | 1 + fa/signatures/test-project-elf/test-basic.sig | 9 ++++---- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a379676..ed29942 100644 --- a/README.md +++ b/README.md @@ -348,21 +348,21 @@ To view the list of available commands, [view the list below](#available-command # mark as 'case_opcode_bl' label label case_opcode_bl - # make: #define is_bl (1) - clear - add 1 - set-const is_bl - - # finish script by jumping to end - b end + # make: #define is_bl (1) + clear + add 1 + set-const is_bl + + # finish script by jumping to end + b end # mark as 'not_bl' label label not_bl - # make: #define is_ldr (1) - clear - add 1 - set-const is_ldr + # make: #define is_ldr (1) + clear + add 1 + set-const is_ldr # mark script end label end diff --git a/fa/commands/b.py b/fa/commands/b.py index bfa0442..2925995 100644 --- a/fa/commands/b.py +++ b/fa/commands/b.py @@ -26,5 +26,6 @@ def get_parser(): def run(segments, args, addresses, interpreter=None, **kwargs): interpreter.set_pc(args.label) + # pc is incremented by 1, after each instruction interpreter.dec_pc() return addresses diff --git a/fa/commands/python_if.py b/fa/commands/python_if.py index 3b6385a..0004368 100644 --- a/fa/commands/python_if.py +++ b/fa/commands/python_if.py @@ -33,5 +33,6 @@ def get_parser(): def run(segments, args, addresses, interpreter=None, **kwargs): if eval(args.cond, interpreter.get_all_variables()): interpreter.set_pc(args.label) + # pc is incremented by 1, after each instruction interpreter.dec_pc() return addresses diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index e9a4b12..7e027e2 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -119,11 +119,12 @@ verify-single store is_single1 python-if is_single1 is_single_label1 - add 1 - set-name test_is_single_false1 - b end1 + add 1 + set-name test_is_single_false1 + b end1 + label is_single_label1 - set-name test_is_single_true1 + set-name test_is_single_true1 label end1 From b88b588d3f0eb06f6a345e9222fd10de4377b23c Mon Sep 17 00:00:00 2001 From: DoronZ Date: Thu, 27 Aug 2020 23:02:55 +0300 Subject: [PATCH 10/18] command: remove history command --- commands.md | 23 -------------- fa/commands/back.py | 30 ------------------- fa/fainterp.py | 3 -- .../sublime/sig.sublime-completions | 5 ---- 4 files changed, 61 deletions(-) delete mode 100644 fa/commands/back.py diff --git a/commands.md b/commands.md index af30224..4579ad5 100644 --- a/commands.md +++ b/commands.md @@ -6,7 +6,6 @@ Below is the list of available commands: - [align](#align) - [argument](#argument) - [b](#b) -- [back](#back) - [clear](#clear) - [find](#find) - [find-bytes](#find-bytes) @@ -155,28 +154,6 @@ EXAMPLE: positional arguments: label label to jump to -optional arguments: - -h, --help show this help message and exit -``` -## back -``` -usage: back [-h] amount - -go back to previous result-set - -EXAMPLE: - find-bytes --or 01 02 03 04 - results = [0, 0x100, 0x200] - - find-bytes --or 05 06 07 08 - results = [0, 0x100, 0x200, 0x300, 0x400] - - -> back -3 - results = [0, 0x100, 0x200] - -positional arguments: - amount amount of command results to go back by - optional arguments: -h, --help show this help message and exit ``` diff --git a/fa/commands/back.py b/fa/commands/back.py deleted file mode 100644 index 848eaa1..0000000 --- a/fa/commands/back.py +++ /dev/null @@ -1,30 +0,0 @@ -from argparse import RawTextHelpFormatter -from fa import utils - - -DESCRIPTION = '''go back to previous result-set - -EXAMPLE: - find-bytes --or 01 02 03 04 - results = [0, 0x100, 0x200] - - find-bytes --or 05 06 07 08 - results = [0, 0x100, 0x200, 0x300, 0x400] - - -> back -3 - results = [0, 0x100, 0x200] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('back', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('amount', type=int, - help='amount of command results to go back by') - return p - - -@utils.deprecated -def run(segments, args, addresses, interpreter=None, **kwargs): - return interpreter.history[-args.amount] diff --git a/fa/fainterp.py b/fa/fainterp.py index 96714e7..47b9323 100644 --- a/fa/fainterp.py +++ b/fa/fainterp.py @@ -405,8 +405,6 @@ def find_from_instructions_list(self, instructions, addresses=None): if addresses is None: addresses = [] - self.history = [] - self._push_stack_frame() labels, instructions = self._get_labeled_instructions(instructions) @@ -426,7 +424,6 @@ def find_from_instructions_list(self, instructions, addresses=None): .format(line, str(m))) addresses = new_addresses - self.history.append(addresses) self.inc_pc() self._pop_stack_frame() diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index 308dd4c..9962ff1 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -26,11 +26,6 @@ "trigger": "b", "contents": "b ${1:label}" }, - { - "kind": "snippet", - "trigger": "back", - "contents": "back ${1:amount}" - }, { "kind": "snippet", "trigger": "clear", From b55ae21ed5d729ba7d4591399a9aa91907734e5a Mon Sep 17 00:00:00 2001 From: DoronZ Date: Thu, 27 Aug 2020 23:10:27 +0300 Subject: [PATCH 11/18] command: add if command --- commands.md | 29 ++++++++++++++ fa/commands/if.py | 38 +++++++++++++++++++ fa/commands/python_if.py | 1 + fa/signatures/test-project-elf/test-basic.sig | 19 ++++++++++ fa/signatures/test-project-ida/test-basic.sig | 28 ++++++++++++-- .../sublime/sig.sublime-completions | 5 +++ tests/test_commands/test_elf.py | 4 +- tests/test_commands/test_idalink.py | 3 ++ 8 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 fa/commands/if.py diff --git a/commands.md b/commands.md index 4579ad5..fb96ab5 100644 --- a/commands.md +++ b/commands.md @@ -16,6 +16,7 @@ Below is the list of available commands: - [function-lines](#function-lines) - [function-start](#function-start) - [goto-ref](#goto-ref) +- [if](#if) - [intersect](#intersect) - [keystone-find-opcodes](#keystone-find-opcodes) - [keystone-verify-opcodes](#keystone-verify-opcodes) @@ -355,6 +356,33 @@ optional arguments: --code include code references --data include data references ``` +## if +``` +usage: if [-h] cond label + +perform an 'if' statement to create conditional branches +using an FA command + +EXAMPLE: + results = [0, 4, 8] + + -> if 'verify-single' a_is_single_label + + set-name a_isnt_single + b end + + label a_is_single_label + set-name a_is_single + + label end + +positional arguments: + cond condition as an FA command + label label to jump to if condition is true + +optional arguments: + -h, --help show this help message and exit +``` ## intersect ``` usage: intersect [-h] variables [variables ...] @@ -616,6 +644,7 @@ optional arguments: usage: python-if [-h] cond label perform an 'if' statement to create conditional branches +using an eval'ed expression EXAMPLE: results = [0, 4, 8] diff --git a/fa/commands/if.py b/fa/commands/if.py new file mode 100644 index 0000000..c080fa4 --- /dev/null +++ b/fa/commands/if.py @@ -0,0 +1,38 @@ +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''perform an 'if' statement to create conditional branches +using an FA command + +EXAMPLE: + results = [0, 4, 8] + + -> if 'verify-single' a_is_single_label + + set-name a_isnt_single + b end + + label a_is_single_label + set-name a_is_single + + label end +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('if', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('cond', help='condition as an FA command') + p.add_argument('label', help='label to jump to if condition is true') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + if len(interpreter.find_from_instructions_list([args.cond], + addresses=addresses)): + interpreter.set_pc(args.label) + + # pc is incremented by 1, after each instruction + interpreter.dec_pc() + return addresses diff --git a/fa/commands/python_if.py b/fa/commands/python_if.py index 0004368..3b1a678 100644 --- a/fa/commands/python_if.py +++ b/fa/commands/python_if.py @@ -2,6 +2,7 @@ from fa import utils DESCRIPTION = '''perform an 'if' statement to create conditional branches +using an eval'ed expression EXAMPLE: results = [0, 4, 8] diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index 7e027e2..507af25 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -144,5 +144,24 @@ label end2 + clear + + add 1 + + if 'verify-single' is_single_label3 + + clear + add 1 + set-name test_else3 + b end3 + + label is_single_label3 + + clear + add 1 + set-name test_if3 + + label end3 + ] } diff --git a/fa/signatures/test-project-ida/test-basic.sig b/fa/signatures/test-project-ida/test-basic.sig index e9a4b12..507af25 100644 --- a/fa/signatures/test-project-ida/test-basic.sig +++ b/fa/signatures/test-project-ida/test-basic.sig @@ -119,11 +119,12 @@ verify-single store is_single1 python-if is_single1 is_single_label1 - add 1 - set-name test_is_single_false1 - b end1 + add 1 + set-name test_is_single_false1 + b end1 + label is_single_label1 - set-name test_is_single_true1 + set-name test_is_single_true1 label end1 @@ -143,5 +144,24 @@ label end2 + clear + + add 1 + + if 'verify-single' is_single_label3 + + clear + add 1 + set-name test_else3 + b end3 + + label is_single_label3 + + clear + add 1 + set-name test_if3 + + label end3 + ] } diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index 9962ff1..53d6f99 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -76,6 +76,11 @@ "trigger": "goto-ref", "contents": "goto-ref --code --data" }, + { + "kind": "snippet", + "trigger": "if", + "contents": "if ${1:cond} ${2:label}" + }, { "kind": "snippet", "trigger": "intersect", diff --git a/tests/test_commands/test_elf.py b/tests/test_commands/test_elf.py index f91a68a..9e9fa3b 100644 --- a/tests/test_commands/test_elf.py +++ b/tests/test_commands/test_elf.py @@ -34,9 +34,11 @@ def test_elf_symbols(sample_elf): assert 'test_intersect_abc' not in symbols # test for branches - print(symbols) assert 'test_is_single_false1' in symbols assert 'test_is_single_true1' not in symbols assert 'test_is_single_false2' not in symbols assert 'test_is_single_true2' in symbols + + assert 'test_else3' not in symbols + assert 'test_if3' in symbols diff --git a/tests/test_commands/test_idalink.py b/tests/test_commands/test_idalink.py index 4c4f6cd..29ff5b2 100644 --- a/tests/test_commands/test_idalink.py +++ b/tests/test_commands/test_idalink.py @@ -99,6 +99,9 @@ def test_ida_symbols(ida, sample_elf): assert 'test_is_single_false2' not in symbols assert 'test_is_single_true2' in symbols + assert 'test_else3' not in symbols + assert 'test_if3' in symbols + # from test-ida-context assert symbols['test_find_bytes_ida'] == 0x1240 assert symbols['test_xref'] == 0x125c From 8d452c31f3d40911f29330555a94df3b3943e38e Mon Sep 17 00:00:00 2001 From: DoronZ Date: Thu, 27 Aug 2020 23:12:21 +0300 Subject: [PATCH 12/18] doc: update README --- README.md | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ed29942..277b25f 100644 --- a/README.md +++ b/README.md @@ -332,37 +332,24 @@ To view the list of available commands, [view the list below](#available-command # goto printf function locate printf - # save current resultset - store tmp - - # is_bl = is current opcode a bl? - verify-operand bl - store is_bl - - # load previous resultset - load tmp - # goto 'case_opcode_bl' if current opcode is bl - if is_bl case_opcode_bl + if 'verify-operand bl' case_opcode_bl - # mark as 'case_opcode_bl' label - label case_opcode_bl - - # make: #define is_bl (1) + # make: #define is_bl (0) clear - add 1 + add 0 set-const is_bl # finish script by jumping to end b end - # mark as 'not_bl' label - label not_bl + # mark as 'case_opcode_bl' label + label case_opcode_bl - # make: #define is_ldr (1) + # make: #define is_bl (1) clear add 1 - set-const is_ldr + set-const is_bl # mark script end label end From 0f9455c82d6220df81130284fa4d0c35d24b3a36 Mon Sep 17 00:00:00 2001 From: Alon Karasik Date: Sat, 29 Aug 2020 17:04:47 +0300 Subject: [PATCH 13/18] pytest: ignore deprecation warning for collections in idalink The deprecation warning is for python 3.3 onwards, but idalink currently only supports python 2 anyways, as we don't have IDA 7.4+ to adapt it to python3. --- pytest.ini | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 pytest.ini diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..66ee939 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore:Using or importing the ABCs from 'collections' instead of from 'collections\.abc' is deprecated:DeprecationWarning \ No newline at end of file From 8fb0a8cb11246ff4e3395ba5ebe9bdff1da2a0f7 Mon Sep 17 00:00:00 2001 From: Alon Karasik Date: Sat, 29 Aug 2020 16:41:50 +0300 Subject: [PATCH 14/18] git: pre-commit: run with python3 --- commands.md | 3 +- .../sublime/sig.sublime-completions | 332 +++++++++--------- scripts/git/pre-commit | 2 +- 3 files changed, 168 insertions(+), 169 deletions(-) diff --git a/commands.md b/commands.md index fb96ab5..a328a7d 100644 --- a/commands.md +++ b/commands.md @@ -1006,8 +1006,7 @@ optional arguments: ``` ## xrefs-to ``` -usage: xrefs-to [-h] [--function-start] [--or] [--and] [--name NAME] - [--bytes BYTES] +usage: xrefs-to [-h] [--function-start] [--or] [--and] [--name NAME] [--bytes BYTES] search for xrefs pointing at given parameter diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index 53d6f99..15fa081 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -1,280 +1,280 @@ { - "scope": "source.hjson meta.structure.dictionary.hjson meta.structure.key-value.hjson meta.structure.array.hjson", + "scope": "source.hjson meta.structure.dictionary.hjson meta.structure.key-value.hjson meta.structure.array.hjson", "completions": [ { - "kind": "snippet", - "trigger": "add", + "trigger": "add", + "kind": "snippet", "contents": "add ${1:value}" - }, + }, { - "kind": "snippet", - "trigger": "add-offset-range", + "trigger": "add-offset-range", + "kind": "snippet", "contents": "add-offset-range ${1:start} ${2:end} ${3:step}" - }, + }, { - "kind": "snippet", - "trigger": "align", + "trigger": "align", + "kind": "snippet", "contents": "align ${1:value}" - }, + }, { - "kind": "snippet", - "trigger": "argument", + "trigger": "argument", + "kind": "snippet", "contents": "argument ${1:arg}" - }, + }, { - "kind": "snippet", - "trigger": "b", + "trigger": "b", + "kind": "snippet", "contents": "b ${1:label}" - }, + }, { - "kind": "snippet", - "trigger": "clear", + "trigger": "clear", + "kind": "snippet", "contents": "clear " - }, + }, { - "kind": "snippet", - "trigger": "find", + "trigger": "find", + "kind": "snippet", "contents": "find ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "find-bytes", + "trigger": "find-bytes", + "kind": "snippet", "contents": "find-bytes ${1:hex_str}" - }, + }, { - "kind": "snippet", - "trigger": "find-bytes-ida", + "trigger": "find-bytes-ida", + "kind": "snippet", "contents": "find-bytes-ida ${1:expression}" - }, + }, { - "kind": "snippet", - "trigger": "find-immediate", + "trigger": "find-immediate", + "kind": "snippet", "contents": "find-immediate ${1:expression}" - }, + }, { - "kind": "snippet", - "trigger": "find-str", + "trigger": "find-str", + "kind": "snippet", "contents": "find-str --null-terminated ${1:hex_str}" - }, + }, { - "kind": "snippet", - "trigger": "function-end", + "trigger": "function-end", + "kind": "snippet", "contents": "function-end " - }, + }, { - "kind": "snippet", - "trigger": "function-lines", + "trigger": "function-lines", + "kind": "snippet", "contents": "function-lines --after | --before" - }, + }, { - "kind": "snippet", - "trigger": "function-start", + "trigger": "function-start", + "kind": "snippet", "contents": "function-start ${1:cmd} ${2:cmd} ..." - }, + }, { - "kind": "snippet", - "trigger": "goto-ref", + "trigger": "goto-ref", + "kind": "snippet", "contents": "goto-ref --code --data" - }, + }, { - "kind": "snippet", - "trigger": "if", + "trigger": "if", + "kind": "snippet", "contents": "if ${1:cond} ${2:label}" - }, + }, { - "kind": "snippet", - "trigger": "intersect", + "trigger": "intersect", + "kind": "snippet", "contents": "intersect ${1:variables} ${2:variables} ..." - }, + }, { - "kind": "snippet", - "trigger": "keystone-find-opcodes", + "trigger": "keystone-find-opcodes", + "kind": "snippet", "contents": "keystone-find-opcodes --bele --or ${1:arch} ${2:mode} ${3:code}" - }, + }, { - "kind": "snippet", - "trigger": "keystone-verify-opcodes", + "trigger": "keystone-verify-opcodes", + "kind": "snippet", "contents": "keystone-verify-opcodes --bele --until ${1:UNTIL} ${2:arch} ${3:mode} ${4:code}" - }, + }, { - "kind": "snippet", - "trigger": "load", + "trigger": "load", + "kind": "snippet", "contents": "load ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "locate", + "trigger": "locate", + "kind": "snippet", "contents": "locate ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "make-code", + "trigger": "make-code", + "kind": "snippet", "contents": "make-code " - }, + }, { - "kind": "snippet", - "trigger": "make-comment", + "trigger": "make-comment", + "kind": "snippet", "contents": "make-comment ${1:comment}" - }, + }, { - "kind": "snippet", - "trigger": "make-function", + "trigger": "make-function", + "kind": "snippet", "contents": "make-function " - }, + }, { - "kind": "snippet", - "trigger": "make-literal", + "trigger": "make-literal", + "kind": "snippet", "contents": "make-literal " - }, + }, { - "kind": "snippet", - "trigger": "make-unknown", + "trigger": "make-unknown", + "kind": "snippet", "contents": "make-unknown " - }, + }, { - "kind": "snippet", - "trigger": "max-xrefs", + "trigger": "max-xrefs", + "kind": "snippet", "contents": "max-xrefs " - }, + }, { - "kind": "snippet", - "trigger": "min-xrefs", + "trigger": "min-xrefs", + "kind": "snippet", "contents": "min-xrefs " - }, + }, { - "kind": "snippet", - "trigger": "most-common", + "trigger": "most-common", + "kind": "snippet", "contents": "most-common " - }, + }, { - "kind": "snippet", - "trigger": "offset", + "trigger": "offset", + "kind": "snippet", "contents": "offset ${1:offset}" - }, + }, { - "kind": "snippet", - "trigger": "operand", + "trigger": "operand", + "kind": "snippet", "contents": "operand ${1:op}" - }, + }, { - "kind": "snippet", - "trigger": "print", + "trigger": "print", + "kind": "snippet", "contents": "print ${1:phrase}" - }, + }, { - "kind": "snippet", - "trigger": "python-if", + "trigger": "python-if", + "kind": "snippet", "contents": "python-if ${1:cond} ${2:label}" - }, + }, { - "kind": "snippet", - "trigger": "run", + "trigger": "run", + "kind": "snippet", "contents": "run ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "set-const", + "trigger": "set-const", + "kind": "snippet", "contents": "set-const ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "set-enum", + "trigger": "set-enum", + "kind": "snippet", "contents": "set-enum ${1:enum_name} ${2:enum_key}" - }, + }, { - "kind": "snippet", - "trigger": "set-name", + "trigger": "set-name", + "kind": "snippet", "contents": "set-name ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "set-struct-member", + "trigger": "set-struct-member", + "kind": "snippet", "contents": "set-struct-member ${1:struct_name} ${2:member_name} ${3:member_type}" - }, + }, { - "kind": "snippet", - "trigger": "set-type", + "trigger": "set-type", + "kind": "snippet", "contents": "set-type ${1:type_str}" - }, + }, { - "kind": "snippet", - "trigger": "single", + "trigger": "single", + "kind": "snippet", "contents": "single ${1:index}" - }, + }, { - "kind": "snippet", - "trigger": "sort", + "trigger": "sort", + "kind": "snippet", "contents": "sort " - }, + }, { - "kind": "snippet", - "trigger": "stop-if-empty", + "trigger": "stop-if-empty", + "kind": "snippet", "contents": "stop-if-empty " - }, + }, { - "kind": "snippet", - "trigger": "store", + "trigger": "store", + "kind": "snippet", "contents": "store ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "trace", + "trigger": "trace", + "kind": "snippet", "contents": "trace " - }, + }, { - "kind": "snippet", - "trigger": "unique", + "trigger": "unique", + "kind": "snippet", "contents": "unique " - }, + }, { - "kind": "snippet", - "trigger": "verify-aligned", + "trigger": "verify-aligned", + "kind": "snippet", "contents": "verify-aligned ${1:value}" - }, + }, { - "kind": "snippet", - "trigger": "verify-bytes", + "trigger": "verify-bytes", + "kind": "snippet", "contents": "verify-bytes ${1:hex_str}" - }, + }, { - "kind": "snippet", - "trigger": "verify-name", + "trigger": "verify-name", + "kind": "snippet", "contents": "verify-name ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "verify-operand", + "trigger": "verify-operand", + "kind": "snippet", "contents": "verify-operand --op0 ${1:OP0} --op1 ${2:OP1} --op2 ${3:OP2} ${4:name}" - }, + }, { - "kind": "snippet", - "trigger": "verify-ref", + "trigger": "verify-ref", + "kind": "snippet", "contents": "verify-ref --code --data --name ${1:NAME}" - }, + }, { - "kind": "snippet", - "trigger": "verify-segment", + "trigger": "verify-segment", + "kind": "snippet", "contents": "verify-segment ${1:name}" - }, + }, { - "kind": "snippet", - "trigger": "verify-single", + "trigger": "verify-single", + "kind": "snippet", "contents": "verify-single " - }, + }, { - "kind": "snippet", - "trigger": "verify-str", + "trigger": "verify-str", + "kind": "snippet", "contents": "verify-str --null-terminated ${1:hex_str}" - }, + }, { - "kind": "snippet", - "trigger": "xref", + "trigger": "xref", + "kind": "snippet", "contents": "xref " - }, + }, { - "kind": "snippet", - "trigger": "xrefs-to", - "contents": "xrefs-to --function-start --or --and --name ${1:NAME} --bytes ${2:BYTES}" + "trigger": "xrefs-to", + "kind": "snippet", + "contents": "xrefs-to --function-start --or --and --name ${1:NAME} --bytes ${2:BYTES}" } ] } \ No newline at end of file diff --git a/scripts/git/pre-commit b/scripts/git/pre-commit index cddaacd..5180915 100644 --- a/scripts/git/pre-commit +++ b/scripts/git/pre-commit @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 import json import re from collections import OrderedDict From 47d2082ecba74a546c7a1c81b04d9ba05aa0fa9f Mon Sep 17 00:00:00 2001 From: Alon Karasik Date: Sat, 29 Aug 2020 16:47:19 +0300 Subject: [PATCH 15/18] git: pre-commit: update diff warning Updated diff warning to reflect the fact that the script may change not only commands.md but also ide-completions/ directory. --- scripts/git/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/git/pre-commit b/scripts/git/pre-commit index 5180915..eb94fa4 100644 --- a/scripts/git/pre-commit +++ b/scripts/git/pre-commit @@ -95,7 +95,7 @@ def main(): f.write(json.dumps(sublime_completions, indent=4)) if current_buf != commands_md_buf: - print('commands.md has been changed. please review and then commit') + print('commands.md and / or ide-completions/ has been changed. Please review and then commit again.') sys.exit(1) From cf25814b170129d4a72f9de7a6fdac0af7047163 Mon Sep 17 00:00:00 2001 From: Alon Karasik Date: Sat, 29 Aug 2020 16:16:58 +0300 Subject: [PATCH 16/18] commads: add symdiff command --- commands.md | 23 +++++++++++++ fa/commands/symdiff.py | 33 +++++++++++++++++++ fa/signatures/test-project-elf/test-basic.sig | 28 ++++++++++++++++ .../sublime/sig.sublime-completions | 5 +++ tests/test_commands/test_elf.py | 3 ++ 5 files changed, 92 insertions(+) create mode 100644 fa/commands/symdiff.py diff --git a/commands.md b/commands.md index a328a7d..0e97828 100644 --- a/commands.md +++ b/commands.md @@ -44,6 +44,7 @@ Below is the list of available commands: - [sort](#sort) - [stop-if-empty](#stop-if-empty) - [store](#store) +- [symdiff](#symdiff) - [trace](#trace) - [unique](#unique) - [verify-aligned](#verify-aligned) @@ -812,6 +813,28 @@ EXAMPLE: positional arguments: name name of variable to use +optional arguments: + -h, --help show this help message and exit +``` +## symdiff +``` +usage: symdiff [-h] variables [variables ...] + +symmetric difference between two or more variables + +EXAMPLE: + results = [0, 4, 8] + store a + ... + results = [0, 12, 20] + store b + + -> symdiff a b + results = [4, 8, 12, 20] + +positional arguments: + variables variable names + optional arguments: -h, --help show this help message and exit ``` diff --git a/fa/commands/symdiff.py b/fa/commands/symdiff.py new file mode 100644 index 0000000..02d9228 --- /dev/null +++ b/fa/commands/symdiff.py @@ -0,0 +1,33 @@ +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''symmetric difference between two or more variables + +EXAMPLE: + results = [0, 4, 8] + store a + ... + results = [0, 12, 20] + store b + + -> symdiff a b + results = [4, 8, 12, 20] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('symdiff', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('variables', nargs='+', help='variable names') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + first_var = args.variables[0] + results = set(interpreter.get_variable(first_var)) + + for c in args.variables[1:]: + results.symmetric_difference_update(interpreter.get_variable(c)) + + return list(results) diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index 507af25..11351a4 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -113,6 +113,34 @@ clear + add 2 + + store a + + add 4 + + store b + store c + + clear + + add 8 + + store d + + clear + + symdiff a b + set-name test_symdiff_ab + + symdiff b c + set-name test_symdiff_bc + + symdiff b c d + set-name test_symdiff_bcd + + clear + add 1 add 2 diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index 15fa081..37493f4 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -216,6 +216,11 @@ "kind": "snippet", "contents": "store ${1:name}" }, + { + "trigger": "symdiff", + "kind": "snippet", + "contents": "symdiff ${1:variables} ${2:variables} ..." + }, { "trigger": "trace", "kind": "snippet", diff --git a/tests/test_commands/test_elf.py b/tests/test_commands/test_elf.py index 9e9fa3b..b70d121 100644 --- a/tests/test_commands/test_elf.py +++ b/tests/test_commands/test_elf.py @@ -32,6 +32,9 @@ def test_elf_symbols(sample_elf): assert symbols['test_find'] == 76 assert symbols['test_intersect_ab'] == 2 assert 'test_intersect_abc' not in symbols + assert symbols['test_symdiff_ab'] == 4 + assert 'test_symdiff_bc' not in symbols + assert symbols['test_symdiff_bcd'] == 8 # test for branches assert 'test_is_single_false1' in symbols From 345265ba6870f45427bcc455aba158f6c57808e1 Mon Sep 17 00:00:00 2001 From: DoronZ Date: Sat, 29 Aug 2020 18:32:19 +0300 Subject: [PATCH 17/18] convert all EOLs CRLF->LF --- LICENSE | 1346 +++++++-------- README.md | 1006 ++++++------ commands.md | 3 +- elf_loader.py | 100 +- fa/commands/add.py | 44 +- fa/commands/add_offset_range.py | 66 +- fa/commands/alias | 18 +- fa/commands/align.py | 52 +- fa/commands/b.py | 62 +- fa/commands/find.py | 32 +- fa/commands/find_bytes.py | 72 +- fa/commands/function_start.py | 94 +- fa/commands/if.py | 76 +- fa/commands/intersect.py | 66 +- fa/commands/keystone_find_opcodes.py | 110 +- fa/commands/keystone_verify_opcodes.py | 114 +- fa/commands/load.py | 56 +- fa/commands/make_comment.py | 86 +- fa/commands/offset.py | 54 +- fa/commands/print.py | 36 +- fa/commands/python_if.py | 78 +- fa/commands/run.py | 30 +- fa/commands/single.py | 58 +- fa/commands/sort.py | 52 +- fa/commands/stop_if_empty.py | 56 +- fa/commands/store.py | 58 +- fa/commands/trace.py | 36 +- fa/commands/unique.py | 42 +- fa/commands/verify_aligned.py | 52 +- fa/commands/verify_bytes.py | 86 +- fa/commands/verify_segment.py | 86 +- fa/commands/verify_single.py | 60 +- fa/commands/verify_str.py | 66 +- fa/commands/xrefs_to.py | 106 +- fa/context.py | 102 +- fa/fa_types.py | 176 +- fa/fainterp.py | 1098 ++++++------- fa/ida_plugin.py | 1446 ++++++++--------- fa/signatures/test-project-elf/test-basic.sig | 390 ++--- fa/signatures/test-project-elf/test_dep.dep | 14 +- fa/signatures/test-project-elf/test_find.sig | 14 +- fa/signatures/test-project-ida/test-basic.sig | 334 ++-- .../test-project-ida/test-ida-context.sig | 194 +-- fa/signatures/test-project-ida/test_dep.dep | 14 +- fa/signatures/test-project-ida/test_find.sig | 14 +- fa/utils.py | 216 +-- ide-completions/sublime/README.md | 18 +- .../sublime/sig.sublime-completions | 2 +- requirements.txt | 10 +- requirements_testing.txt | 18 +- scripts/git/install.py | 20 +- scripts/git/pre-commit | 206 +-- setup.py | 56 +- 53 files changed, 4351 insertions(+), 4350 deletions(-) diff --git a/LICENSE b/LICENSE index 871ce8e..e72bfdd 100644 --- a/LICENSE +++ b/LICENSE @@ -1,674 +1,674 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read . \ No newline at end of file diff --git a/README.md b/README.md index 277b25f..f38d5d7 100644 --- a/README.md +++ b/README.md @@ -1,503 +1,503 @@ -![Python application](https://github.com/doronz88/fa/workflows/Python%20application/badge.svg) - -# FA - -## What is it? - -FA stands for Firmware Analysis and intended **For Humans**. - -FA allows one to easily perform code exploration, symbol searching and -other functionality with ease. - -Usually such tasks would require you to understand complicated APIs, -write machine-dependant code and perform other tedious tasks. -FA is meant to replace the steps one usually performs like a robot -(find X string, goto xref, find the next call function, ...) in -a much friendlier and maintainable manner. - -The current codebase is very IDA-plugin-oriented. In the future I'll -consider adding compatibility for other disassemblers such as: -Ghidra, Radare and etc... - - -Pull Requests are of course more than welcome :smirk:. - -## Requirements - -Supported IDA 7.x. - -In your IDA's python directory, run: - -```sh -python -m pip install -r requirements.txt -``` - -And for testing: -```sh -python -m pip install -r requirements_testing.txt -``` - -## Where to start? - -Before using, you should understand the terminology for: -Projects, SIG files and Loaders. - -So, grab a cup of coffee, listen to some [nice music](https://www.youtube.com/watch?v=5rrIx7yrxwQ), and please devote -a few minutes of your time into reading this README. - -### Projects - -The "project" is kind of the namespace for different signatures. -For example, either: linux, linux_x86, linux_arm etc... are good -project names that can be specified if you are working on either -platforms. - -By dividing the signatures into such projects, Windows symbols for -example won't be searched for Linux projects, which will result -in a better directory organization layout, better performance and -less rate for false-positives. - -The signatures are located by default in the `signatures` directory. -If you wish to use a different location, you may create `config.ini` -at FA's root with the following contents: - -```ini -[global] -signatures_root = /a/b/c -``` - -### SIG format - -The SIG format is a core feature of FA regarding symbol searching. Each -SIG file is residing within the project directory and is automatically searched -when requested to generate the project's symbol list. - -The format is Hjson-based and is used to describe what you, -**as a human**, would do in order to perform the given task (symbol searching -or binary exploration). - -SIG syntax (single): -```hjson -{ - name: name - instructions : [ - # Available commands are listed below - command1 - command2 - ] -} -``` - -Each line in the `instructions` list behaves like a shell -command-line that gets the previous results as the input -and outputs the next results -to the next line. - -Confused? That's alright :grinning:. [Just look at the examples below](#examples) - -User may also use his own python script files to perform -the search. Just create a new `.py` file in your project -directory and implement the `run(interpreter)` method. -Also, the project's path is appended to python's `sys.path` -so you may import your scripts from one another. - -To view the list of available commands, [view the list below](#available-commands) - -### Examples - -#### Finding a global struct - -```hjson -{ - name: g_awsome_global, - instructions: [ - # find the byte sequence '11 22 33 44' - find-bytes '11 22 33 44' - - # advance offset by 20 - offset 20 - - # verify the current bytes are 'aa bb cc dd' - verify-bytes 'aa bb cc dd' - - # go back by 20 bytes offset - offset -20 - - # set global name - set-name g_awsome_global - ] -} -``` - -#### Find function by reference to string - -```hjson -{ - name: free - instructions: [ - # search the string "free" - find-str 'free' --null-terminated - - # goto xref - xref - - # goto function's prolog - function-start - - # reduce to the singletone with most xrefs to - max-xrefs - - # set name and type - set-name free - set-type 'void free(void *block)' - ] -} -``` - -#### Performing code exploration - -```hjson -{ - name: arm-explorer - instructions: [ - # search for some potential function prologs - arm-find-all 'push {r4, lr}' - arm-find-all 'push {r4, r5, lr}' - thumb-find-all 'push {r4, lr}' - thumb-find-all 'push {r4, r5, lr}' - - # convert into functions - make-function - ] -} -``` - -#### Performing string exploration - -```hjson -{ - name: arm-string-explorer - instructions: [ - # goto printf - locate printf - - # iterate every xref - xref - - # and for each, go word-word backwards - add-offset-range 0 -40 -4 - - # if ldr to r0 - verify-operand ldr --op0 r0 - - # go to the global string - goto-ref --data - - # and make it literal - make-literal - ] -} -``` - -#### Finding enums and constants - -```hjson -{ - name: consts-finder - instructions: [ - # goto printf - locate printf - - # iterate all its function lines - function-lines - - # save this result - store printf-lines - - # look for: li r7, ??? - verify-operand li --op0 7 - - # extract second operand - operand 1 - - # define the constant - set-const IMPORTANT_OFFSET - - # load previous results - load printf-lines - - # look for: li r7, ??? - verify-operand li --op0 8 - - # get second operand - operand 1 - - # set this enum value - set-enum important_enum_t some_enum_key - ] -} -``` - -#### Adding struct member offsets - -```hjson -{ - name: structs-finder - instructions: [ - # add hard-coded '0' into resultset - add 0 - - # add a first member at offset 0 - set-struct-member struct_t member_at_0 'unsigned int' - - # advance offset by 4 - offset 4 - - # add a second member - set-struct-member struct_t member_at_4 'const char *' - - # goto function printf - locate printf - - # iterate its function lines - function-lines - - # look for the specific mov opcode (MOV R8, ???) - verify-operand mov --op0 8 - - # extract the offset - operand 1 - - # define this offset into the struct - set-struct-member struct_t member_at_r8_offset 'const char *' - ] -} -``` - -#### Finding several functions in a row - -```hjson -{ - name: cool_functions - instructions: [ - # find string - find-str 'init_stuff' --null-terminated - - # goto to xref - xref - - # goto function start - function-start - - # verify only one single result - unique - - # iterating every 4-byte opcode - add-offset-range 0 80 4 - - # if mnemonic is bl - verify-operand bl - - # sort results - sort - - # store resultset in 'BLs' - store BLs - - # set first bl to malloc function - single 0 - goto-ref --code - set-name malloc - set-type 'void *malloc(unsigned int size)' - - # go back to the results from 4 commands ago - # (the sort results) - load BLs - - # rename next symbol :) - single 1 - goto-ref --code - set-name free - set-type 'void free(void *block)' - ] -} -``` - -#### Conditional branches - -```hjson -{ - name: set_opcode_const - instructions: [ - # goto printf function - locate printf - - # goto 'case_opcode_bl' if current opcode is bl - if 'verify-operand bl' case_opcode_bl - - # make: #define is_bl (0) - clear - add 0 - set-const is_bl - - # finish script by jumping to end - b end - - # mark as 'case_opcode_bl' label - label case_opcode_bl - - # make: #define is_bl (1) - clear - add 1 - set-const is_bl - - # mark script end - label end - ] -} -``` - -#### Python script to find a list of symbols - -```python -from fa.commands.find_str import find_str -from fa.commands.set_name import set_name -from fa import context - -def run(interpreter): - # throw an exception if not running within ida context - context.verify_ida('script-name') - - # locate the global string - set_name(find_str('hello world', null_terminated=True), - 'g_hello_world', interpreter) -``` - -#### Python script to automate SIG files interpreter - -```python -TEMPLATE = ''' -find-str '{unique_string}' -xref -function-start -unique -set-name '{function_name}' -''' - -def run(interpreter): - for function_name in ['func1', 'func2', 'func3']: - instructions = TEMPLATE.format(unique_string=function_name, - function_name=function_name).split('\n') - - interpreter.find_from_instructions_list(instructions) -``` - -#### Python script to dynamically add structs - -```python -from fa.commands.set_type import set_type -from fa import fa_types - -TEMPLATE = ''' -find-str '{unique_string}' -xref -''' - -def run(interpreter): - fa_types.add_const('CONST7', 7) - fa_types.add_const('CONST8', 8) - - foo_e = fa_types.FaEnum('foo_e') - foo_e.add_value('val2', 2) - foo_e.add_value('val1', 1) - foo_e.update_idb() - - special_struct_t = fa_types.FaStruct('special_struct_t') - special_struct_t.add_field('member1', 'const char *') - special_struct_t.add_field('member2', 'const char *', offset=0x20) - special_struct_t.update_idb() - - for function_name in ['unique_magic1', 'unique_magic2']: - instructions = TEMPLATE.format(unique_string=function_name, - function_name=function_name).split('\n') - - results = interpreter.find_from_instructions_list(instructions) - for ea in results: - # the set_type can receive either a string, FaStruct - # or FaEnum :-) - set_type(ea, special_struct_t) -``` - -### Aliases - -Each command can be "alias"ed using the file -found in `fa/commands/alias` or in `/alias` - -Syntax for each line is as follows: `alias_command = command` -For example: -``` -ppc32-verify = keystone-verify-opcodes --bele KS_ARCH_PPC KS_MODE_PPC32 -``` - -Project aliases have higher priority. - -### Loaders - -Loaders are the entry point into running FA. -In the future we'll possibly add Ghidra and other tools. - -Please first install the package as follows: - -Clone the repository and install locally: - -```sh -# clone -git clone git@github.com:doronz88/fa.git -cd fa - -# install -python -m pip install -e . -``` - -#### IDA - -Within IDA Python run: - -```python -from fa import ida_plugin -ida_plugin.install() -``` - -You should get a nice prompt inside the output window welcoming you -into using FA. Also, a quick usage guide will also be printed so you -don't have to memorize everything. - -Also, an additional `FA Toolbar` will be added with quick functions that -are also available under the newly created `FA` menu. - -![FA Menu](https://github.com/doronz88/fa/raw/master/fa/res/screenshots/menu.png "FA Menu") - -A QuickStart Tip: - -`Ctrl+6` to select your project, then `Ctrl+7` to find all its defined symbols. - - -You can also run IDA in script mode just to extract symbols using: - -```sh -ida -S"fa/ida_plugin.py --project-name --symbols-file=/tmp/symbols.txt" foo.idb -``` - - -#### ELF - -In order to use FA on a RAW ELF file, simply use the following command-line: - -```sh -python elf_loader.py -``` - -### Available commands - -See [commands.md](commands.md) - +![Python application](https://github.com/doronz88/fa/workflows/Python%20application/badge.svg) + +# FA + +## What is it? + +FA stands for Firmware Analysis and intended **For Humans**. + +FA allows one to easily perform code exploration, symbol searching and +other functionality with ease. + +Usually such tasks would require you to understand complicated APIs, +write machine-dependant code and perform other tedious tasks. +FA is meant to replace the steps one usually performs like a robot +(find X string, goto xref, find the next call function, ...) in +a much friendlier and maintainable manner. + +The current codebase is very IDA-plugin-oriented. In the future I'll +consider adding compatibility for other disassemblers such as: +Ghidra, Radare and etc... + + +Pull Requests are of course more than welcome :smirk:. + +## Requirements + +Supported IDA 7.x. + +In your IDA's python directory, run: + +```sh +python -m pip install -r requirements.txt +``` + +And for testing: +```sh +python -m pip install -r requirements_testing.txt +``` + +## Where to start? + +Before using, you should understand the terminology for: +Projects, SIG files and Loaders. + +So, grab a cup of coffee, listen to some [nice music](https://www.youtube.com/watch?v=5rrIx7yrxwQ), and please devote +a few minutes of your time into reading this README. + +### Projects + +The "project" is kind of the namespace for different signatures. +For example, either: linux, linux_x86, linux_arm etc... are good +project names that can be specified if you are working on either +platforms. + +By dividing the signatures into such projects, Windows symbols for +example won't be searched for Linux projects, which will result +in a better directory organization layout, better performance and +less rate for false-positives. + +The signatures are located by default in the `signatures` directory. +If you wish to use a different location, you may create `config.ini` +at FA's root with the following contents: + +```ini +[global] +signatures_root = /a/b/c +``` + +### SIG format + +The SIG format is a core feature of FA regarding symbol searching. Each +SIG file is residing within the project directory and is automatically searched +when requested to generate the project's symbol list. + +The format is Hjson-based and is used to describe what you, +**as a human**, would do in order to perform the given task (symbol searching +or binary exploration). + +SIG syntax (single): +```hjson +{ + name: name + instructions : [ + # Available commands are listed below + command1 + command2 + ] +} +``` + +Each line in the `instructions` list behaves like a shell +command-line that gets the previous results as the input +and outputs the next results +to the next line. + +Confused? That's alright :grinning:. [Just look at the examples below](#examples) + +User may also use his own python script files to perform +the search. Just create a new `.py` file in your project +directory and implement the `run(interpreter)` method. +Also, the project's path is appended to python's `sys.path` +so you may import your scripts from one another. + +To view the list of available commands, [view the list below](#available-commands) + +### Examples + +#### Finding a global struct + +```hjson +{ + name: g_awsome_global, + instructions: [ + # find the byte sequence '11 22 33 44' + find-bytes '11 22 33 44' + + # advance offset by 20 + offset 20 + + # verify the current bytes are 'aa bb cc dd' + verify-bytes 'aa bb cc dd' + + # go back by 20 bytes offset + offset -20 + + # set global name + set-name g_awsome_global + ] +} +``` + +#### Find function by reference to string + +```hjson +{ + name: free + instructions: [ + # search the string "free" + find-str 'free' --null-terminated + + # goto xref + xref + + # goto function's prolog + function-start + + # reduce to the singletone with most xrefs to + max-xrefs + + # set name and type + set-name free + set-type 'void free(void *block)' + ] +} +``` + +#### Performing code exploration + +```hjson +{ + name: arm-explorer + instructions: [ + # search for some potential function prologs + arm-find-all 'push {r4, lr}' + arm-find-all 'push {r4, r5, lr}' + thumb-find-all 'push {r4, lr}' + thumb-find-all 'push {r4, r5, lr}' + + # convert into functions + make-function + ] +} +``` + +#### Performing string exploration + +```hjson +{ + name: arm-string-explorer + instructions: [ + # goto printf + locate printf + + # iterate every xref + xref + + # and for each, go word-word backwards + add-offset-range 0 -40 -4 + + # if ldr to r0 + verify-operand ldr --op0 r0 + + # go to the global string + goto-ref --data + + # and make it literal + make-literal + ] +} +``` + +#### Finding enums and constants + +```hjson +{ + name: consts-finder + instructions: [ + # goto printf + locate printf + + # iterate all its function lines + function-lines + + # save this result + store printf-lines + + # look for: li r7, ??? + verify-operand li --op0 7 + + # extract second operand + operand 1 + + # define the constant + set-const IMPORTANT_OFFSET + + # load previous results + load printf-lines + + # look for: li r7, ??? + verify-operand li --op0 8 + + # get second operand + operand 1 + + # set this enum value + set-enum important_enum_t some_enum_key + ] +} +``` + +#### Adding struct member offsets + +```hjson +{ + name: structs-finder + instructions: [ + # add hard-coded '0' into resultset + add 0 + + # add a first member at offset 0 + set-struct-member struct_t member_at_0 'unsigned int' + + # advance offset by 4 + offset 4 + + # add a second member + set-struct-member struct_t member_at_4 'const char *' + + # goto function printf + locate printf + + # iterate its function lines + function-lines + + # look for the specific mov opcode (MOV R8, ???) + verify-operand mov --op0 8 + + # extract the offset + operand 1 + + # define this offset into the struct + set-struct-member struct_t member_at_r8_offset 'const char *' + ] +} +``` + +#### Finding several functions in a row + +```hjson +{ + name: cool_functions + instructions: [ + # find string + find-str 'init_stuff' --null-terminated + + # goto to xref + xref + + # goto function start + function-start + + # verify only one single result + unique + + # iterating every 4-byte opcode + add-offset-range 0 80 4 + + # if mnemonic is bl + verify-operand bl + + # sort results + sort + + # store resultset in 'BLs' + store BLs + + # set first bl to malloc function + single 0 + goto-ref --code + set-name malloc + set-type 'void *malloc(unsigned int size)' + + # go back to the results from 4 commands ago + # (the sort results) + load BLs + + # rename next symbol :) + single 1 + goto-ref --code + set-name free + set-type 'void free(void *block)' + ] +} +``` + +#### Conditional branches + +```hjson +{ + name: set_opcode_const + instructions: [ + # goto printf function + locate printf + + # goto 'case_opcode_bl' if current opcode is bl + if 'verify-operand bl' case_opcode_bl + + # make: #define is_bl (0) + clear + add 0 + set-const is_bl + + # finish script by jumping to end + b end + + # mark as 'case_opcode_bl' label + label case_opcode_bl + + # make: #define is_bl (1) + clear + add 1 + set-const is_bl + + # mark script end + label end + ] +} +``` + +#### Python script to find a list of symbols + +```python +from fa.commands.find_str import find_str +from fa.commands.set_name import set_name +from fa import context + +def run(interpreter): + # throw an exception if not running within ida context + context.verify_ida('script-name') + + # locate the global string + set_name(find_str('hello world', null_terminated=True), + 'g_hello_world', interpreter) +``` + +#### Python script to automate SIG files interpreter + +```python +TEMPLATE = ''' +find-str '{unique_string}' +xref +function-start +unique +set-name '{function_name}' +''' + +def run(interpreter): + for function_name in ['func1', 'func2', 'func3']: + instructions = TEMPLATE.format(unique_string=function_name, + function_name=function_name).split('\n') + + interpreter.find_from_instructions_list(instructions) +``` + +#### Python script to dynamically add structs + +```python +from fa.commands.set_type import set_type +from fa import fa_types + +TEMPLATE = ''' +find-str '{unique_string}' +xref +''' + +def run(interpreter): + fa_types.add_const('CONST7', 7) + fa_types.add_const('CONST8', 8) + + foo_e = fa_types.FaEnum('foo_e') + foo_e.add_value('val2', 2) + foo_e.add_value('val1', 1) + foo_e.update_idb() + + special_struct_t = fa_types.FaStruct('special_struct_t') + special_struct_t.add_field('member1', 'const char *') + special_struct_t.add_field('member2', 'const char *', offset=0x20) + special_struct_t.update_idb() + + for function_name in ['unique_magic1', 'unique_magic2']: + instructions = TEMPLATE.format(unique_string=function_name, + function_name=function_name).split('\n') + + results = interpreter.find_from_instructions_list(instructions) + for ea in results: + # the set_type can receive either a string, FaStruct + # or FaEnum :-) + set_type(ea, special_struct_t) +``` + +### Aliases + +Each command can be "alias"ed using the file +found in `fa/commands/alias` or in `/alias` + +Syntax for each line is as follows: `alias_command = command` +For example: +``` +ppc32-verify = keystone-verify-opcodes --bele KS_ARCH_PPC KS_MODE_PPC32 +``` + +Project aliases have higher priority. + +### Loaders + +Loaders are the entry point into running FA. +In the future we'll possibly add Ghidra and other tools. + +Please first install the package as follows: + +Clone the repository and install locally: + +```sh +# clone +git clone git@github.com:doronz88/fa.git +cd fa + +# install +python -m pip install -e . +``` + +#### IDA + +Within IDA Python run: + +```python +from fa import ida_plugin +ida_plugin.install() +``` + +You should get a nice prompt inside the output window welcoming you +into using FA. Also, a quick usage guide will also be printed so you +don't have to memorize everything. + +Also, an additional `FA Toolbar` will be added with quick functions that +are also available under the newly created `FA` menu. + +![FA Menu](https://github.com/doronz88/fa/raw/master/fa/res/screenshots/menu.png "FA Menu") + +A QuickStart Tip: + +`Ctrl+6` to select your project, then `Ctrl+7` to find all its defined symbols. + + +You can also run IDA in script mode just to extract symbols using: + +```sh +ida -S"fa/ida_plugin.py --project-name --symbols-file=/tmp/symbols.txt" foo.idb +``` + + +#### ELF + +In order to use FA on a RAW ELF file, simply use the following command-line: + +```sh +python elf_loader.py +``` + +### Available commands + +See [commands.md](commands.md) + diff --git a/commands.md b/commands.md index 0e97828..bb046b7 100644 --- a/commands.md +++ b/commands.md @@ -1029,7 +1029,8 @@ optional arguments: ``` ## xrefs-to ``` -usage: xrefs-to [-h] [--function-start] [--or] [--and] [--name NAME] [--bytes BYTES] +usage: xrefs-to [-h] [--function-start] [--or] [--and] [--name NAME] + [--bytes BYTES] search for xrefs pointing at given parameter diff --git a/elf_loader.py b/elf_loader.py index 3402aaa..517541e 100644 --- a/elf_loader.py +++ b/elf_loader.py @@ -1,50 +1,50 @@ -from elftools.elf import elffile -import click - -from fa import fainterp - - -class ElfLoader(fainterp.FaInterp): - def __init__(self): - super(ElfLoader, self).__init__() - self._elf = None - - def reload_segments(self): - pass - - def set_input(self, input_): - self._elf = elffile.ELFFile(input_) - self.endianity = '<' if self._elf.little_endian else '>' - - self._segments = {} - for s in self._elf.iter_segments(): - if s.header['p_type'] != 'PT_LOAD': - continue - self.segments[s.header['p_vaddr']] = s.data() - - @property - def segments(self): - return self._segments - - -@click.command() -@click.argument('elf_file', type=click.File('rb')) -@click.argument('signatures_root') -@click.argument('project') -def main(elf_file, signatures_root, project): - interp = ElfLoader() - interp.set_input(elf_file) - interp.set_signatures_root(signatures_root) - interp.set_project(project) - - for k, v in interp.symbols().items(): - if isinstance(v, list) or isinstance(v, set): - if len(v) > 1: - print('# {} multiple matches'.format(k)) - continue - v = v.pop() - print('{} = 0x{:x};'.format(k, v)) - - -if __name__ == '__main__': - main() +from elftools.elf import elffile +import click + +from fa import fainterp + + +class ElfLoader(fainterp.FaInterp): + def __init__(self): + super(ElfLoader, self).__init__() + self._elf = None + + def reload_segments(self): + pass + + def set_input(self, input_): + self._elf = elffile.ELFFile(input_) + self.endianity = '<' if self._elf.little_endian else '>' + + self._segments = {} + for s in self._elf.iter_segments(): + if s.header['p_type'] != 'PT_LOAD': + continue + self.segments[s.header['p_vaddr']] = s.data() + + @property + def segments(self): + return self._segments + + +@click.command() +@click.argument('elf_file', type=click.File('rb')) +@click.argument('signatures_root') +@click.argument('project') +def main(elf_file, signatures_root, project): + interp = ElfLoader() + interp.set_input(elf_file) + interp.set_signatures_root(signatures_root) + interp.set_project(project) + + for k, v in interp.symbols().items(): + if isinstance(v, list) or isinstance(v, set): + if len(v) > 1: + print('# {} multiple matches'.format(k)) + continue + v = v.pop() + print('{} = 0x{:x};'.format(k, v)) + + +if __name__ == '__main__': + main() diff --git a/fa/commands/add.py b/fa/commands/add.py index a66d4c0..453f417 100644 --- a/fa/commands/add.py +++ b/fa/commands/add.py @@ -1,22 +1,22 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''add an hard-coded value into resultset - -EXAMPLE: - results = [] - -> add 80 - result = [80] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('add', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('value') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return addresses + [eval(args.value)] +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''add an hard-coded value into resultset + +EXAMPLE: + results = [] + -> add 80 + result = [80] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('add', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('value') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return addresses + [eval(args.value)] diff --git a/fa/commands/add_offset_range.py b/fa/commands/add_offset_range.py index 27334d6..bd832ec 100644 --- a/fa/commands/add_offset_range.py +++ b/fa/commands/add_offset_range.py @@ -1,33 +1,33 @@ -from argparse import RawTextHelpFormatter -from fa import utils - - -DESCRIPTION = '''adds a python-range to resultset - -EXAMPLE: - result = [0, 0x200] - -> add-offset-range 0 4 8 - result = [0, 4, 8, 0x200, 0x204, 0x208] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('add-offset-range', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('start') - p.add_argument('end') - p.add_argument('step') - return p - - -def add_offset_range(addresses, start, end, step): - for ea in addresses: - for i in range(start, end, step): - yield ea + i - - -def run(segments, args, addresses, interpreter=None, **kwargs): - gen = add_offset_range(addresses, eval(args.start), eval(args.end), - eval(args.step)) - return list(gen) +from argparse import RawTextHelpFormatter +from fa import utils + + +DESCRIPTION = '''adds a python-range to resultset + +EXAMPLE: + result = [0, 0x200] + -> add-offset-range 0 4 8 + result = [0, 4, 8, 0x200, 0x204, 0x208] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('add-offset-range', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('start') + p.add_argument('end') + p.add_argument('step') + return p + + +def add_offset_range(addresses, start, end, step): + for ea in addresses: + for i in range(start, end, step): + yield ea + i + + +def run(segments, args, addresses, interpreter=None, **kwargs): + gen = add_offset_range(addresses, eval(args.start), eval(args.end), + eval(args.step)) + return list(gen) diff --git a/fa/commands/alias b/fa/commands/alias index 066ba27..2b57962 100644 --- a/fa/commands/alias +++ b/fa/commands/alias @@ -1,9 +1,9 @@ -ppc32-big-find-all = keystone-find-opcodes KS_ARCH_PPC KS_MODE_BIG_ENDIAN|KS_MODE_PPC32 -ppc32-find-all = keystone-find-opcodes --bele KS_ARCH_PPC KS_MODE_PPC32 -ppc32-big-verify = keystone-verify-opcodes KS_ARCH_PPC KS_MODE_BIG_ENDIAN|KS_MODE_PPC32 -ppc32-verify = keystone-verify-opcodes --bele KS_ARCH_PPC KS_MODE_PPC32 -arm-find-all = keystone-find-opcodes --bele KS_ARCH_ARM KS_MODE_ARM -thumb-find-all = keystone-find-opcodes --bele KS_ARCH_ARM KS_MODE_THUMB -arm-verify = keystone-verify-opcodes --bele KS_ARCH_ARM KS_MODE_ARM -find-imm = find-immediate -save = store +ppc32-big-find-all = keystone-find-opcodes KS_ARCH_PPC KS_MODE_BIG_ENDIAN|KS_MODE_PPC32 +ppc32-find-all = keystone-find-opcodes --bele KS_ARCH_PPC KS_MODE_PPC32 +ppc32-big-verify = keystone-verify-opcodes KS_ARCH_PPC KS_MODE_BIG_ENDIAN|KS_MODE_PPC32 +ppc32-verify = keystone-verify-opcodes --bele KS_ARCH_PPC KS_MODE_PPC32 +arm-find-all = keystone-find-opcodes --bele KS_ARCH_ARM KS_MODE_ARM +thumb-find-all = keystone-find-opcodes --bele KS_ARCH_ARM KS_MODE_THUMB +arm-verify = keystone-verify-opcodes --bele KS_ARCH_ARM KS_MODE_ARM +find-imm = find-immediate +save = store diff --git a/fa/commands/align.py b/fa/commands/align.py index 866f63d..656398e 100644 --- a/fa/commands/align.py +++ b/fa/commands/align.py @@ -1,26 +1,26 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''align results to given base (round-up) - -EXAMPLE: - results = [0, 2, 4, 6, 8] - -> align 4 - results = [0, 4, 4, 8, 8] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('align', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('value') - return p - - -def align(addresses, value): - return [((ea + (value - 1)) // value) * value for ea in addresses] - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return list(align(addresses, eval(args.value))) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''align results to given base (round-up) + +EXAMPLE: + results = [0, 2, 4, 6, 8] + -> align 4 + results = [0, 4, 4, 8, 8] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('align', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('value') + return p + + +def align(addresses, value): + return [((ea + (value - 1)) // value) * value for ea in addresses] + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return list(align(addresses, eval(args.value))) diff --git a/fa/commands/b.py b/fa/commands/b.py index 2925995..c779be9 100644 --- a/fa/commands/b.py +++ b/fa/commands/b.py @@ -1,31 +1,31 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''branch unconditionally to label - -EXAMPLE: - results = [] - - add 1 - -> b skip - add 2 - label skip - add 3 - - results = [1, 3] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('b', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('label', help='label to jump to') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - interpreter.set_pc(args.label) - # pc is incremented by 1, after each instruction - interpreter.dec_pc() - return addresses +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''branch unconditionally to label + +EXAMPLE: + results = [] + + add 1 + -> b skip + add 2 + label skip + add 3 + + results = [1, 3] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('b', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('label', help='label to jump to') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + interpreter.set_pc(args.label) + # pc is incremented by 1, after each instruction + interpreter.dec_pc() + return addresses diff --git a/fa/commands/find.py b/fa/commands/find.py index c0351e0..e208804 100644 --- a/fa/commands/find.py +++ b/fa/commands/find.py @@ -1,16 +1,16 @@ -from fa import utils - - -def get_parser(): - p = utils.ArgumentParserNoExit('find', - description='find another symbol defined ' - 'in other SIG files') - p.add_argument('name', help='symbol name') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - interpreter.find(args.name) - - # return an empty result-set - return [] +from fa import utils + + +def get_parser(): + p = utils.ArgumentParserNoExit('find', + description='find another symbol defined ' + 'in other SIG files') + p.add_argument('name', help='symbol name') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + interpreter.find(args.name) + + # return an empty result-set + return [] diff --git a/fa/commands/find_bytes.py b/fa/commands/find_bytes.py index 8ef4f5e..840f156 100644 --- a/fa/commands/find_bytes.py +++ b/fa/commands/find_bytes.py @@ -1,36 +1,36 @@ -from argparse import RawTextHelpFormatter -import binascii - -from fa import utils - -DESCRIPTION = '''expands the result-set with the occurrences of the given bytes - -EXAMPLE: - 0x00000000: 01 02 03 04 - 0x00000004: 05 06 07 08 - - results = [] - -> find-bytes 01020304 - result = [0] - - -> find-bytes 05060708 - results = [0, 4] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('find-bytes', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('hex_str') - return p - - -def find_bytes(hex_str, segments=None): - needle = binascii.unhexlify(''.join(hex_str.split(' '))) - return utils.find_raw(needle, segments=segments) - - -def run(segments, args, addresses, interpreter=None, **kwargs): - results = list(find_bytes(args.hex_str, segments=segments)) - return addresses + results +from argparse import RawTextHelpFormatter +import binascii + +from fa import utils + +DESCRIPTION = '''expands the result-set with the occurrences of the given bytes + +EXAMPLE: + 0x00000000: 01 02 03 04 + 0x00000004: 05 06 07 08 + + results = [] + -> find-bytes 01020304 + result = [0] + + -> find-bytes 05060708 + results = [0, 4] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('find-bytes', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('hex_str') + return p + + +def find_bytes(hex_str, segments=None): + needle = binascii.unhexlify(''.join(hex_str.split(' '))) + return utils.find_raw(needle, segments=segments) + + +def run(segments, args, addresses, interpreter=None, **kwargs): + results = list(find_bytes(args.hex_str, segments=segments)) + return addresses + results diff --git a/fa/commands/function_start.py b/fa/commands/function_start.py index d3f9dc8..c7cbb66 100644 --- a/fa/commands/function_start.py +++ b/fa/commands/function_start.py @@ -1,47 +1,47 @@ -from argparse import RawTextHelpFormatter -from fa import utils, context - -try: - import idc -except ImportError: - pass - -DESCRIPTION = '''goto function's start - -EXAMPLE: - 0x00000000: push {r4-r7, lr} -> function's prolog - ... - 0x000000f0: pop {r4-r7, pc} -> function's epilog - - results = [0xf0] - -> function-start - result = [0] -''' - - -def get_function_start(segments, ea): - start = idc.get_func_attr(ea, idc.FUNCATTR_START) - return start - - # TODO: consider add support locate of function heads manually - - -def get_parser(): - p = utils.ArgumentParserNoExit('function-start', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('cmd', nargs='*', default='', help='command') - return p - - -@context.ida_context -def function_start(addresses): - for ea in addresses: - if ea != idc.BADADDR: - func_start = idc.get_func_attr(ea, idc.FUNCATTR_START) - if func_start != idc.BADADDR: - yield func_start - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return list(function_start(addresses)) +from argparse import RawTextHelpFormatter +from fa import utils, context + +try: + import idc +except ImportError: + pass + +DESCRIPTION = '''goto function's start + +EXAMPLE: + 0x00000000: push {r4-r7, lr} -> function's prolog + ... + 0x000000f0: pop {r4-r7, pc} -> function's epilog + + results = [0xf0] + -> function-start + result = [0] +''' + + +def get_function_start(segments, ea): + start = idc.get_func_attr(ea, idc.FUNCATTR_START) + return start + + # TODO: consider add support locate of function heads manually + + +def get_parser(): + p = utils.ArgumentParserNoExit('function-start', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('cmd', nargs='*', default='', help='command') + return p + + +@context.ida_context +def function_start(addresses): + for ea in addresses: + if ea != idc.BADADDR: + func_start = idc.get_func_attr(ea, idc.FUNCATTR_START) + if func_start != idc.BADADDR: + yield func_start + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return list(function_start(addresses)) diff --git a/fa/commands/if.py b/fa/commands/if.py index c080fa4..3104a9e 100644 --- a/fa/commands/if.py +++ b/fa/commands/if.py @@ -1,38 +1,38 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''perform an 'if' statement to create conditional branches -using an FA command - -EXAMPLE: - results = [0, 4, 8] - - -> if 'verify-single' a_is_single_label - - set-name a_isnt_single - b end - - label a_is_single_label - set-name a_is_single - - label end -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('if', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('cond', help='condition as an FA command') - p.add_argument('label', help='label to jump to if condition is true') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - if len(interpreter.find_from_instructions_list([args.cond], - addresses=addresses)): - interpreter.set_pc(args.label) - - # pc is incremented by 1, after each instruction - interpreter.dec_pc() - return addresses +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''perform an 'if' statement to create conditional branches +using an FA command + +EXAMPLE: + results = [0, 4, 8] + + -> if 'verify-single' a_is_single_label + + set-name a_isnt_single + b end + + label a_is_single_label + set-name a_is_single + + label end +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('if', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('cond', help='condition as an FA command') + p.add_argument('label', help='label to jump to if condition is true') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + if len(interpreter.find_from_instructions_list([args.cond], + addresses=addresses)): + interpreter.set_pc(args.label) + + # pc is incremented by 1, after each instruction + interpreter.dec_pc() + return addresses diff --git a/fa/commands/intersect.py b/fa/commands/intersect.py index 3ec9904..4306c38 100644 --- a/fa/commands/intersect.py +++ b/fa/commands/intersect.py @@ -1,33 +1,33 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''intersect two or more variables - -EXAMPLE: - results = [0, 4, 8] - store a - ... - results = [0, 12, 20] - store b - - -> intersect a b - results = [0] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('intersect', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('variables', nargs='+', help='variable names') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - first_var = args.variables[0] - results = set(interpreter.get_variable(first_var)) - - for c in args.variables[1:]: - results.intersection_update(interpreter.get_variable(c)) - - return list(results) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''intersect two or more variables + +EXAMPLE: + results = [0, 4, 8] + store a + ... + results = [0, 12, 20] + store b + + -> intersect a b + results = [0] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('intersect', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('variables', nargs='+', help='variable names') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + first_var = args.variables[0] + results = set(interpreter.get_variable(first_var)) + + for c in args.variables[1:]: + results.intersection_update(interpreter.get_variable(c)) + + return list(results) diff --git a/fa/commands/keystone_find_opcodes.py b/fa/commands/keystone_find_opcodes.py index c37da47..c1ab397 100644 --- a/fa/commands/keystone_find_opcodes.py +++ b/fa/commands/keystone_find_opcodes.py @@ -1,55 +1,55 @@ -from argparse import RawTextHelpFormatter -try: - # flake8: noqa - from keystone import * -except ImportError: - print('keystone-engine module not installed') - -import binascii - -from fa.commands import find_bytes -from fa import utils - -DESCRIPTION = '''use keystone to search for the supplied opcodes - -EXAMPLE: - 0x00000000: push {r4-r7, lr} - 0x00000004: mov r0, r1 - - results = [] - -> keystone-find-opcodes --bele KS_ARCH_ARM KS_MODE_ARM 'mov r0, r1;' - result = [4] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('keystone-find-opcodes', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('--bele', action='store_true', - help='figure out the endianity from IDA instead of ' - 'explicit mode') - p.add_argument('--or', action='store_true', - help='mandatory. expands search results') - p.add_argument('arch', - help='keystone architecture const (evaled)') - p.add_argument('mode', - help='keystone mode const (evald)') - p.add_argument('code', - help='keystone architecture const (opcodes to compile)') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - arch = eval(args.arch) - mode = eval(args.mode) - - if args.bele: - mode |= KS_MODE_BIG_ENDIAN if \ - interpreter.endianity == '>' else KS_MODE_LITTLE_ENDIAN - - ks = Ks(arch, mode) - compiled_buf = bytearray(ks.asm(args.code)[0]) - - setattr(args, 'hex_str', binascii.hexlify(compiled_buf).decode('utf8')) - return find_bytes.run(segments, args, addresses, **kwargs) +from argparse import RawTextHelpFormatter +try: + # flake8: noqa + from keystone import * +except ImportError: + print('keystone-engine module not installed') + +import binascii + +from fa.commands import find_bytes +from fa import utils + +DESCRIPTION = '''use keystone to search for the supplied opcodes + +EXAMPLE: + 0x00000000: push {r4-r7, lr} + 0x00000004: mov r0, r1 + + results = [] + -> keystone-find-opcodes --bele KS_ARCH_ARM KS_MODE_ARM 'mov r0, r1;' + result = [4] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('keystone-find-opcodes', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('--bele', action='store_true', + help='figure out the endianity from IDA instead of ' + 'explicit mode') + p.add_argument('--or', action='store_true', + help='mandatory. expands search results') + p.add_argument('arch', + help='keystone architecture const (evaled)') + p.add_argument('mode', + help='keystone mode const (evald)') + p.add_argument('code', + help='keystone architecture const (opcodes to compile)') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + arch = eval(args.arch) + mode = eval(args.mode) + + if args.bele: + mode |= KS_MODE_BIG_ENDIAN if \ + interpreter.endianity == '>' else KS_MODE_LITTLE_ENDIAN + + ks = Ks(arch, mode) + compiled_buf = bytearray(ks.asm(args.code)[0]) + + setattr(args, 'hex_str', binascii.hexlify(compiled_buf).decode('utf8')) + return find_bytes.run(segments, args, addresses, **kwargs) diff --git a/fa/commands/keystone_verify_opcodes.py b/fa/commands/keystone_verify_opcodes.py index 7bbd7c3..a2c3ad7 100644 --- a/fa/commands/keystone_verify_opcodes.py +++ b/fa/commands/keystone_verify_opcodes.py @@ -1,57 +1,57 @@ -from argparse import RawTextHelpFormatter -try: - # flake8: noqa - from keystone import * -except ImportError: - print('keystone-engine module not installed') - - -import binascii - -from fa.commands import verify_bytes -from fa import utils - -DESCRIPTION = '''use keystone to verify the result-set matches the given -opcodes - -EXAMPLE: - 0x00000000: push {r4-r7, lr} - 0x00000004: mov r0, r1 - - results = [0, 4] - -> keystone-verify-opcodes --bele KS_ARCH_ARM KS_MODE_ARM 'mov r0, r1' - result = [4] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('keystone-verify-opcodes', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('--bele', action='store_true', - help='figure out the endianity from IDA instead of ' - 'explicit mode') - p.add_argument('--until', type=int, - help='keep going onwards opcode-opcode until verified') - p.add_argument('arch', - help='keystone architecture const (evaled)') - p.add_argument('mode', - help='keystone mode const (evald)') - p.add_argument('code', - help='keystone architecture const (opcodes to compile)') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - arch = eval(args.arch) - mode = eval(args.mode) - - if args.bele: - mode |= KS_MODE_BIG_ENDIAN if \ - interpreter.endianity == '>' else KS_MODE_LITTLE_ENDIAN - - ks = Ks(arch, mode) - compiled_buf = bytearray(ks.asm(args.code)[0]) - - setattr(args, 'hex_str', binascii.hexlify(compiled_buf).decode('utf8')) - return verify_bytes.run(segments, args, addresses, **kwargs) +from argparse import RawTextHelpFormatter +try: + # flake8: noqa + from keystone import * +except ImportError: + print('keystone-engine module not installed') + + +import binascii + +from fa.commands import verify_bytes +from fa import utils + +DESCRIPTION = '''use keystone to verify the result-set matches the given +opcodes + +EXAMPLE: + 0x00000000: push {r4-r7, lr} + 0x00000004: mov r0, r1 + + results = [0, 4] + -> keystone-verify-opcodes --bele KS_ARCH_ARM KS_MODE_ARM 'mov r0, r1' + result = [4] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('keystone-verify-opcodes', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('--bele', action='store_true', + help='figure out the endianity from IDA instead of ' + 'explicit mode') + p.add_argument('--until', type=int, + help='keep going onwards opcode-opcode until verified') + p.add_argument('arch', + help='keystone architecture const (evaled)') + p.add_argument('mode', + help='keystone mode const (evald)') + p.add_argument('code', + help='keystone architecture const (opcodes to compile)') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + arch = eval(args.arch) + mode = eval(args.mode) + + if args.bele: + mode |= KS_MODE_BIG_ENDIAN if \ + interpreter.endianity == '>' else KS_MODE_LITTLE_ENDIAN + + ks = Ks(arch, mode) + compiled_buf = bytearray(ks.asm(args.code)[0]) + + setattr(args, 'hex_str', binascii.hexlify(compiled_buf).decode('utf8')) + return verify_bytes.run(segments, args, addresses, **kwargs) diff --git a/fa/commands/load.py b/fa/commands/load.py index 5d4b46f..97b89f3 100644 --- a/fa/commands/load.py +++ b/fa/commands/load.py @@ -1,28 +1,28 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''go back to previous result-set saved by 'store' command. - -EXAMPLE: - results = [0, 4, 8] - store foo - - find-bytes 12345678 - results = [0, 4, 8, 10, 20] - - -> load foo - results = [0, 4, 8] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('load', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('name', help='name of variable in history to go back ' - 'to') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return interpreter.get_variable(args.name) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''go back to previous result-set saved by 'store' command. + +EXAMPLE: + results = [0, 4, 8] + store foo + + find-bytes 12345678 + results = [0, 4, 8, 10, 20] + + -> load foo + results = [0, 4, 8] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('load', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('name', help='name of variable in history to go back ' + 'to') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return interpreter.get_variable(args.name) diff --git a/fa/commands/make_comment.py b/fa/commands/make_comment.py index 198ee99..192f6ed 100644 --- a/fa/commands/make_comment.py +++ b/fa/commands/make_comment.py @@ -1,43 +1,43 @@ -from argparse import RawTextHelpFormatter - -try: - import idc -except ImportError: - pass - -from fa import utils, context - -DESCRIPTION = '''add comment for given addresses - -EXAMPLE: - 0x00000200: 01 02 03 04 - 0x00000204: 30 31 32 33 - - results = [0x200] - -> make-comment 'bla bla' - results = [0x200] - - 0x00000200: 01 02 03 04 ; bla bla - 0x00000204: 30 31 32 33 -''' - - -@context.ida_context -def make_comment(addresses, comment): - for ea in addresses: - idc.set_cmt(ea, comment, 0) - yield ea - - -def get_parser(): - p = utils.ArgumentParserNoExit() - p.add_argument('comment', help='comment string') - - p.prog = 'make-comment' - p.description = DESCRIPTION - p.formatter_class = RawTextHelpFormatter - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return list(make_comment(addresses, args.comment)) +from argparse import RawTextHelpFormatter + +try: + import idc +except ImportError: + pass + +from fa import utils, context + +DESCRIPTION = '''add comment for given addresses + +EXAMPLE: + 0x00000200: 01 02 03 04 + 0x00000204: 30 31 32 33 + + results = [0x200] + -> make-comment 'bla bla' + results = [0x200] + + 0x00000200: 01 02 03 04 ; bla bla + 0x00000204: 30 31 32 33 +''' + + +@context.ida_context +def make_comment(addresses, comment): + for ea in addresses: + idc.set_cmt(ea, comment, 0) + yield ea + + +def get_parser(): + p = utils.ArgumentParserNoExit() + p.add_argument('comment', help='comment string') + + p.prog = 'make-comment' + p.description = DESCRIPTION + p.formatter_class = RawTextHelpFormatter + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return list(make_comment(addresses, args.comment)) diff --git a/fa/commands/offset.py b/fa/commands/offset.py index a30031f..ef9e6df 100644 --- a/fa/commands/offset.py +++ b/fa/commands/offset.py @@ -1,27 +1,27 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''advance the result-set by a given offset - -EXAMPLE: - results = [0, 4, 8, 12] - -> offset 4 - result = [4, 8, 12, 16] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('offset', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('offset') - return p - - -def offset(addresses, advance_by): - for ea in addresses: - yield ea + advance_by - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return list(offset(addresses, eval(args.offset))) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''advance the result-set by a given offset + +EXAMPLE: + results = [0, 4, 8, 12] + -> offset 4 + result = [4, 8, 12, 16] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('offset', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('offset') + return p + + +def offset(addresses, advance_by): + for ea in addresses: + yield ea + advance_by + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return list(offset(addresses, eval(args.offset))) diff --git a/fa/commands/print.py b/fa/commands/print.py index 0cd7626..c4e710e 100644 --- a/fa/commands/print.py +++ b/fa/commands/print.py @@ -1,18 +1,18 @@ -from fa import utils - -DESCRIPTION = '''prints the current result-set (for debugging)''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('print', - description=DESCRIPTION) - p.add_argument('phrase', nargs='?', default='', help='optional string') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - log_line = 'FA Debug Print: {}\n'.format(args.phrase) - for ea in addresses: - log_line += '\t0x{:x}\n'.format(ea) - print(log_line) - return addresses +from fa import utils + +DESCRIPTION = '''prints the current result-set (for debugging)''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('print', + description=DESCRIPTION) + p.add_argument('phrase', nargs='?', default='', help='optional string') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + log_line = 'FA Debug Print: {}\n'.format(args.phrase) + for ea in addresses: + log_line += '\t0x{:x}\n'.format(ea) + print(log_line) + return addresses diff --git a/fa/commands/python_if.py b/fa/commands/python_if.py index 3b1a678..99a25e5 100644 --- a/fa/commands/python_if.py +++ b/fa/commands/python_if.py @@ -1,39 +1,39 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''perform an 'if' statement to create conditional branches -using an eval'ed expression - -EXAMPLE: - results = [0, 4, 8] - - verify-single - store a - - # jump to a_is_single_label since a == [] - -> python-if a a_is_single_label - set-name a_isnt_single - b end - - label a_is_single_label - set-name a_is_single - - label end -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('python-if', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('cond', help='condition to evaluate (being eval\'ed)') - p.add_argument('label', help='label to jump to if condition is true') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - if eval(args.cond, interpreter.get_all_variables()): - interpreter.set_pc(args.label) - # pc is incremented by 1, after each instruction - interpreter.dec_pc() - return addresses +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''perform an 'if' statement to create conditional branches +using an eval'ed expression + +EXAMPLE: + results = [0, 4, 8] + + verify-single + store a + + # jump to a_is_single_label since a == [] + -> python-if a a_is_single_label + set-name a_isnt_single + b end + + label a_is_single_label + set-name a_is_single + + label end +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('python-if', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('cond', help='condition to evaluate (being eval\'ed)') + p.add_argument('label', help='label to jump to if condition is true') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + if eval(args.cond, interpreter.get_all_variables()): + interpreter.set_pc(args.label) + # pc is incremented by 1, after each instruction + interpreter.dec_pc() + return addresses diff --git a/fa/commands/run.py b/fa/commands/run.py index 905ecab..32cef34 100644 --- a/fa/commands/run.py +++ b/fa/commands/run.py @@ -1,15 +1,15 @@ -from fa import utils - - -def get_parser(): - p = utils.ArgumentParserNoExit('run', - description='run another SIG file') - p.add_argument('name', help='SIG filename') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - interpreter.find_from_sig_path(args.name) - - # return an empty result-set - return [] +from fa import utils + + +def get_parser(): + p = utils.ArgumentParserNoExit('run', + description='run another SIG file') + p.add_argument('name', help='SIG filename') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + interpreter.find_from_sig_path(args.name) + + # return an empty result-set + return [] diff --git a/fa/commands/single.py b/fa/commands/single.py index 01b441e..51bd2da 100644 --- a/fa/commands/single.py +++ b/fa/commands/single.py @@ -1,29 +1,29 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''peek a single result from the result-set (zero-based) - -EXAMPLE: - results = [0, 4, 8, 12] - -> single 2 - result = [8] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('single', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('index', default='0', help='result index') - return p - - -def single(addresses, index): - if index + 1 > len(addresses): - return [] - else: - return [addresses[index]] - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return single(addresses, eval(args.index)) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''peek a single result from the result-set (zero-based) + +EXAMPLE: + results = [0, 4, 8, 12] + -> single 2 + result = [8] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('single', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('index', default='0', help='result index') + return p + + +def single(addresses, index): + if index + 1 > len(addresses): + return [] + else: + return [addresses[index]] + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return single(addresses, eval(args.index)) diff --git a/fa/commands/sort.py b/fa/commands/sort.py index 10fddf4..8906332 100644 --- a/fa/commands/sort.py +++ b/fa/commands/sort.py @@ -1,26 +1,26 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''performs a sort on the current result-set - -EXAMPLE: - results = [4, 12, 0, 8] - -> sort - result = [0, 4, 8 ,12] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('sort', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - return p - - -def sort(addresses): - addresses.sort() - return addresses - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return sort(addresses) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''performs a sort on the current result-set + +EXAMPLE: + results = [4, 12, 0, 8] + -> sort + result = [0, 4, 8 ,12] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('sort', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + return p + + +def sort(addresses): + addresses.sort() + return addresses + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return sort(addresses) diff --git a/fa/commands/stop_if_empty.py b/fa/commands/stop_if_empty.py index 54f12bf..f2cd1d6 100644 --- a/fa/commands/stop_if_empty.py +++ b/fa/commands/stop_if_empty.py @@ -1,28 +1,28 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''exit if current resultset is empty - -EXAMPLE: - results = [] - - -> stop-if-empty - add 1 - - results = [] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('stop-if-empty', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - if len(addresses) == 0: - # just a big enough value which is always greater - # then max available pc - interpreter.set_pc(0xffffffff) - return addresses +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''exit if current resultset is empty + +EXAMPLE: + results = [] + + -> stop-if-empty + add 1 + + results = [] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('stop-if-empty', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + if len(addresses) == 0: + # just a big enough value which is always greater + # then max available pc + interpreter.set_pc(0xffffffff) + return addresses diff --git a/fa/commands/store.py b/fa/commands/store.py index 8c38374..a3994f4 100644 --- a/fa/commands/store.py +++ b/fa/commands/store.py @@ -1,29 +1,29 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''save current result-set in a variable. -You can later load the result-set using 'load' - -EXAMPLE: - results = [0, 4, 8] - -> store foo - - find-bytes --or 12345678 - results = [0, 4, 8, 10, 20] - - load foo - results = [0, 4, 8] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('store', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('name', help='name of variable to use') - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - interpreter.set_variable(args.name, addresses) - return addresses +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''save current result-set in a variable. +You can later load the result-set using 'load' + +EXAMPLE: + results = [0, 4, 8] + -> store foo + + find-bytes --or 12345678 + results = [0, 4, 8, 10, 20] + + load foo + results = [0, 4, 8] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('store', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('name', help='name of variable to use') + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + interpreter.set_variable(args.name, addresses) + return addresses diff --git a/fa/commands/trace.py b/fa/commands/trace.py index e11a92f..27caed4 100644 --- a/fa/commands/trace.py +++ b/fa/commands/trace.py @@ -1,18 +1,18 @@ -import pdb - -from fa import utils - - -def get_parser(): - p = utils.ArgumentParserNoExit('trace', - description='sets a pdb breakpoint') - return p - - -def trace(addresses): - pdb.set_trace() - return addresses - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return trace(addresses) +import pdb + +from fa import utils + + +def get_parser(): + p = utils.ArgumentParserNoExit('trace', + description='sets a pdb breakpoint') + return p + + +def trace(addresses): + pdb.set_trace() + return addresses + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return trace(addresses) diff --git a/fa/commands/unique.py b/fa/commands/unique.py index 789e653..91ddfef 100644 --- a/fa/commands/unique.py +++ b/fa/commands/unique.py @@ -1,21 +1,21 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''make the resultset unique - -EXAMPLE: - results = [0, 4, 8, 8, 12] - -> unique - result = [0, 4, 8, 12] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('unique', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return list(set(addresses)) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''make the resultset unique + +EXAMPLE: + results = [0, 4, 8, 8, 12] + -> unique + result = [0, 4, 8, 12] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('unique', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return list(set(addresses)) diff --git a/fa/commands/verify_aligned.py b/fa/commands/verify_aligned.py index c9be117..e746053 100644 --- a/fa/commands/verify_aligned.py +++ b/fa/commands/verify_aligned.py @@ -1,26 +1,26 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''leave only results fitting required alignment - -EXAMPLE: - results = [0, 2, 4, 6, 8] - -> verify-aligned 4 - results = [0, 4, 8] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('verify-aligned', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('value', type=int) - return p - - -def aligned(addresses, value): - return [ea for ea in addresses if ea % value == 0] - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return list(aligned(addresses, args.value)) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''leave only results fitting required alignment + +EXAMPLE: + results = [0, 2, 4, 6, 8] + -> verify-aligned 4 + results = [0, 4, 8] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('verify-aligned', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('value', type=int) + return p + + +def aligned(addresses, value): + return [ea for ea in addresses if ea % value == 0] + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return list(aligned(addresses, args.value)) diff --git a/fa/commands/verify_bytes.py b/fa/commands/verify_bytes.py index eccb163..eec4a7e 100644 --- a/fa/commands/verify_bytes.py +++ b/fa/commands/verify_bytes.py @@ -1,43 +1,43 @@ -from argparse import RawTextHelpFormatter -import binascii - -from fa import utils - -DESCRIPTION = '''reduce the result-set to those matching the given bytes - -EXAMPLE: - 0x00000000: 01 02 03 04 - 0x00000004: 05 06 07 08 - - results = [0, 2, 4, 6, 8] - -> verify-bytes '05 06 07 08' - results = [4] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('verify-bytes', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - p.add_argument('hex_str') - return p - - -def verify_bytes(addresses, hex_str, segments=None, until=None): - magic = binascii.unhexlify(''.join(hex_str.split(' '))) - - results = [ea for ea in addresses - if utils.read_memory(segments, ea, len(magic)) == magic] - - if len(results) > 0: - return results - - return results - - -def run(segments, args, addresses, interpreter=None, **kwargs): - until = None - if 'until' in args and args.until is not None: - until = args.until - return verify_bytes(addresses, args.hex_str, - segments=segments, until=until) +from argparse import RawTextHelpFormatter +import binascii + +from fa import utils + +DESCRIPTION = '''reduce the result-set to those matching the given bytes + +EXAMPLE: + 0x00000000: 01 02 03 04 + 0x00000004: 05 06 07 08 + + results = [0, 2, 4, 6, 8] + -> verify-bytes '05 06 07 08' + results = [4] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('verify-bytes', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + p.add_argument('hex_str') + return p + + +def verify_bytes(addresses, hex_str, segments=None, until=None): + magic = binascii.unhexlify(''.join(hex_str.split(' '))) + + results = [ea for ea in addresses + if utils.read_memory(segments, ea, len(magic)) == magic] + + if len(results) > 0: + return results + + return results + + +def run(segments, args, addresses, interpreter=None, **kwargs): + until = None + if 'until' in args and args.until is not None: + until = args.until + return verify_bytes(addresses, args.hex_str, + segments=segments, until=until) diff --git a/fa/commands/verify_segment.py b/fa/commands/verify_segment.py index 79cfbfb..c63c887 100644 --- a/fa/commands/verify_segment.py +++ b/fa/commands/verify_segment.py @@ -1,43 +1,43 @@ -from argparse import RawTextHelpFormatter - -try: - import idc -except ImportError: - pass - -from fa import utils, context - -DESCRIPTION = '''reduce the result-set to those in the given segment name - -EXAMPLE: - .text:0x00000000 01 02 03 04 - .text:0x00000004 30 31 32 33 - - .data:0x00000200 01 02 03 04 - .data:0x00000204 30 31 32 33 - - results = [0, 0x200] - -> verify-segment .data - results = [0x200] -''' - - -@context.ida_context -def verify_segment(addresses, segment_name): - for ea in addresses: - if segment_name == idc.get_segm_name(ea): - yield ea - - -def get_parser(): - p = utils.ArgumentParserNoExit() - p.add_argument('name', help='segment name') - - p.prog = 'verify-segment' - p.description = DESCRIPTION - p.formatter_class = RawTextHelpFormatter - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return list(verify_segment(addresses, args.name)) +from argparse import RawTextHelpFormatter + +try: + import idc +except ImportError: + pass + +from fa import utils, context + +DESCRIPTION = '''reduce the result-set to those in the given segment name + +EXAMPLE: + .text:0x00000000 01 02 03 04 + .text:0x00000004 30 31 32 33 + + .data:0x00000200 01 02 03 04 + .data:0x00000204 30 31 32 33 + + results = [0, 0x200] + -> verify-segment .data + results = [0x200] +''' + + +@context.ida_context +def verify_segment(addresses, segment_name): + for ea in addresses: + if segment_name == idc.get_segm_name(ea): + yield ea + + +def get_parser(): + p = utils.ArgumentParserNoExit() + p.add_argument('name', help='segment name') + + p.prog = 'verify-segment' + p.description = DESCRIPTION + p.formatter_class = RawTextHelpFormatter + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return list(verify_segment(addresses, args.name)) diff --git a/fa/commands/verify_single.py b/fa/commands/verify_single.py index 27b8f3d..e40ff58 100644 --- a/fa/commands/verify_single.py +++ b/fa/commands/verify_single.py @@ -1,30 +1,30 @@ -from argparse import RawTextHelpFormatter -from fa import utils - -DESCRIPTION = '''verifies the result-list contains a single value - -EXAMPLE #1: - results = [4, 12, 0, 8] - -> unique - result = [] - -EXAMPLE #2: - results = [4] - -> unique - result = [4] -''' - - -def get_parser(): - p = utils.ArgumentParserNoExit('verify-single', - description=DESCRIPTION, - formatter_class=RawTextHelpFormatter) - return p - - -def verify_single(addresses): - return addresses if len(addresses) == 1 else [] - - -def run(segments, args, addresses, interpreter=None, **kwargs): - return verify_single(addresses) +from argparse import RawTextHelpFormatter +from fa import utils + +DESCRIPTION = '''verifies the result-list contains a single value + +EXAMPLE #1: + results = [4, 12, 0, 8] + -> unique + result = [] + +EXAMPLE #2: + results = [4] + -> unique + result = [4] +''' + + +def get_parser(): + p = utils.ArgumentParserNoExit('verify-single', + description=DESCRIPTION, + formatter_class=RawTextHelpFormatter) + return p + + +def verify_single(addresses): + return addresses if len(addresses) == 1 else [] + + +def run(segments, args, addresses, interpreter=None, **kwargs): + return verify_single(addresses) diff --git a/fa/commands/verify_str.py b/fa/commands/verify_str.py index 89346b8..a090510 100644 --- a/fa/commands/verify_str.py +++ b/fa/commands/verify_str.py @@ -1,33 +1,33 @@ -from argparse import RawTextHelpFormatter -import binascii - -from fa.commands import verify_bytes - -DESCRIPTION = '''reduce the result-set to those matching the given string - -EXAMPLE: - 0x00000000: 01 02 03 04 - 0x00000004: 30 31 32 33 -> ascii '0123' - - results = [0, 2, 4] - -> verify-str '0123' - results = [4] -''' - - -def get_parser(): - p = verify_bytes.get_parser() - p.add_argument('--null-terminated', action='store_true') - - p.prog = 'verify-str' - p.description = DESCRIPTION - p.formatter_class = RawTextHelpFormatter - return p - - -def run(segments, args, addresses, interpreter=None, **kwargs): - hex_str = binascii.hexlify(args.hex_str) - hex_str += '00' if args.null_terminated else '' - - setattr(args, 'hex_str', hex_str) - return verify_bytes.run(segments, args, addresses, **kwargs) +from argparse import RawTextHelpFormatter +import binascii + +from fa.commands import verify_bytes + +DESCRIPTION = '''reduce the result-set to those matching the given string + +EXAMPLE: + 0x00000000: 01 02 03 04 + 0x00000004: 30 31 32 33 -> ascii '0123' + + results = [0, 2, 4] + -> verify-str '0123' + results = [4] +''' + + +def get_parser(): + p = verify_bytes.get_parser() + p.add_argument('--null-terminated', action='store_true') + + p.prog = 'verify-str' + p.description = DESCRIPTION + p.formatter_class = RawTextHelpFormatter + return p + + +def run(segments, args, addresses, interpreter=None, **kwargs): + hex_str = binascii.hexlify(args.hex_str) + hex_str += '00' if args.null_terminated else '' + + setattr(args, 'hex_str', hex_str) + return verify_bytes.run(segments, args, addresses, **kwargs) diff --git a/fa/commands/xrefs_to.py b/fa/commands/xrefs_to.py index 7dbe27b..60c4ac4 100644 --- a/fa/commands/xrefs_to.py +++ b/fa/commands/xrefs_to.py @@ -1,53 +1,53 @@ -from fa import utils, context -from fa.commands import function_start - -try: - import idc - import idautils -except ImportError: - pass - - -def get_parser(): - p = utils.ArgumentParserNoExit(prog='xrefs-to', - description='search for xrefs pointing ' - 'at given parameter') - p.add_argument('--function-start', action='store_true', - help='goto function prolog for each xref') - p.add_argument('--or', action='store_true', - help='expand the current result set') - p.add_argument('--and', action='store_true', - help='reduce the current result set') - p.add_argument('--name', help='parameter as label name') - p.add_argument('--bytes', help='parameter as bytes') - return p - - -@context.ida_context -def run(segments, args, addresses, interpreter=None, **kwargs): - if args.name: - ea = idc.LocByName(args.name) - occurrences = [ea] if ea != idc.BADADDR else [] - else: - occurrences = list(utils.ida_find_all(args.bytes)) - - frm = set() - for ea in occurrences: - froms = [ref.frm for ref in idautils.XrefsTo(ea)] - - if args.function_start: - froms = [function_start.get_function_start(segments, ea) - for ea in froms] - - frm.update(frm for frm in froms if frm != idc.BADADDR) - - retval = set() - retval.update(addresses) - - if getattr(args, 'or'): - retval.update(frm) - - elif getattr(args, 'and'): - retval.intersection_update(frm) - - return list(retval) +from fa import utils, context +from fa.commands import function_start + +try: + import idc + import idautils +except ImportError: + pass + + +def get_parser(): + p = utils.ArgumentParserNoExit(prog='xrefs-to', + description='search for xrefs pointing ' + 'at given parameter') + p.add_argument('--function-start', action='store_true', + help='goto function prolog for each xref') + p.add_argument('--or', action='store_true', + help='expand the current result set') + p.add_argument('--and', action='store_true', + help='reduce the current result set') + p.add_argument('--name', help='parameter as label name') + p.add_argument('--bytes', help='parameter as bytes') + return p + + +@context.ida_context +def run(segments, args, addresses, interpreter=None, **kwargs): + if args.name: + ea = idc.LocByName(args.name) + occurrences = [ea] if ea != idc.BADADDR else [] + else: + occurrences = list(utils.ida_find_all(args.bytes)) + + frm = set() + for ea in occurrences: + froms = [ref.frm for ref in idautils.XrefsTo(ea)] + + if args.function_start: + froms = [function_start.get_function_start(segments, ea) + for ea in froms] + + frm.update(frm for frm in froms if frm != idc.BADADDR) + + retval = set() + retval.update(addresses) + + if getattr(args, 'or'): + retval.update(frm) + + elif getattr(args, 'and'): + retval.intersection_update(frm) + + return list(retval) diff --git a/fa/context.py b/fa/context.py index 9bbde24..99cc27e 100644 --- a/fa/context.py +++ b/fa/context.py @@ -1,51 +1,51 @@ -IDA_MODULE = False - -try: - import idc # noqa: F401 - - IDA_MODULE = True -except ImportError: - pass - - -class InvalidContextException(Exception): - pass - - -def get_correct_implementation(function_name, params, ida=None, unknown=None, - **kwargs): - """ - Get and execute the correct implementation according to the currently - executing context - :param function_name: function name to be executes - :param params: parameters to pass to function - :param ida: IDA context implementation - :param unknown: Unknown context implementation - :return: The execution result from the correctly running context - """ - if ida and IDA_MODULE: - return ida(*params, **kwargs) - if unknown: - return unknown(*params, **kwargs) - - raise InvalidContextException( - 'function "{}" must be executed from a specific context' - .format(function_name)) - - -def verify_ida(function_name): - if not IDA_MODULE: - raise InvalidContextException( - 'operation "{}" must be executed from an IDA context' - .format(function_name)) - - -def ida_context(function): - if IDA_MODULE: - return function - else: - def invalid_context(*kwargs): - raise InvalidContextException( - 'function "{}" must be executed from an IDA context' - .format(function.__name__)) - return invalid_context +IDA_MODULE = False + +try: + import idc # noqa: F401 + + IDA_MODULE = True +except ImportError: + pass + + +class InvalidContextException(Exception): + pass + + +def get_correct_implementation(function_name, params, ida=None, unknown=None, + **kwargs): + """ + Get and execute the correct implementation according to the currently + executing context + :param function_name: function name to be executes + :param params: parameters to pass to function + :param ida: IDA context implementation + :param unknown: Unknown context implementation + :return: The execution result from the correctly running context + """ + if ida and IDA_MODULE: + return ida(*params, **kwargs) + if unknown: + return unknown(*params, **kwargs) + + raise InvalidContextException( + 'function "{}" must be executed from a specific context' + .format(function_name)) + + +def verify_ida(function_name): + if not IDA_MODULE: + raise InvalidContextException( + 'operation "{}" must be executed from an IDA context' + .format(function_name)) + + +def ida_context(function): + if IDA_MODULE: + return function + else: + def invalid_context(*kwargs): + raise InvalidContextException( + 'function "{}" must be executed from an IDA context' + .format(function.__name__)) + return invalid_context diff --git a/fa/fa_types.py b/fa/fa_types.py index 9785e64..8132b0c 100644 --- a/fa/fa_types.py +++ b/fa/fa_types.py @@ -1,88 +1,88 @@ -from abc import abstractmethod -from collections import namedtuple - - -try: - import idc - import idaapi - import ida_auto - import ida_enum - import ida_struct - - IDA_MODULE = True -except ImportError: - pass - - -class FaType(object): - def __init__(self, name): - self._name = name - - def get_name(self): - return self._name - - def exists(self): - return -1 != ida_struct.get_struc_id(self._name) - - @abstractmethod - def update_idb(self): - pass - - -class FaEnum(FaType): - def __init__(self, name): - super(FaEnum, self).__init__(name) - self._values = {} - - def add_value(self, name, value): - self._values[value] = name - - def update_idb(self): - id = ida_enum.get_enum(self._name) - if idc.BADADDR == id: - id = ida_enum.add_enum(idc.BADADDR, self._name, idaapi.decflag()) - - keys = self._values.keys() - keys.sort() - - for k in keys: - ida_enum.add_enum_member(id, self._values[k], k) - - -class FaStruct(FaType): - Field = namedtuple('Field', ['name', 'type', 'offset']) - - def __init__(self, name): - super(FaStruct, self).__init__(name) - self._fields = [] - - def add_field(self, name, type_, offset=0xffffffff): - self._fields.append(self.Field(name, type_, offset)) - - def update_idb(self, delete_existing_members=True): - sid = ida_struct.get_struc_id(self._name) - sptr = ida_struct.get_struc(sid) - - if sid == idc.BADADDR: - sid = ida_struct.add_struc(idc.BADADDR, self._name, 0) - sptr = ida_struct.get_struc(sid) - else: - if delete_existing_members: - ida_struct.del_struc_members(sptr, 0, 0xffffffff) - - for f in self._fields: - ida_struct.add_struc_member(sptr, f.name, f.offset, - (idc.FF_BYTE | idc.FF_DATA) - & 0xFFFFFFFF, - None, 1) - member_name = "{}.{}".format(self._name, f.name) - idc.SetType(idaapi.get_member_by_fullname(member_name)[0].id, - f.type) - - ida_auto.auto_wait() - - -def add_const(name, value): - fa_consts = FaEnum('FA_CONSTS') - fa_consts.add_value(name, value) - fa_consts.update_idb() +from abc import abstractmethod +from collections import namedtuple + + +try: + import idc + import idaapi + import ida_auto + import ida_enum + import ida_struct + + IDA_MODULE = True +except ImportError: + pass + + +class FaType(object): + def __init__(self, name): + self._name = name + + def get_name(self): + return self._name + + def exists(self): + return -1 != ida_struct.get_struc_id(self._name) + + @abstractmethod + def update_idb(self): + pass + + +class FaEnum(FaType): + def __init__(self, name): + super(FaEnum, self).__init__(name) + self._values = {} + + def add_value(self, name, value): + self._values[value] = name + + def update_idb(self): + id = ida_enum.get_enum(self._name) + if idc.BADADDR == id: + id = ida_enum.add_enum(idc.BADADDR, self._name, idaapi.decflag()) + + keys = self._values.keys() + keys.sort() + + for k in keys: + ida_enum.add_enum_member(id, self._values[k], k) + + +class FaStruct(FaType): + Field = namedtuple('Field', ['name', 'type', 'offset']) + + def __init__(self, name): + super(FaStruct, self).__init__(name) + self._fields = [] + + def add_field(self, name, type_, offset=0xffffffff): + self._fields.append(self.Field(name, type_, offset)) + + def update_idb(self, delete_existing_members=True): + sid = ida_struct.get_struc_id(self._name) + sptr = ida_struct.get_struc(sid) + + if sid == idc.BADADDR: + sid = ida_struct.add_struc(idc.BADADDR, self._name, 0) + sptr = ida_struct.get_struc(sid) + else: + if delete_existing_members: + ida_struct.del_struc_members(sptr, 0, 0xffffffff) + + for f in self._fields: + ida_struct.add_struc_member(sptr, f.name, f.offset, + (idc.FF_BYTE | idc.FF_DATA) + & 0xFFFFFFFF, + None, 1) + member_name = "{}.{}".format(self._name, f.name) + idc.SetType(idaapi.get_member_by_fullname(member_name)[0].id, + f.type) + + ida_auto.auto_wait() + + +def add_const(name, value): + fa_consts = FaEnum('FA_CONSTS') + fa_consts.add_value(name, value) + fa_consts.update_idb() diff --git a/fa/fainterp.py b/fa/fainterp.py index 47b9323..cfe597e 100644 --- a/fa/fainterp.py +++ b/fa/fainterp.py @@ -1,549 +1,549 @@ -import time - -from tkinter import ttk, Tk -from configparser import ConfigParser - -from abc import ABCMeta, abstractmethod -from collections import OrderedDict, namedtuple -import shlex -import sys -import os - -import hjson - -from fa.utils import ArgumentParserNoExit - -CONFIG_PATH = os.path.join( - os.path.dirname(os.path.abspath(__file__)), '..', 'config.ini') -DEFAULT_SIGNATURES_ROOT = os.path.join( - os.path.dirname(os.path.abspath(__file__)), 'signatures') -COMMANDS_ROOT = os.path.join( - os.path.dirname(os.path.abspath(__file__)), 'commands') - - -class InterpreterState: - def __init__(self): - self.pc = 0 - self.variables = {} - self.labels = {} - - -class FaInterp: - """ - FA Interpreter base class - """ - __metaclass__ = ABCMeta - - def __init__(self, config_path=CONFIG_PATH): - """ - Constructor - :param config_path: config.ini path. used to load global settings - instead of setting each of the options manually - (signatures_root, project, ...) - """ - self._project = None - self._input = None - self._segments = OrderedDict() - self._signatures_root = DEFAULT_SIGNATURES_ROOT - self._symbols = {} - self._consts = {} - self.history = [] - self.endianity = '<' - self._config_path = config_path - self.stack = [] - - if (config_path is not None) and (os.path.exists(config_path)): - self._signatures_root = os.path.expanduser( - self.config_get('global', 'signatures_root')) - self._project = self.config_get('global', 'project', None) - - def _push_stack_frame(self): - self.stack.append(InterpreterState()) - - def _pop_stack_frame(self): - self.stack.pop() - - def set_labels(self, labels): - self.stack[-1].labels = labels - - def get_pc(self): - return self.stack[-1].pc - - def set_pc(self, pc): - if isinstance(pc, int): - self.stack[-1].pc = pc - elif isinstance(pc, str): - self.stack[-1].pc = self.stack[-1].labels[pc] - else: - raise KeyError('invalid pc: {}'.format(pc)) - - def dec_pc(self): - self.stack[-1].pc -= 1 - - def inc_pc(self): - self.stack[-1].pc += 1 - - def set_variable(self, name, value): - self.stack[-1].variables[name] = value - - def get_variable(self, name): - return self.stack[-1].variables[name] - - def get_all_variables(self): - return self.stack[-1].variables - - @abstractmethod - def set_input(self, input_): - """ - Set file input - :param input_: file to work on - :return: - """ - pass - - def config_get(self, section, key, default=None): - """ - Read configuration setting. This is loaded from INI config file. - :param section: section name - :param key: key name - :param default: default value, if key doesn't exist inside section - :return: the value in the specified section-key - """ - if not os.path.exists(self._config_path): - return default - - config = ConfigParser() - - with open(self._config_path) as f: - config.read_file(f) - - if not config.has_section(section) or \ - not config.has_option(section, key): - return default - - return config.get(section, key) - - def config_set(self, section, key, value): - """ - Write configuration setting. This is saved into INI config file - :param section: section name - :param key: key name - :param value: value to set - :return: None - """ - config = ConfigParser() - - if sys.version[0] == '2': - section = section.decode('utf8') - key = key.decode('utf8') - value = value.decode('utf8') - - if os.path.exists(self._config_path): - config.read(self._config_path) - - if not config.has_section(section): - config.add_section(section) - - config.set(section, key, value) - - with open(self._config_path, 'w') as f: - config.write(f) - - def set_signatures_root(self, path, save=False): - """ - Change signatures root path (where the projects are searched). - :param path: signatures root path (where the projects are searched). - :param save: should save into configuration file? - :return: None - """ - self.log('signatures root: {}'.format(path)) - self._signatures_root = path - - if save: - self.config_set('global', 'signatures_root', path) - - def verify_project(self): - """ - Throws IOError if no project has been selected or points into an - invalid path - :return: None - """ - if self._project is None: - raise IOError('No project has been selected') - - if not os.path.exists(os.path.join(self._signatures_root, - self._project)): - raise IOError("Selected project's path doesn't exist.\n" - "Please re-select)") - - def set_project(self, project, save=True): - """ - Set currently active project (where SIG files are placed) - :param project: project name - :param save: should save this setting into configuration file? - :return: None - """ - self._project = project - self.log('project set: {}'.format(project)) - - self.set_signatures_root(self._signatures_root, save=save) - if save: - self.config_set('global', 'project', project) - - def symbols(self): - """ - Run find for all SIG files in currently active project - :return: dictionary of found symbols - """ - self.get_python_symbols() - - for sig in self.get_json_signatures(): - self.find(sig['name']) - - return self._symbols - - def interactive_set_project(self): - """ - Show GUI for selecting a project from signatures_root - :return: None - """ - app = Tk() - # app.geometry('200x30') - - label = ttk.Label(app, - text="Choose current project") - label.grid(column=0, row=0) - - combo = ttk.Combobox(app, - values=self.list_projects()) - combo.grid(column=0, row=1) - - def combobox_change_project(event): - self.set_project(combo.get()) - - combo.bind("<>", combobox_change_project) - - app.mainloop() - - def list_projects(self): - """ - Get a list of all available projects in signatures_root - :return: list of all available projects in signatures_root - """ - projects = [] - for root, dirs, files in os.walk(self._signatures_root): - projects += \ - [os.path.relpath(os.path.join(root, filename), - self._signatures_root) for filename in dirs] - return [str(p) for p in projects if p[0] != '.'] - - @staticmethod - def log(message): - """ - Log message - :param message: - :return: - """ - for line in str(message).splitlines(): - print('FA> {}'.format(line)) - - @abstractmethod - def reload_segments(self): - """ - Reload memory segments - :return: - """ - pass - - @staticmethod - def get_module(name, filename): - """ - Load a python module by filename - :param name: module name - :param filename: module filename - :return: loaded python module - """ - if not os.path.exists(filename): - raise NotImplementedError("no such filename: {}".format(filename)) - - if sys.version == '3': - # TODO: support python 3.0-3.4 - import importlib.util - spec = importlib.util.spec_from_file_location(name, filename) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - else: - import imp - module = imp.load_source(name, filename) - - return module - - @staticmethod - def get_command(command): - """ - Get fa command as a loaded python-module - :param command: command name - :return: command's python-module - """ - filename = os.path.join(COMMANDS_ROOT, "{}.py".format(command)) - return FaInterp.get_module(command, filename) - - def run_command(self, command, addresses): - """ - Run fa command with given address list and output the result - :param command: fa command name - :param addresses: input address list - :return: output address list - """ - args = '' - if ' ' in command: - command, args = command.split(' ', 1) - args = shlex.split(args) - - command = command.replace('-', '_') - - module = self.get_command(command) - p = module.get_parser() - args = p.parse_args(args) - return module.run(self._segments, args, addresses, - interpreter=self) - - def get_alias(self): - """ - Get dictionary of all defined aliases globally and by project. - Project aliases loaded last so are considered stronger. - :return: dictionary of all fa command aliases - """ - retval = {} - with open(os.path.join(COMMANDS_ROOT, 'alias')) as f: - for line in f.readlines(): - line = line.strip() - k, v = line.split('=') - retval[k.strip()] = v.strip() - - if self._project: - # include also project alias - project_root = os.path.join(self._signatures_root, self._project) - project_alias_filename = os.path.join(project_root, 'alias') - if os.path.exists(project_alias_filename): - with open(project_alias_filename) as f: - for line in f.readlines(): - line = line.strip() - k, v = line.split('=') - retval[k.strip()] = v.strip() - - return retval - - def save_signature(self, filename): - """ - Save given signature object (by dictionary) into active project - as a new SIG file. If symbol name already exists, then create another - file (never overwrites). - :param filename: Dictionary of signature object - :return: None - """ - with open(filename) as f: - sig = hjson.load(f) - f.seek(0) - sig_text = f.read() - - filename = os.path.join( - self._signatures_root, - self._project, - sig['name'] + '.sig') - i = 1 - while os.path.exists(filename): - filename = os.path.join(self._signatures_root, self._project, - sig['name'] + '.{}.sig'.format(i)) - i += 1 - - with open(filename, 'w') as f: - f.write(sig_text) - - def _get_labeled_instructions(self, instructions): - labels = {} - processed_instructions = [] - - label_parser = ArgumentParserNoExit('label') - label_parser.add_argument('name') - - alias_items = self.get_alias().items() - - pc = 0 - for line in instructions: - line = line.strip() - - if len(line) == 0: - continue - - if line.startswith('#'): - # treat as comment - continue - - if line.startswith('label '): - args = label_parser.parse_args(shlex.split(line)[1:]) - labels[args.name] = pc - continue - - for k, v in alias_items: - # handle aliases - if line.startswith(k): - line = line.replace(k, v) - - processed_instructions.append(line) - pc += 1 - - return labels, processed_instructions - - def find_from_instructions_list(self, instructions, addresses=None): - """ - Run the given instruction list and output the result - :param instructions: instruction list - :param addresses: input address list (if any) - :return: output address list - """ - if addresses is None: - addresses = [] - - self._push_stack_frame() - - labels, instructions = self._get_labeled_instructions(instructions) - - self.set_labels(labels) - - n = len(instructions) - - while self.get_pc() < n: - line = instructions[self.get_pc()] - - new_addresses = [] - try: - new_addresses = self.run_command(line, addresses) - except ImportError as m: - FaInterp.log('failed to run: {}. error: {}' - .format(line, str(m))) - - addresses = new_addresses - self.inc_pc() - - self._pop_stack_frame() - return addresses - - def find_from_sig_json(self, signature_json, decremental=False): - """ - Find a signature from a signature JSON data. - :param dict signature_json: Data of signature's JSON. - :param bool decremental: - :return: Addresses of matching signatures. - :rtype: result list of last returns instruction - """ - self.log('interpreting SIG for: {}'.format(signature_json['name'])) - start = time.time() - retval = self.find_from_instructions_list( - signature_json['instructions']) - self.log('interpretation took: {}s'.format(time.time() - start)) - return retval - - def find_from_sig_path(self, signature_path, decremental=False): - """ - Find a signature from a signature file path. - :param str signature_path: Path to a signature file. - :param bool decremental: - :return: Addresses of matching signatures. - :rtype: result list of last returns instruction - """ - local_path = os.path.join( - self._signatures_root, self._project, signature_path) - if os.path.exists(local_path): - # prefer local signatures, then external - signature_path = local_path - - with open(signature_path) as f: - sig = hjson.load(f) - return self.find_from_sig_json(sig, decremental) - - def get_python_symbols(self, file_name=None): - """ - Run all python scripts found in currently active project and return - the dictionary of all found symbols - :param file_name: optional, specify which python script to execute - inside the currently active project - :return: dictionary of all found symbols - """ - project_root = os.path.join(self._signatures_root, self._project) - sys.path.append(project_root) - - for root, dirs, files in os.walk(project_root): - for filename in sorted(files): - if not filename.lower().endswith('.py'): - continue - - if not file_name or file_name == filename: - name = os.path.splitext(filename)[0] - filename = os.path.join(root, filename) - m = FaInterp.get_module(name, filename) - if not hasattr(m, 'run'): - self.log('skipping: {}'.format(filename)) - else: - m.run(self) - - def get_json_signatures(self, symbol_name=None): - """ - Get a list of all json SIG objects in currently active project. - :param symbol_name: optional, select a specific SIG file by symbol name - :return: list of all json SIG objects in currently active project. - """ - signatures = [] - project_root = os.path.join(self._signatures_root, self._project) - - for root, dirs, files in os.walk(project_root): - for filename in sorted(files): - if not filename.lower().endswith('.sig'): - continue - - filename = os.path.join(project_root, filename) - with open(filename) as f: - try: - signature = hjson.load(f) - except ValueError as e: - self.log('error in json: {}'.format(filename)) - raise e - - if (symbol_name is None) or (signature['name'] == symbol_name): - signatures.append(signature) - - return signatures - - def set_const(self, name, value): - self._consts[name] = value - - def set_symbol(self, symbol_name, value): - self._symbols[symbol_name] = value - - def get_consts(self): - return self._consts - - def find(self, symbol_name, decremental=False): - """ - Find symbol by its name in the SIG file - :param symbol_name: symbol name - :param decremental: Should stop *before* the last command which - returned zero results - :return: list of matches for the given symbol - """ - results = [] - signatures = self.get_json_signatures(symbol_name) - if len(signatures) == 0: - raise NotImplementedError('no signature found for: {}' - .format(symbol_name)) - - for sig in signatures: - sig_results = self.find_from_sig_json(sig) - - if isinstance(sig_results, dict): - if symbol_name in sig_results: - results += sig_results[symbol_name] - else: - results += sig_results - - return list(set(results)) +import time + +from tkinter import ttk, Tk +from configparser import ConfigParser + +from abc import ABCMeta, abstractmethod +from collections import OrderedDict, namedtuple +import shlex +import sys +import os + +import hjson + +from fa.utils import ArgumentParserNoExit + +CONFIG_PATH = os.path.join( + os.path.dirname(os.path.abspath(__file__)), '..', 'config.ini') +DEFAULT_SIGNATURES_ROOT = os.path.join( + os.path.dirname(os.path.abspath(__file__)), 'signatures') +COMMANDS_ROOT = os.path.join( + os.path.dirname(os.path.abspath(__file__)), 'commands') + + +class InterpreterState: + def __init__(self): + self.pc = 0 + self.variables = {} + self.labels = {} + + +class FaInterp: + """ + FA Interpreter base class + """ + __metaclass__ = ABCMeta + + def __init__(self, config_path=CONFIG_PATH): + """ + Constructor + :param config_path: config.ini path. used to load global settings + instead of setting each of the options manually + (signatures_root, project, ...) + """ + self._project = None + self._input = None + self._segments = OrderedDict() + self._signatures_root = DEFAULT_SIGNATURES_ROOT + self._symbols = {} + self._consts = {} + self.history = [] + self.endianity = '<' + self._config_path = config_path + self.stack = [] + + if (config_path is not None) and (os.path.exists(config_path)): + self._signatures_root = os.path.expanduser( + self.config_get('global', 'signatures_root')) + self._project = self.config_get('global', 'project', None) + + def _push_stack_frame(self): + self.stack.append(InterpreterState()) + + def _pop_stack_frame(self): + self.stack.pop() + + def set_labels(self, labels): + self.stack[-1].labels = labels + + def get_pc(self): + return self.stack[-1].pc + + def set_pc(self, pc): + if isinstance(pc, int): + self.stack[-1].pc = pc + elif isinstance(pc, str): + self.stack[-1].pc = self.stack[-1].labels[pc] + else: + raise KeyError('invalid pc: {}'.format(pc)) + + def dec_pc(self): + self.stack[-1].pc -= 1 + + def inc_pc(self): + self.stack[-1].pc += 1 + + def set_variable(self, name, value): + self.stack[-1].variables[name] = value + + def get_variable(self, name): + return self.stack[-1].variables[name] + + def get_all_variables(self): + return self.stack[-1].variables + + @abstractmethod + def set_input(self, input_): + """ + Set file input + :param input_: file to work on + :return: + """ + pass + + def config_get(self, section, key, default=None): + """ + Read configuration setting. This is loaded from INI config file. + :param section: section name + :param key: key name + :param default: default value, if key doesn't exist inside section + :return: the value in the specified section-key + """ + if not os.path.exists(self._config_path): + return default + + config = ConfigParser() + + with open(self._config_path) as f: + config.read_file(f) + + if not config.has_section(section) or \ + not config.has_option(section, key): + return default + + return config.get(section, key) + + def config_set(self, section, key, value): + """ + Write configuration setting. This is saved into INI config file + :param section: section name + :param key: key name + :param value: value to set + :return: None + """ + config = ConfigParser() + + if sys.version[0] == '2': + section = section.decode('utf8') + key = key.decode('utf8') + value = value.decode('utf8') + + if os.path.exists(self._config_path): + config.read(self._config_path) + + if not config.has_section(section): + config.add_section(section) + + config.set(section, key, value) + + with open(self._config_path, 'w') as f: + config.write(f) + + def set_signatures_root(self, path, save=False): + """ + Change signatures root path (where the projects are searched). + :param path: signatures root path (where the projects are searched). + :param save: should save into configuration file? + :return: None + """ + self.log('signatures root: {}'.format(path)) + self._signatures_root = path + + if save: + self.config_set('global', 'signatures_root', path) + + def verify_project(self): + """ + Throws IOError if no project has been selected or points into an + invalid path + :return: None + """ + if self._project is None: + raise IOError('No project has been selected') + + if not os.path.exists(os.path.join(self._signatures_root, + self._project)): + raise IOError("Selected project's path doesn't exist.\n" + "Please re-select)") + + def set_project(self, project, save=True): + """ + Set currently active project (where SIG files are placed) + :param project: project name + :param save: should save this setting into configuration file? + :return: None + """ + self._project = project + self.log('project set: {}'.format(project)) + + self.set_signatures_root(self._signatures_root, save=save) + if save: + self.config_set('global', 'project', project) + + def symbols(self): + """ + Run find for all SIG files in currently active project + :return: dictionary of found symbols + """ + self.get_python_symbols() + + for sig in self.get_json_signatures(): + self.find(sig['name']) + + return self._symbols + + def interactive_set_project(self): + """ + Show GUI for selecting a project from signatures_root + :return: None + """ + app = Tk() + # app.geometry('200x30') + + label = ttk.Label(app, + text="Choose current project") + label.grid(column=0, row=0) + + combo = ttk.Combobox(app, + values=self.list_projects()) + combo.grid(column=0, row=1) + + def combobox_change_project(event): + self.set_project(combo.get()) + + combo.bind("<>", combobox_change_project) + + app.mainloop() + + def list_projects(self): + """ + Get a list of all available projects in signatures_root + :return: list of all available projects in signatures_root + """ + projects = [] + for root, dirs, files in os.walk(self._signatures_root): + projects += \ + [os.path.relpath(os.path.join(root, filename), + self._signatures_root) for filename in dirs] + return [str(p) for p in projects if p[0] != '.'] + + @staticmethod + def log(message): + """ + Log message + :param message: + :return: + """ + for line in str(message).splitlines(): + print('FA> {}'.format(line)) + + @abstractmethod + def reload_segments(self): + """ + Reload memory segments + :return: + """ + pass + + @staticmethod + def get_module(name, filename): + """ + Load a python module by filename + :param name: module name + :param filename: module filename + :return: loaded python module + """ + if not os.path.exists(filename): + raise NotImplementedError("no such filename: {}".format(filename)) + + if sys.version == '3': + # TODO: support python 3.0-3.4 + import importlib.util + spec = importlib.util.spec_from_file_location(name, filename) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + else: + import imp + module = imp.load_source(name, filename) + + return module + + @staticmethod + def get_command(command): + """ + Get fa command as a loaded python-module + :param command: command name + :return: command's python-module + """ + filename = os.path.join(COMMANDS_ROOT, "{}.py".format(command)) + return FaInterp.get_module(command, filename) + + def run_command(self, command, addresses): + """ + Run fa command with given address list and output the result + :param command: fa command name + :param addresses: input address list + :return: output address list + """ + args = '' + if ' ' in command: + command, args = command.split(' ', 1) + args = shlex.split(args) + + command = command.replace('-', '_') + + module = self.get_command(command) + p = module.get_parser() + args = p.parse_args(args) + return module.run(self._segments, args, addresses, + interpreter=self) + + def get_alias(self): + """ + Get dictionary of all defined aliases globally and by project. + Project aliases loaded last so are considered stronger. + :return: dictionary of all fa command aliases + """ + retval = {} + with open(os.path.join(COMMANDS_ROOT, 'alias')) as f: + for line in f.readlines(): + line = line.strip() + k, v = line.split('=') + retval[k.strip()] = v.strip() + + if self._project: + # include also project alias + project_root = os.path.join(self._signatures_root, self._project) + project_alias_filename = os.path.join(project_root, 'alias') + if os.path.exists(project_alias_filename): + with open(project_alias_filename) as f: + for line in f.readlines(): + line = line.strip() + k, v = line.split('=') + retval[k.strip()] = v.strip() + + return retval + + def save_signature(self, filename): + """ + Save given signature object (by dictionary) into active project + as a new SIG file. If symbol name already exists, then create another + file (never overwrites). + :param filename: Dictionary of signature object + :return: None + """ + with open(filename) as f: + sig = hjson.load(f) + f.seek(0) + sig_text = f.read() + + filename = os.path.join( + self._signatures_root, + self._project, + sig['name'] + '.sig') + i = 1 + while os.path.exists(filename): + filename = os.path.join(self._signatures_root, self._project, + sig['name'] + '.{}.sig'.format(i)) + i += 1 + + with open(filename, 'w') as f: + f.write(sig_text) + + def _get_labeled_instructions(self, instructions): + labels = {} + processed_instructions = [] + + label_parser = ArgumentParserNoExit('label') + label_parser.add_argument('name') + + alias_items = self.get_alias().items() + + pc = 0 + for line in instructions: + line = line.strip() + + if len(line) == 0: + continue + + if line.startswith('#'): + # treat as comment + continue + + if line.startswith('label '): + args = label_parser.parse_args(shlex.split(line)[1:]) + labels[args.name] = pc + continue + + for k, v in alias_items: + # handle aliases + if line.startswith(k): + line = line.replace(k, v) + + processed_instructions.append(line) + pc += 1 + + return labels, processed_instructions + + def find_from_instructions_list(self, instructions, addresses=None): + """ + Run the given instruction list and output the result + :param instructions: instruction list + :param addresses: input address list (if any) + :return: output address list + """ + if addresses is None: + addresses = [] + + self._push_stack_frame() + + labels, instructions = self._get_labeled_instructions(instructions) + + self.set_labels(labels) + + n = len(instructions) + + while self.get_pc() < n: + line = instructions[self.get_pc()] + + new_addresses = [] + try: + new_addresses = self.run_command(line, addresses) + except ImportError as m: + FaInterp.log('failed to run: {}. error: {}' + .format(line, str(m))) + + addresses = new_addresses + self.inc_pc() + + self._pop_stack_frame() + return addresses + + def find_from_sig_json(self, signature_json, decremental=False): + """ + Find a signature from a signature JSON data. + :param dict signature_json: Data of signature's JSON. + :param bool decremental: + :return: Addresses of matching signatures. + :rtype: result list of last returns instruction + """ + self.log('interpreting SIG for: {}'.format(signature_json['name'])) + start = time.time() + retval = self.find_from_instructions_list( + signature_json['instructions']) + self.log('interpretation took: {}s'.format(time.time() - start)) + return retval + + def find_from_sig_path(self, signature_path, decremental=False): + """ + Find a signature from a signature file path. + :param str signature_path: Path to a signature file. + :param bool decremental: + :return: Addresses of matching signatures. + :rtype: result list of last returns instruction + """ + local_path = os.path.join( + self._signatures_root, self._project, signature_path) + if os.path.exists(local_path): + # prefer local signatures, then external + signature_path = local_path + + with open(signature_path) as f: + sig = hjson.load(f) + return self.find_from_sig_json(sig, decremental) + + def get_python_symbols(self, file_name=None): + """ + Run all python scripts found in currently active project and return + the dictionary of all found symbols + :param file_name: optional, specify which python script to execute + inside the currently active project + :return: dictionary of all found symbols + """ + project_root = os.path.join(self._signatures_root, self._project) + sys.path.append(project_root) + + for root, dirs, files in os.walk(project_root): + for filename in sorted(files): + if not filename.lower().endswith('.py'): + continue + + if not file_name or file_name == filename: + name = os.path.splitext(filename)[0] + filename = os.path.join(root, filename) + m = FaInterp.get_module(name, filename) + if not hasattr(m, 'run'): + self.log('skipping: {}'.format(filename)) + else: + m.run(self) + + def get_json_signatures(self, symbol_name=None): + """ + Get a list of all json SIG objects in currently active project. + :param symbol_name: optional, select a specific SIG file by symbol name + :return: list of all json SIG objects in currently active project. + """ + signatures = [] + project_root = os.path.join(self._signatures_root, self._project) + + for root, dirs, files in os.walk(project_root): + for filename in sorted(files): + if not filename.lower().endswith('.sig'): + continue + + filename = os.path.join(project_root, filename) + with open(filename) as f: + try: + signature = hjson.load(f) + except ValueError as e: + self.log('error in json: {}'.format(filename)) + raise e + + if (symbol_name is None) or (signature['name'] == symbol_name): + signatures.append(signature) + + return signatures + + def set_const(self, name, value): + self._consts[name] = value + + def set_symbol(self, symbol_name, value): + self._symbols[symbol_name] = value + + def get_consts(self): + return self._consts + + def find(self, symbol_name, decremental=False): + """ + Find symbol by its name in the SIG file + :param symbol_name: symbol name + :param decremental: Should stop *before* the last command which + returned zero results + :return: list of matches for the given symbol + """ + results = [] + signatures = self.get_json_signatures(symbol_name) + if len(signatures) == 0: + raise NotImplementedError('no signature found for: {}' + .format(symbol_name)) + + for sig in signatures: + sig_results = self.find_from_sig_json(sig) + + if isinstance(sig_results, dict): + if symbol_name in sig_results: + results += sig_results[symbol_name] + else: + results += sig_results + + return list(set(results)) diff --git a/fa/ida_plugin.py b/fa/ida_plugin.py index fe2795e..fc5fce2 100644 --- a/fa/ida_plugin.py +++ b/fa/ida_plugin.py @@ -1,723 +1,723 @@ -import binascii -import re -import traceback -from collections import namedtuple -import subprocess -import tempfile -import sys -import os - -sys.path.append('.') # noqa: E402 - -import hjson -import click - -from ida_kernwin import Form -import ida_kernwin -import ida_typeinf -import ida_bytes -import idautils -import ida_pro -import idaapi -import idc - -from fa import fainterp, fa_types - -# Filename for the temporary created signature -TEMP_SIG_FILENAME = os.path.join(tempfile.gettempdir(), 'fa_tmp_sig.sig') - -# IDA fa plugin filename -PLUGIN_FILENAME = 'fa_ida_plugin.py' - - -def open_file(filename): - """ - Attempt to open the given filename by OS' default editor - :param filename: filename to open - :return: None - """ - if sys.platform == "win32": - try: - os.startfile(filename) - except Exception as error_code: - if error_code[0] == 1155: - os.spawnl(os.P_NOWAIT, - os.path.join(os.environ['WINDIR'], - 'system32', 'Rundll32.exe'), - 'Rundll32.exe SHELL32.DLL, OpenAs_RunDLL {}' - .format(filename)) - else: - print("other error") - else: - opener = "open" if sys.platform == "darwin" else "xdg-open" - subprocess.call([opener, filename]) - - -class IdaLoader(fainterp.FaInterp): - """ - IDA loader - Includes improved GUI interaction for accessing the interpreter - functionality. - """ - - def __init__(self): - super(IdaLoader, self).__init__() - self._create_template_symbol = eval(self.config_get( - 'global', - 'create_symbol_template', - 'False' - )) - - def set_symbol_template(self, status): - """ - Should the create-temp-signature feature attempt to create a default - signature by predefined template? - :param status: new boolean setting - :return: None - """ - self._create_template_symbol = status - self.config_set('global', 'create_symbol_template', str(status)) - - def create_symbol(self): - """ - Create a temporary symbol signature from the current function on the - IDA screen. - """ - self.log('creating temporary signature') - - current_ea = idc.get_screen_ea() - - signature = { - 'name': idc.get_func_name(current_ea), - 'instructions': [] - } - - if self._create_template_symbol: - find_bytes_ida = "find-bytes-ida '" - - for ea in idautils.FuncItems(current_ea): - mnem = idc.print_insn_mnem(ea).lower() - opcode_size = idc.get_item_size(ea) - - # ppc - if mnem.startswith('b') or mnem in ('lis', 'lwz', 'addi'): - # relative opcodes - find_bytes_ida += '?? ' * opcode_size - continue - - # arm - if mnem.startswith('b') or mnem in ('ldr', 'str'): - # relative opcodes - find_bytes_ida += '?? ' * opcode_size - continue - - opcode = binascii.hexlify(idc.get_bytes(ea, opcode_size)) - formatted_hex = ' '.join(opcode[i:i + 2] for i in - range(0, len(opcode), 2)) - find_bytes_ida += formatted_hex + ' ' - - find_bytes_ida += "'" - - signature['instructions'].append(find_bytes_ida) - signature['instructions'].append('function-start') - signature['instructions'].append('set-name "{}"'.format( - idc.get_func_name(current_ea))) - - with open(TEMP_SIG_FILENAME, 'w') as f: - hjson.dump(signature, f, indent=4) - - self.log('Signature created at {}'.format(TEMP_SIG_FILENAME)) - return TEMP_SIG_FILENAME - - def extended_create_symbol(self): - """ - Creates a temporary symbol of the currently active function - and open it using OS default editor - :return: None - """ - filename = self.create_symbol() - open_file(filename) - - def find_symbol(self): - """ - Find the last create symbol signature. - :return: None - """ - with open(TEMP_SIG_FILENAME) as f: - sig = hjson.load(f) - - results = self.find_from_sig_json(sig, decremental=False) - - for address in results: - self.log('Search result: 0x{:x}'.format(address)) - self.log('Search done') - - if len(results) == 1: - # if remote sig has a proper name, but current one is not - ida_kernwin.jumpto(results[0]) - - def verify_project(self): - """ - Verify a valid project is currently active. - Show IDA warning if not. - :return: None - """ - try: - super(IdaLoader, self).verify_project() - except IOError as e: - ida_kernwin.warning(e.message) - raise e - - def prompt_save_signature(self): - """ - Save last-created-temp-signature if user agrees to in IDA prompt - :return: None - """ - self.verify_project() - - if ida_kernwin.ask_yn(1, 'Are you sure you want ' - 'to save this signature?') != 1: - return - - self.save_signature(TEMP_SIG_FILENAME) - - def find(self, symbol_name, decremental=False): - """ - Find symbol by name (as specified in SIG file) - Show an IDA waitbox while doing so - :param symbol_name: symbol name - :return: output address list - """ - ida_kernwin.replace_wait_box('Searching symbol: \'{}\'...' - .format(symbol_name)) - return super(IdaLoader, self).find(symbol_name) - - def get_python_symbols(self, file_name=None): - """ - Run all python scripts inside the currently active project. - Show an IDA waitbox while doing so - :param file_name: filter a specific filename to execute - :return: dictionary of all found symbols - """ - ida_kernwin.replace_wait_box('Running python scripts...') - return super(IdaLoader, self).get_python_symbols(file_name=file_name) - - @staticmethod - def extract_all_user_names(filename=None): - """ - Get all user-named labels inside IDA. Also prints into output window. - :return: dictionary of all user named labels: label_name -> ea - """ - results = {} - output = '' - - for ea, name in idautils.Names(): - if ida_kernwin.user_cancelled(): - return results - - if '_' in name: - if name.split('_')[0] in ('def', 'sub', 'loc', 'jpt', 'j', - 'nullsub'): - continue - flags = ida_bytes.get_full_flags(ea) - if ida_bytes.has_user_name(flags): - results[name] = ea - output += '{} = 0x{:08x};\n'.format(name, ea) - - if filename is not None: - with open(filename, 'w') as f: - f.write(output) - - return results - - def set_const(self, name, value): - super(IdaLoader, self).set_const(name, value) - fa_types.add_const(name, value) - - def set_symbol(self, symbol_name, value): - super(IdaLoader, self).set_symbol(symbol_name, value) - idc.set_name(value, symbol_name, idc.SN_CHECK) - - def symbols(self, output_file_path=None): - """ - Run find for all SIG files in currently active project. - Show an IDA waitbox while doing so - :param output_file_path: optional, save found symbols into output file - :return: dictionary of found symbols - """ - self.verify_project() - results = {} - - try: - ida_kernwin.show_wait_box('Searching...') - results = super(IdaLoader, self).symbols() - - ida_kernwin.replace_wait_box('Extracting...') - ida_symbols = IdaLoader.extract_all_user_names(output_file_path) - - results.update(ida_symbols) - - except Exception as e: - traceback.print_exc() - finally: - ida_kernwin.hide_wait_box() - - return results - - def export(self): - """ - Show an export dialog to export symbols and header file for given - IDB. - :return: None - """ - class ExportForm(Form): - def __init__(self): - description = ''' -

Export

- - Select a directory to export IDB data into. - ''' - - Form.__init__(self, - r"""BUTTON YES* Save - Export - {StringLabel} - <#Symbols#Symbols filename:{iSymbolsFilename}> - <#C Header#C Header filename:{iHeaderFilename}> - <#ifdef_macro#ifdef'ed:{iIfdef}> - <#Select dir#Browse for dir:{iDir}> - """, { - 'iDir': Form.DirInput(), - 'StringLabel': - Form.StringLabel(description, - tp=Form.FT_HTML_LABEL), - 'iSymbolsFilename': Form.StringInput( - value='symbols.txt'), - 'iHeaderFilename': Form.StringInput( - value='fa_structs.h'), - 'iIfdef': Form.StringInput( - value='FA_STRUCTS_H'), - }) - self.__n = 0 - - def OnFormChange(self, fid): - return 1 - - form = ExportForm() - form, args = form.Compile() - ok = form.Execute() - if ok == 1: - # save symbols - symbols_filename = os.path.join(form.iDir.value, - form.iSymbolsFilename.value) - with open(symbols_filename, 'w') as f: - results = IdaLoader.extract_all_user_names(None) - for k, v in results.items(): - f.write('{} = 0x{:08x};\n'.format(k, v)) - - # save c header - idati = ida_typeinf.get_idati() - c_header_filename = os.path.join(form.iDir.value, - form.iHeaderFilename.value) - - consts_ordinal = None - ordinals = [] - for ordinal in range(1, ida_typeinf.get_ordinal_qty(idati) + 1): - ti = ida_typeinf.tinfo_t() - if ti.get_numbered_type(idati, ordinal): - if ti.get_type_name() == 'FA_CONSTS': - # convert into macro definitions - consts_ordinal = ordinal - elif ti.get_type_name() in ('__va_list_tag', - 'va_list'): - continue - else: - ordinals.append(str(ordinal)) - - with open(c_header_filename, 'w') as f: - ifdef_name = form.iIfdef.value.strip() - - if len(ifdef_name) > 0: - f.write('#ifndef {ifdef_name}\n' - '#define {ifdef_name}\n\n' - .format(ifdef_name=ifdef_name)) - - if consts_ordinal is not None: - consts = re.findall('\s*(.+?) = (.+?),', - idc.print_decls( - str(consts_ordinal), 0)) - for k, v in consts: - f.write('#define {} ({})\n'.format(k, v)) - - # ida exports using this type - f.write('#define _BYTE char\n') - f.write('\n') - - structs_buf = idc.print_decls(','.join(ordinals), - idc.PDF_DEF_BASE) - - for struct_name in re.findall( - r'struct .*?([a-zA-Z0-9_\-]+?)\s+\{', - structs_buf): - f.write('typedef struct {struct_name} {struct_name};\n' - .format(struct_name=struct_name)) - f.write('\n') - f.write(structs_buf) - f.write('\n') - - if len(ifdef_name) > 0: - f.write('#endif // {ifdef_name}\n' - .format(ifdef_name=ifdef_name)) - - form.Free() - - def set_input(self, input_): - """ - Mock for change_input. Just reload current loaded data settings. - :param input_: doesn't matter - :return: None - """ - self.endianity = '>' if idaapi.get_inf_structure().is_be() else '<' - self._input = input_ - self.reload_segments() - - def reload_segments(self): - """ - memory searches will use IDA's API instead - which is much faster so this is just a stub. - :return: None - """ - return - - def interactive_settings(self): - """ - Show settings dialog - :return: None - """ - class SettingsForm(Form): - def __init__(self, signatures_root, use_template): - description = ''' -

Settings

-
- Here you can change global FA settings. -
- - ''' - - Form.__init__(self, - r"""BUTTON YES* Save - FA Settings - {{FormChangeCb}} - {{StringLabel}} - - - """.format(signatures_root), { - 'FormChangeCb': - Form.FormChangeCb(self.OnFormChange), - 'signaturesRoot': - Form.DirInput(value=signatures_root), - 'StringLabel': - Form.StringLabel(description, - tp=Form.FT_HTML_LABEL), - 'signatureGeneration': - Form.DropdownListControl( - items=['Default', - 'Using function bytes'], - readonly=True, - selval=use_template), - }) - self.__n = 0 - - def OnFormChange(self, fid): - return 1 - - f = SettingsForm(self._signatures_root, self._create_template_symbol) - f, args = f.Compile() - ok = f.Execute() - if ok == 1: - self.set_signatures_root(f.signaturesRoot.value, save=True) - self.set_symbol_template(f.signatureGeneration.value == 1) - f.Free() - - def interactive_set_project(self): - """ - Show set-project dialog - :return: None - """ - class SetProjectForm(Form): - def __init__(self, signatures_root, projects, current): - description = ''' -

Project Selector

-
- Select project you wish to work on from your - signatures root: -
-
{}
-
(Note: You may change this in config.ini)
- - '''.format(signatures_root) - - Form.__init__(self, - r"""BUTTON YES* OK - FA Project Select - {{FormChangeCb}} - {{StringLabel}} - - """.format(signatures_root), { - 'FormChangeCb': - Form.FormChangeCb(self.OnFormChange), - 'cbReadonly': - Form.DropdownListControl( - items=projects, - readonly=True, - selval=projects.index(current) - if current in projects else 0), - 'StringLabel': - Form.StringLabel(description, - tp=Form.FT_HTML_LABEL), - }) - self.__n = 0 - - def OnFormChange(self, fid): - return 1 - - projects = self.list_projects() - f = SetProjectForm(self._signatures_root, projects, self._project) - f, args = f.Compile() - ok = f.Execute() - if ok == 1: - self.set_project(projects[f.cbReadonly.value]) - f.Free() - - -fa_instance = None - -Action = namedtuple('action', 'name icon_filename handler label hotkey') - - -def add_action(action): - """ - Add an ida-action - :param action: action given as the `Action` namedtuple - :return: None - """ - class Handler(ida_kernwin.action_handler_t): - def __init__(self): - ida_kernwin.action_handler_t.__init__(self) - - def activate(self, ctx): - action.handler() - return 1 - - def update(self, ctx): - return ida_kernwin.AST_ENABLE_FOR_WIDGET - - act_icon = -1 - if action.icon_filename: - icon_full_filename = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - 'res', 'icons', action.icon_filename) - with open(icon_full_filename, 'rb') as f: - icon_data = f.read() - act_icon = ida_kernwin.load_custom_icon(data=icon_data, format="png") - - act_name = action.name - - ida_kernwin.unregister_action(act_name) - if ida_kernwin.register_action(ida_kernwin.action_desc_t( - act_name, # Name. Acts as an ID. Must be unique. - action.label, # Label. That's what users see. - Handler(), # Handler. Called when activated, and for updating - action.hotkey, # Shortcut (optional) - None, # Tooltip (optional) - act_icon)): # Icon ID (optional) - - # Insert the action in the menu - if not ida_kernwin.attach_action_to_menu( - "FA/", act_name, ida_kernwin.SETMENU_APP): - print("Failed attaching to menu.") - - # Insert the action in a toolbar - if not ida_kernwin.attach_action_to_toolbar("fa", act_name): - print("Failed attaching to toolbar.") - - class Hooks(ida_kernwin.UI_Hooks): - def finish_populating_widget_popup(self, widget, popup): - if ida_kernwin.get_widget_type(widget) == \ - ida_kernwin.BWN_DISASM: - ida_kernwin.attach_action_to_popup(widget, - popup, - act_name, - None) - - hooks = Hooks() - hooks.hook() - - -def load_ui(): - """ - Load FA's GUI buttons - :return: None - """ - actions = [ - Action(name='fa:settings', - icon_filename='settings.png', - handler=fa_instance.interactive_settings, - label='Settings', - hotkey=None), - - Action(name='fa:set-project', - icon_filename='suitcase.png', - handler=fa_instance.interactive_set_project, - label='Set project...', - hotkey='Ctrl+6'), - - Action(name='fa:symbols', icon_filename='find_all.png', - handler=fa_instance.symbols, - label='Find all project\'s symbols', - hotkey='Ctrl+7'), - - Action(name='fa:export', icon_filename='export.png', - handler=fa_instance.export, - label='Export symbols', - hotkey=None), - - Action(name='fa:extended-create-signature', - icon_filename='create_sig.png', - handler=fa_instance.extended_create_symbol, - label='Create temp signature...', - hotkey='Ctrl+8'), - - Action(name='fa:find-symbol', - icon_filename='find.png', - handler=fa_instance.find_symbol, - label='Find last created temp signature', - hotkey='Ctrl+9'), - - Action(name='fa:prompt-save', - icon_filename='save.png', - handler=fa_instance.prompt_save_signature, - label='Save last created temp signature', - hotkey='Ctrl+0'), - ] - - # init toolbar - ida_kernwin.delete_toolbar('fa') - ida_kernwin.create_toolbar('fa', 'FA Toolbar') - - # init menu - ida_kernwin.delete_menu('fa') - ida_kernwin.create_menu('fa', 'FA') - - for action in actions: - add_action(action) - - -def install(): - """ - Install FA ida plugin - :return: None - """ - fa_plugin_dir = os.path.join( - idaapi.get_user_idadir(), 'plugins') - - if not os.path.exists(fa_plugin_dir): - os.makedirs(fa_plugin_dir) - - fa_plugin_filename = os.path.join(fa_plugin_dir, PLUGIN_FILENAME) - if os.path.exists(fa_plugin_filename): - IdaLoader.log('already installed') - return - - with open(fa_plugin_filename, 'w') as f: - f.writelines("""from __future__ import print_function -try: - from fa.ida_plugin import PLUGIN_ENTRY, FAIDAPlugIn -except ImportError: - print("[WARN] Could not load FA plugin. " - "FA Python package doesn\'t seem to be installed.") -""") - - idaapi.load_plugin(PLUGIN_FILENAME) - - IdaLoader.log('Successfully installed :)') - - -@click.command() -@click.argument('signatures_root', default='.') -@click.option('--project_name', default=None) -@click.option('--symbols-file', default=None) -def main(signatures_root, project_name, symbols_file=None): - plugin_main(signatures_root, project_name, symbols_file) - - -def plugin_main(signatures_root, project_name, symbols_file=None): - global fa_instance - - fa_instance = IdaLoader() - fa_instance.set_input('ida') - - if project_name is not None: - fa_instance.set_project(project_name) - - load_ui() - - IdaLoader.log(''' --------------------------------- - FA Loaded successfully - - Quick usage: - print(fa_instance.find(symbol_name)) # searches for the specific symbol - fa_instance.get_python_symbols(filename=None) # run project's python - scripts (all or single) - fa_instance.set_symbol_template(status) # enable/disable template temp - signature - fa_instance.symbols() # searches for the symbols in the current project - ---------------------------------''') - - if symbols_file is not None: - fa_instance.set_signatures_root(signatures_root) - fa_instance.symbols(symbols_file) - ida_pro.qexit(0) - - # TODO: consider adding as autostart script - # install() - - -try: - class FAIDAPlugIn(idaapi.plugin_t): - wanted_name = "FA" - wanted_hotkey = "Shift-," - flags = 0 - comment = "" - help = "Load FA in IDA Pro" - - def init(self): - plugin_main('.', None, None) - return idaapi.PLUGIN_KEEP - - def run(self, args): - pass - - def term(self): - pass -except TypeError: - print('ignoring rpyc bug') - - -def PLUGIN_ENTRY(): - """ - Entry point for IDA plugins - :return: - """ - return FAIDAPlugIn() - - -if __name__ == '__main__': - # Entry point for IDA in script mode (-S) - main(standalone_mode=False, args=idc.ARGV[1:]) +import binascii +import re +import traceback +from collections import namedtuple +import subprocess +import tempfile +import sys +import os + +sys.path.append('.') # noqa: E402 + +import hjson +import click + +from ida_kernwin import Form +import ida_kernwin +import ida_typeinf +import ida_bytes +import idautils +import ida_pro +import idaapi +import idc + +from fa import fainterp, fa_types + +# Filename for the temporary created signature +TEMP_SIG_FILENAME = os.path.join(tempfile.gettempdir(), 'fa_tmp_sig.sig') + +# IDA fa plugin filename +PLUGIN_FILENAME = 'fa_ida_plugin.py' + + +def open_file(filename): + """ + Attempt to open the given filename by OS' default editor + :param filename: filename to open + :return: None + """ + if sys.platform == "win32": + try: + os.startfile(filename) + except Exception as error_code: + if error_code[0] == 1155: + os.spawnl(os.P_NOWAIT, + os.path.join(os.environ['WINDIR'], + 'system32', 'Rundll32.exe'), + 'Rundll32.exe SHELL32.DLL, OpenAs_RunDLL {}' + .format(filename)) + else: + print("other error") + else: + opener = "open" if sys.platform == "darwin" else "xdg-open" + subprocess.call([opener, filename]) + + +class IdaLoader(fainterp.FaInterp): + """ + IDA loader + Includes improved GUI interaction for accessing the interpreter + functionality. + """ + + def __init__(self): + super(IdaLoader, self).__init__() + self._create_template_symbol = eval(self.config_get( + 'global', + 'create_symbol_template', + 'False' + )) + + def set_symbol_template(self, status): + """ + Should the create-temp-signature feature attempt to create a default + signature by predefined template? + :param status: new boolean setting + :return: None + """ + self._create_template_symbol = status + self.config_set('global', 'create_symbol_template', str(status)) + + def create_symbol(self): + """ + Create a temporary symbol signature from the current function on the + IDA screen. + """ + self.log('creating temporary signature') + + current_ea = idc.get_screen_ea() + + signature = { + 'name': idc.get_func_name(current_ea), + 'instructions': [] + } + + if self._create_template_symbol: + find_bytes_ida = "find-bytes-ida '" + + for ea in idautils.FuncItems(current_ea): + mnem = idc.print_insn_mnem(ea).lower() + opcode_size = idc.get_item_size(ea) + + # ppc + if mnem.startswith('b') or mnem in ('lis', 'lwz', 'addi'): + # relative opcodes + find_bytes_ida += '?? ' * opcode_size + continue + + # arm + if mnem.startswith('b') or mnem in ('ldr', 'str'): + # relative opcodes + find_bytes_ida += '?? ' * opcode_size + continue + + opcode = binascii.hexlify(idc.get_bytes(ea, opcode_size)) + formatted_hex = ' '.join(opcode[i:i + 2] for i in + range(0, len(opcode), 2)) + find_bytes_ida += formatted_hex + ' ' + + find_bytes_ida += "'" + + signature['instructions'].append(find_bytes_ida) + signature['instructions'].append('function-start') + signature['instructions'].append('set-name "{}"'.format( + idc.get_func_name(current_ea))) + + with open(TEMP_SIG_FILENAME, 'w') as f: + hjson.dump(signature, f, indent=4) + + self.log('Signature created at {}'.format(TEMP_SIG_FILENAME)) + return TEMP_SIG_FILENAME + + def extended_create_symbol(self): + """ + Creates a temporary symbol of the currently active function + and open it using OS default editor + :return: None + """ + filename = self.create_symbol() + open_file(filename) + + def find_symbol(self): + """ + Find the last create symbol signature. + :return: None + """ + with open(TEMP_SIG_FILENAME) as f: + sig = hjson.load(f) + + results = self.find_from_sig_json(sig, decremental=False) + + for address in results: + self.log('Search result: 0x{:x}'.format(address)) + self.log('Search done') + + if len(results) == 1: + # if remote sig has a proper name, but current one is not + ida_kernwin.jumpto(results[0]) + + def verify_project(self): + """ + Verify a valid project is currently active. + Show IDA warning if not. + :return: None + """ + try: + super(IdaLoader, self).verify_project() + except IOError as e: + ida_kernwin.warning(e.message) + raise e + + def prompt_save_signature(self): + """ + Save last-created-temp-signature if user agrees to in IDA prompt + :return: None + """ + self.verify_project() + + if ida_kernwin.ask_yn(1, 'Are you sure you want ' + 'to save this signature?') != 1: + return + + self.save_signature(TEMP_SIG_FILENAME) + + def find(self, symbol_name, decremental=False): + """ + Find symbol by name (as specified in SIG file) + Show an IDA waitbox while doing so + :param symbol_name: symbol name + :return: output address list + """ + ida_kernwin.replace_wait_box('Searching symbol: \'{}\'...' + .format(symbol_name)) + return super(IdaLoader, self).find(symbol_name) + + def get_python_symbols(self, file_name=None): + """ + Run all python scripts inside the currently active project. + Show an IDA waitbox while doing so + :param file_name: filter a specific filename to execute + :return: dictionary of all found symbols + """ + ida_kernwin.replace_wait_box('Running python scripts...') + return super(IdaLoader, self).get_python_symbols(file_name=file_name) + + @staticmethod + def extract_all_user_names(filename=None): + """ + Get all user-named labels inside IDA. Also prints into output window. + :return: dictionary of all user named labels: label_name -> ea + """ + results = {} + output = '' + + for ea, name in idautils.Names(): + if ida_kernwin.user_cancelled(): + return results + + if '_' in name: + if name.split('_')[0] in ('def', 'sub', 'loc', 'jpt', 'j', + 'nullsub'): + continue + flags = ida_bytes.get_full_flags(ea) + if ida_bytes.has_user_name(flags): + results[name] = ea + output += '{} = 0x{:08x};\n'.format(name, ea) + + if filename is not None: + with open(filename, 'w') as f: + f.write(output) + + return results + + def set_const(self, name, value): + super(IdaLoader, self).set_const(name, value) + fa_types.add_const(name, value) + + def set_symbol(self, symbol_name, value): + super(IdaLoader, self).set_symbol(symbol_name, value) + idc.set_name(value, symbol_name, idc.SN_CHECK) + + def symbols(self, output_file_path=None): + """ + Run find for all SIG files in currently active project. + Show an IDA waitbox while doing so + :param output_file_path: optional, save found symbols into output file + :return: dictionary of found symbols + """ + self.verify_project() + results = {} + + try: + ida_kernwin.show_wait_box('Searching...') + results = super(IdaLoader, self).symbols() + + ida_kernwin.replace_wait_box('Extracting...') + ida_symbols = IdaLoader.extract_all_user_names(output_file_path) + + results.update(ida_symbols) + + except Exception as e: + traceback.print_exc() + finally: + ida_kernwin.hide_wait_box() + + return results + + def export(self): + """ + Show an export dialog to export symbols and header file for given + IDB. + :return: None + """ + class ExportForm(Form): + def __init__(self): + description = ''' +

Export

+ + Select a directory to export IDB data into. + ''' + + Form.__init__(self, + r"""BUTTON YES* Save + Export + {StringLabel} + <#Symbols#Symbols filename:{iSymbolsFilename}> + <#C Header#C Header filename:{iHeaderFilename}> + <#ifdef_macro#ifdef'ed:{iIfdef}> + <#Select dir#Browse for dir:{iDir}> + """, { + 'iDir': Form.DirInput(), + 'StringLabel': + Form.StringLabel(description, + tp=Form.FT_HTML_LABEL), + 'iSymbolsFilename': Form.StringInput( + value='symbols.txt'), + 'iHeaderFilename': Form.StringInput( + value='fa_structs.h'), + 'iIfdef': Form.StringInput( + value='FA_STRUCTS_H'), + }) + self.__n = 0 + + def OnFormChange(self, fid): + return 1 + + form = ExportForm() + form, args = form.Compile() + ok = form.Execute() + if ok == 1: + # save symbols + symbols_filename = os.path.join(form.iDir.value, + form.iSymbolsFilename.value) + with open(symbols_filename, 'w') as f: + results = IdaLoader.extract_all_user_names(None) + for k, v in results.items(): + f.write('{} = 0x{:08x};\n'.format(k, v)) + + # save c header + idati = ida_typeinf.get_idati() + c_header_filename = os.path.join(form.iDir.value, + form.iHeaderFilename.value) + + consts_ordinal = None + ordinals = [] + for ordinal in range(1, ida_typeinf.get_ordinal_qty(idati) + 1): + ti = ida_typeinf.tinfo_t() + if ti.get_numbered_type(idati, ordinal): + if ti.get_type_name() == 'FA_CONSTS': + # convert into macro definitions + consts_ordinal = ordinal + elif ti.get_type_name() in ('__va_list_tag', + 'va_list'): + continue + else: + ordinals.append(str(ordinal)) + + with open(c_header_filename, 'w') as f: + ifdef_name = form.iIfdef.value.strip() + + if len(ifdef_name) > 0: + f.write('#ifndef {ifdef_name}\n' + '#define {ifdef_name}\n\n' + .format(ifdef_name=ifdef_name)) + + if consts_ordinal is not None: + consts = re.findall('\s*(.+?) = (.+?),', + idc.print_decls( + str(consts_ordinal), 0)) + for k, v in consts: + f.write('#define {} ({})\n'.format(k, v)) + + # ida exports using this type + f.write('#define _BYTE char\n') + f.write('\n') + + structs_buf = idc.print_decls(','.join(ordinals), + idc.PDF_DEF_BASE) + + for struct_name in re.findall( + r'struct .*?([a-zA-Z0-9_\-]+?)\s+\{', + structs_buf): + f.write('typedef struct {struct_name} {struct_name};\n' + .format(struct_name=struct_name)) + f.write('\n') + f.write(structs_buf) + f.write('\n') + + if len(ifdef_name) > 0: + f.write('#endif // {ifdef_name}\n' + .format(ifdef_name=ifdef_name)) + + form.Free() + + def set_input(self, input_): + """ + Mock for change_input. Just reload current loaded data settings. + :param input_: doesn't matter + :return: None + """ + self.endianity = '>' if idaapi.get_inf_structure().is_be() else '<' + self._input = input_ + self.reload_segments() + + def reload_segments(self): + """ + memory searches will use IDA's API instead + which is much faster so this is just a stub. + :return: None + """ + return + + def interactive_settings(self): + """ + Show settings dialog + :return: None + """ + class SettingsForm(Form): + def __init__(self, signatures_root, use_template): + description = ''' +

Settings

+
+ Here you can change global FA settings. +
+ + ''' + + Form.__init__(self, + r"""BUTTON YES* Save + FA Settings + {{FormChangeCb}} + {{StringLabel}} + + + """.format(signatures_root), { + 'FormChangeCb': + Form.FormChangeCb(self.OnFormChange), + 'signaturesRoot': + Form.DirInput(value=signatures_root), + 'StringLabel': + Form.StringLabel(description, + tp=Form.FT_HTML_LABEL), + 'signatureGeneration': + Form.DropdownListControl( + items=['Default', + 'Using function bytes'], + readonly=True, + selval=use_template), + }) + self.__n = 0 + + def OnFormChange(self, fid): + return 1 + + f = SettingsForm(self._signatures_root, self._create_template_symbol) + f, args = f.Compile() + ok = f.Execute() + if ok == 1: + self.set_signatures_root(f.signaturesRoot.value, save=True) + self.set_symbol_template(f.signatureGeneration.value == 1) + f.Free() + + def interactive_set_project(self): + """ + Show set-project dialog + :return: None + """ + class SetProjectForm(Form): + def __init__(self, signatures_root, projects, current): + description = ''' +

Project Selector

+
+ Select project you wish to work on from your + signatures root: +
+
{}
+
(Note: You may change this in config.ini)
+ + '''.format(signatures_root) + + Form.__init__(self, + r"""BUTTON YES* OK + FA Project Select + {{FormChangeCb}} + {{StringLabel}} + + """.format(signatures_root), { + 'FormChangeCb': + Form.FormChangeCb(self.OnFormChange), + 'cbReadonly': + Form.DropdownListControl( + items=projects, + readonly=True, + selval=projects.index(current) + if current in projects else 0), + 'StringLabel': + Form.StringLabel(description, + tp=Form.FT_HTML_LABEL), + }) + self.__n = 0 + + def OnFormChange(self, fid): + return 1 + + projects = self.list_projects() + f = SetProjectForm(self._signatures_root, projects, self._project) + f, args = f.Compile() + ok = f.Execute() + if ok == 1: + self.set_project(projects[f.cbReadonly.value]) + f.Free() + + +fa_instance = None + +Action = namedtuple('action', 'name icon_filename handler label hotkey') + + +def add_action(action): + """ + Add an ida-action + :param action: action given as the `Action` namedtuple + :return: None + """ + class Handler(ida_kernwin.action_handler_t): + def __init__(self): + ida_kernwin.action_handler_t.__init__(self) + + def activate(self, ctx): + action.handler() + return 1 + + def update(self, ctx): + return ida_kernwin.AST_ENABLE_FOR_WIDGET + + act_icon = -1 + if action.icon_filename: + icon_full_filename = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + 'res', 'icons', action.icon_filename) + with open(icon_full_filename, 'rb') as f: + icon_data = f.read() + act_icon = ida_kernwin.load_custom_icon(data=icon_data, format="png") + + act_name = action.name + + ida_kernwin.unregister_action(act_name) + if ida_kernwin.register_action(ida_kernwin.action_desc_t( + act_name, # Name. Acts as an ID. Must be unique. + action.label, # Label. That's what users see. + Handler(), # Handler. Called when activated, and for updating + action.hotkey, # Shortcut (optional) + None, # Tooltip (optional) + act_icon)): # Icon ID (optional) + + # Insert the action in the menu + if not ida_kernwin.attach_action_to_menu( + "FA/", act_name, ida_kernwin.SETMENU_APP): + print("Failed attaching to menu.") + + # Insert the action in a toolbar + if not ida_kernwin.attach_action_to_toolbar("fa", act_name): + print("Failed attaching to toolbar.") + + class Hooks(ida_kernwin.UI_Hooks): + def finish_populating_widget_popup(self, widget, popup): + if ida_kernwin.get_widget_type(widget) == \ + ida_kernwin.BWN_DISASM: + ida_kernwin.attach_action_to_popup(widget, + popup, + act_name, + None) + + hooks = Hooks() + hooks.hook() + + +def load_ui(): + """ + Load FA's GUI buttons + :return: None + """ + actions = [ + Action(name='fa:settings', + icon_filename='settings.png', + handler=fa_instance.interactive_settings, + label='Settings', + hotkey=None), + + Action(name='fa:set-project', + icon_filename='suitcase.png', + handler=fa_instance.interactive_set_project, + label='Set project...', + hotkey='Ctrl+6'), + + Action(name='fa:symbols', icon_filename='find_all.png', + handler=fa_instance.symbols, + label='Find all project\'s symbols', + hotkey='Ctrl+7'), + + Action(name='fa:export', icon_filename='export.png', + handler=fa_instance.export, + label='Export symbols', + hotkey=None), + + Action(name='fa:extended-create-signature', + icon_filename='create_sig.png', + handler=fa_instance.extended_create_symbol, + label='Create temp signature...', + hotkey='Ctrl+8'), + + Action(name='fa:find-symbol', + icon_filename='find.png', + handler=fa_instance.find_symbol, + label='Find last created temp signature', + hotkey='Ctrl+9'), + + Action(name='fa:prompt-save', + icon_filename='save.png', + handler=fa_instance.prompt_save_signature, + label='Save last created temp signature', + hotkey='Ctrl+0'), + ] + + # init toolbar + ida_kernwin.delete_toolbar('fa') + ida_kernwin.create_toolbar('fa', 'FA Toolbar') + + # init menu + ida_kernwin.delete_menu('fa') + ida_kernwin.create_menu('fa', 'FA') + + for action in actions: + add_action(action) + + +def install(): + """ + Install FA ida plugin + :return: None + """ + fa_plugin_dir = os.path.join( + idaapi.get_user_idadir(), 'plugins') + + if not os.path.exists(fa_plugin_dir): + os.makedirs(fa_plugin_dir) + + fa_plugin_filename = os.path.join(fa_plugin_dir, PLUGIN_FILENAME) + if os.path.exists(fa_plugin_filename): + IdaLoader.log('already installed') + return + + with open(fa_plugin_filename, 'w') as f: + f.writelines("""from __future__ import print_function +try: + from fa.ida_plugin import PLUGIN_ENTRY, FAIDAPlugIn +except ImportError: + print("[WARN] Could not load FA plugin. " + "FA Python package doesn\'t seem to be installed.") +""") + + idaapi.load_plugin(PLUGIN_FILENAME) + + IdaLoader.log('Successfully installed :)') + + +@click.command() +@click.argument('signatures_root', default='.') +@click.option('--project_name', default=None) +@click.option('--symbols-file', default=None) +def main(signatures_root, project_name, symbols_file=None): + plugin_main(signatures_root, project_name, symbols_file) + + +def plugin_main(signatures_root, project_name, symbols_file=None): + global fa_instance + + fa_instance = IdaLoader() + fa_instance.set_input('ida') + + if project_name is not None: + fa_instance.set_project(project_name) + + load_ui() + + IdaLoader.log(''' --------------------------------- + FA Loaded successfully + + Quick usage: + print(fa_instance.find(symbol_name)) # searches for the specific symbol + fa_instance.get_python_symbols(filename=None) # run project's python + scripts (all or single) + fa_instance.set_symbol_template(status) # enable/disable template temp + signature + fa_instance.symbols() # searches for the symbols in the current project + ---------------------------------''') + + if symbols_file is not None: + fa_instance.set_signatures_root(signatures_root) + fa_instance.symbols(symbols_file) + ida_pro.qexit(0) + + # TODO: consider adding as autostart script + # install() + + +try: + class FAIDAPlugIn(idaapi.plugin_t): + wanted_name = "FA" + wanted_hotkey = "Shift-," + flags = 0 + comment = "" + help = "Load FA in IDA Pro" + + def init(self): + plugin_main('.', None, None) + return idaapi.PLUGIN_KEEP + + def run(self, args): + pass + + def term(self): + pass +except TypeError: + print('ignoring rpyc bug') + + +def PLUGIN_ENTRY(): + """ + Entry point for IDA plugins + :return: + """ + return FAIDAPlugIn() + + +if __name__ == '__main__': + # Entry point for IDA in script mode (-S) + main(standalone_mode=False, args=idc.ARGV[1:]) diff --git a/fa/signatures/test-project-elf/test-basic.sig b/fa/signatures/test-project-elf/test-basic.sig index 11351a4..168ef66 100644 --- a/fa/signatures/test-project-elf/test-basic.sig +++ b/fa/signatures/test-project-elf/test-basic.sig @@ -1,195 +1,195 @@ -{ - "name": "test", - "instructions": [ - add 80 - set-name test_add - store 80 - - offset 1 - set-name test_pos_offset - - offset -1 - set-name test_neg_offset - - add-offset-range 0 21 4 - single -1 - set-name test_add_offset_range - - clear - load 80 - set-name test_load - - offset 1 - align 4 - set-name test_align - - clear - - add 1 - add 2 - add 3 - add 2 - most-common - set-name test_most_common - - clear - - add 1 - add 2 - add 3 - add 2 - sort - single -1 - set-name test_sort - - clear - - add 1 - add 1 - verify-single - set-name test_verify_single_fail - - clear - - add 1 - verify-single - set-name test_verify_single_success - - clear - - run test_dep.dep - - clear - - arm-find-all 'loop: b loop' - set-name test_alias - set-name test_keystone_find_opcodes - - arm-verify 'loop: b loop' - set-name test_keystone_verify_opcodes - - clear - - find-bytes 11223344 - set-name test_find_bytes - - verify-bytes 11223344 - set-name test_verify_bytes - - clear - - find-str '3DUfw' - set-name test_find_str - - clear - - find test_find - - clear - - add 1 - add 2 - add 3 - - store a - - clear - - add 2 - add 8 - add 12 - - store b - - clear - - store c - - intersect a b - set-name test_intersect_ab - - intersect a b c - set-name test_intersect_abc - - clear - - add 2 - - store a - - add 4 - - store b - store c - - clear - - add 8 - - store d - - clear - - symdiff a b - set-name test_symdiff_ab - - symdiff b c - set-name test_symdiff_bc - - symdiff b c d - set-name test_symdiff_bcd - - clear - - add 1 - add 2 - - verify-single - store is_single1 - python-if is_single1 is_single_label1 - add 1 - set-name test_is_single_false1 - b end1 - - label is_single_label1 - set-name test_is_single_true1 - - label end1 - - clear - - add 1 - - verify-single - store is_single2 - - python-if is_single2 is_single_label2 - set-name test_is_single_false2 - b end2 - - label is_single_label2 - set-name test_is_single_true2 - - label end2 - - clear - - add 1 - - if 'verify-single' is_single_label3 - - clear - add 1 - set-name test_else3 - b end3 - - label is_single_label3 - - clear - add 1 - set-name test_if3 - - label end3 - - ] -} +{ + "name": "test", + "instructions": [ + add 80 + set-name test_add + store 80 + + offset 1 + set-name test_pos_offset + + offset -1 + set-name test_neg_offset + + add-offset-range 0 21 4 + single -1 + set-name test_add_offset_range + + clear + load 80 + set-name test_load + + offset 1 + align 4 + set-name test_align + + clear + + add 1 + add 2 + add 3 + add 2 + most-common + set-name test_most_common + + clear + + add 1 + add 2 + add 3 + add 2 + sort + single -1 + set-name test_sort + + clear + + add 1 + add 1 + verify-single + set-name test_verify_single_fail + + clear + + add 1 + verify-single + set-name test_verify_single_success + + clear + + run test_dep.dep + + clear + + arm-find-all 'loop: b loop' + set-name test_alias + set-name test_keystone_find_opcodes + + arm-verify 'loop: b loop' + set-name test_keystone_verify_opcodes + + clear + + find-bytes 11223344 + set-name test_find_bytes + + verify-bytes 11223344 + set-name test_verify_bytes + + clear + + find-str '3DUfw' + set-name test_find_str + + clear + + find test_find + + clear + + add 1 + add 2 + add 3 + + store a + + clear + + add 2 + add 8 + add 12 + + store b + + clear + + store c + + intersect a b + set-name test_intersect_ab + + intersect a b c + set-name test_intersect_abc + + clear + + add 2 + + store a + + add 4 + + store b + store c + + clear + + add 8 + + store d + + clear + + symdiff a b + set-name test_symdiff_ab + + symdiff b c + set-name test_symdiff_bc + + symdiff b c d + set-name test_symdiff_bcd + + clear + + add 1 + add 2 + + verify-single + store is_single1 + python-if is_single1 is_single_label1 + add 1 + set-name test_is_single_false1 + b end1 + + label is_single_label1 + set-name test_is_single_true1 + + label end1 + + clear + + add 1 + + verify-single + store is_single2 + + python-if is_single2 is_single_label2 + set-name test_is_single_false2 + b end2 + + label is_single_label2 + set-name test_is_single_true2 + + label end2 + + clear + + add 1 + + if 'verify-single' is_single_label3 + + clear + add 1 + set-name test_else3 + b end3 + + label is_single_label3 + + clear + add 1 + set-name test_if3 + + label end3 + + ] +} diff --git a/fa/signatures/test-project-elf/test_dep.dep b/fa/signatures/test-project-elf/test_dep.dep index 6686d7d..da37972 100644 --- a/fa/signatures/test-project-elf/test_dep.dep +++ b/fa/signatures/test-project-elf/test_dep.dep @@ -1,7 +1,7 @@ -{ - "name": "test", - "instructions": [ - add 67 - set-name test_run - ] -} +{ + "name": "test", + "instructions": [ + add 67 + set-name test_run + ] +} diff --git a/fa/signatures/test-project-elf/test_find.sig b/fa/signatures/test-project-elf/test_find.sig index d00ac90..f866257 100644 --- a/fa/signatures/test-project-elf/test_find.sig +++ b/fa/signatures/test-project-elf/test_find.sig @@ -1,7 +1,7 @@ -{ - "name": "test_find", - "instructions": [ - add 76 - set-name test_find - ] -} +{ + "name": "test_find", + "instructions": [ + add 76 + set-name test_find + ] +} diff --git a/fa/signatures/test-project-ida/test-basic.sig b/fa/signatures/test-project-ida/test-basic.sig index 507af25..9082ad4 100644 --- a/fa/signatures/test-project-ida/test-basic.sig +++ b/fa/signatures/test-project-ida/test-basic.sig @@ -1,167 +1,167 @@ -{ - "name": "test", - "instructions": [ - add 80 - set-name test_add - store 80 - - offset 1 - set-name test_pos_offset - - offset -1 - set-name test_neg_offset - - add-offset-range 0 21 4 - single -1 - set-name test_add_offset_range - - clear - load 80 - set-name test_load - - offset 1 - align 4 - set-name test_align - - clear - - add 1 - add 2 - add 3 - add 2 - most-common - set-name test_most_common - - clear - - add 1 - add 2 - add 3 - add 2 - sort - single -1 - set-name test_sort - - clear - - add 1 - add 1 - verify-single - set-name test_verify_single_fail - - clear - - add 1 - verify-single - set-name test_verify_single_success - - clear - - run test_dep.dep - - clear - - arm-find-all 'loop: b loop' - set-name test_alias - set-name test_keystone_find_opcodes - - arm-verify 'loop: b loop' - set-name test_keystone_verify_opcodes - - clear - - find-bytes 11223344 - set-name test_find_bytes - - verify-bytes 11223344 - set-name test_verify_bytes - - clear - - find-str '3DUfw' - set-name test_find_str - - clear - - find test_find - - clear - - add 1 - add 2 - add 3 - - store a - - clear - - add 2 - add 8 - add 12 - - store b - - clear - - store c - - intersect a b - set-name test_intersect_ab - - intersect a b c - set-name test_intersect_abc - - clear - - add 1 - add 2 - - verify-single - store is_single1 - python-if is_single1 is_single_label1 - add 1 - set-name test_is_single_false1 - b end1 - - label is_single_label1 - set-name test_is_single_true1 - - label end1 - - clear - - add 1 - - verify-single - store is_single2 - - python-if is_single2 is_single_label2 - set-name test_is_single_false2 - b end2 - - label is_single_label2 - set-name test_is_single_true2 - - label end2 - - clear - - add 1 - - if 'verify-single' is_single_label3 - - clear - add 1 - set-name test_else3 - b end3 - - label is_single_label3 - - clear - add 1 - set-name test_if3 - - label end3 - - ] -} +{ + "name": "test", + "instructions": [ + add 80 + set-name test_add + store 80 + + offset 1 + set-name test_pos_offset + + offset -1 + set-name test_neg_offset + + add-offset-range 0 21 4 + single -1 + set-name test_add_offset_range + + clear + load 80 + set-name test_load + + offset 1 + align 4 + set-name test_align + + clear + + add 1 + add 2 + add 3 + add 2 + most-common + set-name test_most_common + + clear + + add 1 + add 2 + add 3 + add 2 + sort + single -1 + set-name test_sort + + clear + + add 1 + add 1 + verify-single + set-name test_verify_single_fail + + clear + + add 1 + verify-single + set-name test_verify_single_success + + clear + + run test_dep.dep + + clear + + arm-find-all 'loop: b loop' + set-name test_alias + set-name test_keystone_find_opcodes + + arm-verify 'loop: b loop' + set-name test_keystone_verify_opcodes + + clear + + find-bytes 11223344 + set-name test_find_bytes + + verify-bytes 11223344 + set-name test_verify_bytes + + clear + + find-str '3DUfw' + set-name test_find_str + + clear + + find test_find + + clear + + add 1 + add 2 + add 3 + + store a + + clear + + add 2 + add 8 + add 12 + + store b + + clear + + store c + + intersect a b + set-name test_intersect_ab + + intersect a b c + set-name test_intersect_abc + + clear + + add 1 + add 2 + + verify-single + store is_single1 + python-if is_single1 is_single_label1 + add 1 + set-name test_is_single_false1 + b end1 + + label is_single_label1 + set-name test_is_single_true1 + + label end1 + + clear + + add 1 + + verify-single + store is_single2 + + python-if is_single2 is_single_label2 + set-name test_is_single_false2 + b end2 + + label is_single_label2 + set-name test_is_single_true2 + + label end2 + + clear + + add 1 + + if 'verify-single' is_single_label3 + + clear + add 1 + set-name test_else3 + b end3 + + label is_single_label3 + + clear + add 1 + set-name test_if3 + + label end3 + + ] +} diff --git a/fa/signatures/test-project-ida/test-ida-context.sig b/fa/signatures/test-project-ida/test-ida-context.sig index a9a5389..1ac4e69 100644 --- a/fa/signatures/test-project-ida/test-ida-context.sig +++ b/fa/signatures/test-project-ida/test-ida-context.sig @@ -1,97 +1,97 @@ -{ - "name": "test", - "instructions": [ - find-bytes-ida 11223344 - set-name test_find_bytes_ida - - xref - set-name test_xref - - function-start - set-name test_function_start - store func - - function-end - set-name test_function_end - - load func - offset 10 - function-lines - single 0 - set-name test_function_lines - - function-lines - verify-operand ldr --op0 0 - set-name test_verify_operand - store ref - - verify-ref --code --data - set-name test_verify_ref_no_name - - goto-ref --data - set-name test_verify_goto_ref - - load ref - verify-ref --name test_verify_goto_ref --code --data - set-name test_verify_ref_name - - locate test_function_lines - set-name test_locate - - clear - - find_immediate 0x11223344 - set-name test_find_immediate - - clear - - add 4 - set-const TEST_CONST_VALUE_4 - set-enum TEST_ENUM_NAME TEST_ENUM_KEY1_VALUE_4 - - clear - - add 6 - set-enum TEST_ENUM_NAME TEST_ENUM_KEY2_VALUE_6 - - clear - - arm-find-all 'mov r0, 1' - single 0 - operand 1 - set-name test_operand - - clear - - add 0 - set-struct-member test_struct_t test_member_offset_0 'unsigned int' - - offset 4 - set-struct-member test_struct_t test_member_offset_4 'unsigned int' - - clear - - arm-find-all 'mov r0, 1; bx lr' - set-name funcy - set-type 'int func(int)' - xref - sort - single 1 - argument 0 - - set-name test_argument - - clear - - arm-find-all 'mov r0, 1; bx lr' - - verify-operand mov --op0 0 - store tmp - - python-if tmp test_branch1 - set-name test_branch1_false - - label test_branch1 - set-name test_branch1_true - ] -} +{ + "name": "test", + "instructions": [ + find-bytes-ida 11223344 + set-name test_find_bytes_ida + + xref + set-name test_xref + + function-start + set-name test_function_start + store func + + function-end + set-name test_function_end + + load func + offset 10 + function-lines + single 0 + set-name test_function_lines + + function-lines + verify-operand ldr --op0 0 + set-name test_verify_operand + store ref + + verify-ref --code --data + set-name test_verify_ref_no_name + + goto-ref --data + set-name test_verify_goto_ref + + load ref + verify-ref --name test_verify_goto_ref --code --data + set-name test_verify_ref_name + + locate test_function_lines + set-name test_locate + + clear + + find_immediate 0x11223344 + set-name test_find_immediate + + clear + + add 4 + set-const TEST_CONST_VALUE_4 + set-enum TEST_ENUM_NAME TEST_ENUM_KEY1_VALUE_4 + + clear + + add 6 + set-enum TEST_ENUM_NAME TEST_ENUM_KEY2_VALUE_6 + + clear + + arm-find-all 'mov r0, 1' + single 0 + operand 1 + set-name test_operand + + clear + + add 0 + set-struct-member test_struct_t test_member_offset_0 'unsigned int' + + offset 4 + set-struct-member test_struct_t test_member_offset_4 'unsigned int' + + clear + + arm-find-all 'mov r0, 1; bx lr' + set-name funcy + set-type 'int func(int)' + xref + sort + single 1 + argument 0 + + set-name test_argument + + clear + + arm-find-all 'mov r0, 1; bx lr' + + verify-operand mov --op0 0 + store tmp + + python-if tmp test_branch1 + set-name test_branch1_false + + label test_branch1 + set-name test_branch1_true + ] +} diff --git a/fa/signatures/test-project-ida/test_dep.dep b/fa/signatures/test-project-ida/test_dep.dep index 6686d7d..da37972 100644 --- a/fa/signatures/test-project-ida/test_dep.dep +++ b/fa/signatures/test-project-ida/test_dep.dep @@ -1,7 +1,7 @@ -{ - "name": "test", - "instructions": [ - add 67 - set-name test_run - ] -} +{ + "name": "test", + "instructions": [ + add 67 + set-name test_run + ] +} diff --git a/fa/signatures/test-project-ida/test_find.sig b/fa/signatures/test-project-ida/test_find.sig index d00ac90..f866257 100644 --- a/fa/signatures/test-project-ida/test_find.sig +++ b/fa/signatures/test-project-ida/test_find.sig @@ -1,7 +1,7 @@ -{ - "name": "test_find", - "instructions": [ - add 76 - set-name test_find - ] -} +{ + "name": "test_find", + "instructions": [ + add 76 + set-name test_find + ] +} diff --git a/fa/utils.py b/fa/utils.py index 1946fd5..e600a33 100644 --- a/fa/utils.py +++ b/fa/utils.py @@ -1,108 +1,108 @@ -import argparse -import inspect -import os -import warnings - -IDA_MODULE = False - -try: - import idc - import ida_struct - - IDA_MODULE = True -except ImportError: - pass - - -def index_of(needle, haystack): - try: - return haystack.index(needle) - except ValueError: - return -1 - - -def find_raw(needle, segments=None): - if segments is None: - segments = dict() - - if IDA_MODULE: - # ida optimization - needle = bytearray(needle) - payload = ' '.join(['{:02x}'.format(b) for b in needle]) - for address in ida_find_all(payload): - yield address - return - - for segment_ea, data in segments.items(): - offset = index_of(needle, data) - extra_offset = 0 - - while offset != -1: - address = segment_ea + offset + extra_offset - yield address - - extra_offset += offset+1 - data = data[offset+1:] - - offset = index_of(needle, data) - - -def ida_find_all(payload): - ea = idc.find_binary(0, idc.SEARCH_DOWN | idc.SEARCH_REGEX, payload) - while ea != idc.BADADDR: - yield ea - ea = idc.find_binary(ea + 1, idc.SEARCH_DOWN | idc.SEARCH_REGEX, - payload) - - -def read_memory(segments, ea, size): - if IDA_MODULE: - return idc.get_bytes(ea, size) - - for segment_ea, data in segments.items(): - if (ea <= segment_ea + len(data)) and (ea >= segment_ea): - offset = ea - segment_ea - return data[offset:offset+size] - - -def yield_unique(func): - def wrapper(*args, **kwargs): - results = set() - for i in func(*args, **kwargs): - if i not in results: - yield i - results.add(i) - return wrapper - - -class ArgumentParserNoExit(argparse.ArgumentParser): - def error(self, message): - raise ValueError(message) - - -def deprecated(function): - frame = inspect.stack()[1] - module = inspect.getmodule(frame[0]) - filename = module.__file__ - command_name = os.path.splitext(os.path.basename(filename))[0] - - warnings.warn('command: "{}" is deperected and will be removed in ' - 'the future.'.format(command_name, DeprecationWarning)) - return function - - -def add_struct_to_idb(name): - idc.import_type(-1, name) - - -def find_or_create_struct(name): - sid = ida_struct.get_struc_id(name) - if sid == idc.BADADDR: - sid = idc.add_struc(-1, name, 0) - print("added struct \"{0}\", id: {1}".format(name, sid)) - else: - print("struct \"{0}\" already exists, id: ".format(name, sid)) - - add_struct_to_idb(name) - - return sid +import argparse +import inspect +import os +import warnings + +IDA_MODULE = False + +try: + import idc + import ida_struct + + IDA_MODULE = True +except ImportError: + pass + + +def index_of(needle, haystack): + try: + return haystack.index(needle) + except ValueError: + return -1 + + +def find_raw(needle, segments=None): + if segments is None: + segments = dict() + + if IDA_MODULE: + # ida optimization + needle = bytearray(needle) + payload = ' '.join(['{:02x}'.format(b) for b in needle]) + for address in ida_find_all(payload): + yield address + return + + for segment_ea, data in segments.items(): + offset = index_of(needle, data) + extra_offset = 0 + + while offset != -1: + address = segment_ea + offset + extra_offset + yield address + + extra_offset += offset+1 + data = data[offset+1:] + + offset = index_of(needle, data) + + +def ida_find_all(payload): + ea = idc.find_binary(0, idc.SEARCH_DOWN | idc.SEARCH_REGEX, payload) + while ea != idc.BADADDR: + yield ea + ea = idc.find_binary(ea + 1, idc.SEARCH_DOWN | idc.SEARCH_REGEX, + payload) + + +def read_memory(segments, ea, size): + if IDA_MODULE: + return idc.get_bytes(ea, size) + + for segment_ea, data in segments.items(): + if (ea <= segment_ea + len(data)) and (ea >= segment_ea): + offset = ea - segment_ea + return data[offset:offset+size] + + +def yield_unique(func): + def wrapper(*args, **kwargs): + results = set() + for i in func(*args, **kwargs): + if i not in results: + yield i + results.add(i) + return wrapper + + +class ArgumentParserNoExit(argparse.ArgumentParser): + def error(self, message): + raise ValueError(message) + + +def deprecated(function): + frame = inspect.stack()[1] + module = inspect.getmodule(frame[0]) + filename = module.__file__ + command_name = os.path.splitext(os.path.basename(filename))[0] + + warnings.warn('command: "{}" is deperected and will be removed in ' + 'the future.'.format(command_name, DeprecationWarning)) + return function + + +def add_struct_to_idb(name): + idc.import_type(-1, name) + + +def find_or_create_struct(name): + sid = ida_struct.get_struc_id(name) + if sid == idc.BADADDR: + sid = idc.add_struc(-1, name, 0) + print("added struct \"{0}\", id: {1}".format(name, sid)) + else: + print("struct \"{0}\" already exists, id: ".format(name, sid)) + + add_struct_to_idb(name) + + return sid diff --git a/ide-completions/sublime/README.md b/ide-completions/sublime/README.md index 865949f..bb082a9 100644 --- a/ide-completions/sublime/README.md +++ b/ide-completions/sublime/README.md @@ -1,9 +1,9 @@ -# Sublime completions - - -To install in sublime: - - - Install hJson schema. - - Goto `Preferences -> Browse Packages...` and place - [sig.sublime-completions](sig.sublime-completions) somewhere inside. - - All done :) +# Sublime completions + + +To install in sublime: + + - Install hJson schema. + - Goto `Preferences -> Browse Packages...` and place + [sig.sublime-completions](sig.sublime-completions) somewhere inside. + - All done :) diff --git a/ide-completions/sublime/sig.sublime-completions b/ide-completions/sublime/sig.sublime-completions index 37493f4..47df3d4 100644 --- a/ide-completions/sublime/sig.sublime-completions +++ b/ide-completions/sublime/sig.sublime-completions @@ -279,7 +279,7 @@ { "trigger": "xrefs-to", "kind": "snippet", - "contents": "xrefs-to --function-start --or --and --name ${1:NAME} --bytes ${2:BYTES}" + "contents": "xrefs-to --function-start --or --and --name ${1:NAME} --bytes ${2:BYTES}" } ] } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index bc595ff..b6bb25f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ -keystone-engine -capstone -click -hjson -future +keystone-engine +capstone +click +hjson +future configparser \ No newline at end of file diff --git a/requirements_testing.txt b/requirements_testing.txt index cbd57b8..ecdad29 100644 --- a/requirements_testing.txt +++ b/requirements_testing.txt @@ -1,10 +1,10 @@ -keystone-engine -capstone -click -hjson -future -configparser -idalink -pytest -simpleelf +keystone-engine +capstone +click +hjson +future +configparser +idalink +pytest +simpleelf pyelftools \ No newline at end of file diff --git a/scripts/git/install.py b/scripts/git/install.py index 6d6e2a7..456ea53 100644 --- a/scripts/git/install.py +++ b/scripts/git/install.py @@ -1,10 +1,10 @@ -#!/usr/bin/python -import os -import shutil - -shutil.copyfile(os.path.join(os.path.dirname(__file__), - 'pre-commit'), - os.path.join( - os.path.dirname( - os.path.dirname(os.path.dirname(__file__))), - '.git', 'hooks', 'pre-commit')) +#!/usr/bin/python +import os +import shutil + +shutil.copyfile(os.path.join(os.path.dirname(__file__), + 'pre-commit'), + os.path.join( + os.path.dirname( + os.path.dirname(os.path.dirname(__file__))), + '.git', 'hooks', 'pre-commit')) diff --git a/scripts/git/pre-commit b/scripts/git/pre-commit index eb94fa4..753faee 100644 --- a/scripts/git/pre-commit +++ b/scripts/git/pre-commit @@ -1,103 +1,103 @@ -#!/usr/bin/python3 -import json -import re -from collections import OrderedDict -import os -import sys - -sys.path.append('.') # noqa: E402 - -from fa.fainterp import FaInterp - -COMMANDS_ROOT = os.path.join( - os.path.dirname(os.path.abspath(__file__)), '..', '..', 'fa', 'commands') - -COMMANDS_MD = os.path.join( - os.path.dirname(os.path.abspath(__file__)), '..', '..', 'commands.md') - -SUBLIME_COMP = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - '..', '..', 'ide-completions', 'sublime', 'sig.sublime-completions') - - -def main(): - command_usage = OrderedDict() - command_usage['label'] = 'label' - - command_help = OrderedDict() - command_help['label'] = 'builtin interpreter command. mark a label\n' - - commands = os.listdir(COMMANDS_ROOT) - commands.sort() - - sublime_completions = { - 'scope': 'source.hjson meta.structure.dictionary.hjson ' - 'meta.structure.key-value.hjson meta.structure.array.hjson', - 'completions': [] - } - - for filename in commands: - if filename.endswith('.py') and \ - filename not in ('__init__.py',): - command = os.path.splitext(filename)[0] - command = FaInterp.get_command(command) - - p = command.get_parser() - command_help[p.prog] = p.format_help() - - snippet = p.format_usage().split('usage: ', 1)[1]\ - .replace('\n', '').strip().replace(' [-h]', '')\ - .replace('[', '').replace(']', '') - - def replacer(m): - buf = '' - global index - for g in m.groups(): - if g.startswith('--'): - buf += g - else: - buf += '${%d:%s}' % (index, g) - index += 1 - return buf - - args = '' - cmd = snippet - - if ' ' in snippet: - cmd, args = snippet.split(' ', 1) - globals()['index'] = 1 - args = re.sub('([\-\w]+)', replacer, args) - - sublime_completions['completions'].append({ - 'trigger': p.prog, - 'kind': 'snippet', - 'contents': cmd + ' ' + args, - }) - - commands_md_buf = '' - commands_md_buf += '# FA Command List\n' - commands_md_buf += 'Below is the list of available commands:\n' - - for command in command_help.keys(): - commands_md_buf += '- [{command}](#{command})\n'\ - .format(command=command) - - for command, help in command_help.items(): - commands_md_buf += '## {}\n```\n{}```\n'.format(command, help) - - with open(COMMANDS_MD, 'rt') as f: - current_buf = f.read() - - with open(COMMANDS_MD, 'wt') as f: - f.write(commands_md_buf) - - with open(SUBLIME_COMP, 'wt') as f: - f.write(json.dumps(sublime_completions, indent=4)) - - if current_buf != commands_md_buf: - print('commands.md and / or ide-completions/ has been changed. Please review and then commit again.') - sys.exit(1) - - -if __name__ == '__main__': - main() +#!/usr/bin/python3 +import json +import re +from collections import OrderedDict +import os +import sys + +sys.path.append('.') # noqa: E402 + +from fa.fainterp import FaInterp + +COMMANDS_ROOT = os.path.join( + os.path.dirname(os.path.abspath(__file__)), '..', '..', 'fa', 'commands') + +COMMANDS_MD = os.path.join( + os.path.dirname(os.path.abspath(__file__)), '..', '..', 'commands.md') + +SUBLIME_COMP = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + '..', '..', 'ide-completions', 'sublime', 'sig.sublime-completions') + + +def main(): + command_usage = OrderedDict() + command_usage['label'] = 'label' + + command_help = OrderedDict() + command_help['label'] = 'builtin interpreter command. mark a label\n' + + commands = os.listdir(COMMANDS_ROOT) + commands.sort() + + sublime_completions = { + 'scope': 'source.hjson meta.structure.dictionary.hjson ' + 'meta.structure.key-value.hjson meta.structure.array.hjson', + 'completions': [] + } + + for filename in commands: + if filename.endswith('.py') and \ + filename not in ('__init__.py',): + command = os.path.splitext(filename)[0] + command = FaInterp.get_command(command) + + p = command.get_parser() + command_help[p.prog] = p.format_help() + + snippet = p.format_usage().split('usage: ', 1)[1]\ + .replace('\n', '').strip().replace(' [-h]', '')\ + .replace('[', '').replace(']', '') + + def replacer(m): + buf = '' + global index + for g in m.groups(): + if g.startswith('--'): + buf += g + else: + buf += '${%d:%s}' % (index, g) + index += 1 + return buf + + args = '' + cmd = snippet + + if ' ' in snippet: + cmd, args = snippet.split(' ', 1) + globals()['index'] = 1 + args = re.sub('([\-\w]+)', replacer, args) + + sublime_completions['completions'].append({ + 'trigger': p.prog, + 'kind': 'snippet', + 'contents': cmd + ' ' + args, + }) + + commands_md_buf = '' + commands_md_buf += '# FA Command List\n' + commands_md_buf += 'Below is the list of available commands:\n' + + for command in command_help.keys(): + commands_md_buf += '- [{command}](#{command})\n'\ + .format(command=command) + + for command, help in command_help.items(): + commands_md_buf += '## {}\n```\n{}```\n'.format(command, help) + + with open(COMMANDS_MD, 'rt') as f: + current_buf = f.read() + + with open(COMMANDS_MD, 'wt') as f: + f.write(commands_md_buf) + + with open(SUBLIME_COMP, 'wt') as f: + f.write(json.dumps(sublime_completions, indent=4)) + + if current_buf != commands_md_buf: + print('commands.md and / or ide-completions/ has been changed. Please review and then commit again.') + sys.exit(1) + + +if __name__ == '__main__': + main() diff --git a/setup.py b/setup.py index 1f16944..fb8a071 100644 --- a/setup.py +++ b/setup.py @@ -1,28 +1,28 @@ -from setuptools import setup - -setup( - name='fa', - version='0.2', - description='FA Plugin', - author='DoronZ', - author_email='doron88@gmail.com', - url='https://github.com/doronz88/fa', - packages=['fa', 'fa.commands'], - package_dir={'fa': 'fa'}, - data_files=[(r'fa/res/icons', [r'fa/res/icons/create_sig.png', - r'fa/res/icons/export.png', - r'fa/res/icons/find.png', - r'fa/res/icons/find_all.png', - r'fa/res/icons/save.png', - r'fa/res/icons/settings.png', - r'fa/res/icons/suitcase.png']), - (r'fa/commands', ['fa/commands/alias']), - ], - install_requires=['keystone-engine', - 'capstone', - 'click', - 'hjson', - 'future', - 'configparser'], - python_requires='>=2.7' -) +from setuptools import setup + +setup( + name='fa', + version='0.2', + description='FA Plugin', + author='DoronZ', + author_email='doron88@gmail.com', + url='https://github.com/doronz88/fa', + packages=['fa', 'fa.commands'], + package_dir={'fa': 'fa'}, + data_files=[(r'fa/res/icons', [r'fa/res/icons/create_sig.png', + r'fa/res/icons/export.png', + r'fa/res/icons/find.png', + r'fa/res/icons/find_all.png', + r'fa/res/icons/save.png', + r'fa/res/icons/settings.png', + r'fa/res/icons/suitcase.png']), + (r'fa/commands', ['fa/commands/alias']), + ], + install_requires=['keystone-engine', + 'capstone', + 'click', + 'hjson', + 'future', + 'configparser'], + python_requires='>=2.7' +) From 0837e9e7d7ce0e06168692457d70eaa127a85efc Mon Sep 17 00:00:00 2001 From: DoronZ Date: Sat, 29 Aug 2020 18:34:42 +0300 Subject: [PATCH 18/18] fix pep8 --- commands.md | 14 +++++++------- fa/commands/b.py | 2 +- fa/commands/if.py | 4 ++-- fa/commands/python_if.py | 6 +++--- fa/commands/stop_if_empty.py | 2 +- fa/fainterp.py | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/commands.md b/commands.md index bb046b7..e0ec144 100644 --- a/commands.md +++ b/commands.md @@ -150,7 +150,7 @@ EXAMPLE: add 2 label skip add 3 - + results = [1, 3] positional arguments: @@ -366,12 +366,12 @@ using an FA command EXAMPLE: results = [0, 4, 8] - + -> if 'verify-single' a_is_single_label set-name a_isnt_single b end - + label a_is_single_label set-name a_is_single @@ -649,15 +649,15 @@ using an eval'ed expression EXAMPLE: results = [0, 4, 8] - + verify-single store a - + # jump to a_is_single_label since a == [] -> python-if a a_is_single_label set-name a_isnt_single b end - + label a_is_single_label set-name a_is_single @@ -787,7 +787,7 @@ EXAMPLE: -> stop-if-empty add 1 - + results = [] optional arguments: diff --git a/fa/commands/b.py b/fa/commands/b.py index c779be9..b275007 100644 --- a/fa/commands/b.py +++ b/fa/commands/b.py @@ -11,7 +11,7 @@ add 2 label skip add 3 - + results = [1, 3] ''' diff --git a/fa/commands/if.py b/fa/commands/if.py index 3104a9e..4be8422 100644 --- a/fa/commands/if.py +++ b/fa/commands/if.py @@ -6,12 +6,12 @@ EXAMPLE: results = [0, 4, 8] - + -> if 'verify-single' a_is_single_label set-name a_isnt_single b end - + label a_is_single_label set-name a_is_single diff --git a/fa/commands/python_if.py b/fa/commands/python_if.py index 99a25e5..e499acf 100644 --- a/fa/commands/python_if.py +++ b/fa/commands/python_if.py @@ -6,15 +6,15 @@ EXAMPLE: results = [0, 4, 8] - + verify-single store a - + # jump to a_is_single_label since a == [] -> python-if a a_is_single_label set-name a_isnt_single b end - + label a_is_single_label set-name a_is_single diff --git a/fa/commands/stop_if_empty.py b/fa/commands/stop_if_empty.py index f2cd1d6..9d18a00 100644 --- a/fa/commands/stop_if_empty.py +++ b/fa/commands/stop_if_empty.py @@ -8,7 +8,7 @@ -> stop-if-empty add 1 - + results = [] ''' diff --git a/fa/fainterp.py b/fa/fainterp.py index cfe597e..0dad056 100644 --- a/fa/fainterp.py +++ b/fa/fainterp.py @@ -4,7 +4,7 @@ from configparser import ConfigParser from abc import ABCMeta, abstractmethod -from collections import OrderedDict, namedtuple +from collections import OrderedDict import shlex import sys import os @@ -408,7 +408,7 @@ def find_from_instructions_list(self, instructions, addresses=None): self._push_stack_frame() labels, instructions = self._get_labeled_instructions(instructions) - + self.set_labels(labels) n = len(instructions)