From 270206607b75d9cd9918cf837b3ed5f15d121d47 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 25 Nov 2021 22:28:20 -0500 Subject: [PATCH] update disassembler.py, create output file Signed-off-by: Andrew --- evm/disassembler.py | 10 +++++----- evm/evm.py | 24 +++++++++++++----------- evm/output | 1 + 3 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 evm/output diff --git a/evm/disassembler.py b/evm/disassembler.py index cce15d7..0f02f5e 100644 --- a/evm/disassembler.py +++ b/evm/disassembler.py @@ -1,24 +1,24 @@ import sys import os -#import capstone +# import capstone from evm import EVM # import convert_bytecode def get_list_with_prefix(prefix, len): - return ["{}{}".format(prefix, i) for i in range(len)] + return ['{}{}'.format(prefix, i) for i in range(len)] if __name__ == "__main__": - evm = EVM(bytes.fromhex(input('>> Enter your hex. '))) + evm = EVM(bytes.fromhex(input('>> Enter your hex: '))) insts, func_list, blocks = evm.disassemble() with open("output", "w") as output: output.write("FUNCTIONS:\n") - for addr, infpo in sorted(func_list.items()): + for addr, info in sorted(func_list.items()): output.write( " FUNC_{:04X}({}) -> ({})\n" .format( addr, - ', '.join(get_list_with_prefix("arg", info[0])), + ', '.join(get_list_with_prefix('arg', info[0])), '. '.join(get_list_with_prefix("r", info[1])), ) ) diff --git a/evm/evm.py b/evm/evm.py index 7598b43..9c5fc76 100644 --- a/evm/evm.py +++ b/evm/evm.py @@ -30,7 +30,7 @@ def __init__(self, data, **kwargs): self._table = { int(k): v for k, v in json.load(opcode).items()} self._terminal_ops = ['*STOP', '*RETURN', '*REVERT', '*BALANCE'] - self._jumps_ops = ["*JUMP", "*JUMPI"] + self._jump_ops = ['*JUMP', '*JUMPI'] self._opcodes_func = { 0: self._stop, @@ -179,7 +179,7 @@ def __init__(self, data, **kwargs): 255: self._selfdestruct, } - def _insert_entry_list_dic(self, dict, k, v): + def _insert_entry_list_dict(self, dict, k, v): if k not in dict: dict[k] = [] if v not in dict[k]: @@ -271,8 +271,10 @@ def _get_func_ret_vals(self, addr): def _annotation_jump(self, addr, cond): - return '// Incoming jump from 0x{:04X}'.format(addr), - cond + return ( + '// Incoming jump from 0x{:04X}'.format(addr), + cond + ) def _annotation_call(self): return ( @@ -292,8 +294,9 @@ def _annotation_return(self, addr): None ) def disassemble(self): + self._recursive_run() - self.linear_run() + self._linear_run() return self._visited, self._blocks, self._func_list def _recursive_run(self): @@ -329,7 +332,7 @@ def _recursive_run(self): self._blocks, self._pc, # might have to concatenate not into cond - self._annotation_jump(self._pc - 1, "not" + str(cond)) + self._annotation_jump(self._pc - 1, 'not ' + cond) ) if type(jump_addr) != int: @@ -339,7 +342,7 @@ def _recursive_run(self): self._insert_entry_list_dict( self._blocks, jump_addr, - self._annotation_call(self._pc, - 1, cond) + self._annotation_call() ) else: @@ -525,8 +528,7 @@ def _mulmod(self): self._stack.append('({} * {}) % {}'.format(operand_1, operand_2, operand_3)) - - + def _exp(self): operand_1 = self._stack_pop() operand_2 = self._stack_pop() @@ -884,9 +886,9 @@ def _jumpdest(self): return def _push(self): - imm_width = int(self._table[self._data[self.pc - 1]][4:]) + imm_width = int(self._table[self._data[self._pc - 1]][4:]) imm_val = self._data[self._pc:self._pc+imm_width].hex() - if len(self._visited[elf._pc - 1][0]) <= 6: + if len(self._visited[self._pc - 1][0]) <= 6: self._visited[self._pc - 1][0] += '0x{}'.format(imm_val) self._stack.append(int(imm_val, 16)) self._pc += imm_width diff --git a/evm/output b/evm/output new file mode 100644 index 0000000..49d91e2 --- /dev/null +++ b/evm/output @@ -0,0 +1 @@ +FUNCTIONS: